11a82be8223787ab41169c95c7e0f0a1cedf0eac
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aai / simulator / 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.aai.simulator.service.providers;
21
22 import java.util.List;
23 import java.util.Optional;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.stream.Collectors;
26 import org.onap.aai.domain.yang.Customer;
27 import org.onap.aai.domain.yang.ServiceInstance;
28 import org.onap.aai.domain.yang.ServiceInstances;
29 import org.onap.aai.domain.yang.ServiceSubscription;
30 import org.onap.so.aai.simulator.utils.Constants;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.cache.Cache;
35 import org.springframework.cache.CacheManager;
36 import org.springframework.stereotype.Service;
37
38 /**
39  * @author waqas.ikram@ericsson.com
40  *
41  */
42 @Service
43 public class CustomerCacheServiceProviderImpl implements CustomerCacheServiceProvider {
44     private static final Logger LOGGER = LoggerFactory.getLogger(CustomerCacheServiceProviderImpl.class);
45
46     public final CacheManager cacheManager;
47
48     @Autowired
49     public CustomerCacheServiceProviderImpl(final CacheManager cacheManager) {
50         this.cacheManager = cacheManager;
51     }
52
53     @Override
54     public Optional<Customer> getCustomer(final String globalCustomerId) {
55         LOGGER.info("getting customer from cache using key: {}", globalCustomerId);
56         final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE);
57         final Customer value = cache.get(globalCustomerId, Customer.class);
58         if (value != null) {
59             return Optional.of(value);
60         }
61         return Optional.empty();
62     }
63
64     @Override
65     public void putCustomer(final String globalCustomerId, final Customer customer) {
66         LOGGER.info("Adding customer: {} with key: {} in cache ...", customer, globalCustomerId);
67         final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE);
68
69         cache.put(globalCustomerId, customer);
70     }
71
72     @Override
73     public Optional<ServiceSubscription> getServiceSubscription(final String globalCustomerId,
74             final String serviceType) {
75         LOGGER.info("getting service subscription from cache for globalCustomerId: {} and serviceType: {}",
76                 globalCustomerId, serviceType);
77
78         final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE);
79
80         final Customer value = cache.get(globalCustomerId, Customer.class);
81
82         if (value != null) {
83             return Optional.ofNullable(value.getServiceSubscriptions().getServiceSubscription().stream()
84                     .filter(s -> serviceType.equals(s.getServiceType())).findFirst().orElse(null));
85         }
86         return Optional.empty();
87
88     }
89
90     @Override
91     public Optional<ServiceInstances> getServiceInstances(final String globalCustomerId, final String serviceType,
92             final String serviceInstanceName) {
93
94         final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE);
95         final Customer value = cache.get(globalCustomerId, Customer.class);
96
97         if (value != null) {
98             final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
99                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
100
101             if (serviceSubscription.isPresent()) {
102                 LOGGER.info("Found service subscription ...");
103                 final List<ServiceInstance> serviceInstancesList = serviceSubscription.get().getServiceInstances()
104                         .getServiceInstance().stream()
105                         .filter(serviceInstance -> serviceInstanceName.equals(serviceInstance.getServiceInstanceName()))
106                         .collect(Collectors.toList());
107                 if (serviceInstancesList != null && !serviceInstancesList.isEmpty()) {
108                     LOGGER.info("Found {} service instances ", serviceInstancesList.size());
109                     final ServiceInstances serviceInstances = new ServiceInstances();
110                     serviceInstances.getServiceInstance().addAll(serviceInstancesList);
111                     return Optional.of(serviceInstances);
112
113                 }
114             }
115         }
116         return Optional.empty();
117     }
118
119     @Override
120     public Optional<ServiceInstance> getServiceInstance(final String globalCustomerId, final String serviceType,
121             final String serviceInstanceId) {
122         final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE);
123         final Customer value = cache.get(globalCustomerId, Customer.class);
124
125         if (value != null) {
126             final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
127                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
128
129             if (serviceSubscription.isPresent()) {
130                 LOGGER.info("Found service subscription ...");
131                 final ServiceInstances serviceInstances = serviceSubscription.get().getServiceInstances();
132                 if (serviceInstances != null) {
133                     return Optional.ofNullable(serviceInstances.getServiceInstance().stream()
134                             .filter(serviceInstance -> serviceInstanceId.equals(serviceInstance.getServiceInstanceId()))
135                             .findFirst().orElse(null));
136                 }
137
138             }
139         }
140         return Optional.empty();
141     }
142
143     @Override
144     public boolean putServiceInstance(final String globalCustomerId, final String serviceType,
145             final String serviceInstanceId, final ServiceInstance serviceInstance) {
146         LOGGER.info("Adding serviceInstance: {} in cache ...", serviceInstance, globalCustomerId);
147
148         final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE);
149         final Customer value = cache.get(globalCustomerId, Customer.class);
150
151         if (value != null) {
152             final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
153                     .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
154
155             if (serviceSubscription.isPresent()) {
156                 final ServiceInstances serviceInstances = getServiceInstances(serviceSubscription);
157
158
159                 if (!serviceInstances.getServiceInstance().stream()
160                         .filter(existing -> serviceInstanceId.equals(existing.getServiceInstanceId())).findFirst()
161                         .isPresent()) {
162                     return serviceInstances.getServiceInstance().add(serviceInstance);
163                 }
164                 LOGGER.error("Service {} already exists ....", serviceInstanceId);
165                 return false;
166             }
167             LOGGER.error("Couldn't find  service subscription with serviceType: {} in cache ", serviceType);
168             return false;
169         }
170         LOGGER.error("Couldn't find  Customer with key: {} in cache ", globalCustomerId);
171         return false;
172     }
173
174     private ServiceInstances getServiceInstances(final Optional<ServiceSubscription> optional) {
175         final ServiceSubscription serviceSubscription = optional.get();
176         final ServiceInstances serviceInstances = serviceSubscription.getServiceInstances();
177         if (serviceInstances == null) {
178             final ServiceInstances instances = new ServiceInstances();
179             serviceSubscription.setServiceInstances(instances);
180             return instances;
181         }
182         return serviceInstances;
183     }
184
185     @Override
186     public void clearAll() {
187         final Cache cache = cacheManager.getCache(Constants.CUSTOMER_CACHE);
188         final ConcurrentHashMap<?, ?> nativeCache = (ConcurrentHashMap<?, ?>) cache.getNativeCache();
189         LOGGER.info("Clear all entries from cahce: {}", cache.getName());
190         nativeCache.clear();
191     }
192
193 }