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.aaisimulator.service.providers;
22 import java.util.List;
23 import java.util.Optional;
24 import java.util.stream.Collectors;
25 import org.onap.aai.domain.yang.Customer;
26 import org.onap.aai.domain.yang.ServiceInstance;
27 import org.onap.aai.domain.yang.ServiceInstances;
28 import org.onap.aai.domain.yang.ServiceSubscription;
29 import org.onap.aai.domain.yang.ServiceSubscriptions;
30 import org.onap.so.aaisimulator.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 extends AbstractCacheServiceProvider
44 implements CustomerCacheServiceProvider {
45 private static final Logger LOGGER = LoggerFactory.getLogger(CustomerCacheServiceProviderImpl.class);
49 public CustomerCacheServiceProviderImpl(final CacheManager cacheManager) {
54 public Optional<Customer> getCustomer(final String globalCustomerId) {
55 LOGGER.info("getting customer from cache using key: {}", globalCustomerId);
56 final Cache cache = 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 = 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 = 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 = 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 = 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 = 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);
175 public boolean putServiceSubscription(final String globalCustomerId, final String serviceType,
176 final ServiceSubscription serviceSubscription) {
178 final Optional<Customer> customerOptional = getCustomer(globalCustomerId);
180 if (customerOptional.isPresent()) {
181 final Customer customer = customerOptional.get();
182 if (customer.getServiceSubscriptions() == null) {
183 final ServiceSubscriptions serviceSubscriptions = new ServiceSubscriptions();
184 customer.setServiceSubscriptions(serviceSubscriptions);
185 return serviceSubscriptions.getServiceSubscription().add(serviceSubscription);
188 final Optional<ServiceSubscription> serviceSubscriptionOptional = customer.getServiceSubscriptions()
189 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
191 if (!serviceSubscriptionOptional.isPresent()) {
192 return customer.getServiceSubscriptions().getServiceSubscription().add(serviceSubscription);
194 LOGGER.error("ServiceSubscription already exists {}", serviceSubscriptionOptional.get().getServiceType());
197 LOGGER.error("Unable to add ServiceSubscription to cache becuase customer does not exits ...");
202 public boolean patchServiceInstance(final String globalCustomerId, final String serviceType,
203 final String serviceInstanceId, final ServiceInstance serviceInstance) {
204 final Optional<ServiceInstance> instance = getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
205 if (instance.isPresent()) {
206 final ServiceInstance cachedServiceInstance = instance.get();
207 LOGGER.info("Changing OrchestrationStatus from {} to {} ", cachedServiceInstance.getOrchestrationStatus(),
208 serviceInstance.getOrchestrationStatus());
209 cachedServiceInstance.setOrchestrationStatus(serviceInstance.getOrchestrationStatus());
212 LOGGER.error("Unable to find ServiceInstance ...");
216 private ServiceInstances getServiceInstances(final Optional<ServiceSubscription> optional) {
217 final ServiceSubscription serviceSubscription = optional.get();
218 final ServiceInstances serviceInstances = serviceSubscription.getServiceInstances();
219 if (serviceInstances == null) {
220 final ServiceInstances instances = new ServiceInstances();
221 serviceSubscription.setServiceInstances(instances);
224 return serviceInstances;
228 public void clearAll() {
229 clearCahce(Constants.CUSTOMER_CACHE);