Adding AAI ESR endpoints
[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);
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 = getRelationship(relatedLink, genericVnf);
164             return Optional.of(resultantRelationship);
165         }
166         return Optional.empty();
167     }
168
169     @Override
170     public boolean patchGenericVnf(final String vnfId, final GenericVnf genericVnf) {
171         final Optional<GenericVnf> optional = getGenericVnf(vnfId);
172         if (optional.isPresent()) {
173             final GenericVnf cachedGenericVnf = optional.get();
174             try {
175                 ShallowBeanCopy.copy(genericVnf, cachedGenericVnf);
176                 return true;
177             } catch (final Exception exception) {
178                 LOGGER.error("Unable to update GenericVnf for vnfId: {}", vnfId, exception);
179             }
180         }
181         LOGGER.error("Unable to find GenericVnf ...");
182         return false;
183     }
184
185     @Override
186     public List<GenericVnf> getGenericVnfs(final String selflink) {
187         final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
188         if (cache != null) {
189             final Object nativeCache = cache.getNativeCache();
190             if (nativeCache instanceof ConcurrentHashMap) {
191                 @SuppressWarnings("unchecked")
192                 final ConcurrentHashMap<Object, Object> concurrentHashMap =
193                         (ConcurrentHashMap<Object, Object>) nativeCache;
194                 final List<GenericVnf> result = new ArrayList<>();
195
196                 concurrentHashMap.keySet().stream().forEach(key -> {
197                     final Optional<GenericVnf> optional = getGenericVnf(key.toString());
198                     if (optional.isPresent()) {
199                         final GenericVnf genericVnf = optional.get();
200                         final String genericVnfSelfLink = genericVnf.getSelflink();
201                         final String genericVnfId = genericVnf.getSelflink();
202
203                         if (genericVnfSelfLink != null && genericVnfSelfLink.equals(selflink)) {
204                             LOGGER.info("Found matching vnf for selflink: {}, vnf-id: {}", genericVnfSelfLink,
205                                     genericVnfId);
206                             result.add(genericVnf);
207                         }
208                     }
209                 });
210                 return result;
211             }
212         }
213         LOGGER.error("No match found for selflink: {}", selflink);
214         return Collections.emptyList();
215     }
216
217     private Relationship getRelationship(final String relatedLink, final GenericVnf genericVnf) {
218         final Relationship relationShip = new Relationship();
219         relationShip.setRelatedTo(GENERIC_VNF);
220         relationShip.setRelationshipLabel(COMPOSED_OF);
221         relationShip.setRelatedLink(relatedLink);
222
223         final RelationshipData relationshipData = new RelationshipData();
224         relationshipData.setRelationshipKey(GENERIC_VNF_VNF_ID);
225         relationshipData.setRelationshipValue(genericVnf.getVnfId());
226         relationShip.getRelationshipData().add(relationshipData);
227
228         final RelatedToProperty relatedToProperty = new RelatedToProperty();
229         relatedToProperty.setPropertyKey(GENERIC_VNF_VNF_NAME);
230         relatedToProperty.setPropertyValue(genericVnf.getVnfName());
231         relationShip.getRelatedToProperty().add(relatedToProperty);
232         return relationShip;
233     }
234
235     @Override
236     public void clearAll() {
237         clearCache(GENERIC_VNF_CACHE.getName());
238     }
239
240 }