Adding basic support for sdnc sim
[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 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);
113
114                 }
115             }
116         }
117         return Optional.empty();
118     }
119
120     @Override
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);
125
126         if (value != null) {
127             final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
128                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
129
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));
137                 }
138
139             }
140         }
141         return Optional.empty();
142     }
143
144     @Override
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);
148
149         final Cache cache = getCache(Constants.CUSTOMER_CACHE);
150         final Customer value = cache.get(globalCustomerId, Customer.class);
151
152         if (value != null) {
153             final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
154                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
155
156             if (serviceSubscription.isPresent()) {
157                 final ServiceInstances serviceInstances = getServiceInstances(serviceSubscription);
158
159
160                 if (!serviceInstances.getServiceInstance().stream()
161                         .filter(existing -> serviceInstanceId.equals(existing.getServiceInstanceId())).findFirst()
162                         .isPresent()) {
163                     return serviceInstances.getServiceInstance().add(serviceInstance);
164                 }
165                 LOGGER.error("Service {} already exists ....", serviceInstanceId);
166                 return false;
167             }
168             LOGGER.error("Couldn't find  service subscription with serviceType: {} in cache ", serviceType);
169             return false;
170         }
171         LOGGER.error("Couldn't find  Customer with key: {} in cache ", globalCustomerId);
172         return false;
173     }
174
175     @Override
176     public boolean putServiceSubscription(final String globalCustomerId, final String serviceType,
177             final ServiceSubscription serviceSubscription) {
178
179         final Optional<Customer> customerOptional = getCustomer(globalCustomerId);
180
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);
187             }
188
189             final Optional<ServiceSubscription> serviceSubscriptionOptional = customer.getServiceSubscriptions()
190                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
191
192             if (!serviceSubscriptionOptional.isPresent()) {
193                 return customer.getServiceSubscriptions().getServiceSubscription().add(serviceSubscription);
194             }
195             LOGGER.error("ServiceSubscription already exists {}", serviceSubscriptionOptional.get().getServiceType());
196             return false;
197         }
198         LOGGER.error("Unable to add ServiceSubscription to cache becuase customer does not exits ...");
199         return false;
200     }
201
202     @Override
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());
211             return true;
212         }
213         LOGGER.error("Unable to find ServiceInstance ...");
214         return false;
215     }
216
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);
223             return instances;
224         }
225         return serviceInstances;
226     }
227
228     @Override
229     public void clearAll() {
230         clearCahce(Constants.CUSTOMER_CACHE);
231     }
232
233 }