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.USES;
28 import static org.onap.so.aaisimulator.utils.Constants.VF_MODULE;
29 import static org.onap.so.aaisimulator.utils.Constants.VF_MODULE_VF_MODULE_ID;
30 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getBiDirectionalRelationShipListRelatedLink;
31 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getRelationShipListRelatedLink;
32 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getTargetUrl;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.Optional;
37 import java.util.concurrent.ConcurrentHashMap;
38 import org.onap.aai.domain.yang.GenericVnf;
39 import org.onap.aai.domain.yang.RelatedToProperty;
40 import org.onap.aai.domain.yang.Relationship;
41 import org.onap.aai.domain.yang.RelationshipData;
42 import org.onap.aai.domain.yang.RelationshipList;
43 import org.onap.aai.domain.yang.VfModule;
44 import org.onap.aai.domain.yang.VfModules;
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;
68 public GenericVnfCacheServiceProviderImpl(final CacheManager cacheManager,
69 final HttpRestServiceProvider httpRestServiceProvider) {
71 this.httpRestServiceProvider = httpRestServiceProvider;
75 public void putGenericVnf(final String vnfId, final GenericVnf genericVnf) {
76 LOGGER.info("Adding customer: {} with key: {} in cache ...", genericVnf, vnfId);
77 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
78 cache.put(vnfId, genericVnf);
82 public Optional<GenericVnf> getGenericVnf(final String vnfId) {
83 LOGGER.info("getting GenericVnf from cache using key: {}", vnfId);
84 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
85 final GenericVnf value = cache.get(vnfId, GenericVnf.class);
87 return Optional.of(value);
89 LOGGER.error("Unable to find GenericVnf ...");
90 return Optional.empty();
94 public Optional<String> getGenericVnfId(final String vnfName) {
95 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
97 final Object nativeCache = cache.getNativeCache();
98 if (nativeCache instanceof ConcurrentHashMap) {
99 @SuppressWarnings("unchecked")
100 final ConcurrentHashMap<Object, Object> concurrentHashMap =
101 (ConcurrentHashMap<Object, Object>) nativeCache;
102 for (final Object key : concurrentHashMap.keySet()) {
103 final Optional<GenericVnf> optional = getGenericVnf(key.toString());
104 if (optional.isPresent()) {
105 final GenericVnf value = optional.get();
106 final String genericVnfName = value.getVnfName();
107 if (genericVnfName != null && genericVnfName.equals(vnfName)) {
108 final String genericVnfId = value.getVnfId();
109 LOGGER.info("Found matching vnf for name: {}, vnf-id: {}", genericVnfName, genericVnfId);
110 return Optional.of(genericVnfId);
116 LOGGER.error("No match found for vnf name: {}", vnfName);
117 return Optional.empty();
121 public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
122 final String requestUriString, final String vnfId, final Relationship relationship) {
124 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
125 if (optional.isPresent()) {
126 final GenericVnf genericVnf = optional.get();
127 final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
128 final Relationship outGoingRelationShip =
129 getRelationship(getRelationShipListRelatedLink(requestUriString), genericVnf, COMPOSED_OF);
130 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
131 outGoingRelationShip, targetUrl, Relationship.class);
132 if (optionalRelationship.isPresent()) {
133 final Relationship resultantRelationship = optionalRelationship.get();
135 RelationshipList relationshipList = genericVnf.getRelationshipList();
136 if (relationshipList == null) {
137 relationshipList = new RelationshipList();
138 genericVnf.setRelationshipList(relationshipList);
140 if (relationshipList.getRelationship().add(resultantRelationship)) {
141 LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
146 } catch (final Exception exception) {
147 LOGGER.error("Unable to add two-way relationship for vnfId: {}", vnfId, exception);
149 LOGGER.error("Unable to add relationship in cache for vnfId: {}", vnfId);
154 public Optional<Relationship> addRelationShip(final String vnfId, final Relationship relationship,
155 final String requestURI) {
156 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
157 if (optional.isPresent()) {
158 final GenericVnf genericVnf = optional.get();
159 RelationshipList relationshipList = genericVnf.getRelationshipList();
160 if (relationshipList == null) {
161 relationshipList = new RelationshipList();
162 genericVnf.setRelationshipList(relationshipList);
164 relationshipList.getRelationship().add(relationship);
165 LOGGER.info("Successfully added relation to GenericVnf for vnfId: {}", vnfId);
167 final String relatedLink = getBiDirectionalRelationShipListRelatedLink(requestURI);
168 final Relationship resultantRelationship =
169 getRelationship(relatedLink, genericVnf, relationship.getRelationshipLabel());
170 return Optional.of(resultantRelationship);
172 return Optional.empty();
176 public boolean patchGenericVnf(final String vnfId, final GenericVnf genericVnf) {
177 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
178 if (optional.isPresent()) {
179 final GenericVnf cachedGenericVnf = optional.get();
181 ShallowBeanCopy.copy(genericVnf, cachedGenericVnf);
183 } catch (final Exception exception) {
184 LOGGER.error("Unable to update GenericVnf for vnfId: {}", vnfId, exception);
187 LOGGER.error("Unable to find GenericVnf ...");
192 public List<GenericVnf> getGenericVnfs() {
193 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
195 final Object nativeCache = cache.getNativeCache();
196 if (nativeCache instanceof ConcurrentHashMap) {
197 @SuppressWarnings("unchecked")
198 final ConcurrentHashMap<Object, Object> concurrentHashMap =
199 (ConcurrentHashMap<Object, Object>) nativeCache;
200 final List<GenericVnf> result = new ArrayList<>();
202 concurrentHashMap.keySet().forEach(key -> {
203 final Optional<GenericVnf> optional = getGenericVnf(key.toString());
204 if (optional.isPresent()) {
205 final GenericVnf genericVnf = optional.get();
206 result.add(genericVnf);
212 LOGGER.error("No match found");
213 return Collections.emptyList();
217 public boolean deleteGenericVnf(final String vnfId, final String resourceVersion) {
218 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
219 if (optional.isPresent()) {
220 final GenericVnf genericVnf = optional.get();
221 if (genericVnf.getResourceVersion() != null && genericVnf.getResourceVersion().equals(resourceVersion)) {
222 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
223 LOGGER.info("Will evict GenericVnf from cache with vnfId: {}", genericVnf.getVnfId());
228 LOGGER.error("Unable to find GenericVnf for vnfId: {} and resourceVersion: {} ...", vnfId, resourceVersion);
232 private Relationship getRelationship(final String relatedLink, final GenericVnf genericVnf,
233 final String relationshipLabel) {
234 final Relationship relationShip = new Relationship();
235 relationShip.setRelatedTo(GENERIC_VNF);
236 relationShip.setRelationshipLabel(relationshipLabel);
237 relationShip.setRelatedLink(relatedLink);
239 final RelationshipData relationshipData = new RelationshipData();
240 relationshipData.setRelationshipKey(GENERIC_VNF_VNF_ID);
241 relationshipData.setRelationshipValue(genericVnf.getVnfId());
242 relationShip.getRelationshipData().add(relationshipData);
244 final RelatedToProperty relatedToProperty = new RelatedToProperty();
245 relatedToProperty.setPropertyKey(GENERIC_VNF_VNF_NAME);
246 relatedToProperty.setPropertyValue(genericVnf.getVnfName());
247 relationShip.getRelatedToProperty().add(relatedToProperty);
252 public void clearAll() {
253 clearCache(GENERIC_VNF_CACHE.getName());
257 public Optional<VfModule> getVfModule(final String vnfId, final String vfModuleId) {
258 LOGGER.info("Getting vfModule from cache for vnfId: {} and vfModuleId: {}", vnfId, vfModuleId);
259 final Optional<GenericVnf> genericVnfOptional = getGenericVnf(vnfId);
260 final GenericVnf value = genericVnfOptional.get();
261 final VfModules vfmodules = value.getVfModules();
262 if (vfmodules != null) {
263 for (final VfModule vfModule : vfmodules.getVfModule()) {
264 if (vfModule.getVfModuleId().equalsIgnoreCase(vfModuleId)) {
265 return Optional.of(vfModule);
269 return Optional.empty();
274 public void putVfModule(final String vnfId, final String vfModuleId, final VfModule vfModule) {
275 LOGGER.info("Adding vfModule for vnfId: {} and vfModuleId: {}", vnfId, vfModuleId);
276 final Optional<GenericVnf> genericVnfOptional = getGenericVnf(vnfId);
277 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
278 if (genericVnfOptional.isPresent()) {
279 final GenericVnf genericVnf = genericVnfOptional.get();
280 VfModules vfModules = null;
281 if (genericVnf.getVfModules() == null) {
282 vfModules = new VfModules();
283 genericVnf.setVfModules(vfModules);
285 vfModules = genericVnf.getVfModules();
288 vfModules.getVfModule().add(vfModule);
289 cache.put(vfModuleId, vfModule);
294 public boolean patchVfModule(final String vnfId, final String vfModuleId, final VfModule vfModule) {
295 final Optional<GenericVnf> genericVnfOptional = getGenericVnf(vnfId);
296 LOGGER.info("Create vfModule for vnfId: {} and vfModuleId: {}", vnfId, vfModuleId);
297 if (genericVnfOptional.isPresent()) {
298 final GenericVnf cachedGenericVnf = genericVnfOptional.get();
299 final VfModules vfmodules = cachedGenericVnf.getVfModules();
300 LOGGER.info("vfModuleId is Matched");
302 vfmodules.getVfModule().stream()
303 .filter(tempVfModule -> tempVfModule.getVfModuleId().equalsIgnoreCase(vfModuleId)).forEach(
304 tempVfModule -> tempVfModule.setOrchestrationStatus(vfModule.getOrchestrationStatus()));
306 } catch (final Exception exception) {
307 LOGGER.error("Unable to update VfModule for vfModuleId: {}", vfModule, exception);
310 LOGGER.error("Unable to find VfModule ...");
315 public Optional<Relationship> addRelationShip(final String vnfId, final String vfModuleId,
316 final Relationship relationship, final String requestURI) {
317 final Optional<VfModule> optional = getVfModule(vnfId, vfModuleId);
318 if (optional.isPresent()) {
319 final VfModule vfModule = optional.get();
321 RelationshipList relationshipList = vfModule.getRelationshipList();
322 if (relationshipList == null) {
323 relationshipList = new RelationshipList();
324 vfModule.setRelationshipList(relationshipList);
326 relationshipList.getRelationship().add(relationship);
327 LOGGER.info("Successfully added relation to VfModule for vnfId: {} and vfModuleId: {}", vnfId, vfModuleId);
329 final String relatedLink = getBiDirectionalRelationShipListRelatedLink(requestURI);
331 final Relationship resultantRelationship = new Relationship();
332 resultantRelationship.setRelatedTo(VF_MODULE);
333 resultantRelationship.setRelationshipLabel(USES);
334 resultantRelationship.setRelatedLink(relatedLink);
336 final RelationshipData genericVnfRelationshipData = new RelationshipData();
337 genericVnfRelationshipData.setRelationshipKey(GENERIC_VNF_VNF_ID);
338 genericVnfRelationshipData.setRelationshipValue(vnfId);
339 resultantRelationship.getRelationshipData().add(genericVnfRelationshipData);
341 final RelationshipData vfModuleRelationshipData = new RelationshipData();
342 vfModuleRelationshipData.setRelationshipKey(VF_MODULE_VF_MODULE_ID);
343 vfModuleRelationshipData.setRelationshipValue(vfModuleId);
344 resultantRelationship.getRelationshipData().add(vfModuleRelationshipData);
345 return Optional.of(resultantRelationship);
348 LOGGER.error("Unable to find VfModule ...");
349 return Optional.empty();
353 public boolean deleteVfModule(final String vnfId, final String vfModuleId, final String resourceVersion) {
354 final Optional<GenericVnf> genericVnfOptional = getGenericVnf(vnfId);
355 final Optional<VfModule> vfModuleOptional = getVfModule(vnfId, vfModuleId);
356 if (genericVnfOptional.isPresent() && vfModuleOptional.isPresent()) {
357 final GenericVnf genericVnf = genericVnfOptional.get();
358 final VfModule vfModule = vfModuleOptional.get();
359 if (genericVnf.getVfModules() != null && vfModule.getResourceVersion().equals(resourceVersion)) {
360 LOGGER.info("VfModule: {} deleted from the Generic VNF: {}",vfModuleId, vnfId);
361 return genericVnf.getVfModules().getVfModule().remove(vfModule);
364 LOGGER.error("There are no VfModules associated to vnf ID: {}", vnfId);