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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.aaisimulator.service.providers;
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;
51 * @author Waqas Ikram (waqas.ikram@est.tech)
55 public class GenericVnfCacheServiceProviderImpl extends AbstractCacheServiceProvider
56 implements GenericVnfCacheServiceProvider {
58 private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfCacheServiceProviderImpl.class);
60 private final HttpRestServiceProvider httpRestServiceProvider;
63 public GenericVnfCacheServiceProviderImpl(final CacheManager cacheManager,
64 final HttpRestServiceProvider httpRestServiceProvider) {
66 this.httpRestServiceProvider = httpRestServiceProvider;
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);
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);
82 return Optional.of(value);
84 LOGGER.error("Unable to find GenericVnf ...");
85 return Optional.empty();
89 public Optional<String> getGenericVnfId(final String vnfName) {
90 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
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);
111 LOGGER.error("No match found for vnf name: {}", vnfName);
112 return Optional.empty();
116 public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
117 final String requestUriString, final String vnfId, final Relationship relationship) {
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();
130 RelationshipList relationshipList = genericVnf.getRelationshipList();
131 if (relationshipList == null) {
132 relationshipList = new RelationshipList();
133 genericVnf.setRelationshipList(relationshipList);
135 if (relationshipList.getRelationship().add(resultantRelationship)) {
136 LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
141 } catch (final Exception exception) {
142 LOGGER.error("Unable to add two-way relationship for vnfId: {}", vnfId, exception);
144 LOGGER.error("Unable to add relationship in cache for vnfId: {}", vnfId);
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);
159 relationshipList.getRelationship().add(relationship);
160 LOGGER.info("Successfully added relation to GenericVnf for vnfId: {}", vnfId);
162 final String relatedLink = getBiDirectionalRelationShipListRelatedLink(requestURI);
163 final Relationship resultantRelationship =
164 getRelationship(relatedLink, genericVnf, relationship.getRelationshipLabel());
165 return Optional.of(resultantRelationship);
167 return Optional.empty();
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();
176 ShallowBeanCopy.copy(genericVnf, cachedGenericVnf);
178 } catch (final Exception exception) {
179 LOGGER.error("Unable to update GenericVnf for vnfId: {}", vnfId, exception);
182 LOGGER.error("Unable to find GenericVnf ...");
187 public List<GenericVnf> getGenericVnfs(final String selflink) {
188 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
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<>();
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();
204 if (genericVnfSelfLink != null && genericVnfSelfLink.equals(selflink)) {
205 LOGGER.info("Found matching vnf for selflink: {}, vnf-id: {}", genericVnfSelfLink,
207 result.add(genericVnf);
214 LOGGER.error("No match found for selflink: {}", selflink);
215 return Collections.emptyList();
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());
230 LOGGER.error("Unable to find GenericVnf for vnfId: {} and resourceVersion: {} ...", vnfId, resourceVersion);
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);
241 final RelationshipData relationshipData = new RelationshipData();
242 relationshipData.setRelationshipKey(GENERIC_VNF_VNF_ID);
243 relationshipData.setRelationshipValue(genericVnf.getVnfId());
244 relationShip.getRelationshipData().add(relationshipData);
246 final RelatedToProperty relatedToProperty = new RelatedToProperty();
247 relatedToProperty.setPropertyKey(GENERIC_VNF_VNF_NAME);
248 relatedToProperty.setPropertyValue(genericVnf.getVnfName());
249 relationShip.getRelatedToProperty().add(relatedToProperty);
254 public void clearAll() {
255 clearCache(GENERIC_VNF_CACHE.getName());