7193ade1f2cf68541e8b55241a0754f92c34d211
[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.CUSTOMER_GLOBAL_CUSTOMER_ID;
24 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF;
25 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF_VNF_NAME;
26 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_ID;
27 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_NAME;
28 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_SUBSCRIPTION_SERVICE_TYPE;
29 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getBiDirectionalRelationShipListRelatedLink;
30 import java.util.List;
31 import java.util.Optional;
32 import java.util.stream.Collectors;
33 import org.onap.aai.domain.yang.Customer;
34 import org.onap.aai.domain.yang.RelatedToProperty;
35 import org.onap.aai.domain.yang.Relationship;
36 import org.onap.aai.domain.yang.RelationshipData;
37 import org.onap.aai.domain.yang.RelationshipList;
38 import org.onap.aai.domain.yang.ServiceInstance;
39 import org.onap.aai.domain.yang.ServiceInstances;
40 import org.onap.aai.domain.yang.ServiceSubscription;
41 import org.onap.aai.domain.yang.ServiceSubscriptions;
42 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.cache.Cache;
47 import org.springframework.cache.CacheManager;
48 import org.springframework.stereotype.Service;
49
50 /**
51  * @author waqas.ikram@ericsson.com
52  *
53  */
54 @Service
55 public class CustomerCacheServiceProviderImpl extends AbstractCacheServiceProvider
56         implements CustomerCacheServiceProvider {
57     private static final Logger LOGGER = LoggerFactory.getLogger(CustomerCacheServiceProviderImpl.class);
58
59     @Autowired
60     public CustomerCacheServiceProviderImpl(final CacheManager cacheManager) {
61         super(cacheManager);
62     }
63
64     @Override
65     public Optional<Customer> getCustomer(final String globalCustomerId) {
66         LOGGER.info("getting customer from cache using key: {}", globalCustomerId);
67         final Cache cache = getCache(CUSTOMER_CACHE.getName());
68         final Customer value = cache.get(globalCustomerId, Customer.class);
69         if (value != null) {
70             return Optional.of(value);
71         }
72         return Optional.empty();
73     }
74
75     @Override
76     public void putCustomer(final String globalCustomerId, final Customer customer) {
77         LOGGER.info("Adding customer: {} with key: {} in cache ...", customer, globalCustomerId);
78         final Cache cache = getCache(CUSTOMER_CACHE.getName());
79
80         cache.put(globalCustomerId, customer);
81     }
82
83     @Override
84     public Optional<ServiceSubscription> getServiceSubscription(final String globalCustomerId,
85             final String serviceType) {
86         LOGGER.info("getting service subscription from cache for globalCustomerId: {} and serviceType: {}",
87                 globalCustomerId, serviceType);
88
89         final Cache cache = getCache(CUSTOMER_CACHE.getName());
90
91         final Customer value = cache.get(globalCustomerId, Customer.class);
92
93         if (value != null) {
94             return Optional.ofNullable(value.getServiceSubscriptions().getServiceSubscription().stream()
95                     .filter(s -> serviceType.equals(s.getServiceType())).findFirst().orElse(null));
96         }
97         return Optional.empty();
98
99     }
100
101     @Override
102     public Optional<ServiceInstances> getServiceInstances(final String globalCustomerId, final String serviceType,
103             final String serviceInstanceName) {
104
105         final Cache cache = getCache(CUSTOMER_CACHE.getName());
106         final Customer value = cache.get(globalCustomerId, Customer.class);
107
108         if (value != null) {
109             final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
110                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
111
112             if (serviceSubscription.isPresent()) {
113                 LOGGER.info("Found service subscription ...");
114                 final ServiceInstances serviceInstances = serviceSubscription.get().getServiceInstances();
115                 if (serviceInstances != null) {
116                     final List<ServiceInstance> serviceInstancesList =
117                             serviceInstances.getServiceInstance().stream()
118                                     .filter(serviceInstance -> serviceInstanceName
119                                             .equals(serviceInstance.getServiceInstanceName()))
120                                     .collect(Collectors.toList());
121                     if (serviceInstancesList != null && !serviceInstancesList.isEmpty()) {
122                         LOGGER.info("Found {} service instances ", serviceInstancesList.size());
123                         final ServiceInstances result = new ServiceInstances();
124                         result.getServiceInstance().addAll(serviceInstancesList);
125                         return Optional.of(result);
126
127                     }
128                 }
129             }
130         }
131         return Optional.empty();
132     }
133
134     @Override
135     public Optional<ServiceInstance> getServiceInstance(final String globalCustomerId, final String serviceType,
136             final String serviceInstanceId) {
137         final Cache cache = getCache(CUSTOMER_CACHE.getName());
138         final Customer value = cache.get(globalCustomerId, Customer.class);
139
140         if (value != null) {
141             final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
142                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
143
144             if (serviceSubscription.isPresent()) {
145                 LOGGER.info("Found service subscription ...");
146                 final ServiceInstances serviceInstances = serviceSubscription.get().getServiceInstances();
147                 if (serviceInstances != null) {
148                     return Optional.ofNullable(serviceInstances.getServiceInstance().stream()
149                             .filter(serviceInstance -> serviceInstanceId.equals(serviceInstance.getServiceInstanceId()))
150                             .findFirst().orElse(null));
151                 }
152
153             }
154         }
155         LOGGER.error(
156                 "Unable to find ServiceInstance using globalCustomerId: {}, serviceType: {} and serviceInstanceId: {} ...",
157                 globalCustomerId, serviceType, serviceInstanceId);
158         return Optional.empty();
159     }
160
161     @Override
162     public boolean putServiceInstance(final String globalCustomerId, final String serviceType,
163             final String serviceInstanceId, final ServiceInstance serviceInstance) {
164         LOGGER.info("Adding serviceInstance: {} in cache ...", serviceInstance, globalCustomerId);
165
166         final Cache cache = getCache(CUSTOMER_CACHE.getName());
167         final Customer value = cache.get(globalCustomerId, Customer.class);
168
169         if (value != null) {
170             final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
171                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
172
173             if (serviceSubscription.isPresent()) {
174                 final ServiceInstances serviceInstances = getServiceInstances(serviceSubscription);
175
176
177                 if (!serviceInstances.getServiceInstance().stream()
178                         .filter(existing -> serviceInstanceId.equals(existing.getServiceInstanceId())).findFirst()
179                         .isPresent()) {
180                     return serviceInstances.getServiceInstance().add(serviceInstance);
181                 }
182                 LOGGER.error("Service {} already exists ....", serviceInstanceId);
183                 return false;
184             }
185             LOGGER.error("Couldn't find  service subscription with serviceType: {} in cache ", serviceType);
186             return false;
187         }
188         LOGGER.error("Couldn't find  Customer with key: {} in cache ", globalCustomerId);
189         return false;
190     }
191
192     @Override
193     public boolean putServiceSubscription(final String globalCustomerId, final String serviceType,
194             final ServiceSubscription serviceSubscription) {
195
196         final Optional<Customer> customerOptional = getCustomer(globalCustomerId);
197
198         if (customerOptional.isPresent()) {
199             final Customer customer = customerOptional.get();
200             if (customer.getServiceSubscriptions() == null) {
201                 final ServiceSubscriptions serviceSubscriptions = new ServiceSubscriptions();
202                 customer.setServiceSubscriptions(serviceSubscriptions);
203                 return serviceSubscriptions.getServiceSubscription().add(serviceSubscription);
204             }
205
206             final Optional<ServiceSubscription> serviceSubscriptionOptional = customer.getServiceSubscriptions()
207                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
208
209             if (!serviceSubscriptionOptional.isPresent()) {
210                 return customer.getServiceSubscriptions().getServiceSubscription().add(serviceSubscription);
211             }
212             LOGGER.error("ServiceSubscription already exists {}", serviceSubscriptionOptional.get().getServiceType());
213             return false;
214         }
215         LOGGER.error("Unable to add ServiceSubscription to cache becuase customer does not exits ...");
216         return false;
217     }
218
219     @Override
220     public boolean patchServiceInstance(final String globalCustomerId, final String serviceType,
221             final String serviceInstanceId, final ServiceInstance serviceInstance) {
222         final Optional<ServiceInstance> instance = getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
223         if (instance.isPresent()) {
224             final ServiceInstance cachedServiceInstance = instance.get();
225             LOGGER.info("Changing OrchestrationStatus from {} to {} ", cachedServiceInstance.getOrchestrationStatus(),
226                     serviceInstance.getOrchestrationStatus());
227             cachedServiceInstance.setOrchestrationStatus(serviceInstance.getOrchestrationStatus());
228             return true;
229         }
230         LOGGER.error("Unable to find ServiceInstance ...");
231         return false;
232     }
233
234     @Override
235     public boolean deleteSericeInstance(final String globalCustomerId, final String serviceType,
236             final String serviceInstanceId, final String resourceVersion) {
237         final Cache cache = getCache(CUSTOMER_CACHE.getName());
238         final Customer value = cache.get(globalCustomerId, Customer.class);
239
240         if (value != null) {
241             final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
242                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
243
244             if (serviceSubscription.isPresent()) {
245                 LOGGER.info("Found service subscription ...");
246                 final ServiceInstances serviceInstances = serviceSubscription.get().getServiceInstances();
247                 if (serviceInstances != null) {
248
249                     serviceInstances.getServiceInstance().removeIf(serviceInstance -> {
250                         final String existingServiceInstanceId = serviceInstance.getServiceInstanceId();
251                         final String existingResourceVersion = serviceInstance.getResourceVersion();
252                         if (existingServiceInstanceId != null && existingServiceInstanceId.equals(serviceInstanceId)
253                                 && existingResourceVersion != null && existingResourceVersion.equals(resourceVersion)) {
254                             LOGGER.info("Removing ServiceInstance with serviceInstanceId: {} and resourceVersion: {}",
255                                     existingServiceInstanceId, existingResourceVersion);
256                             return true;
257                         }
258                         return false;
259                     });
260
261
262                     return true;
263                 }
264
265             }
266         }
267         return false;
268     }
269
270     private ServiceInstances getServiceInstances(final Optional<ServiceSubscription> optional) {
271         final ServiceSubscription serviceSubscription = optional.get();
272         final ServiceInstances serviceInstances = serviceSubscription.getServiceInstances();
273         if (serviceInstances == null) {
274             final ServiceInstances instances = new ServiceInstances();
275             serviceSubscription.setServiceInstances(instances);
276             return instances;
277         }
278         return serviceInstances;
279     }
280
281     @Override
282     public Optional<Relationship> getRelationship(final String globalCustomerId, final String serviceType,
283             final String serviceInstanceId, final String vnfName) {
284         final Optional<ServiceInstance> optional = getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
285
286         if (optional.isPresent()) {
287             LOGGER.info("Found service instance ...");
288             final ServiceInstance serviceInstance = optional.get();
289             final RelationshipList relationshipList = serviceInstance.getRelationshipList();
290
291             if (relationshipList != null) {
292                 final List<Relationship> relationship = relationshipList.getRelationship();
293                 return relationship.stream().filter(
294                         relationShip -> relationShip.getRelatedToProperty().stream().filter(relatedToProperty -> {
295                             final String propertyKey = relatedToProperty.getPropertyKey();
296                             final String propertyValue = relatedToProperty.getPropertyValue();
297                             return GENERIC_VNF_VNF_NAME.equals(propertyKey) && propertyValue != null
298                                     && propertyValue.equals(vnfName);
299                         }).findFirst().isPresent()).findFirst();
300             }
301             LOGGER.warn("Relationship list is nulll ...");
302         }
303         LOGGER.error("Unable to RelationShip with property value: {}... ", vnfName);
304
305         return Optional.empty();
306     }
307
308     @Override
309     public Optional<Relationship> addRelationShip(final String globalCustomerId, final String serviceType,
310             final String serviceInstanceId, final Relationship relationship, final String requestUri) {
311         final Optional<ServiceInstance> optional = getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
312         if (optional.isPresent()) {
313             final ServiceInstance serviceInstance = optional.get();
314             RelationshipList relationshipList = serviceInstance.getRelationshipList();
315             if (relationshipList == null) {
316                 relationshipList = new RelationshipList();
317                 serviceInstance.setRelationshipList(relationshipList);
318             }
319             relationshipList.getRelationship().add(relationship);
320
321             LOGGER.info("Successfully added relation to ServiceInstance");
322
323             final Relationship resultantRelationship = new Relationship();
324             resultantRelationship.setRelatedTo(GENERIC_VNF);
325             resultantRelationship.setRelationshipLabel(relationship.getRelationshipLabel());
326             resultantRelationship.setRelatedLink(getBiDirectionalRelationShipListRelatedLink(requestUri));
327
328             final List<RelationshipData> relationshipDataList = resultantRelationship.getRelationshipData();
329             relationshipDataList.add(getRelationshipData(CUSTOMER_GLOBAL_CUSTOMER_ID, globalCustomerId));
330             relationshipDataList.add(getRelationshipData(SERVICE_SUBSCRIPTION_SERVICE_TYPE, serviceType));
331             relationshipDataList.add(getRelationshipData(SERVICE_INSTANCE_SERVICE_INSTANCE_ID, serviceInstanceId));
332
333             final List<RelatedToProperty> relatedToProperty = resultantRelationship.getRelatedToProperty();
334             relatedToProperty.add(getRelatedToProperty(SERVICE_INSTANCE_SERVICE_INSTANCE_NAME,
335                     serviceInstance.getServiceInstanceName()));
336
337             return Optional.of(resultantRelationship);
338
339         }
340         LOGGER.error("Unable to find ServiceInstance ...");
341         return Optional.empty();
342     }
343
344     @Override
345     public void clearAll() {
346         clearCache(CUSTOMER_CACHE.getName());
347     }
348
349     private RelatedToProperty getRelatedToProperty(final String key, final String value) {
350         final RelatedToProperty relatedToProperty = new RelatedToProperty();
351         relatedToProperty.setPropertyKey(key);
352         relatedToProperty.setPropertyValue(value);
353         return relatedToProperty;
354     }
355
356     private RelationshipData getRelationshipData(final String key, final String value) {
357         final RelationshipData relationshipData = new RelationshipData();
358         relationshipData.setRelationshipKey(key);
359         relationshipData.setRelationshipValue(value);
360         return relationshipData;
361     }
362
363
364
365 }