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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.aaisimulator.service.providers;
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;
43 * @author waqas.ikram@ericsson.com
47 public class CustomerCacheServiceProviderImpl extends AbstractCacheServiceProvider
48 implements CustomerCacheServiceProvider {
49 private static final Logger LOGGER = LoggerFactory.getLogger(CustomerCacheServiceProviderImpl.class);
52 public CustomerCacheServiceProviderImpl(final CacheManager cacheManager) {
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);
62 return Optional.of(value);
64 return Optional.empty();
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());
72 cache.put(globalCustomerId, customer);
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);
81 final Cache cache = getCache(CUSTOMER_CACHE.getName());
83 final Customer value = cache.get(globalCustomerId, Customer.class);
86 return Optional.ofNullable(value.getServiceSubscriptions().getServiceSubscription().stream()
87 .filter(s -> serviceType.equals(s.getServiceType())).findFirst().orElse(null));
89 return Optional.empty();
94 public Optional<ServiceInstances> getServiceInstances(final String globalCustomerId, final String serviceType,
95 final String serviceInstanceName) {
97 final Cache cache = getCache(CUSTOMER_CACHE.getName());
98 final Customer value = cache.get(globalCustomerId, Customer.class);
101 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
102 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
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);
123 return Optional.empty();
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);
133 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
134 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
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));
147 return Optional.empty();
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);
155 final Cache cache = getCache(CUSTOMER_CACHE.getName());
156 final Customer value = cache.get(globalCustomerId, Customer.class);
159 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
160 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
162 if (serviceSubscription.isPresent()) {
163 final ServiceInstances serviceInstances = getServiceInstances(serviceSubscription);
166 if (!serviceInstances.getServiceInstance().stream()
167 .filter(existing -> serviceInstanceId.equals(existing.getServiceInstanceId())).findFirst()
169 return serviceInstances.getServiceInstance().add(serviceInstance);
171 LOGGER.error("Service {} already exists ....", serviceInstanceId);
174 LOGGER.error("Couldn't find service subscription with serviceType: {} in cache ", serviceType);
177 LOGGER.error("Couldn't find Customer with key: {} in cache ", globalCustomerId);
182 public boolean putServiceSubscription(final String globalCustomerId, final String serviceType,
183 final ServiceSubscription serviceSubscription) {
185 final Optional<Customer> customerOptional = getCustomer(globalCustomerId);
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);
195 final Optional<ServiceSubscription> serviceSubscriptionOptional = customer.getServiceSubscriptions()
196 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
198 if (!serviceSubscriptionOptional.isPresent()) {
199 return customer.getServiceSubscriptions().getServiceSubscription().add(serviceSubscription);
201 LOGGER.error("ServiceSubscription already exists {}", serviceSubscriptionOptional.get().getServiceType());
204 LOGGER.error("Unable to add ServiceSubscription to cache becuase customer does not exits ...");
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());
219 LOGGER.error("Unable to find ServiceInstance ...");
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);
231 return serviceInstances;
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);
239 if (optional.isPresent()) {
240 LOGGER.info("Found service instance ...");
241 final ServiceInstance serviceInstance = optional.get();
242 final RelationshipList relationshipList = serviceInstance.getRelationshipList();
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();
254 LOGGER.warn("Relationship list is nulll ...");
256 LOGGER.error("Unable to RelationShip with property value: {}... ", vnfName);
258 return Optional.empty();
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);
272 relationshipList.getRelationship().add(relationship);
273 return Optional.of(serviceInstance);
276 LOGGER.error("Unable to find ServiceInstance ...");
277 return Optional.empty();
282 public void clearAll() {
283 clearCahce(CUSTOMER_CACHE.getName());