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