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;
63 //final VfModules vfModules = new VfModules();
66 public GenericVnfCacheServiceProviderImpl(final CacheManager cacheManager,
67 final HttpRestServiceProvider httpRestServiceProvider) {
69 this.httpRestServiceProvider = httpRestServiceProvider;
73 public void putGenericVnf(final String vnfId, final GenericVnf genericVnf) {
74 LOGGER.info("Adding customer: {} with key: {} in cache ...", genericVnf, vnfId);
75 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
76 cache.put(vnfId, genericVnf);
80 public Optional<GenericVnf> getGenericVnf(final String vnfId) {
81 LOGGER.info("getting GenericVnf from cache using key: {}", vnfId);
82 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
83 final GenericVnf value = cache.get(vnfId, GenericVnf.class);
85 return Optional.of(value);
87 LOGGER.error("Unable to find GenericVnf ...");
88 return Optional.empty();
92 public Optional<String> getGenericVnfId(final String vnfName) {
93 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
95 final Object nativeCache = cache.getNativeCache();
96 if (nativeCache instanceof ConcurrentHashMap) {
97 @SuppressWarnings("unchecked")
98 final ConcurrentHashMap<Object, Object> concurrentHashMap =
99 (ConcurrentHashMap<Object, Object>) nativeCache;
100 for (final Object key : concurrentHashMap.keySet()) {
101 final Optional<GenericVnf> optional = getGenericVnf(key.toString());
102 if (optional.isPresent()) {
103 final GenericVnf value = optional.get();
104 final String genericVnfName = value.getVnfName();
105 if (genericVnfName != null && genericVnfName.equals(vnfName)) {
106 final String genericVnfId = value.getVnfId();
107 LOGGER.info("Found matching vnf for name: {}, vnf-id: {}", genericVnfName, genericVnfId);
108 return Optional.of(genericVnfId);
114 LOGGER.error("No match found for vnf name: {}", vnfName);
115 return Optional.empty();
119 public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
120 final String requestUriString, final String vnfId, final Relationship relationship) {
122 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
123 if (optional.isPresent()) {
124 final GenericVnf genericVnf = optional.get();
125 final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
126 final Relationship outGoingRelationShip =
127 getRelationship(getRelationShipListRelatedLink(requestUriString), genericVnf, COMPOSED_OF);
128 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
129 outGoingRelationShip, targetUrl, Relationship.class);
130 if (optionalRelationship.isPresent()) {
131 final Relationship resultantRelationship = optionalRelationship.get();
133 RelationshipList relationshipList = genericVnf.getRelationshipList();
134 if (relationshipList == null) {
135 relationshipList = new RelationshipList();
136 genericVnf.setRelationshipList(relationshipList);
138 if (relationshipList.getRelationship().add(resultantRelationship)) {
139 LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
144 } catch (final Exception exception) {
145 LOGGER.error("Unable to add two-way relationship for vnfId: {}", vnfId, exception);
147 LOGGER.error("Unable to add relationship in cache for vnfId: {}", vnfId);
152 public Optional<Relationship> addRelationShip(final String vnfId, final Relationship relationship,
153 final String requestURI) {
154 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
155 if (optional.isPresent()) {
156 final GenericVnf genericVnf = optional.get();
157 RelationshipList relationshipList = genericVnf.getRelationshipList();
158 if (relationshipList == null) {
159 relationshipList = new RelationshipList();
160 genericVnf.setRelationshipList(relationshipList);
162 relationshipList.getRelationship().add(relationship);
163 LOGGER.info("Successfully added relation to GenericVnf for vnfId: {}", vnfId);
165 final String relatedLink = getBiDirectionalRelationShipListRelatedLink(requestURI);
166 final Relationship resultantRelationship =
167 getRelationship(relatedLink, genericVnf, relationship.getRelationshipLabel());
168 return Optional.of(resultantRelationship);
170 return Optional.empty();
174 public boolean patchGenericVnf(final String vnfId, final GenericVnf genericVnf) {
175 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
176 if (optional.isPresent()) {
177 final GenericVnf cachedGenericVnf = optional.get();
179 ShallowBeanCopy.copy(genericVnf, cachedGenericVnf);
181 } catch (final Exception exception) {
182 LOGGER.error("Unable to update GenericVnf for vnfId: {}", vnfId, exception);
185 LOGGER.error("Unable to find GenericVnf ...");
190 public List<GenericVnf> getGenericVnfs(final String selflink) {
191 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
193 final Object nativeCache = cache.getNativeCache();
194 if (nativeCache instanceof ConcurrentHashMap) {
195 @SuppressWarnings("unchecked")
196 final ConcurrentHashMap<Object, Object> concurrentHashMap =
197 (ConcurrentHashMap<Object, Object>) nativeCache;
198 final List<GenericVnf> result = new ArrayList<>();
200 concurrentHashMap.keySet().stream().forEach(key -> {
201 final Optional<GenericVnf> optional = getGenericVnf(key.toString());
202 if (optional.isPresent()) {
203 final GenericVnf genericVnf = optional.get();
204 final String genericVnfSelfLink = genericVnf.getSelflink();
205 final String genericVnfId = genericVnf.getSelflink();
207 if (genericVnfSelfLink != null && genericVnfSelfLink.equals(selflink)) {
208 LOGGER.info("Found matching vnf for selflink: {}, vnf-id: {}", genericVnfSelfLink,
210 result.add(genericVnf);
217 LOGGER.error("No match found for selflink: {}", selflink);
218 return Collections.emptyList();
222 public boolean deleteGenericVnf(final String vnfId, final String resourceVersion) {
223 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
224 if (optional.isPresent()) {
225 final GenericVnf genericVnf = optional.get();
226 if (genericVnf.getResourceVersion() != null && genericVnf.getResourceVersion().equals(resourceVersion)) {
227 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
228 LOGGER.info("Will evict GenericVnf from cache with vnfId: {}", genericVnf.getVnfId());
233 LOGGER.error("Unable to find GenericVnf for vnfId: {} and resourceVersion: {} ...", vnfId, resourceVersion);
237 private Relationship getRelationship(final String relatedLink, final GenericVnf genericVnf,
238 final String relationshipLabel) {
239 final Relationship relationShip = new Relationship();
240 relationShip.setRelatedTo(GENERIC_VNF);
241 relationShip.setRelationshipLabel(relationshipLabel);
242 relationShip.setRelatedLink(relatedLink);
244 final RelationshipData relationshipData = new RelationshipData();
245 relationshipData.setRelationshipKey(GENERIC_VNF_VNF_ID);
246 relationshipData.setRelationshipValue(genericVnf.getVnfId());
247 relationShip.getRelationshipData().add(relationshipData);
249 final RelatedToProperty relatedToProperty = new RelatedToProperty();
250 relatedToProperty.setPropertyKey(GENERIC_VNF_VNF_NAME);
251 relatedToProperty.setPropertyValue(genericVnf.getVnfName());
252 relationShip.getRelatedToProperty().add(relatedToProperty);
257 public void clearAll() {
258 clearCache(GENERIC_VNF_CACHE.getName());
262 public Optional<VfModule> getVfModule(final String vnfId, final String vfModuleId) {
263 LOGGER.info("Getting vfModule from cache for vnfId: {} and vfModuleId: {}",
265 final Optional<GenericVnf> genericVnfOptional = getGenericVnf(vnfId);
266 final GenericVnf value = genericVnfOptional.get();
267 final VfModules vfmodules = value.getVfModules();
268 if (vfmodules != null) {
269 for (VfModule vfModule : vfmodules.getVfModule()) {
270 if (vfModule.getVfModuleId().equalsIgnoreCase(vfModuleId)){
271 return Optional.of(vfModule);
275 return Optional.empty();
280 public void putVfModule(final String vnfId, final String vfModuleId, final VfModule vfModule) {
281 LOGGER.info("Adding vfModule for vnfId: {} and vfModuleId: {}",
283 final Optional<GenericVnf> genericVnfOptional = getGenericVnf(vnfId);
284 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
285 if (genericVnfOptional.isPresent()) {
286 final GenericVnf genericVnf = genericVnfOptional.get();
287 VfModules vfModules = null;
288 if(genericVnf.getVfModules()==null){
289 vfModules = new VfModules();
290 genericVnf.setVfModules(vfModules);
292 vfModules = genericVnf.getVfModules();
295 vfModules.getVfModule().add(vfModule);
296 cache.put(vfModuleId, vfModule);
301 public boolean patchVfModule(final String vnfId, final String vfModuleId, final VfModule vfModule) {
302 final Optional<GenericVnf> genericVnfOptional = getGenericVnf(vnfId);
303 LOGGER.info("Create vfModule for vnfId: {} and vfModuleId: {}",
305 if (genericVnfOptional.isPresent()) {
306 final GenericVnf cachedGenericVnf = genericVnfOptional.get();
307 final VfModules vfmodules = cachedGenericVnf.getVfModules();
308 LOGGER.info("vfModuleId is Matched");
310 vfmodules.getVfModule().stream().filter(tempVfModule ->
311 tempVfModule.getVfModuleId().equalsIgnoreCase(vfModuleId)).forEach(tempVfModule ->
312 tempVfModule.setOrchestrationStatus(vfModule.getOrchestrationStatus()));
314 } catch (final Exception exception) {
315 LOGGER.error("Unable to update VfModule for vfModuleId: {}", vfModule, exception);
318 LOGGER.error("Unable to find VfModule ...");