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