e7a42106199e2bc080a1cdcf61ef72afa9e3cbd6
[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.so.aaisimulator.utils.ShallowBeanCopy;
41 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.cache.Cache;
46 import org.springframework.cache.CacheManager;
47 import org.springframework.http.HttpHeaders;
48 import org.springframework.stereotype.Service;
49
50 /**
51  * @author Waqas Ikram (waqas.ikram@est.tech)
52  *
53  */
54 @Service
55 public class GenericVnfCacheServiceProviderImpl extends AbstractCacheServiceProvider
56         implements GenericVnfCacheServiceProvider {
57
58     private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfCacheServiceProviderImpl.class);
59
60     private final HttpRestServiceProvider httpRestServiceProvider;
61
62     @Autowired
63     public GenericVnfCacheServiceProviderImpl(final CacheManager cacheManager,
64             final HttpRestServiceProvider httpRestServiceProvider) {
65         super(cacheManager);
66         this.httpRestServiceProvider = httpRestServiceProvider;
67     }
68
69     @Override
70     public void putGenericVnf(final String vnfId, final GenericVnf genericVnf) {
71         LOGGER.info("Adding customer: {} with key: {} in cache ...", genericVnf, vnfId);
72         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
73         cache.put(vnfId, genericVnf);
74     }
75
76     @Override
77     public Optional<GenericVnf> getGenericVnf(final String vnfId) {
78         LOGGER.info("getting GenericVnf from cache using key: {}", vnfId);
79         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
80         final GenericVnf value = cache.get(vnfId, GenericVnf.class);
81         if (value != null) {
82             return Optional.of(value);
83         }
84         LOGGER.error("Unable to find GenericVnf ...");
85         return Optional.empty();
86     }
87
88     @Override
89     public Optional<String> getGenericVnfId(final String vnfName) {
90         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
91         if (cache != null) {
92             final Object nativeCache = cache.getNativeCache();
93             if (nativeCache instanceof ConcurrentHashMap) {
94                 @SuppressWarnings("unchecked")
95                 final ConcurrentHashMap<Object, Object> concurrentHashMap =
96                         (ConcurrentHashMap<Object, Object>) nativeCache;
97                 for (final Object key : concurrentHashMap.keySet()) {
98                     final Optional<GenericVnf> optional = getGenericVnf(key.toString());
99                     if (optional.isPresent()) {
100                         final GenericVnf value = optional.get();
101                         final String genericVnfName = value.getVnfName();
102                         if (genericVnfName != null && genericVnfName.equals(vnfName)) {
103                             final String genericVnfId = value.getVnfId();
104                             LOGGER.info("Found matching vnf for name: {}, vnf-id: {}", genericVnfName, genericVnfId);
105                             return Optional.of(genericVnfId);
106                         }
107                     }
108                 }
109             }
110         }
111         LOGGER.error("No match found for vnf name: {}", vnfName);
112         return Optional.empty();
113     }
114
115     @Override
116     public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
117             final String requestUriString, final String vnfId, final Relationship relationship) {
118         try {
119             final Optional<GenericVnf> optional = getGenericVnf(vnfId);
120             if (optional.isPresent()) {
121                 final GenericVnf genericVnf = optional.get();
122                 final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
123                 final Relationship outGoingRelationShip =
124                         getRelationship(getRelationShipListRelatedLink(requestUriString), genericVnf, COMPOSED_OF);
125                 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
126                         outGoingRelationShip, targetUrl, Relationship.class);
127                 if (optionalRelationship.isPresent()) {
128                     final Relationship resultantRelationship = optionalRelationship.get();
129
130                     RelationshipList relationshipList = genericVnf.getRelationshipList();
131                     if (relationshipList == null) {
132                         relationshipList = new RelationshipList();
133                         genericVnf.setRelationshipList(relationshipList);
134                     }
135                     if (relationshipList.getRelationship().add(resultantRelationship)) {
136                         LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
137                         return true;
138                     }
139                 }
140             }
141         } catch (final Exception exception) {
142             LOGGER.error("Unable to add two-way relationship for vnfId: {}", vnfId, exception);
143         }
144         LOGGER.error("Unable to add relationship in cache for vnfId: {}", vnfId);
145         return false;
146     }
147
148     @Override
149     public Optional<Relationship> addRelationShip(final String vnfId, final Relationship relationship,
150             final String requestURI) {
151         final Optional<GenericVnf> optional = getGenericVnf(vnfId);
152         if (optional.isPresent()) {
153             final GenericVnf genericVnf = optional.get();
154             RelationshipList relationshipList = genericVnf.getRelationshipList();
155             if (relationshipList == null) {
156                 relationshipList = new RelationshipList();
157                 genericVnf.setRelationshipList(relationshipList);
158             }
159             relationshipList.getRelationship().add(relationship);
160             LOGGER.info("Successfully added relation to GenericVnf for vnfId: {}", vnfId);
161
162             final String relatedLink = getBiDirectionalRelationShipListRelatedLink(requestURI);
163             final Relationship resultantRelationship =
164                     getRelationship(relatedLink, genericVnf, relationship.getRelationshipLabel());
165             return Optional.of(resultantRelationship);
166         }
167         return Optional.empty();
168     }
169
170     @Override
171     public boolean patchGenericVnf(final String vnfId, final GenericVnf genericVnf) {
172         final Optional<GenericVnf> optional = getGenericVnf(vnfId);
173         if (optional.isPresent()) {
174             final GenericVnf cachedGenericVnf = optional.get();
175             try {
176                 ShallowBeanCopy.copy(genericVnf, cachedGenericVnf);
177                 return true;
178             } catch (final Exception exception) {
179                 LOGGER.error("Unable to update GenericVnf for vnfId: {}", vnfId, exception);
180             }
181         }
182         LOGGER.error("Unable to find GenericVnf ...");
183         return false;
184     }
185
186     @Override
187     public List<GenericVnf> getGenericVnfs(final String selflink) {
188         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
189         if (cache != null) {
190             final Object nativeCache = cache.getNativeCache();
191             if (nativeCache instanceof ConcurrentHashMap) {
192                 @SuppressWarnings("unchecked")
193                 final ConcurrentHashMap<Object, Object> concurrentHashMap =
194                         (ConcurrentHashMap<Object, Object>) nativeCache;
195                 final List<GenericVnf> result = new ArrayList<>();
196
197                 concurrentHashMap.keySet().stream().forEach(key -> {
198                     final Optional<GenericVnf> optional = getGenericVnf(key.toString());
199                     if (optional.isPresent()) {
200                         final GenericVnf genericVnf = optional.get();
201                         final String genericVnfSelfLink = genericVnf.getSelflink();
202                         final String genericVnfId = genericVnf.getSelflink();
203
204                         if (genericVnfSelfLink != null && genericVnfSelfLink.equals(selflink)) {
205                             LOGGER.info("Found matching vnf for selflink: {}, vnf-id: {}", genericVnfSelfLink,
206                                     genericVnfId);
207                             result.add(genericVnf);
208                         }
209                     }
210                 });
211                 return result;
212             }
213         }
214         LOGGER.error("No match found for selflink: {}", selflink);
215         return Collections.emptyList();
216     }
217
218     @Override
219     public boolean deleteGenericVnf(final String vnfId, final String resourceVersion) {
220         final Optional<GenericVnf> optional = getGenericVnf(vnfId);
221         if (optional.isPresent()) {
222             final GenericVnf genericVnf = optional.get();
223             if (genericVnf.getResourceVersion() != null && genericVnf.getResourceVersion().equals(resourceVersion)) {
224                 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
225                 LOGGER.info("Will evict GenericVnf from cache with vnfId: {}", genericVnf.getVnfId());
226                 cache.evict(vnfId);
227                 return true;
228             }
229         }
230         LOGGER.error("Unable to find GenericVnf for vnfId: {} and resourceVersion: {} ...", vnfId, resourceVersion);
231         return false;
232     }
233
234     private Relationship getRelationship(final String relatedLink, final GenericVnf genericVnf,
235             final String relationshipLabel) {
236         final Relationship relationShip = new Relationship();
237         relationShip.setRelatedTo(GENERIC_VNF);
238         relationShip.setRelationshipLabel(relationshipLabel);
239         relationShip.setRelatedLink(relatedLink);
240
241         final RelationshipData relationshipData = new RelationshipData();
242         relationshipData.setRelationshipKey(GENERIC_VNF_VNF_ID);
243         relationshipData.setRelationshipValue(genericVnf.getVnfId());
244         relationShip.getRelationshipData().add(relationshipData);
245
246         final RelatedToProperty relatedToProperty = new RelatedToProperty();
247         relatedToProperty.setPropertyKey(GENERIC_VNF_VNF_NAME);
248         relatedToProperty.setPropertyValue(genericVnf.getVnfName());
249         relationShip.getRelatedToProperty().add(relatedToProperty);
250         return relationShip;
251     }
252
253     @Override
254     public void clearAll() {
255         clearCache(GENERIC_VNF_CACHE.getName());
256     }
257
258 }