2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 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 org.onap.aai.domain.yang.v15.Pnf;
23 import org.onap.so.aaisimulator.utils.ShallowBeanCopy;
24 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.cache.Cache;
29 import org.springframework.cache.CacheManager;
30 import org.springframework.stereotype.Service;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Optional;
36 import java.util.concurrent.ConcurrentHashMap;
38 import static org.onap.so.aaisimulator.utils.CacheName.PNF_CACHE;
41 * @author Raj Gumma (raj.gumma@est.tech)
44 public class PnfCacheServiceProviderImpl extends AbstractCacheServiceProvider implements PnfCacheServiceProvider {
47 private static final Logger LOGGER = LoggerFactory.getLogger(PnfCacheServiceProvider.class);
49 private final Cache cache;
52 public PnfCacheServiceProviderImpl(final CacheManager cacheManager) {
54 cache = getCache(PNF_CACHE.getName());
58 public void putPnf(final String pnfId, final Pnf pnf) {
59 LOGGER.info("Adding pnf: {} with key: {} in cache ...", pnf, pnfId);
60 cache.put(pnfId, pnf);
64 public Optional<Pnf> getPnf(final String pnfId) {
65 LOGGER.info("getting Pnf from cache using key: {}", pnfId);
66 final Pnf value = cache.get(pnfId, Pnf.class);
67 return Optional.ofNullable(value);
71 public Optional<String> getPnfId(final String pnfName) {
72 final Object nativeCache = cache.getNativeCache();
73 if (nativeCache instanceof ConcurrentHashMap) {
74 @SuppressWarnings("unchecked") final ConcurrentHashMap<Object, Object> concurrentHashMap =
75 (ConcurrentHashMap<Object, Object>) nativeCache;
76 for (final Object key : concurrentHashMap.keySet()) {
77 final Optional<Pnf> optional = getPnf(key.toString());
78 if (optional.isPresent()) {
79 final String cachedPnfName = optional.get().getPnfName();
80 if (cachedPnfName != null && cachedPnfName.equals(cachedPnfName)) {
81 final String pnfId = optional.get().getPnfId();
82 LOGGER.info("Found matching pnf for name: {}, pnf-id: {}", cachedPnfName, pnfId);
83 return Optional.of(pnfId);
88 return Optional.empty();
92 public boolean patchPnf(final String pnfId, final Pnf pnf) {
93 final Optional<Pnf> optional = getPnf(pnfId);
94 if (optional.isPresent()) {
95 final Pnf cachedPnf = optional.get();
97 ShallowBeanCopy.copy(pnf, cachedPnf);
99 } catch (final Exception exception) {
100 LOGGER.error("Unable to update Pnf for pnfId: {}", pnfId, exception);
103 LOGGER.error("Unable to find Pnf for pnfID : {}", pnfId);
108 public List<Pnf> getPnfs(String selfLink) {
109 final Object nativeCache = cache.getNativeCache();
110 if (nativeCache instanceof ConcurrentHashMap) {
111 @SuppressWarnings("unchecked") final ConcurrentHashMap<Object, Object> concurrentHashMap =
112 (ConcurrentHashMap<Object, Object>) nativeCache;
113 final List<Pnf> result = new ArrayList<>();
115 concurrentHashMap.keySet().stream().forEach(key -> {
116 final Optional<Pnf> optional = getPnf(key.toString());
117 if (optional.isPresent()) {
118 final Pnf pnf = optional.get();
119 final String pnfSelfLink = pnf.getSelflink();
120 final String pnfId = pnf.getSelflink();
122 if (pnfSelfLink != null && pnfSelfLink.equals(selfLink)) {
123 LOGGER.info("Found matching pnf for selflink: {}, pnf-id: {}", pnfSelfLink,
131 LOGGER.error("No match found for selflink: {}", selfLink);
132 return Collections.emptyList();
136 public boolean deletePnf(String pnfId, String resourceVersion) {
137 final Optional<Pnf> optional = getPnf(pnfId);
138 if (optional.isPresent()) {
139 final Pnf pnf = optional.get();
140 if (pnf.getResourceVersion() != null && pnf.getResourceVersion().equals(resourceVersion)) {
141 LOGGER.info("Will evict pnf from cache with pnfId: {}", pnf.getPnfId());
146 LOGGER.error("Unable to find Pnf for pnfId: {} and resourceVersion: {} ...", pnfId, resourceVersion);
151 public void clearAll() {
152 clearCache(cache.getName());