Merge "INT:1183 fix csit for sdnc_netconf_tls_post_deploy"
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aaisimulator / service / providers / CustomerCacheServiceProviderImpl.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.aaisimulator.service.providers;
21
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;
38
39 /**
40  * @author waqas.ikram@ericsson.com
41  *
42  */
43 @Service
44 public class CustomerCacheServiceProviderImpl extends AbstractCacheServiceProvider
45         implements CustomerCacheServiceProvider {
46     private static final Logger LOGGER = LoggerFactory.getLogger(CustomerCacheServiceProviderImpl.class);
47
48
49     @Autowired
50     public CustomerCacheServiceProviderImpl(final CacheManager cacheManager) {
51         super(cacheManager);
52     }
53
54     @Override
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);
59         if (value != null) {
60             return Optional.of(value);
61         }
62         return Optional.empty();
63     }
64
65     @Override
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);
69
70         cache.put(globalCustomerId, customer);
71     }
72
73     @Override
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);
78
79         final Cache cache = getCache(Constants.CUSTOMER_CACHE);
80
81         final Customer value = cache.get(globalCustomerId, Customer.class);
82
83         if (value != null) {
84             return Optional.ofNullable(value.getServiceSubscriptions().getServiceSubscription().stream()
85                     .filter(s -> serviceType.equals(s.getServiceType())).findFirst().orElse(null));
86         }
87         return Optional.empty();
88
89     }
90
91     @Override
92     public Optional<ServiceInstances> getServiceInstances(final String globalCustomerId, final String serviceType,
93             final String serviceInstanceName) {
94
95         final Cache cache = getCache(Constants.CUSTOMER_CACHE);
96         final Customer value = cache.get(globalCustomerId, Customer.class);
97
98         if (value != null) {
99             final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
100                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
101
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);
116
117                     }
118                 }
119             }
120         }
121         return Optional.empty();
122     }
123
124     @Override
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);
129
130         if (value != null) {
131             final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
132                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
133
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));
141                 }
142
143             }
144         }
145         return Optional.empty();
146     }
147
148     @Override
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);
152
153         final Cache cache = getCache(Constants.CUSTOMER_CACHE);
154         final Customer value = cache.get(globalCustomerId, Customer.class);
155
156         if (value != null) {
157             final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
158                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
159
160             if (serviceSubscription.isPresent()) {
161                 final ServiceInstances serviceInstances = getServiceInstances(serviceSubscription);
162
163
164                 if (!serviceInstances.getServiceInstance().stream()
165                         .filter(existing -> serviceInstanceId.equals(existing.getServiceInstanceId())).findFirst()
166                         .isPresent()) {
167                     return serviceInstances.getServiceInstance().add(serviceInstance);
168                 }
169                 LOGGER.error("Service {} already exists ....", serviceInstanceId);
170                 return false;
171             }
172             LOGGER.error("Couldn't find  service subscription with serviceType: {} in cache ", serviceType);
173             return false;
174         }
175         LOGGER.error("Couldn't find  Customer with key: {} in cache ", globalCustomerId);
176         return false;
177     }
178
179     @Override
180     public boolean putServiceSubscription(final String globalCustomerId, final String serviceType,
181             final ServiceSubscription serviceSubscription) {
182
183         final Optional<Customer> customerOptional = getCustomer(globalCustomerId);
184
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);
191             }
192
193             final Optional<ServiceSubscription> serviceSubscriptionOptional = customer.getServiceSubscriptions()
194                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
195
196             if (!serviceSubscriptionOptional.isPresent()) {
197                 return customer.getServiceSubscriptions().getServiceSubscription().add(serviceSubscription);
198             }
199             LOGGER.error("ServiceSubscription already exists {}", serviceSubscriptionOptional.get().getServiceType());
200             return false;
201         }
202         LOGGER.error("Unable to add ServiceSubscription to cache becuase customer does not exits ...");
203         return false;
204     }
205
206     @Override
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());
215             return true;
216         }
217         LOGGER.error("Unable to find ServiceInstance ...");
218         return false;
219     }
220
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);
227             return instances;
228         }
229         return serviceInstances;
230     }
231
232     @Override
233     public void clearAll() {
234         clearCahce(Constants.CUSTOMER_CACHE);
235     }
236
237 }