Fixing SO build
[so.git] / adapters / etsi-sol003-adapter / etsi-sol003-package-management / etsi-sol003-package-management-adapter / src / main / java / org / onap / so / adapters / vnfmadapter / packagemanagement / subscriptionmanagement / cache / PackageManagementCacheServiceProviderImpl.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
21 package org.onap.so.adapters.vnfmadapter.packagemanagement.subscriptionmanagement.cache;
22
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.Optional;
28 import java.util.concurrent.ConcurrentHashMap;
29 import org.onap.so.adapters.vnfmadapter.PackageManagementConstants;
30 import org.onap.so.adapters.vnfmadapter.packagemanagement.model.PkgmSubscriptionRequest;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.cache.Cache;
35 import org.springframework.cache.CacheManager;
36 import org.springframework.stereotype.Service;
37
38 /**
39  * Implementation which provides methods for communicating with the cache
40  *
41  * @author Ronan Kenny (ronan.kenny@est.tech)
42  * @author Gareth Roper (gareth.roper@est.tech)
43  */
44 @Service
45 public class PackageManagementCacheServiceProviderImpl extends AbstractCacheServiceProvider
46         implements PackageManagementCacheServiceProvider {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(PackageManagementCacheServiceProviderImpl.class);
49
50     @Autowired
51     public PackageManagementCacheServiceProviderImpl(final CacheManager cacheManager) {
52         super(PackageManagementConstants.PACKAGE_MANAGEMENT_SUBSCRIPTION_CACHE, cacheManager);
53     }
54
55     @Override
56     public void addSubscription(final String subscriptionId, final PkgmSubscriptionRequest pkgmSubscriptionRequest) {
57         LOGGER.debug("Adding {} to cache with subscription id: {}", pkgmSubscriptionRequest, subscriptionId);
58         getCache().put(subscriptionId, pkgmSubscriptionRequest);
59     }
60
61     @Override
62     public Optional<PkgmSubscriptionRequest> getSubscription(final String subscriptionId) {
63         LOGGER.debug("Getting subscription from cache using Id: {}", subscriptionId);
64         final Cache cache = getCache();
65         final PkgmSubscriptionRequest cacheValue = cache.get(subscriptionId, PkgmSubscriptionRequest.class);
66         if (cacheValue != null) {
67             return Optional.of(cacheValue);
68         }
69         LOGGER.error("Unable to find Subscription in cache using Id: {}", subscriptionId);
70         return Optional.empty();
71     }
72
73     @Override
74     public Map<String, PkgmSubscriptionRequest> getSubscriptions() {
75         LOGGER.info("Getting all subscriptions from cache");
76         final Cache cache = getCache();
77
78         final Object nativeCache = cache.getNativeCache();
79         if (nativeCache instanceof ConcurrentHashMap) {
80             @SuppressWarnings("unchecked")
81             final ConcurrentHashMap<Object, Object> concurrentHashMap = (ConcurrentHashMap<Object, Object>) nativeCache;
82             final Map<String, PkgmSubscriptionRequest> result = new HashMap<>();
83             concurrentHashMap.keySet().forEach(key -> {
84                 final Optional<PkgmSubscriptionRequest> optional = getSubscription(key.toString());
85                 optional.ifPresent(pkgmSubscriptionRequest -> result.put(key.toString(), pkgmSubscriptionRequest));
86             });
87             return result;
88         }
89         LOGGER.error("Unable to find Subscriptions in cache");
90         return Collections.emptyMap();
91     }
92
93     @Override
94     public boolean deleteSubscription(final String subscriptionId) {
95         final Cache cache = getCache();
96         final Optional<PkgmSubscriptionRequest> optional = getSubscription(subscriptionId);
97         if (optional.isPresent()) {
98             cache.evict(subscriptionId);
99             return true;
100         }
101         return false;
102     }
103
104     @Override
105     public Optional<String> getSubscriptionId(final PkgmSubscriptionRequest pkgmSubscriptionRequest) {
106         final Cache cache = getCache();
107         final Object nativeCache = cache.getNativeCache();
108         if (nativeCache instanceof ConcurrentHashMap) {
109             @SuppressWarnings("unchecked")
110             final ConcurrentHashMap<Object, Object> concurrentHashMap = (ConcurrentHashMap<Object, Object>) nativeCache;
111             final Optional<Entry<Object, Object>> optional = concurrentHashMap.entrySet().stream()
112                     .filter(entry -> entry.getValue().equals(pkgmSubscriptionRequest)).findAny();
113             if (optional.isPresent()) {
114                 return Optional.of(optional.get().getKey().toString());
115             }
116         }
117         return Optional.empty();
118     }
119 }