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.ESR_VNFM_CACHE;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Optional;
27 import java.util.concurrent.ConcurrentHashMap;
28 import org.onap.aai.domain.yang.EsrSystemInfo;
29 import org.onap.aai.domain.yang.EsrSystemInfoList;
30 import org.onap.aai.domain.yang.EsrVnfm;
31 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.cache.Cache;
36 import org.springframework.cache.CacheManager;
37 import org.springframework.stereotype.Service;
40 * @author Waqas Ikram (waqas.ikram@est.tech)
44 public class ExternalSystemCacheServiceProviderImpl extends AbstractCacheServiceProvider
45 implements ExternalSystemCacheServiceProvider {
47 private static final Logger LOGGER = LoggerFactory.getLogger(ExternalSystemCacheServiceProviderImpl.class);
50 public ExternalSystemCacheServiceProviderImpl(final CacheManager cacheManager) {
55 public void putEsrVnfm(final String vnfmId, final EsrVnfm esrVnfm) {
56 LOGGER.info("Adding esrVnfm: {} with name to cache", esrVnfm);
57 final Cache cache = getCache(ESR_VNFM_CACHE.getName());
58 cache.put(vnfmId, esrVnfm);
62 public Optional<EsrVnfm> getEsrVnfm(final String vnfmId) {
63 LOGGER.info("getting EsrVnfm from cache using key: {}", vnfmId);
64 final Cache cache = getCache(ESR_VNFM_CACHE.getName());
65 final EsrVnfm value = cache.get(vnfmId, EsrVnfm.class);
67 return Optional.of(value);
69 LOGGER.error("Unable to find EsrVnfm in cache using vnfmId: {} ", vnfmId);
70 return Optional.empty();
74 public List<EsrVnfm> getAllEsrVnfm() {
75 final Cache cache = getCache(ESR_VNFM_CACHE.getName());
77 final Object nativeCache = cache.getNativeCache();
78 if (nativeCache instanceof ConcurrentHashMap) {
79 @SuppressWarnings("unchecked")
80 final ConcurrentHashMap<Object, Object> concurrentHashMap =
81 (ConcurrentHashMap<Object, Object>) nativeCache;
82 final List<EsrVnfm> result = new ArrayList<>();
83 concurrentHashMap.keySet().stream().forEach(key -> {
84 final Optional<EsrVnfm> optional = getEsrVnfm(key.toString());
85 if (optional.isPresent()) {
86 result.add(optional.get());
92 LOGGER.error("Unable to get all esr vnfms ... ");
93 return Collections.emptyList();
98 public Optional<EsrSystemInfoList> getEsrSystemInfoList(final String vnfmId) {
99 final Optional<EsrVnfm> optional = getEsrVnfm(vnfmId);
100 if (optional.isPresent()) {
101 final EsrVnfm esrVnfm = optional.get();
102 if (esrVnfm.getEsrSystemInfoList() != null) {
103 return Optional.of(esrVnfm.getEsrSystemInfoList());
105 LOGGER.error("EsrSystemInfoList is null for vnfmId: {} ", vnfmId);
107 LOGGER.error("Unable to find EsrVnfm in cache using vnfmId: {} ", vnfmId);
108 return Optional.empty();
112 public boolean putEsrSystemInfo(final String vnfmId, final String esrSystemInfoId,
113 final EsrSystemInfo esrSystemInfo) {
114 final Optional<EsrVnfm> optional = getEsrVnfm(vnfmId);
115 if (optional.isPresent()) {
116 final EsrVnfm esrVnfm = optional.get();
117 final List<EsrSystemInfo> esrSystemInfoList = getEsrSystemInfoList(esrVnfm);
119 final Optional<EsrSystemInfo> existingEsrSystemInfo =
120 esrSystemInfoList.stream().filter(existing -> existing.getEsrSystemInfoId() != null
121 && existing.getEsrSystemInfoId().equals(esrSystemInfoId)).findFirst();
122 if (existingEsrSystemInfo.isPresent()) {
123 LOGGER.error("EsrSystemInfo already exists {}", existingEsrSystemInfo.get());
127 return esrSystemInfoList.add(esrSystemInfo);
129 LOGGER.error("Unable to add EsrSystemInfo in cache for vnfmId: {} ", vnfmId);
133 private List<EsrSystemInfo> getEsrSystemInfoList(final EsrVnfm esrVnfm) {
134 EsrSystemInfoList esrSystemInfoList = esrVnfm.getEsrSystemInfoList();
135 if (esrSystemInfoList == null) {
136 esrSystemInfoList = new EsrSystemInfoList();
137 esrVnfm.setEsrSystemInfoList(esrSystemInfoList);
139 return esrSystemInfoList.getEsrSystemInfo();
143 public void clearAll() {
144 clearCache(ESR_VNFM_CACHE.getName());