Adding cloud region endpoints
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aaisimulator / service / providers / GenericVnfCacheServiceProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.aaisimulator.service.providers;
21
22 import static org.onap.so.aaisimulator.utils.CacheName.GENERIC_VNF_CACHE;
23 import java.util.Optional;
24 import java.util.concurrent.ConcurrentHashMap;
25 import org.onap.aai.domain.yang.GenericVnf;
26 import org.onap.aai.domain.yang.Relationship;
27 import org.onap.aai.domain.yang.RelationshipList;
28 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.cache.Cache;
33 import org.springframework.cache.CacheManager;
34 import org.springframework.stereotype.Service;
35
36 /**
37  * @author Waqas Ikram (waqas.ikram@est.tech)
38  *
39  */
40 @Service
41 public class GenericVnfCacheServiceProviderImpl extends AbstractCacheServiceProvider
42         implements GenericVnfCacheServiceProvider {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfCacheServiceProviderImpl.class);
45
46     @Autowired
47     public GenericVnfCacheServiceProviderImpl(final CacheManager cacheManager) {
48         super(cacheManager);
49     }
50
51     @Override
52     public void putGenericVnf(final String vnfId, final GenericVnf genericVnf) {
53         LOGGER.info("Adding customer: {} with key: {} in cache ...", genericVnf, vnfId);
54         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
55         cache.put(vnfId, genericVnf);
56     }
57
58     @Override
59     public Optional<GenericVnf> getGenericVnf(final String vnfId) {
60         LOGGER.info("getting GenericVnf from cache using key: {}", vnfId);
61         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
62         final GenericVnf value = cache.get(vnfId, GenericVnf.class);
63         if (value != null) {
64             return Optional.of(value);
65         }
66         LOGGER.error("Unable to find GenericVnf ...");
67         return Optional.empty();
68     }
69
70     @Override
71     public boolean addRelationShip(final String vnfId, final Relationship relationship) {
72         final Optional<GenericVnf> optional = getGenericVnf(vnfId);
73         if (optional.isPresent()) {
74             final GenericVnf genericVnf = optional.get();
75             RelationshipList relationshipList = genericVnf.getRelationshipList();
76             if (relationshipList == null) {
77                 relationshipList = new RelationshipList();
78                 genericVnf.setRelationshipList(relationshipList);
79             }
80             return relationshipList.getRelationship().add(relationship);
81         }
82         LOGGER.error("Unable to find GenericVnf ...");
83         return false;
84     }
85
86     @Override
87     public Optional<String> getGenericVnfId(final String vnfName) {
88         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
89         if (cache != null) {
90             final Object nativeCache = cache.getNativeCache();
91             if (nativeCache instanceof ConcurrentHashMap) {
92                 @SuppressWarnings("unchecked")
93                 final ConcurrentHashMap<Object, Object> concurrentHashMap =
94                         (ConcurrentHashMap<Object, Object>) nativeCache;
95                 for (final Object key : concurrentHashMap.keySet()) {
96                     final GenericVnf value = cache.get(key, GenericVnf.class);
97                     final String genericVnfName = value.getVnfName();
98                     if (value != null && genericVnfName.equals(vnfName)) {
99                         final String genericVnfId = value.getVnfId();
100                         LOGGER.info("Found matching vnf for name: {}, vnf-id: {}", genericVnfName, genericVnfId);
101                         return Optional.of(genericVnfId);
102                     }
103                 }
104             }
105         }
106         LOGGER.info("No match found for vnf name: {}", vnfName);
107         return Optional.empty();
108     }
109
110     @Override
111     public void clearAll() {
112         clearCahce(GENERIC_VNF_CACHE.getName());
113     }
114
115 }