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.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.cache.Cache;
36 import org.springframework.cache.CacheManager;
37 import org.springframework.stereotype.Service;
40 * @author waqas.ikram@ericsson.com
44 public class CustomerCacheServiceProviderImpl extends AbstractCacheServiceProvider
45 implements CustomerCacheServiceProvider {
46 private static final Logger LOGGER = LoggerFactory.getLogger(CustomerCacheServiceProviderImpl.class);
50 public CustomerCacheServiceProviderImpl(final CacheManager cacheManager) {
55 public Optional<Customer> getCustomer(final String globalCustomerId) {
56 LOGGER.info("getting customer from cache using key: {}", globalCustomerId);
57 final Cache cache = getCache(Constants.CUSTOMER_CACHE);
58 final Customer value = cache.get(globalCustomerId, Customer.class);
60 return Optional.of(value);
62 return Optional.empty();
66 public void putCustomer(final String globalCustomerId, final Customer customer) {
67 LOGGER.info("Adding customer: {} with key: {} in cache ...", customer, globalCustomerId);
68 final Cache cache = getCache(Constants.CUSTOMER_CACHE);
70 cache.put(globalCustomerId, customer);
74 public Optional<ServiceSubscription> getServiceSubscription(final String globalCustomerId,
75 final String serviceType) {
76 LOGGER.info("getting service subscription from cache for globalCustomerId: {} and serviceType: {}",
77 globalCustomerId, serviceType);
79 final Cache cache = getCache(Constants.CUSTOMER_CACHE);
81 final Customer value = cache.get(globalCustomerId, Customer.class);
84 return Optional.ofNullable(value.getServiceSubscriptions().getServiceSubscription().stream()
85 .filter(s -> serviceType.equals(s.getServiceType())).findFirst().orElse(null));
87 return Optional.empty();
92 public Optional<ServiceInstances> getServiceInstances(final String globalCustomerId, final String serviceType,
93 final String serviceInstanceName) {
95 final Cache cache = getCache(Constants.CUSTOMER_CACHE);
96 final Customer value = cache.get(globalCustomerId, Customer.class);
99 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
100 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
102 if (serviceSubscription.isPresent()) {
103 LOGGER.info("Found service subscription ...");
104 final ServiceInstances serviceInstances = serviceSubscription.get().getServiceInstances();
105 if (serviceInstances != null) {
106 final List<ServiceInstance> serviceInstancesList =
107 serviceInstances.getServiceInstance().stream()
108 .filter(serviceInstance -> serviceInstanceName
109 .equals(serviceInstance.getServiceInstanceName()))
110 .collect(Collectors.toList());
111 if (serviceInstancesList != null && !serviceInstancesList.isEmpty()) {
112 LOGGER.info("Found {} service instances ", serviceInstancesList.size());
113 final ServiceInstances result = new ServiceInstances();
114 result.getServiceInstance().addAll(serviceInstancesList);
115 return Optional.of(result);
121 return Optional.empty();
125 public Optional<ServiceInstance> getServiceInstance(final String globalCustomerId, final String serviceType,
126 final String serviceInstanceId) {
127 final Cache cache = getCache(Constants.CUSTOMER_CACHE);
128 final Customer value = cache.get(globalCustomerId, Customer.class);
131 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
132 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
134 if (serviceSubscription.isPresent()) {
135 LOGGER.info("Found service subscription ...");
136 final ServiceInstances serviceInstances = serviceSubscription.get().getServiceInstances();
137 if (serviceInstances != null) {
138 return Optional.ofNullable(serviceInstances.getServiceInstance().stream()
139 .filter(serviceInstance -> serviceInstanceId.equals(serviceInstance.getServiceInstanceId()))
140 .findFirst().orElse(null));
145 return Optional.empty();
149 public boolean putServiceInstance(final String globalCustomerId, final String serviceType,
150 final String serviceInstanceId, final ServiceInstance serviceInstance) {
151 LOGGER.info("Adding serviceInstance: {} in cache ...", serviceInstance, globalCustomerId);
153 final Cache cache = getCache(Constants.CUSTOMER_CACHE);
154 final Customer value = cache.get(globalCustomerId, Customer.class);
157 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
158 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
160 if (serviceSubscription.isPresent()) {
161 final ServiceInstances serviceInstances = getServiceInstances(serviceSubscription);
164 if (!serviceInstances.getServiceInstance().stream()
165 .filter(existing -> serviceInstanceId.equals(existing.getServiceInstanceId())).findFirst()
167 return serviceInstances.getServiceInstance().add(serviceInstance);
169 LOGGER.error("Service {} already exists ....", serviceInstanceId);
172 LOGGER.error("Couldn't find service subscription with serviceType: {} in cache ", serviceType);
175 LOGGER.error("Couldn't find Customer with key: {} in cache ", globalCustomerId);
180 public boolean putServiceSubscription(final String globalCustomerId, final String serviceType,
181 final ServiceSubscription serviceSubscription) {
183 final Optional<Customer> customerOptional = getCustomer(globalCustomerId);
185 if (customerOptional.isPresent()) {
186 final Customer customer = customerOptional.get();
187 if (customer.getServiceSubscriptions() == null) {
188 final ServiceSubscriptions serviceSubscriptions = new ServiceSubscriptions();
189 customer.setServiceSubscriptions(serviceSubscriptions);
190 return serviceSubscriptions.getServiceSubscription().add(serviceSubscription);
193 final Optional<ServiceSubscription> serviceSubscriptionOptional = customer.getServiceSubscriptions()
194 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
196 if (!serviceSubscriptionOptional.isPresent()) {
197 return customer.getServiceSubscriptions().getServiceSubscription().add(serviceSubscription);
199 LOGGER.error("ServiceSubscription already exists {}", serviceSubscriptionOptional.get().getServiceType());
202 LOGGER.error("Unable to add ServiceSubscription to cache becuase customer does not exits ...");
207 public boolean patchServiceInstance(final String globalCustomerId, final String serviceType,
208 final String serviceInstanceId, final ServiceInstance serviceInstance) {
209 final Optional<ServiceInstance> instance = getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
210 if (instance.isPresent()) {
211 final ServiceInstance cachedServiceInstance = instance.get();
212 LOGGER.info("Changing OrchestrationStatus from {} to {} ", cachedServiceInstance.getOrchestrationStatus(),
213 serviceInstance.getOrchestrationStatus());
214 cachedServiceInstance.setOrchestrationStatus(serviceInstance.getOrchestrationStatus());
217 LOGGER.error("Unable to find ServiceInstance ...");
221 private ServiceInstances getServiceInstances(final Optional<ServiceSubscription> optional) {
222 final ServiceSubscription serviceSubscription = optional.get();
223 final ServiceInstances serviceInstances = serviceSubscription.getServiceInstances();
224 if (serviceInstances == null) {
225 final ServiceInstances instances = new ServiceInstances();
226 serviceSubscription.setServiceInstances(instances);
229 return serviceInstances;
233 public void clearAll() {
234 clearCahce(Constants.CUSTOMER_CACHE);