Update docker-compose for Macroflow with HEAT and small refactoring
[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 static org.onap.so.aaisimulator.utils.Constants.COMPOSED_OF;
24 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF;
25 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF_VNF_ID;
26 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF_VNF_NAME;
27 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getBiDirectionalRelationShipListRelatedLink;
28 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getRelationShipListRelatedLink;
29 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getTargetUrl;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Optional;
34 import java.util.concurrent.ConcurrentHashMap;
35 import org.onap.aai.domain.yang.GenericVnf;
36 import org.onap.aai.domain.yang.RelatedToProperty;
37 import org.onap.aai.domain.yang.Relationship;
38 import org.onap.aai.domain.yang.RelationshipData;
39 import org.onap.aai.domain.yang.RelationshipList;
40 import org.onap.aai.domain.yang.VfModule;
41 import org.onap.aai.domain.yang.VfModules;
42 import org.onap.so.aaisimulator.utils.ShallowBeanCopy;
43 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.cache.Cache;
48 import org.springframework.cache.CacheManager;
49 import org.springframework.http.HttpHeaders;
50 import org.springframework.stereotype.Service;
51
52 /**
53  * @author Waqas Ikram (waqas.ikram@est.tech)
54  *
55  */
56 @Service
57 public class GenericVnfCacheServiceProviderImpl extends AbstractCacheServiceProvider
58         implements GenericVnfCacheServiceProvider {
59
60     private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfCacheServiceProviderImpl.class);
61
62     private final HttpRestServiceProvider httpRestServiceProvider;
63
64     @Autowired
65     public GenericVnfCacheServiceProviderImpl(final CacheManager cacheManager,
66             final HttpRestServiceProvider httpRestServiceProvider) {
67         super(cacheManager);
68         this.httpRestServiceProvider = httpRestServiceProvider;
69     }
70
71     @Override
72     public void putGenericVnf(final String vnfId, final GenericVnf genericVnf) {
73         LOGGER.info("Adding customer: {} with key: {} in cache ...", genericVnf, vnfId);
74         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
75         cache.put(vnfId, genericVnf);
76     }
77
78     @Override
79     public Optional<GenericVnf> getGenericVnf(final String vnfId) {
80         LOGGER.info("getting GenericVnf from cache using key: {}", vnfId);
81         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
82         final GenericVnf value = cache.get(vnfId, GenericVnf.class);
83         if (value != null) {
84             return Optional.of(value);
85         }
86         LOGGER.error("Unable to find GenericVnf ...");
87         return Optional.empty();
88     }
89
90     @Override
91     public Optional<String> getGenericVnfId(final String vnfName) {
92         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
93         if (cache != null) {
94             final Object nativeCache = cache.getNativeCache();
95             if (nativeCache instanceof ConcurrentHashMap) {
96                 @SuppressWarnings("unchecked")
97                 final ConcurrentHashMap<Object, Object> concurrentHashMap =
98                         (ConcurrentHashMap<Object, Object>) nativeCache;
99                 for (final Object key : concurrentHashMap.keySet()) {
100                     final Optional<GenericVnf> optional = getGenericVnf(key.toString());
101                     if (optional.isPresent()) {
102                         final GenericVnf value = optional.get();
103                         final String genericVnfName = value.getVnfName();
104                         if (genericVnfName != null && genericVnfName.equals(vnfName)) {
105                             final String genericVnfId = value.getVnfId();
106                             LOGGER.info("Found matching vnf for name: {}, vnf-id: {}", genericVnfName, genericVnfId);
107                             return Optional.of(genericVnfId);
108                         }
109                     }
110                 }
111             }
112         }
113         LOGGER.error("No match found for vnf name: {}", vnfName);
114         return Optional.empty();
115     }
116
117     @Override
118     public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
119             final String requestUriString, final String vnfId, final Relationship relationship) {
120         try {
121             final Optional<GenericVnf> optional = getGenericVnf(vnfId);
122             if (optional.isPresent()) {
123                 final GenericVnf genericVnf = optional.get();
124                 final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
125                 final Relationship outGoingRelationShip =
126                         getRelationship(getRelationShipListRelatedLink(requestUriString), genericVnf, COMPOSED_OF);
127                 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
128                         outGoingRelationShip, targetUrl, Relationship.class);
129                 if (optionalRelationship.isPresent()) {
130                     final Relationship resultantRelationship = optionalRelationship.get();
131
132                     RelationshipList relationshipList = genericVnf.getRelationshipList();
133                     if (relationshipList == null) {
134                         relationshipList = new RelationshipList();
135                         genericVnf.setRelationshipList(relationshipList);
136                     }
137                     if (relationshipList.getRelationship().add(resultantRelationship)) {
138                         LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
139                         return true;
140                     }
141                 }
142             }
143         } catch (final Exception exception) {
144             LOGGER.error("Unable to add two-way relationship for vnfId: {}", vnfId, exception);
145         }
146         LOGGER.error("Unable to add relationship in cache for vnfId: {}", vnfId);
147         return false;
148     }
149
150     @Override
151     public Optional<Relationship> addRelationShip(final String vnfId, final Relationship relationship,
152             final String requestURI) {
153         final Optional<GenericVnf> optional = getGenericVnf(vnfId);
154         if (optional.isPresent()) {
155             final GenericVnf genericVnf = optional.get();
156             RelationshipList relationshipList = genericVnf.getRelationshipList();
157             if (relationshipList == null) {
158                 relationshipList = new RelationshipList();
159                 genericVnf.setRelationshipList(relationshipList);
160             }
161             relationshipList.getRelationship().add(relationship);
162             LOGGER.info("Successfully added relation to GenericVnf for vnfId: {}", vnfId);
163
164             final String relatedLink = getBiDirectionalRelationShipListRelatedLink(requestURI);
165             final Relationship resultantRelationship =
166                     getRelationship(relatedLink, genericVnf, relationship.getRelationshipLabel());
167             return Optional.of(resultantRelationship);
168         }
169         return Optional.empty();
170     }
171
172     @Override
173     public boolean patchGenericVnf(final String vnfId, final GenericVnf genericVnf) {
174         final Optional<GenericVnf> optional = getGenericVnf(vnfId);
175         if (optional.isPresent()) {
176             final GenericVnf cachedGenericVnf = optional.get();
177             try {
178                 ShallowBeanCopy.copy(genericVnf, cachedGenericVnf);
179                 return true;
180             } catch (final Exception exception) {
181                 LOGGER.error("Unable to update GenericVnf for vnfId: {}", vnfId, exception);
182             }
183         }
184         LOGGER.error("Unable to find GenericVnf ...");
185         return false;
186     }
187
188     @Override
189     public List<GenericVnf> getGenericVnfs(final String selflink) {
190         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
191         if (cache != null) {
192             final Object nativeCache = cache.getNativeCache();
193             if (nativeCache instanceof ConcurrentHashMap) {
194                 @SuppressWarnings("unchecked")
195                 final ConcurrentHashMap<Object, Object> concurrentHashMap =
196                         (ConcurrentHashMap<Object, Object>) nativeCache;
197                 final List<GenericVnf> result = new ArrayList<>();
198
199                 concurrentHashMap.keySet().stream().forEach(key -> {
200                     final Optional<GenericVnf> optional = getGenericVnf(key.toString());
201                     if (optional.isPresent()) {
202                         final GenericVnf genericVnf = optional.get();
203                         final String genericVnfSelfLink = genericVnf.getSelflink();
204                         final String genericVnfId = genericVnf.getSelflink();
205
206                         if (genericVnfSelfLink != null && genericVnfSelfLink.equals(selflink)) {
207                             LOGGER.info("Found matching vnf for selflink: {}, vnf-id: {}", genericVnfSelfLink,
208                                     genericVnfId);
209                             result.add(genericVnf);
210                         }
211                     }
212                 });
213                 return result;
214             }
215         }
216         LOGGER.error("No match found for selflink: {}", selflink);
217         return Collections.emptyList();
218     }
219
220     @Override
221     public boolean deleteGenericVnf(final String vnfId, final String resourceVersion) {
222         final Optional<GenericVnf> optional = getGenericVnf(vnfId);
223         if (optional.isPresent()) {
224             final GenericVnf genericVnf = optional.get();
225             if (genericVnf.getResourceVersion() != null && genericVnf.getResourceVersion().equals(resourceVersion)) {
226                 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
227                 LOGGER.info("Will evict GenericVnf from cache with vnfId: {}", genericVnf.getVnfId());
228                 cache.evict(vnfId);
229                 return true;
230             }
231         }
232         LOGGER.error("Unable to find GenericVnf for vnfId: {} and resourceVersion: {} ...", vnfId, resourceVersion);
233         return false;
234     }
235
236     private Relationship getRelationship(final String relatedLink, final GenericVnf genericVnf,
237             final String relationshipLabel) {
238         final Relationship relationShip = new Relationship();
239         relationShip.setRelatedTo(GENERIC_VNF);
240         relationShip.setRelationshipLabel(relationshipLabel);
241         relationShip.setRelatedLink(relatedLink);
242
243         final RelationshipData relationshipData = new RelationshipData();
244         relationshipData.setRelationshipKey(GENERIC_VNF_VNF_ID);
245         relationshipData.setRelationshipValue(genericVnf.getVnfId());
246         relationShip.getRelationshipData().add(relationshipData);
247
248         final RelatedToProperty relatedToProperty = new RelatedToProperty();
249         relatedToProperty.setPropertyKey(GENERIC_VNF_VNF_NAME);
250         relatedToProperty.setPropertyValue(genericVnf.getVnfName());
251         relationShip.getRelatedToProperty().add(relatedToProperty);
252         return relationShip;
253     }
254
255     @Override
256     public void clearAll() {
257         clearCache(GENERIC_VNF_CACHE.getName());
258     }
259
260     @Override
261     public Optional<VfModule> getVfModule(final String vnfId, final String vfModuleId) {
262         LOGGER.info("Getting vfModule from cache for vnfId: {} and vfModuleId: {}",
263                 vnfId, vfModuleId);
264         final Optional<GenericVnf> genericVnfOptional = getGenericVnf(vnfId);
265         final GenericVnf value = genericVnfOptional.get();
266         final VfModules vfmodules = value.getVfModules();
267         if (vfmodules != null) {
268             for (VfModule vfModule : vfmodules.getVfModule()) {
269                  if (vfModule.getVfModuleId().equalsIgnoreCase(vfModuleId)){
270                     return Optional.of(vfModule);
271                 }
272             }
273         }
274         return Optional.empty();
275     }
276
277
278     @Override
279     public void putVfModule(final String vnfId, final String vfModuleId, final VfModule vfModule) {
280         LOGGER.info("Adding vfModule for vnfId: {} and vfModuleId: {}",
281                 vnfId, vfModuleId);
282         final Optional<GenericVnf> genericVnfOptional = getGenericVnf(vnfId);
283         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
284         if (genericVnfOptional.isPresent()) {
285             final GenericVnf genericVnf = genericVnfOptional.get();
286             VfModules vfModules = null;
287             if(genericVnf.getVfModules()==null){
288                 vfModules = new VfModules();
289                 genericVnf.setVfModules(vfModules);
290             } else {
291                 vfModules = genericVnf.getVfModules();
292             }
293
294             vfModules.getVfModule().add(vfModule);
295             cache.put(vfModuleId, vfModule);
296         }
297     }
298
299     @Override
300     public boolean patchVfModule(final String vnfId, final String vfModuleId, final VfModule vfModule) {
301         final Optional<GenericVnf> genericVnfOptional = getGenericVnf(vnfId);
302         LOGGER.info("Create vfModule for vnfId: {} and vfModuleId: {}",
303                 vnfId, vfModuleId);
304         if (genericVnfOptional.isPresent()) {
305             final GenericVnf cachedGenericVnf = genericVnfOptional.get();
306             final VfModules vfmodules = cachedGenericVnf.getVfModules();
307             LOGGER.info("vfModuleId is Matched");
308             try {
309                 vfmodules.getVfModule().stream().filter(tempVfModule ->
310                         tempVfModule.getVfModuleId().equalsIgnoreCase(vfModuleId)).forEach(tempVfModule ->
311                         tempVfModule.setOrchestrationStatus(vfModule.getOrchestrationStatus()));
312                 return true;
313             } catch (final Exception exception) {
314                 LOGGER.error("Unable to update VfModule for vfModuleId: {}", vfModule, exception);
315             }
316         }
317         LOGGER.error("Unable to find VfModule ...");
318         return false;
319     }
320 }