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.subscriptionmanagement.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 PackageManagementCacheServiceProviderImpl extends AbstractCacheServiceProvider
 
  46         implements PackageManagementCacheServiceProvider {
 
  48     private static final Logger LOGGER = LoggerFactory.getLogger(PackageManagementCacheServiceProviderImpl.class);
 
  51     public PackageManagementCacheServiceProviderImpl(final CacheManager cacheManager) {
 
  52         super(PackageManagementConstants.PACKAGE_MANAGEMENT_SUBSCRIPTION_CACHE, cacheManager);
 
  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);
 
  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);
 
  69         LOGGER.error("Unable to find Subscription in cache using Id: {}", subscriptionId);
 
  70         return Optional.empty();
 
  74     public Map<String, PkgmSubscriptionRequest> getSubscriptions() {
 
  75         LOGGER.info("Getting all subscriptions from cache");
 
  76         final Cache cache = getCache();
 
  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));
 
  89         LOGGER.error("Unable to find Subscriptions in cache");
 
  90         return Collections.emptyMap();
 
  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);
 
 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());
 
 117         return Optional.empty();