Merge "Drools-apps CSIT randomly fails deploying policies"
[integration/csit.git] / plans / usecases-pnf-sw-upgrade / pnf-sw-upgrade / sorch / simulator / aai-simulator / src / main / java / org / onap / so / aaisimulator / service / providers / PnfCacheServiceProviderImpl.java
1 /*-
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
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 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;
31
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;
37
38 import static org.onap.so.aaisimulator.utils.CacheName.PNF_CACHE;
39
40 /**
41  * @author Raj Gumma (raj.gumma@est.tech)
42  */
43 @Service
44 public class PnfCacheServiceProviderImpl extends AbstractCacheServiceProvider implements PnfCacheServiceProvider {
45
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(PnfCacheServiceProvider.class);
48
49     private final Cache cache;
50
51     @Autowired
52     public PnfCacheServiceProviderImpl(final CacheManager cacheManager) {
53         super(cacheManager);
54         cache = getCache(PNF_CACHE.getName());
55     }
56
57     @Override
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);
61     }
62
63     @Override
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);
68     }
69
70     @Override
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);
84                     }
85                 }
86             }
87         }
88         return Optional.empty();
89     }
90
91     @Override
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();
96             try {
97                 ShallowBeanCopy.copy(pnf, cachedPnf);
98                 return true;
99             } catch (final Exception exception) {
100                 LOGGER.error("Unable to update Pnf for pnfId: {}", pnfId, exception);
101             }
102         }
103         LOGGER.error("Unable to find Pnf for pnfID : {}", pnfId);
104         return false;
105     }
106
107     @Override
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<>();
114
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();
121
122                     if (pnfSelfLink != null && pnfSelfLink.equals(selfLink)) {
123                         LOGGER.info("Found matching pnf for selflink: {}, pnf-id: {}", pnfSelfLink,
124                                 pnfId);
125                         result.add(pnf);
126                     }
127                 }
128             });
129             return result;
130         }
131         LOGGER.error("No match found for selflink: {}", selfLink);
132         return Collections.emptyList();
133     }
134
135     @Override
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());
142                 cache.evict(pnfId);
143                 return true;
144             }
145         }
146         LOGGER.error("Unable to find Pnf for pnfId: {} and resourceVersion: {} ...", pnfId, resourceVersion);
147         return false;
148     }
149
150     @Override
151     public void clearAll() {
152         clearCache(cache.getName());
153     }
154 }