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
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=========================================================
21 package org.onap.so.adapters.etsisol003adapter.pkgm.cache;
23 import java.util.Collections;
24 import java.util.HashMap;
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;
39 * Implementation which provides methods for communicating with the cache
41 * @author Ronan Kenny (ronan.kenny@est.tech)
42 * @author Gareth Roper (gareth.roper@est.tech)
45 public class PkgmCacheServiceProviderImpl extends AbstractCacheServiceProvider implements PkgmCacheServiceProvider {
47 private static final Logger LOGGER = LoggerFactory.getLogger(PkgmCacheServiceProviderImpl.class);
50 public PkgmCacheServiceProviderImpl(final CacheManager cacheManager) {
51 super(PackageManagementConstants.PACKAGE_MANAGEMENT_SUBSCRIPTION_CACHE, cacheManager);
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);
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);
68 LOGGER.error("Unable to find Subscription in cache using Id: {}", subscriptionId);
69 return Optional.empty();
73 public Map<String, PkgmSubscriptionRequest> getSubscriptions() {
74 LOGGER.info("Getting all subscriptions from cache");
75 final Cache cache = getCache();
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));
88 LOGGER.error("Unable to find Subscriptions in cache");
89 return Collections.emptyMap();
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);
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());
116 return Optional.empty();