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.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL;
24 import static org.onap.so.aaisimulator.utils.Constants.COMPOSED_OF;
25 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF;
26 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF_VNF_ID;
27 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF_VNF_NAME;
28 import java.util.Optional;
29 import java.util.concurrent.ConcurrentHashMap;
30 import org.onap.aai.domain.yang.GenericVnf;
31 import org.onap.aai.domain.yang.RelatedToProperty;
32 import org.onap.aai.domain.yang.Relationship;
33 import org.onap.aai.domain.yang.RelationshipData;
34 import org.onap.aai.domain.yang.RelationshipList;
35 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.cache.Cache;
40 import org.springframework.cache.CacheManager;
41 import org.springframework.http.HttpHeaders;
42 import org.springframework.stereotype.Service;
43 import org.springframework.web.util.UriComponentsBuilder;
46 * @author Waqas Ikram (waqas.ikram@est.tech)
50 public class GenericVnfCacheServiceProviderImpl extends AbstractCacheServiceProvider
51 implements GenericVnfCacheServiceProvider {
53 private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfCacheServiceProviderImpl.class);
55 private final HttpRestServiceProvider httpRestServiceProvider;
58 public GenericVnfCacheServiceProviderImpl(final CacheManager cacheManager,
59 final HttpRestServiceProvider httpRestServiceProvider) {
61 this.httpRestServiceProvider = httpRestServiceProvider;
65 public void putGenericVnf(final String vnfId, final GenericVnf genericVnf) {
66 LOGGER.info("Adding customer: {} with key: {} in cache ...", genericVnf, vnfId);
67 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
68 cache.put(vnfId, genericVnf);
72 public Optional<GenericVnf> getGenericVnf(final String vnfId) {
73 LOGGER.info("getting GenericVnf from cache using key: {}", vnfId);
74 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
75 final GenericVnf value = cache.get(vnfId, GenericVnf.class);
77 return Optional.of(value);
79 LOGGER.error("Unable to find GenericVnf ...");
80 return Optional.empty();
84 public Optional<String> getGenericVnfId(final String vnfName) {
85 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
87 final Object nativeCache = cache.getNativeCache();
88 if (nativeCache instanceof ConcurrentHashMap) {
89 @SuppressWarnings("unchecked")
90 final ConcurrentHashMap<Object, Object> concurrentHashMap =
91 (ConcurrentHashMap<Object, Object>) nativeCache;
92 for (final Object key : concurrentHashMap.keySet()) {
93 final GenericVnf value = cache.get(key, GenericVnf.class);
94 final String genericVnfName = value.getVnfName();
95 if (value != null && genericVnfName.equals(vnfName)) {
96 final String genericVnfId = value.getVnfId();
97 LOGGER.info("Found matching vnf for name: {}, vnf-id: {}", genericVnfName, genericVnfId);
98 return Optional.of(genericVnfId);
103 LOGGER.info("No match found for vnf name: {}", vnfName);
104 return Optional.empty();
108 public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
109 final String requestUriString, final String vnfId, final Relationship relationship) {
111 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
112 if (optional.isPresent()) {
113 final GenericVnf genericVnf = optional.get();
114 final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
115 final Relationship outGoingRelationShip = getRelationship(requestUriString, genericVnf);
116 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
117 outGoingRelationShip, targetUrl, Relationship.class);
118 if (optionalRelationship.isPresent()) {
119 final Relationship resultantRelationship = optionalRelationship.get();
121 RelationshipList relationshipList = genericVnf.getRelationshipList();
122 if (relationshipList == null) {
123 relationshipList = new RelationshipList();
124 genericVnf.setRelationshipList(relationshipList);
126 if (relationshipList.getRelationship().add(resultantRelationship)) {
127 LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
132 } catch (final Exception exception) {
133 LOGGER.error("Unable to add two-way relationship for vnfId: {}", vnfId, exception);
135 LOGGER.error("Unable to add relationship in cache for vnfId: {}", vnfId);
140 public Optional<Relationship> addRelationShip(final String vnfId, final Relationship relationship,
141 final String requestURI) {
142 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
143 if (optional.isPresent()) {
144 final GenericVnf genericVnf = optional.get();
145 RelationshipList relationshipList = genericVnf.getRelationshipList();
146 if (relationshipList == null) {
147 relationshipList = new RelationshipList();
148 genericVnf.setRelationshipList(relationshipList);
150 relationshipList.getRelationship().add(relationship);
151 LOGGER.info("Successfully added relation to GenericVnf for vnfId: {}", vnfId);
153 final Relationship resultantRelationship = getRelationship(requestURI, genericVnf);
154 return Optional.of(resultantRelationship);
156 return Optional.empty();
160 public boolean patchGenericVnf(final String vnfId, final GenericVnf genericVnf) {
161 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
162 if (optional.isPresent()) {
163 final GenericVnf cachedGenericVnf = optional.get();
164 LOGGER.info("Changing OrchestrationStatus from {} to {} ", cachedGenericVnf.getOrchestrationStatus(),
165 genericVnf.getOrchestrationStatus());
166 cachedGenericVnf.setOrchestrationStatus(genericVnf.getOrchestrationStatus());
169 LOGGER.error("Unable to find GenericVnf ...");
173 private String getTargetUrl(final String targetBaseUrl, final String relatedLink) {
174 return UriComponentsBuilder.fromUriString(targetBaseUrl).path(relatedLink)
175 .path(BI_DIRECTIONAL_RELATIONSHIP_LIST_URL).toUriString();
178 private Relationship getRelationship(final String relatedLink, final GenericVnf genericVnf) {
179 final Relationship relationShip = new Relationship();
180 relationShip.setRelatedTo(GENERIC_VNF);
181 relationShip.setRelationshipLabel(COMPOSED_OF);
182 relationShip.setRelatedLink(relatedLink);
184 final RelationshipData relationshipData = new RelationshipData();
185 relationshipData.setRelationshipKey(GENERIC_VNF_VNF_ID);
186 relationshipData.setRelationshipValue(genericVnf.getVnfId());
187 relationShip.getRelationshipData().add(relationshipData);
189 final RelatedToProperty relatedToProperty = new RelatedToProperty();
190 relatedToProperty.setPropertyKey(GENERIC_VNF_VNF_NAME);
191 relatedToProperty.setPropertyValue(genericVnf.getVnfName());
192 relationShip.getRelatedToProperty().add(relatedToProperty);
197 public void clearAll() {
198 clearCahce(GENERIC_VNF_CACHE.getName());