Adding tenant endpoints
[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.COMPOSED_OF;
24 import static org.onap.so.aaisimulator.utils.Constants.CUSTOMER_GLOBAL_CUSTOMER_ID;
25 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF;
26 import static org.onap.so.aaisimulator.utils.Constants.GENERIC_VNF_VNF_NAME;
27 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_ID;
28 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_INSTANCE_SERVICE_INSTANCE_NAME;
29 import static org.onap.so.aaisimulator.utils.Constants.SERVICE_SUBSCRIPTION_SERVICE_TYPE;
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         return Optional.empty();
156     }
157
158     @Override
159     public boolean putServiceInstance(final String globalCustomerId, final String serviceType,
160             final String serviceInstanceId, final ServiceInstance serviceInstance) {
161         LOGGER.info("Adding serviceInstance: {} in cache ...", serviceInstance, globalCustomerId);
162
163         final Cache cache = getCache(CUSTOMER_CACHE.getName());
164         final Customer value = cache.get(globalCustomerId, Customer.class);
165
166         if (value != null) {
167             final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
168                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
169
170             if (serviceSubscription.isPresent()) {
171                 final ServiceInstances serviceInstances = getServiceInstances(serviceSubscription);
172
173
174                 if (!serviceInstances.getServiceInstance().stream()
175                         .filter(existing -> serviceInstanceId.equals(existing.getServiceInstanceId())).findFirst()
176                         .isPresent()) {
177                     return serviceInstances.getServiceInstance().add(serviceInstance);
178                 }
179                 LOGGER.error("Service {} already exists ....", serviceInstanceId);
180                 return false;
181             }
182             LOGGER.error("Couldn't find  service subscription with serviceType: {} in cache ", serviceType);
183             return false;
184         }
185         LOGGER.error("Couldn't find  Customer with key: {} in cache ", globalCustomerId);
186         return false;
187     }
188
189     @Override
190     public boolean putServiceSubscription(final String globalCustomerId, final String serviceType,
191             final ServiceSubscription serviceSubscription) {
192
193         final Optional<Customer> customerOptional = getCustomer(globalCustomerId);
194
195         if (customerOptional.isPresent()) {
196             final Customer customer = customerOptional.get();
197             if (customer.getServiceSubscriptions() == null) {
198                 final ServiceSubscriptions serviceSubscriptions = new ServiceSubscriptions();
199                 customer.setServiceSubscriptions(serviceSubscriptions);
200                 return serviceSubscriptions.getServiceSubscription().add(serviceSubscription);
201             }
202
203             final Optional<ServiceSubscription> serviceSubscriptionOptional = customer.getServiceSubscriptions()
204                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
205
206             if (!serviceSubscriptionOptional.isPresent()) {
207                 return customer.getServiceSubscriptions().getServiceSubscription().add(serviceSubscription);
208             }
209             LOGGER.error("ServiceSubscription already exists {}", serviceSubscriptionOptional.get().getServiceType());
210             return false;
211         }
212         LOGGER.error("Unable to add ServiceSubscription to cache becuase customer does not exits ...");
213         return false;
214     }
215
216     @Override
217     public boolean patchServiceInstance(final String globalCustomerId, final String serviceType,
218             final String serviceInstanceId, final ServiceInstance serviceInstance) {
219         final Optional<ServiceInstance> instance = getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
220         if (instance.isPresent()) {
221             final ServiceInstance cachedServiceInstance = instance.get();
222             LOGGER.info("Changing OrchestrationStatus from {} to {} ", cachedServiceInstance.getOrchestrationStatus(),
223                     serviceInstance.getOrchestrationStatus());
224             cachedServiceInstance.setOrchestrationStatus(serviceInstance.getOrchestrationStatus());
225             return true;
226         }
227         LOGGER.error("Unable to find ServiceInstance ...");
228         return false;
229     }
230
231     private ServiceInstances getServiceInstances(final Optional<ServiceSubscription> optional) {
232         final ServiceSubscription serviceSubscription = optional.get();
233         final ServiceInstances serviceInstances = serviceSubscription.getServiceInstances();
234         if (serviceInstances == null) {
235             final ServiceInstances instances = new ServiceInstances();
236             serviceSubscription.setServiceInstances(instances);
237             return instances;
238         }
239         return serviceInstances;
240     }
241
242     @Override
243     public Optional<Relationship> getRelationship(final String globalCustomerId, final String serviceType,
244             final String serviceInstanceId, final String vnfName) {
245         final Optional<ServiceInstance> optional = getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
246
247         if (optional.isPresent()) {
248             LOGGER.info("Found service instance ...");
249             final ServiceInstance serviceInstance = optional.get();
250             final RelationshipList relationshipList = serviceInstance.getRelationshipList();
251
252             if (relationshipList != null) {
253                 final List<Relationship> relationship = relationshipList.getRelationship();
254                 return relationship.stream().filter(
255                         relationShip -> relationShip.getRelatedToProperty().stream().filter(relatedToProperty -> {
256                             final String propertyKey = relatedToProperty.getPropertyKey();
257                             final String propertyValue = relatedToProperty.getPropertyValue();
258                             return GENERIC_VNF_VNF_NAME.equals(propertyKey) && propertyValue != null
259                                     && propertyValue.equals(vnfName);
260                         }).findFirst().isPresent()).findFirst();
261             }
262             LOGGER.warn("Relationship list is nulll ...");
263         }
264         LOGGER.error("Unable to RelationShip with property value: {}... ", vnfName);
265
266         return Optional.empty();
267     }
268
269     @Override
270     public Optional<Relationship> addRelationShip(final String globalCustomerId, final String serviceType,
271             final String serviceInstanceId, final Relationship relationship, final String requestUri) {
272         final Optional<ServiceInstance> optional = getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
273         if (optional.isPresent()) {
274             final ServiceInstance serviceInstance = optional.get();
275             RelationshipList relationshipList = serviceInstance.getRelationshipList();
276             if (relationshipList == null) {
277                 relationshipList = new RelationshipList();
278                 serviceInstance.setRelationshipList(relationshipList);
279             }
280             relationshipList.getRelationship().add(relationship);
281
282             LOGGER.info("Successfully added relation to ServiceInstance");
283
284             final Relationship resultantRelationship = new Relationship();
285             resultantRelationship.setRelatedTo(GENERIC_VNF);
286             resultantRelationship.setRelationshipLabel(COMPOSED_OF);
287             resultantRelationship.setRelatedLink(requestUri);
288
289             final List<RelationshipData> relationshipDataList = resultantRelationship.getRelationshipData();
290             relationshipDataList.add(getRelationshipData(CUSTOMER_GLOBAL_CUSTOMER_ID, globalCustomerId));
291             relationshipDataList.add(getRelationshipData(SERVICE_SUBSCRIPTION_SERVICE_TYPE, serviceType));
292             relationshipDataList.add(getRelationshipData(SERVICE_INSTANCE_SERVICE_INSTANCE_ID, serviceInstanceId));
293
294             final List<RelatedToProperty> relatedToProperty = resultantRelationship.getRelatedToProperty();
295             relatedToProperty.add(getRelatedToProperty(SERVICE_INSTANCE_SERVICE_INSTANCE_NAME,
296                     serviceInstance.getServiceInstanceName()));
297
298             return Optional.of(resultantRelationship);
299
300         }
301         LOGGER.error("Unable to find ServiceInstance ...");
302         return Optional.empty();
303     }
304
305     @Override
306     public void clearAll() {
307         clearCahce(CUSTOMER_CACHE.getName());
308     }
309
310     private RelatedToProperty getRelatedToProperty(final String key, final String value) {
311         final RelatedToProperty relatedToProperty = new RelatedToProperty();
312         relatedToProperty.setPropertyKey(key);
313         relatedToProperty.setPropertyValue(value);
314         return relatedToProperty;
315     }
316
317     private RelationshipData getRelationshipData(final String key, final String value) {
318         final RelationshipData relationshipData = new RelationshipData();
319         relationshipData.setRelationshipKey(key);
320         relationshipData.setRelationshipValue(value);
321         return relationshipData;
322     }
323
324
325
326 }