695bfc0ff0e560f55e0cac0ae6b742821361612d
[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 static org.onap.so.aaisimulator.utils.CacheName.CUSTOMER_CACHE;
23 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF_VNF_NAME;
24 import java.util.List;
25 import java.util.Optional;
26 import java.util.stream.Collectors;
27 import org.onap.aai.domain.yang.Customer;
28 import org.onap.aai.domain.yang.Relationship;
29 import org.onap.aai.domain.yang.RelationshipList;
30 import org.onap.aai.domain.yang.ServiceInstance;
31 import org.onap.aai.domain.yang.ServiceInstances;
32 import org.onap.aai.domain.yang.ServiceSubscription;
33 import org.onap.aai.domain.yang.ServiceSubscriptions;
34 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.cache.Cache;
39 import org.springframework.cache.CacheManager;
40 import org.springframework.stereotype.Service;
41
42 /**
43  * @author waqas.ikram@ericsson.com
44  *
45  */
46 @Service
47 public class CustomerCacheServiceProviderImpl extends AbstractCacheServiceProvider
48         implements CustomerCacheServiceProvider {
49     private static final Logger LOGGER = LoggerFactory.getLogger(CustomerCacheServiceProviderImpl.class);
50
51     @Autowired
52     public CustomerCacheServiceProviderImpl(final CacheManager cacheManager) {
53         super(cacheManager);
54     }
55
56     @Override
57     public Optional<Customer> getCustomer(final String globalCustomerId) {
58         LOGGER.info("getting customer from cache using key: {}", globalCustomerId);
59         final Cache cache = getCache(CUSTOMER_CACHE.getName());
60         final Customer value = cache.get(globalCustomerId, Customer.class);
61         if (value != null) {
62             return Optional.of(value);
63         }
64         return Optional.empty();
65     }
66
67     @Override
68     public void putCustomer(final String globalCustomerId, final Customer customer) {
69         LOGGER.info("Adding customer: {} with key: {} in cache ...", customer, globalCustomerId);
70         final Cache cache = getCache(CUSTOMER_CACHE.getName());
71
72         cache.put(globalCustomerId, customer);
73     }
74
75     @Override
76     public Optional<ServiceSubscription> getServiceSubscription(final String globalCustomerId,
77             final String serviceType) {
78         LOGGER.info("getting service subscription from cache for globalCustomerId: {} and serviceType: {}",
79                 globalCustomerId, serviceType);
80
81         final Cache cache = getCache(CUSTOMER_CACHE.getName());
82
83         final Customer value = cache.get(globalCustomerId, Customer.class);
84
85         if (value != null) {
86             return Optional.ofNullable(value.getServiceSubscriptions().getServiceSubscription().stream()
87                     .filter(s -> serviceType.equals(s.getServiceType())).findFirst().orElse(null));
88         }
89         return Optional.empty();
90
91     }
92
93     @Override
94     public Optional<ServiceInstances> getServiceInstances(final String globalCustomerId, final String serviceType,
95             final String serviceInstanceName) {
96
97         final Cache cache = getCache(CUSTOMER_CACHE.getName());
98         final Customer value = cache.get(globalCustomerId, Customer.class);
99
100         if (value != null) {
101             final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
102                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
103
104             if (serviceSubscription.isPresent()) {
105                 LOGGER.info("Found service subscription ...");
106                 final ServiceInstances serviceInstances = serviceSubscription.get().getServiceInstances();
107                 if (serviceInstances != null) {
108                     final List<ServiceInstance> serviceInstancesList =
109                             serviceInstances.getServiceInstance().stream()
110                                     .filter(serviceInstance -> serviceInstanceName
111                                             .equals(serviceInstance.getServiceInstanceName()))
112                                     .collect(Collectors.toList());
113                     if (serviceInstancesList != null && !serviceInstancesList.isEmpty()) {
114                         LOGGER.info("Found {} service instances ", serviceInstancesList.size());
115                         final ServiceInstances result = new ServiceInstances();
116                         result.getServiceInstance().addAll(serviceInstancesList);
117                         return Optional.of(result);
118
119                     }
120                 }
121             }
122         }
123         return Optional.empty();
124     }
125
126     @Override
127     public Optional<ServiceInstance> getServiceInstance(final String globalCustomerId, final String serviceType,
128             final String serviceInstanceId) {
129         final Cache cache = getCache(CUSTOMER_CACHE.getName());
130         final Customer value = cache.get(globalCustomerId, Customer.class);
131
132         if (value != null) {
133             final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
134                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
135
136             if (serviceSubscription.isPresent()) {
137                 LOGGER.info("Found service subscription ...");
138                 final ServiceInstances serviceInstances = serviceSubscription.get().getServiceInstances();
139                 if (serviceInstances != null) {
140                     return Optional.ofNullable(serviceInstances.getServiceInstance().stream()
141                             .filter(serviceInstance -> serviceInstanceId.equals(serviceInstance.getServiceInstanceId()))
142                             .findFirst().orElse(null));
143                 }
144
145             }
146         }
147         return Optional.empty();
148     }
149
150     @Override
151     public boolean putServiceInstance(final String globalCustomerId, final String serviceType,
152             final String serviceInstanceId, final ServiceInstance serviceInstance) {
153         LOGGER.info("Adding serviceInstance: {} in cache ...", serviceInstance, globalCustomerId);
154
155         final Cache cache = getCache(CUSTOMER_CACHE.getName());
156         final Customer value = cache.get(globalCustomerId, Customer.class);
157
158         if (value != null) {
159             final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
160                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
161
162             if (serviceSubscription.isPresent()) {
163                 final ServiceInstances serviceInstances = getServiceInstances(serviceSubscription);
164
165
166                 if (!serviceInstances.getServiceInstance().stream()
167                         .filter(existing -> serviceInstanceId.equals(existing.getServiceInstanceId())).findFirst()
168                         .isPresent()) {
169                     return serviceInstances.getServiceInstance().add(serviceInstance);
170                 }
171                 LOGGER.error("Service {} already exists ....", serviceInstanceId);
172                 return false;
173             }
174             LOGGER.error("Couldn't find  service subscription with serviceType: {} in cache ", serviceType);
175             return false;
176         }
177         LOGGER.error("Couldn't find  Customer with key: {} in cache ", globalCustomerId);
178         return false;
179     }
180
181     @Override
182     public boolean putServiceSubscription(final String globalCustomerId, final String serviceType,
183             final ServiceSubscription serviceSubscription) {
184
185         final Optional<Customer> customerOptional = getCustomer(globalCustomerId);
186
187         if (customerOptional.isPresent()) {
188             final Customer customer = customerOptional.get();
189             if (customer.getServiceSubscriptions() == null) {
190                 final ServiceSubscriptions serviceSubscriptions = new ServiceSubscriptions();
191                 customer.setServiceSubscriptions(serviceSubscriptions);
192                 return serviceSubscriptions.getServiceSubscription().add(serviceSubscription);
193             }
194
195             final Optional<ServiceSubscription> serviceSubscriptionOptional = customer.getServiceSubscriptions()
196                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
197
198             if (!serviceSubscriptionOptional.isPresent()) {
199                 return customer.getServiceSubscriptions().getServiceSubscription().add(serviceSubscription);
200             }
201             LOGGER.error("ServiceSubscription already exists {}", serviceSubscriptionOptional.get().getServiceType());
202             return false;
203         }
204         LOGGER.error("Unable to add ServiceSubscription to cache becuase customer does not exits ...");
205         return false;
206     }
207
208     @Override
209     public boolean patchServiceInstance(final String globalCustomerId, final String serviceType,
210             final String serviceInstanceId, final ServiceInstance serviceInstance) {
211         final Optional<ServiceInstance> instance = getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
212         if (instance.isPresent()) {
213             final ServiceInstance cachedServiceInstance = instance.get();
214             LOGGER.info("Changing OrchestrationStatus from {} to {} ", cachedServiceInstance.getOrchestrationStatus(),
215                     serviceInstance.getOrchestrationStatus());
216             cachedServiceInstance.setOrchestrationStatus(serviceInstance.getOrchestrationStatus());
217             return true;
218         }
219         LOGGER.error("Unable to find ServiceInstance ...");
220         return false;
221     }
222
223     private ServiceInstances getServiceInstances(final Optional<ServiceSubscription> optional) {
224         final ServiceSubscription serviceSubscription = optional.get();
225         final ServiceInstances serviceInstances = serviceSubscription.getServiceInstances();
226         if (serviceInstances == null) {
227             final ServiceInstances instances = new ServiceInstances();
228             serviceSubscription.setServiceInstances(instances);
229             return instances;
230         }
231         return serviceInstances;
232     }
233
234     @Override
235     public Optional<Relationship> getRelationship(final String globalCustomerId, final String serviceType,
236             final String serviceInstanceId, final String vnfName) {
237         final Optional<ServiceInstance> optional = getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
238
239         if (optional.isPresent()) {
240             LOGGER.info("Found service instance ...");
241             final ServiceInstance serviceInstance = optional.get();
242             final RelationshipList relationshipList = serviceInstance.getRelationshipList();
243
244             if (relationshipList != null) {
245                 final List<Relationship> relationship = relationshipList.getRelationship();
246                 return relationship.stream().filter(
247                         relationShip -> relationShip.getRelatedToProperty().stream().filter(relatedToProperty -> {
248                             final String propertyKey = relatedToProperty.getPropertyKey();
249                             final String propertyValue = relatedToProperty.getPropertyValue();
250                             return GENERIC_VNF_VNF_NAME.equals(propertyKey) && propertyValue != null
251                                     && propertyValue.equals(vnfName);
252                         }).findFirst().isPresent()).findFirst();
253             }
254             LOGGER.warn("Relationship list is nulll ...");
255         }
256         LOGGER.error("Unable to RelationShip with property value: {}... ", vnfName);
257
258         return Optional.empty();
259     }
260
261     @Override
262     public Optional<ServiceInstance> addRelationShip(final String globalCustomerId, final String serviceType,
263             final String serviceInstanceId, final Relationship relationship) {
264         final Optional<ServiceInstance> optional = getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
265         if (optional.isPresent()) {
266             final ServiceInstance serviceInstance = optional.get();
267             RelationshipList relationshipList = serviceInstance.getRelationshipList();
268             if (relationshipList == null) {
269                 relationshipList = new RelationshipList();
270                 serviceInstance.setRelationshipList(relationshipList);
271             }
272             relationshipList.getRelationship().add(relationship);
273             return Optional.of(serviceInstance);
274
275         }
276         LOGGER.error("Unable to find ServiceInstance ...");
277         return Optional.empty();
278
279     }
280
281     @Override
282     public void clearAll() {
283         clearCahce(CUSTOMER_CACHE.getName());
284     }
285
286 }