Refactor SOL003 Adapter to organize its modules
[so.git] / adapters / etsi-sol003-adapter / etsi-sol003-pkgm / etsi-sol003-pkgm-adapter / src / main / java / org / onap / so / adapters / etsisol003adapter / pkgm / cache / PkgmCacheServiceProviderImpl.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.etsisol003adapter.pkgm.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.etsisol003adapter.pkgm.PackageManagementConstants;
30 import org.onap.so.adapters.etsisol003adapter.pkgm.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 PkgmCacheServiceProviderImpl extends AbstractCacheServiceProvider implements PkgmCacheServiceProvider {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(PkgmCacheServiceProviderImpl.class);
48
49     @Autowired
50     public PkgmCacheServiceProviderImpl(final CacheManager cacheManager) {
51         super(PackageManagementConstants.PACKAGE_MANAGEMENT_SUBSCRIPTION_CACHE, cacheManager);
52     }
53
54     @Override
55     public void addSubscription(final String subscriptionId, final PkgmSubscriptionRequest pkgmSubscriptionRequest) {
56         LOGGER.debug("Adding {} to cache with subscription id: {}", pkgmSubscriptionRequest, subscriptionId);
57         getCache().put(subscriptionId, pkgmSubscriptionRequest);
58     }
59
60     @Override
61     public Optional<PkgmSubscriptionRequest> getSubscription(final String subscriptionId) {
62         LOGGER.debug("Getting subscription from cache using Id: {}", subscriptionId);
63         final Cache cache = getCache();
64         final PkgmSubscriptionRequest cacheValue = cache.get(subscriptionId, PkgmSubscriptionRequest.class);
65         if (cacheValue != null) {
66             return Optional.of(cacheValue);
67         }
68         LOGGER.error("Unable to find Subscription in cache using Id: {}", subscriptionId);
69         return Optional.empty();
70     }
71
72     @Override
73     public Map<String, PkgmSubscriptionRequest> getSubscriptions() {
74         LOGGER.info("Getting all subscriptions from cache");
75         final Cache cache = getCache();
76
77         final Object nativeCache = cache.getNativeCache();
78         if (nativeCache instanceof ConcurrentHashMap) {
79             @SuppressWarnings("unchecked")
80             final ConcurrentHashMap<Object, Object> concurrentHashMap = (ConcurrentHashMap<Object, Object>) nativeCache;
81             final Map<String, PkgmSubscriptionRequest> result = new HashMap<>();
82             concurrentHashMap.keySet().forEach(key -> {
83                 final Optional<PkgmSubscriptionRequest> optional = getSubscription(key.toString());
84                 optional.ifPresent(pkgmSubscriptionRequest -> result.put(key.toString(), pkgmSubscriptionRequest));
85             });
86             return result;
87         }
88         LOGGER.error("Unable to find Subscriptions in cache");
89         return Collections.emptyMap();
90     }
91
92     @Override
93     public boolean deleteSubscription(final String subscriptionId) {
94         final Cache cache = getCache();
95         final Optional<PkgmSubscriptionRequest> optional = getSubscription(subscriptionId);
96         if (optional.isPresent()) {
97             cache.evict(subscriptionId);
98             return true;
99         }
100         return false;
101     }
102
103     @Override
104     public Optional<String> getSubscriptionId(final PkgmSubscriptionRequest pkgmSubscriptionRequest) {
105         final Cache cache = getCache();
106         final Object nativeCache = cache.getNativeCache();
107         if (nativeCache instanceof ConcurrentHashMap) {
108             @SuppressWarnings("unchecked")
109             final ConcurrentHashMap<Object, Object> concurrentHashMap = (ConcurrentHashMap<Object, Object>) nativeCache;
110             final Optional<Entry<Object, Object>> optional = concurrentHashMap.entrySet().stream()
111                     .filter(entry -> entry.getValue().equals(pkgmSubscriptionRequest)).findAny();
112             if (optional.isPresent()) {
113                 return Optional.of(optional.get().getKey().toString());
114             }
115         }
116         return Optional.empty();
117     }
118 }