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 boolean addRelationShip(final String vnfId, final Relationship relationship) {
85 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
86 if (optional.isPresent()) {
87 final GenericVnf genericVnf = optional.get();
88 RelationshipList relationshipList = genericVnf.getRelationshipList();
89 if (relationshipList == null) {
90 relationshipList = new RelationshipList();
91 genericVnf.setRelationshipList(relationshipList);
93 return relationshipList.getRelationship().add(relationship);
95 LOGGER.error("Unable to find GenericVnf ...");
100 public Optional<String> getGenericVnfId(final String vnfName) {
101 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
103 final Object nativeCache = cache.getNativeCache();
104 if (nativeCache instanceof ConcurrentHashMap) {
105 @SuppressWarnings("unchecked")
106 final ConcurrentHashMap<Object, Object> concurrentHashMap =
107 (ConcurrentHashMap<Object, Object>) nativeCache;
108 for (final Object key : concurrentHashMap.keySet()) {
109 final GenericVnf value = cache.get(key, GenericVnf.class);
110 final String genericVnfName = value.getVnfName();
111 if (value != null && genericVnfName.equals(vnfName)) {
112 final String genericVnfId = value.getVnfId();
113 LOGGER.info("Found matching vnf for name: {}, vnf-id: {}", genericVnfName, genericVnfId);
114 return Optional.of(genericVnfId);
119 LOGGER.info("No match found for vnf name: {}", vnfName);
120 return Optional.empty();
124 public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
125 final String requestUriString, final String vnfId, final Relationship relationship) {
127 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
128 if (optional.isPresent()) {
129 final GenericVnf genericVnf = optional.get();
130 final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
131 final Relationship outGoingRelationShip = getRelationship(requestUriString, genericVnf);
132 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
133 outGoingRelationShip, targetUrl, Relationship.class);
134 if (optionalRelationship.isPresent()) {
135 final Relationship resultantRelationship = optionalRelationship.get();
136 if (addRelationShip(vnfId, resultantRelationship)) {
137 LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
142 } catch (final Exception exception) {
143 LOGGER.error("Unable to add two-way relationship for vnfId: {}", vnfId, exception);
145 LOGGER.error("Unable to add relationship in cache for vnfId: {}", vnfId);
149 private String getTargetUrl(final String targetBaseUrl, final String relatedLink) {
150 return UriComponentsBuilder.fromUriString(targetBaseUrl).path(relatedLink)
151 .path(BI_DIRECTIONAL_RELATIONSHIP_LIST_URL).toUriString();
154 private Relationship getRelationship(final String relatedLink, final GenericVnf genericVnf) {
155 final Relationship relationShip = new Relationship();
156 relationShip.setRelatedTo(GENERIC_VNF);
157 relationShip.setRelationshipLabel(COMPOSED_OF);
158 relationShip.setRelatedLink(relatedLink);
160 final RelationshipData relationshipData = new RelationshipData();
161 relationshipData.setRelationshipKey(GENERIC_VNF_VNF_ID);
162 relationshipData.setRelationshipValue(genericVnf.getVnfId());
163 relationShip.getRelationshipData().add(relationshipData);
165 final RelatedToProperty relatedToProperty = new RelatedToProperty();
166 relatedToProperty.setPropertyKey(GENERIC_VNF_VNF_NAME);
167 relatedToProperty.setPropertyValue(genericVnf.getVnfName());
168 relationShip.getRelatedToProperty().add(relatedToProperty);
173 public void clearAll() {
174 clearCahce(GENERIC_VNF_CACHE.getName());