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 List<ServiceInstance> serviceInstancesList = serviceSubscription.get().getServiceInstances()
105 .getServiceInstance().stream()
106 .filter(serviceInstance -> serviceInstanceName.equals(serviceInstance.getServiceInstanceName()))
107 .collect(Collectors.toList());
108 if (serviceInstancesList != null && !serviceInstancesList.isEmpty()) {
109 LOGGER.info("Found {} service instances ", serviceInstancesList.size());
110 final ServiceInstances serviceInstances = new ServiceInstances();
111 serviceInstances.getServiceInstance().addAll(serviceInstancesList);
112 return Optional.of(serviceInstances);
117 return Optional.empty();
121 public Optional<ServiceInstance> getServiceInstance(final String globalCustomerId, final String serviceType,
122 final String serviceInstanceId) {
123 final Cache cache = getCache(Constants.CUSTOMER_CACHE);
124 final Customer value = cache.get(globalCustomerId, Customer.class);
127 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
128 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
130 if (serviceSubscription.isPresent()) {
131 LOGGER.info("Found service subscription ...");
132 final ServiceInstances serviceInstances = serviceSubscription.get().getServiceInstances();
133 if (serviceInstances != null) {
134 return Optional.ofNullable(serviceInstances.getServiceInstance().stream()
135 .filter(serviceInstance -> serviceInstanceId.equals(serviceInstance.getServiceInstanceId()))
136 .findFirst().orElse(null));
141 return Optional.empty();
145 public boolean putServiceInstance(final String globalCustomerId, final String serviceType,
146 final String serviceInstanceId, final ServiceInstance serviceInstance) {
147 LOGGER.info("Adding serviceInstance: {} in cache ...", serviceInstance, globalCustomerId);
149 final Cache cache = getCache(Constants.CUSTOMER_CACHE);
150 final Customer value = cache.get(globalCustomerId, Customer.class);
153 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
154 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
156 if (serviceSubscription.isPresent()) {
157 final ServiceInstances serviceInstances = getServiceInstances(serviceSubscription);
160 if (!serviceInstances.getServiceInstance().stream()
161 .filter(existing -> serviceInstanceId.equals(existing.getServiceInstanceId())).findFirst()
163 return serviceInstances.getServiceInstance().add(serviceInstance);
165 LOGGER.error("Service {} already exists ....", serviceInstanceId);
168 LOGGER.error("Couldn't find service subscription with serviceType: {} in cache ", serviceType);
171 LOGGER.error("Couldn't find Customer with key: {} in cache ", globalCustomerId);
176 public boolean putServiceSubscription(final String globalCustomerId, final String serviceType,
177 final ServiceSubscription serviceSubscription) {
179 final Optional<Customer> customerOptional = getCustomer(globalCustomerId);
181 if (customerOptional.isPresent()) {
182 final Customer customer = customerOptional.get();
183 if (customer.getServiceSubscriptions() == null) {
184 final ServiceSubscriptions serviceSubscriptions = new ServiceSubscriptions();
185 customer.setServiceSubscriptions(serviceSubscriptions);
186 return serviceSubscriptions.getServiceSubscription().add(serviceSubscription);
189 final Optional<ServiceSubscription> serviceSubscriptionOptional = customer.getServiceSubscriptions()
190 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
192 if (!serviceSubscriptionOptional.isPresent()) {
193 return customer.getServiceSubscriptions().getServiceSubscription().add(serviceSubscription);
195 LOGGER.error("ServiceSubscription already exists {}", serviceSubscriptionOptional.get().getServiceType());
198 LOGGER.error("Unable to add ServiceSubscription to cache becuase customer does not exits ...");
203 public boolean patchServiceInstance(final String globalCustomerId, final String serviceType,
204 final String serviceInstanceId, final ServiceInstance serviceInstance) {
205 final Optional<ServiceInstance> instance = getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
206 if (instance.isPresent()) {
207 final ServiceInstance cachedServiceInstance = instance.get();
208 LOGGER.info("Changing OrchestrationStatus from {} to {} ", cachedServiceInstance.getOrchestrationStatus(),
209 serviceInstance.getOrchestrationStatus());
210 cachedServiceInstance.setOrchestrationStatus(serviceInstance.getOrchestrationStatus());
213 LOGGER.error("Unable to find ServiceInstance ...");
217 private ServiceInstances getServiceInstances(final Optional<ServiceSubscription> optional) {
218 final ServiceSubscription serviceSubscription = optional.get();
219 final ServiceInstances serviceInstances = serviceSubscription.getServiceInstances();
220 if (serviceInstances == null) {
221 final ServiceInstances instances = new ServiceInstances();
222 serviceSubscription.setServiceInstances(instances);
225 return serviceInstances;
229 public void clearAll() {
230 clearCahce(Constants.CUSTOMER_CACHE);