3c44bed42f915678b397f3d27a417d8e1ff30c9b
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aaisimulator / service / providers / ExternalSystemCacheServiceProviderImpl.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.aaisimulator.service.providers;
21
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;
38
39 /**
40  * @author Waqas Ikram (waqas.ikram@est.tech)
41  *
42  */
43 @Service
44 public class ExternalSystemCacheServiceProviderImpl extends AbstractCacheServiceProvider
45         implements ExternalSystemCacheServiceProvider {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(ExternalSystemCacheServiceProviderImpl.class);
48
49     @Autowired
50     public ExternalSystemCacheServiceProviderImpl(final CacheManager cacheManager) {
51         super(cacheManager);
52     }
53
54     @Override
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);
59     }
60
61     @Override
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);
66         if (value != null) {
67             return Optional.of(value);
68         }
69         LOGGER.error("Unable to find EsrVnfm in cache using vnfmId: {} ", vnfmId);
70         return Optional.empty();
71     }
72
73     @Override
74     public List<EsrVnfm> getAllEsrVnfm() {
75         final Cache cache = getCache(ESR_VNFM_CACHE.getName());
76         if (cache != null) {
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());
87                     }
88                 });
89                 return result;
90             }
91         }
92         LOGGER.error("Unable to get all esr vnfms ... ");
93         return Collections.emptyList();
94
95     }
96
97     @Override
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());
104             }
105             LOGGER.error("EsrSystemInfoList is null for vnfmId: {} ", vnfmId);
106         }
107         LOGGER.error("Unable to find EsrVnfm in cache using vnfmId: {} ", vnfmId);
108         return Optional.empty();
109     }
110
111     @Override
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);
118
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());
124                 return false;
125             }
126
127             return esrSystemInfoList.add(esrSystemInfo);
128         }
129         LOGGER.error("Unable to add EsrSystemInfo in cache for vnfmId: {} ", vnfmId);
130         return false;
131     }
132
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);
138         }
139         return esrSystemInfoList.getEsrSystemInfo();
140     }
141
142     @Override
143     public void clearAll() {
144         clearCache(ESR_VNFM_CACHE.getName());
145
146     }
147
148 }