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