2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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=========================================================
20 package org.onap.so.aai.simulator.service.providers;
22 import java.util.List;
23 import java.util.Optional;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.stream.Collectors;
26 import org.onap.aai.domain.yang.Customer;
27 import org.onap.aai.domain.yang.ServiceInstance;
28 import org.onap.aai.domain.yang.ServiceInstances;
29 import org.onap.aai.domain.yang.ServiceSubscription;
30 import org.onap.so.aai.simulator.utils.Constants;
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 * @author waqas.ikram@ericsson.com
43 public class CustomerCacheServiceProviderImpl implements CustomerCacheServiceProvider {
44 private static final Logger LOGGER = LoggerFactory.getLogger(CustomerCacheServiceProviderImpl.class);
46 public final CacheManager cacheManager;
49 public CustomerCacheServiceProviderImpl(final CacheManager cacheManager) {
50 this.cacheManager = cacheManager;
54 public Optional<Customer> getCustomer(final String globalCustomerId) {
55 LOGGER.info("getting customer from cache using key: {}", globalCustomerId);
56 final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE);
57 final Customer value = cache.get(globalCustomerId, Customer.class);
59 return Optional.of(value);
61 return Optional.empty();
65 public void putCustomer(final String globalCustomerId, final Customer customer) {
66 LOGGER.info("Adding customer: {} with key: {} in cache ...", customer, globalCustomerId);
67 final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE);
69 cache.put(globalCustomerId, customer);
73 public Optional<ServiceSubscription> getServiceSubscription(final String globalCustomerId,
74 final String serviceType) {
75 LOGGER.info("getting service subscription from cache for globalCustomerId: {} and serviceType: {}",
76 globalCustomerId, serviceType);
78 final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE);
80 final Customer value = cache.get(globalCustomerId, Customer.class);
83 return Optional.ofNullable(value.getServiceSubscriptions().getServiceSubscription().stream()
84 .filter(s -> serviceType.equals(s.getServiceType())).findFirst().orElse(null));
86 return Optional.empty();
91 public Optional<ServiceInstances> getServiceInstances(final String globalCustomerId, final String serviceType,
92 final String serviceInstanceName) {
94 final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE);
95 final Customer value = cache.get(globalCustomerId, Customer.class);
98 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
99 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
101 if (serviceSubscription.isPresent()) {
102 LOGGER.info("Found service subscription ...");
103 final List<ServiceInstance> serviceInstancesList = serviceSubscription.get().getServiceInstances()
104 .getServiceInstance().stream()
105 .filter(serviceInstance -> serviceInstanceName.equals(serviceInstance.getServiceInstanceName()))
106 .collect(Collectors.toList());
107 if (serviceInstancesList != null && !serviceInstancesList.isEmpty()) {
108 LOGGER.info("Found {} service instances ", serviceInstancesList.size());
109 final ServiceInstances serviceInstances = new ServiceInstances();
110 serviceInstances.getServiceInstance().addAll(serviceInstancesList);
111 return Optional.of(serviceInstances);
116 return Optional.empty();
120 public Optional<ServiceInstance> getServiceInstance(final String globalCustomerId, final String serviceType,
121 final String serviceInstanceId) {
122 final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE);
123 final Customer value = cache.get(globalCustomerId, Customer.class);
126 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
127 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
129 if (serviceSubscription.isPresent()) {
130 LOGGER.info("Found service subscription ...");
131 final ServiceInstances serviceInstances = serviceSubscription.get().getServiceInstances();
132 if (serviceInstances != null) {
133 return Optional.ofNullable(serviceInstances.getServiceInstance().stream()
134 .filter(serviceInstance -> serviceInstanceId.equals(serviceInstance.getServiceInstanceId()))
135 .findFirst().orElse(null));
140 return Optional.empty();
144 public boolean putServiceInstance(final String globalCustomerId, final String serviceType,
145 final String serviceInstanceId, final ServiceInstance serviceInstance) {
146 LOGGER.info("Adding serviceInstance: {} in cache ...", serviceInstance, globalCustomerId);
148 final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE);
149 final Customer value = cache.get(globalCustomerId, Customer.class);
152 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
153 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
155 if (serviceSubscription.isPresent()) {
156 final ServiceInstances serviceInstances = getServiceInstances(serviceSubscription);
159 if (!serviceInstances.getServiceInstance().stream()
160 .filter(existing -> serviceInstanceId.equals(existing.getServiceInstanceId())).findFirst()
162 return serviceInstances.getServiceInstance().add(serviceInstance);
164 LOGGER.error("Service {} already exists ....", serviceInstanceId);
167 LOGGER.error("Couldn't find service subscription with serviceType: {} in cache ", serviceType);
170 LOGGER.error("Couldn't find Customer with key: {} in cache ", globalCustomerId);
174 private ServiceInstances getServiceInstances(final Optional<ServiceSubscription> optional) {
175 final ServiceSubscription serviceSubscription = optional.get();
176 final ServiceInstances serviceInstances = serviceSubscription.getServiceInstances();
177 if (serviceInstances == null) {
178 final ServiceInstances instances = new ServiceInstances();
179 serviceSubscription.setServiceInstances(instances);
182 return serviceInstances;
186 public void clearAll() {
187 final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE);
188 final ConcurrentHashMap<?, ?> nativeCache = (ConcurrentHashMap<?, ?>) cache.getNativeCache();
189 LOGGER.info("Clear all entries from cahce: {}", cache.getName());