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.aai.domain.yang.VfModule;
41 import org.onap.aai.domain.yang.VfModules;
42 import org.onap.so.aaisimulator.utils.ShallowBeanCopy;
43 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.cache.Cache;
48 import org.springframework.cache.CacheManager;
49 import org.springframework.http.HttpHeaders;
50 import org.springframework.stereotype.Service;
53 * @author Waqas Ikram (waqas.ikram@est.tech)
57 public class GenericVnfCacheServiceProviderImpl extends AbstractCacheServiceProvider
58 implements GenericVnfCacheServiceProvider {
60 private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfCacheServiceProviderImpl.class);
62 private final HttpRestServiceProvider httpRestServiceProvider;
65 public GenericVnfCacheServiceProviderImpl(final CacheManager cacheManager,
66 final HttpRestServiceProvider httpRestServiceProvider) {
68 this.httpRestServiceProvider = httpRestServiceProvider;
72 public void putGenericVnf(final String vnfId, final GenericVnf genericVnf) {
73 LOGGER.info("Adding customer: {} with key: {} in cache ...", genericVnf, vnfId);
74 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
75 cache.put(vnfId, genericVnf);
79 public Optional<GenericVnf> getGenericVnf(final String vnfId) {
80 LOGGER.info("getting GenericVnf from cache using key: {}", vnfId);
81 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
82 final GenericVnf value = cache.get(vnfId, GenericVnf.class);
84 return Optional.of(value);
86 LOGGER.error("Unable to find GenericVnf ...");
87 return Optional.empty();
91 public Optional<String> getGenericVnfId(final String vnfName) {
92 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
94 final Object nativeCache = cache.getNativeCache();
95 if (nativeCache instanceof ConcurrentHashMap) {
96 @SuppressWarnings("unchecked")
97 final ConcurrentHashMap<Object, Object> concurrentHashMap =
98 (ConcurrentHashMap<Object, Object>) nativeCache;
99 for (final Object key : concurrentHashMap.keySet()) {
100 final Optional<GenericVnf> optional = getGenericVnf(key.toString());
101 if (optional.isPresent()) {
102 final GenericVnf value = optional.get();
103 final String genericVnfName = value.getVnfName();
104 if (genericVnfName != null && genericVnfName.equals(vnfName)) {
105 final String genericVnfId = value.getVnfId();
106 LOGGER.info("Found matching vnf for name: {}, vnf-id: {}", genericVnfName, genericVnfId);
107 return Optional.of(genericVnfId);
113 LOGGER.error("No match found for vnf name: {}", vnfName);
114 return Optional.empty();
118 public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
119 final String requestUriString, final String vnfId, final Relationship relationship) {
121 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
122 if (optional.isPresent()) {
123 final GenericVnf genericVnf = optional.get();
124 final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
125 final Relationship outGoingRelationShip =
126 getRelationship(getRelationShipListRelatedLink(requestUriString), genericVnf, COMPOSED_OF);
127 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
128 outGoingRelationShip, targetUrl, Relationship.class);
129 if (optionalRelationship.isPresent()) {
130 final Relationship resultantRelationship = optionalRelationship.get();
132 RelationshipList relationshipList = genericVnf.getRelationshipList();
133 if (relationshipList == null) {
134 relationshipList = new RelationshipList();
135 genericVnf.setRelationshipList(relationshipList);
137 if (relationshipList.getRelationship().add(resultantRelationship)) {
138 LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
143 } catch (final Exception exception) {
144 LOGGER.error("Unable to add two-way relationship for vnfId: {}", vnfId, exception);
146 LOGGER.error("Unable to add relationship in cache for vnfId: {}", vnfId);
151 public Optional<Relationship> addRelationShip(final String vnfId, final Relationship relationship,
152 final String requestURI) {
153 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
154 if (optional.isPresent()) {
155 final GenericVnf genericVnf = optional.get();
156 RelationshipList relationshipList = genericVnf.getRelationshipList();
157 if (relationshipList == null) {
158 relationshipList = new RelationshipList();
159 genericVnf.setRelationshipList(relationshipList);
161 relationshipList.getRelationship().add(relationship);
162 LOGGER.info("Successfully added relation to GenericVnf for vnfId: {}", vnfId);
164 final String relatedLink = getBiDirectionalRelationShipListRelatedLink(requestURI);
165 final Relationship resultantRelationship =
166 getRelationship(relatedLink, genericVnf, relationship.getRelationshipLabel());
167 return Optional.of(resultantRelationship);
169 return Optional.empty();
173 public boolean patchGenericVnf(final String vnfId, final GenericVnf genericVnf) {
174 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
175 if (optional.isPresent()) {
176 final GenericVnf cachedGenericVnf = optional.get();
178 ShallowBeanCopy.copy(genericVnf, cachedGenericVnf);
180 } catch (final Exception exception) {
181 LOGGER.error("Unable to update GenericVnf for vnfId: {}", vnfId, exception);
184 LOGGER.error("Unable to find GenericVnf ...");
189 public List<GenericVnf> getGenericVnfs(final String selflink) {
190 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
192 final Object nativeCache = cache.getNativeCache();
193 if (nativeCache instanceof ConcurrentHashMap) {
194 @SuppressWarnings("unchecked")
195 final ConcurrentHashMap<Object, Object> concurrentHashMap =
196 (ConcurrentHashMap<Object, Object>) nativeCache;
197 final List<GenericVnf> result = new ArrayList<>();
199 concurrentHashMap.keySet().stream().forEach(key -> {
200 final Optional<GenericVnf> optional = getGenericVnf(key.toString());
201 if (optional.isPresent()) {
202 final GenericVnf genericVnf = optional.get();
203 final String genericVnfSelfLink = genericVnf.getSelflink();
204 final String genericVnfId = genericVnf.getSelflink();
206 if (genericVnfSelfLink != null && genericVnfSelfLink.equals(selflink)) {
207 LOGGER.info("Found matching vnf for selflink: {}, vnf-id: {}", genericVnfSelfLink,
209 result.add(genericVnf);
216 LOGGER.error("No match found for selflink: {}", selflink);
217 return Collections.emptyList();
221 public boolean deleteGenericVnf(final String vnfId, final String resourceVersion) {
222 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
223 if (optional.isPresent()) {
224 final GenericVnf genericVnf = optional.get();
225 if (genericVnf.getResourceVersion() != null && genericVnf.getResourceVersion().equals(resourceVersion)) {
226 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
227 LOGGER.info("Will evict GenericVnf from cache with vnfId: {}", genericVnf.getVnfId());
232 LOGGER.error("Unable to find GenericVnf for vnfId: {} and resourceVersion: {} ...", vnfId, resourceVersion);
236 private Relationship getRelationship(final String relatedLink, final GenericVnf genericVnf,
237 final String relationshipLabel) {
238 final Relationship relationShip = new Relationship();
239 relationShip.setRelatedTo(GENERIC_VNF);
240 relationShip.setRelationshipLabel(relationshipLabel);
241 relationShip.setRelatedLink(relatedLink);
243 final RelationshipData relationshipData = new RelationshipData();
244 relationshipData.setRelationshipKey(GENERIC_VNF_VNF_ID);
245 relationshipData.setRelationshipValue(genericVnf.getVnfId());
246 relationShip.getRelationshipData().add(relationshipData);
248 final RelatedToProperty relatedToProperty = new RelatedToProperty();
249 relatedToProperty.setPropertyKey(GENERIC_VNF_VNF_NAME);
250 relatedToProperty.setPropertyValue(genericVnf.getVnfName());
251 relationShip.getRelatedToProperty().add(relatedToProperty);
256 public void clearAll() {
257 clearCache(GENERIC_VNF_CACHE.getName());
261 public Optional<VfModule> getVfModule(final String vnfId, final String vfModuleId) {
262 LOGGER.info("Getting vfModule from cache for vnfId: {} and vfModuleId: {}",
264 final Optional<GenericVnf> genericVnfOptional = getGenericVnf(vnfId);
265 final GenericVnf value = genericVnfOptional.get();
266 final VfModules vfmodules = value.getVfModules();
267 if (vfmodules != null) {
268 for (VfModule vfModule : vfmodules.getVfModule()) {
269 if (vfModule.getVfModuleId().equalsIgnoreCase(vfModuleId)){
270 return Optional.of(vfModule);
274 return Optional.empty();
279 public void putVfModule(final String vnfId, final String vfModuleId, final VfModule vfModule) {
280 LOGGER.info("Adding vfModule for vnfId: {} and vfModuleId: {}",
282 final Optional<GenericVnf> genericVnfOptional = getGenericVnf(vnfId);
283 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
284 if (genericVnfOptional.isPresent()) {
285 final GenericVnf genericVnf = genericVnfOptional.get();
286 VfModules vfModules = null;
287 if(genericVnf.getVfModules()==null){
288 vfModules = new VfModules();
289 genericVnf.setVfModules(vfModules);
291 vfModules = genericVnf.getVfModules();
294 vfModules.getVfModule().add(vfModule);
295 cache.put(vfModuleId, vfModule);
300 public boolean patchVfModule(final String vnfId, final String vfModuleId, final VfModule vfModule) {
301 final Optional<GenericVnf> genericVnfOptional = getGenericVnf(vnfId);
302 LOGGER.info("Create vfModule for vnfId: {} and vfModuleId: {}",
304 if (genericVnfOptional.isPresent()) {
305 final GenericVnf cachedGenericVnf = genericVnfOptional.get();
306 final VfModules vfmodules = cachedGenericVnf.getVfModules();
307 LOGGER.info("vfModuleId is Matched");
309 vfmodules.getVfModule().stream().filter(tempVfModule ->
310 tempVfModule.getVfModuleId().equalsIgnoreCase(vfModuleId)).forEach(tempVfModule ->
311 tempVfModule.setOrchestrationStatus(vfModule.getOrchestrationStatus()));
313 } catch (final Exception exception) {
314 LOGGER.error("Unable to update VfModule for vfModuleId: {}", vfModule, exception);
317 LOGGER.error("Unable to find VfModule ...");