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.Constants.X_HTTP_METHOD_OVERRIDE;
28 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getBiDirectionalRelationShipListRelatedLink;
29 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getRelationShipListRelatedLink;
30 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getTargetUrl;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.Optional;
35 import java.util.concurrent.ConcurrentHashMap;
36 import org.onap.aai.domain.yang.GenericVnf;
37 import org.onap.aai.domain.yang.RelatedToProperty;
38 import org.onap.aai.domain.yang.Relationship;
39 import org.onap.aai.domain.yang.RelationshipData;
40 import org.onap.aai.domain.yang.RelationshipList;
41 import org.onap.aai.domain.yang.VfModule;
42 import org.onap.aai.domain.yang.v10.VfModules;
43 import org.onap.aai.domain.yang.VolumeGroup;
44 import org.onap.aai.domain.yang.v10.VolumeGroups;
45 import org.onap.so.aaisimulator.utils.ShallowBeanCopy;
46 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.cache.Cache;
51 import org.springframework.cache.CacheManager;
52 import org.springframework.http.HttpHeaders;
53 import org.springframework.stereotype.Service;
56 * @author Waqas Ikram (waqas.ikram@est.tech)
60 public class GenericVnfCacheServiceProviderImpl extends AbstractCacheServiceProvider
61 implements GenericVnfCacheServiceProvider {
63 private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfCacheServiceProviderImpl.class);
65 private final HttpRestServiceProvider httpRestServiceProvider;
66 final org.onap.aai.domain.yang.VfModules vfModules = new org.onap.aai.domain.yang.VfModules();
69 public GenericVnfCacheServiceProviderImpl(final CacheManager cacheManager,
70 final HttpRestServiceProvider httpRestServiceProvider) {
72 this.httpRestServiceProvider = httpRestServiceProvider;
76 public void putGenericVnf(final String vnfId, final GenericVnf genericVnf) {
77 LOGGER.info("Adding customer: {} with key: {} in cache ...", genericVnf, vnfId);
78 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
79 cache.put(vnfId, genericVnf);
83 public Optional<GenericVnf> getGenericVnf(final String vnfId) {
84 LOGGER.info("getting GenericVnf from cache using key: {}", vnfId);
85 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
86 final GenericVnf value = cache.get(vnfId, GenericVnf.class);
88 return Optional.of(value);
90 LOGGER.error("Unable to find GenericVnf ...");
91 return Optional.empty();
95 public Optional<String> getGenericVnfId(final String vnfName) {
96 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
98 final Object nativeCache = cache.getNativeCache();
99 if (nativeCache instanceof ConcurrentHashMap) {
100 @SuppressWarnings("unchecked")
101 final ConcurrentHashMap<Object, Object> concurrentHashMap =
102 (ConcurrentHashMap<Object, Object>) nativeCache;
103 for (final Object key : concurrentHashMap.keySet()) {
104 final Optional<GenericVnf> optional = getGenericVnf(key.toString());
105 if (optional.isPresent()) {
106 final GenericVnf value = optional.get();
107 final String genericVnfName = value.getVnfName();
108 if (genericVnfName != null && genericVnfName.equals(vnfName)) {
109 final String genericVnfId = value.getVnfId();
110 LOGGER.info("Found matching vnf for name: {}, vnf-id: {}", genericVnfName, genericVnfId);
111 return Optional.of(genericVnfId);
117 LOGGER.error("No match found for vnf name: {}", vnfName);
118 return Optional.empty();
122 public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
123 final String requestUriString, final String vnfId, final Relationship relationship) {
125 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
126 if (optional.isPresent()) {
127 final GenericVnf genericVnf = optional.get();
128 final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
129 final Relationship outGoingRelationShip =
130 getRelationship(getRelationShipListRelatedLink(requestUriString), genericVnf, COMPOSED_OF);
131 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
132 outGoingRelationShip, targetUrl, Relationship.class);
133 if (optionalRelationship.isPresent()) {
134 final Relationship resultantRelationship = optionalRelationship.get();
136 RelationshipList relationshipList = genericVnf.getRelationshipList();
137 if (relationshipList == null) {
138 relationshipList = new RelationshipList();
139 genericVnf.setRelationshipList(relationshipList);
141 if (relationshipList.getRelationship().add(resultantRelationship)) {
142 LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
147 } catch (final Exception exception) {
148 LOGGER.error("Unable to add two-way relationship for vnfId: {}", vnfId, exception);
150 LOGGER.error("Unable to add relationship in cache for vnfId: {}", vnfId);
155 public Optional<Relationship> addRelationShip(final String vnfId, final Relationship relationship,
156 final String requestURI) {
157 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
158 if (optional.isPresent()) {
159 final GenericVnf genericVnf = optional.get();
160 RelationshipList relationshipList = genericVnf.getRelationshipList();
161 if (relationshipList == null) {
162 relationshipList = new RelationshipList();
163 genericVnf.setRelationshipList(relationshipList);
165 relationshipList.getRelationship().add(relationship);
166 LOGGER.info("Successfully added relation to GenericVnf for vnfId: {}", vnfId);
168 final String relatedLink = getBiDirectionalRelationShipListRelatedLink(requestURI);
169 final Relationship resultantRelationship =
170 getRelationship(relatedLink, genericVnf, relationship.getRelationshipLabel());
171 return Optional.of(resultantRelationship);
173 return Optional.empty();
177 public boolean patchGenericVnf(final String vnfId, final GenericVnf genericVnf) {
178 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
179 if (optional.isPresent()) {
180 final GenericVnf cachedGenericVnf = optional.get();
182 ShallowBeanCopy.copy(genericVnf, cachedGenericVnf);
184 } catch (final Exception exception) {
185 LOGGER.error("Unable to update GenericVnf for vnfId: {}", vnfId, exception);
188 LOGGER.error("Unable to find GenericVnf ...");
193 public List<GenericVnf> getGenericVnfs(final String selflink) {
194 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
196 final Object nativeCache = cache.getNativeCache();
197 if (nativeCache instanceof ConcurrentHashMap) {
198 @SuppressWarnings("unchecked")
199 final ConcurrentHashMap<Object, Object> concurrentHashMap =
200 (ConcurrentHashMap<Object, Object>) nativeCache;
201 final List<GenericVnf> result = new ArrayList<>();
203 concurrentHashMap.keySet().stream().forEach(key -> {
204 final Optional<GenericVnf> optional = getGenericVnf(key.toString());
205 if (optional.isPresent()) {
206 final GenericVnf genericVnf = optional.get();
207 final String genericVnfSelfLink = genericVnf.getSelflink();
208 final String genericVnfId = genericVnf.getSelflink();
210 if (genericVnfSelfLink != null && genericVnfSelfLink.equals(selflink)) {
211 LOGGER.info("Found matching vnf for selflink: {}, vnf-id: {}", genericVnfSelfLink,
213 result.add(genericVnf);
220 LOGGER.error("No match found for selflink: {}", selflink);
221 return Collections.emptyList();
225 public boolean deleteGenericVnf(final String vnfId, final String resourceVersion) {
226 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
227 if (optional.isPresent()) {
228 final GenericVnf genericVnf = optional.get();
229 if (genericVnf.getResourceVersion() != null && genericVnf.getResourceVersion().equals(resourceVersion)) {
230 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
231 LOGGER.info("Will evict GenericVnf from cache with vnfId: {}", genericVnf.getVnfId());
236 LOGGER.error("Unable to find GenericVnf for vnfId: {} and resourceVersion: {} ...", vnfId, resourceVersion);
240 private Relationship getRelationship(final String relatedLink, final GenericVnf genericVnf,
241 final String relationshipLabel) {
242 final Relationship relationShip = new Relationship();
243 relationShip.setRelatedTo(GENERIC_VNF);
244 relationShip.setRelationshipLabel(relationshipLabel);
245 relationShip.setRelatedLink(relatedLink);
247 final RelationshipData relationshipData = new RelationshipData();
248 relationshipData.setRelationshipKey(GENERIC_VNF_VNF_ID);
249 relationshipData.setRelationshipValue(genericVnf.getVnfId());
250 relationShip.getRelationshipData().add(relationshipData);
252 final RelatedToProperty relatedToProperty = new RelatedToProperty();
253 relatedToProperty.setPropertyKey(GENERIC_VNF_VNF_NAME);
254 relatedToProperty.setPropertyValue(genericVnf.getVnfName());
255 relationShip.getRelatedToProperty().add(relatedToProperty);
260 public void clearAll() {
261 clearCache(GENERIC_VNF_CACHE.getName());
265 public Optional<org.onap.aai.domain.yang.VfModule> getVfModule(final String vnfId, final String vfModuleId) {
266 LOGGER.info("Getting vfModule from cache for vnfId: {} and vfModuleId: {}",
268 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
269 final GenericVnf value = cache.get(vnfId, GenericVnf.class);
270 LOGGER.info("Getting vfModule from cache for vnfId: {} and vfModuleId: {}",
272 if (value.getVfModules() != null) {
273 for (int i=0; i<value.getVfModules().getVfModule().size(); i++)
275 if(value.getVfModules().getVfModule().get(i).getVfModuleId().equalsIgnoreCase(vfModuleId)){
276 return Optional.of(value.getVfModules().getVfModule().get(i));
280 return Optional.empty();
284 public void putVfModule(String vnfId, String vfModuleId, VfModule vfModule) {
285 LOGGER.info("Adding vfModule from cache for vnfId: {} and vfModuleId: {}",
287 final Optional<GenericVnf> genericVnfOptional = getGenericVnf(vnfId);
288 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
289 if (genericVnfOptional.isPresent()) {
290 final GenericVnf genericVnf = genericVnfOptional.get();
292 vfModules.getVfModule().add(vfModule);
293 genericVnf.setVfModules(vfModules);
294 cache.put(vfModuleId, vfModule);
299 public boolean patchVfModule(String vnfId, String vfModuleId, VfModule vfModule) {
300 final Optional<GenericVnf> genericVnfOptional = getGenericVnf(vnfId);
301 LOGGER.info("Create vfModule for vnfId: {} and vfModuleId: {}",
303 if (genericVnfOptional.isPresent()) {
304 final GenericVnf cachedGenericVnf = genericVnfOptional.get();
305 LOGGER.info("vfModuleId is Matched");
307 for (int i=0; i<cachedGenericVnf.getVfModules().getVfModule().size(); i++)
309 if(cachedGenericVnf.getVfModules().getVfModule().get(i).getVfModuleId().equalsIgnoreCase(vfModuleId)){
310 cachedGenericVnf.getVfModules().getVfModule().get(i).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 ...");