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 java.util.Optional;
24 import java.util.concurrent.ConcurrentHashMap;
25 import org.onap.aai.domain.yang.GenericVnf;
26 import org.onap.aai.domain.yang.Relationship;
27 import org.onap.aai.domain.yang.RelationshipList;
28 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.cache.Cache;
33 import org.springframework.cache.CacheManager;
34 import org.springframework.stereotype.Service;
37 * @author Waqas Ikram (waqas.ikram@est.tech)
41 public class GenericVnfCacheServiceProviderImpl extends AbstractCacheServiceProvider
42 implements GenericVnfCacheServiceProvider {
44 private static final Logger LOGGER = LoggerFactory.getLogger(GenericVnfCacheServiceProviderImpl.class);
47 public GenericVnfCacheServiceProviderImpl(final CacheManager cacheManager) {
52 public void putGenericVnf(final String vnfId, final GenericVnf genericVnf) {
53 LOGGER.info("Adding customer: {} with key: {} in cache ...", genericVnf, vnfId);
54 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
55 cache.put(vnfId, genericVnf);
59 public Optional<GenericVnf> getGenericVnf(final String vnfId) {
60 LOGGER.info("getting GenericVnf from cache using key: {}", vnfId);
61 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
62 final GenericVnf value = cache.get(vnfId, GenericVnf.class);
64 return Optional.of(value);
66 LOGGER.error("Unable to find GenericVnf ...");
67 return Optional.empty();
71 public boolean addRelationShip(final String vnfId, final Relationship relationship) {
72 final Optional<GenericVnf> optional = getGenericVnf(vnfId);
73 if (optional.isPresent()) {
74 final GenericVnf genericVnf = optional.get();
75 RelationshipList relationshipList = genericVnf.getRelationshipList();
76 if (relationshipList == null) {
77 relationshipList = new RelationshipList();
78 genericVnf.setRelationshipList(relationshipList);
80 return relationshipList.getRelationship().add(relationship);
82 LOGGER.error("Unable to find GenericVnf ...");
87 public Optional<String> getGenericVnfId(final String vnfName) {
88 final Cache cache = getCache(GENERIC_VNF_CACHE.getName());
90 final Object nativeCache = cache.getNativeCache();
91 if (nativeCache instanceof ConcurrentHashMap) {
92 @SuppressWarnings("unchecked")
93 final ConcurrentHashMap<Object, Object> concurrentHashMap =
94 (ConcurrentHashMap<Object, Object>) nativeCache;
95 for (final Object key : concurrentHashMap.keySet()) {
96 final GenericVnf value = cache.get(key, GenericVnf.class);
97 final String genericVnfName = value.getVnfName();
98 if (value != null && genericVnfName.equals(vnfName)) {
99 final String genericVnfId = value.getVnfId();
100 LOGGER.info("Found matching vnf for name: {}, vnf-id: {}", genericVnfName, genericVnfId);
101 return Optional.of(genericVnfId);
106 LOGGER.info("No match found for vnf name: {}", vnfName);
107 return Optional.empty();
111 public void clearAll() {
112 clearCahce(GENERIC_VNF_CACHE.getName());