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.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;
51 * @author waqas.ikram@ericsson.com
55 public class CustomerCacheServiceProviderImpl extends AbstractCacheServiceProvider
56 implements CustomerCacheServiceProvider {
57 private static final Logger LOGGER = LoggerFactory.getLogger(CustomerCacheServiceProviderImpl.class);
60 public CustomerCacheServiceProviderImpl(final CacheManager cacheManager) {
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);
70 return Optional.of(value);
72 return Optional.empty();
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());
80 cache.put(globalCustomerId, customer);
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);
89 final Cache cache = getCache(CUSTOMER_CACHE.getName());
91 final Customer value = cache.get(globalCustomerId, Customer.class);
94 return Optional.ofNullable(value.getServiceSubscriptions().getServiceSubscription().stream()
95 .filter(s -> serviceType.equals(s.getServiceType())).findFirst().orElse(null));
97 return Optional.empty();
102 public Optional<ServiceInstances> getServiceInstances(final String globalCustomerId, final String serviceType,
103 final String serviceInstanceName) {
105 final Cache cache = getCache(CUSTOMER_CACHE.getName());
106 final Customer value = cache.get(globalCustomerId, Customer.class);
109 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
110 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
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);
131 return Optional.empty();
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);
141 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
142 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
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));
155 return Optional.empty();
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);
163 final Cache cache = getCache(CUSTOMER_CACHE.getName());
164 final Customer value = cache.get(globalCustomerId, Customer.class);
167 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
168 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
170 if (serviceSubscription.isPresent()) {
171 final ServiceInstances serviceInstances = getServiceInstances(serviceSubscription);
174 if (!serviceInstances.getServiceInstance().stream()
175 .filter(existing -> serviceInstanceId.equals(existing.getServiceInstanceId())).findFirst()
177 return serviceInstances.getServiceInstance().add(serviceInstance);
179 LOGGER.error("Service {} already exists ....", serviceInstanceId);
182 LOGGER.error("Couldn't find service subscription with serviceType: {} in cache ", serviceType);
185 LOGGER.error("Couldn't find Customer with key: {} in cache ", globalCustomerId);
190 public boolean putServiceSubscription(final String globalCustomerId, final String serviceType,
191 final ServiceSubscription serviceSubscription) {
193 final Optional<Customer> customerOptional = getCustomer(globalCustomerId);
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);
203 final Optional<ServiceSubscription> serviceSubscriptionOptional = customer.getServiceSubscriptions()
204 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
206 if (!serviceSubscriptionOptional.isPresent()) {
207 return customer.getServiceSubscriptions().getServiceSubscription().add(serviceSubscription);
209 LOGGER.error("ServiceSubscription already exists {}", serviceSubscriptionOptional.get().getServiceType());
212 LOGGER.error("Unable to add ServiceSubscription to cache becuase customer does not exits ...");
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());
227 LOGGER.error("Unable to find ServiceInstance ...");
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);
239 return serviceInstances;
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);
247 if (optional.isPresent()) {
248 LOGGER.info("Found service instance ...");
249 final ServiceInstance serviceInstance = optional.get();
250 final RelationshipList relationshipList = serviceInstance.getRelationshipList();
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();
262 LOGGER.warn("Relationship list is nulll ...");
264 LOGGER.error("Unable to RelationShip with property value: {}... ", vnfName);
266 return Optional.empty();
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);
280 relationshipList.getRelationship().add(relationship);
282 LOGGER.info("Successfully added relation to ServiceInstance");
284 final Relationship resultantRelationship = new Relationship();
285 resultantRelationship.setRelatedTo(GENERIC_VNF);
286 resultantRelationship.setRelationshipLabel(relationship.getRelationshipLabel());
287 resultantRelationship.setRelatedLink(getBiDirectionalRelationShipListRelatedLink(requestUri));
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));
294 final List<RelatedToProperty> relatedToProperty = resultantRelationship.getRelatedToProperty();
295 relatedToProperty.add(getRelatedToProperty(SERVICE_INSTANCE_SERVICE_INSTANCE_NAME,
296 serviceInstance.getServiceInstanceName()));
298 return Optional.of(resultantRelationship);
301 LOGGER.error("Unable to find ServiceInstance ...");
302 return Optional.empty();
306 public void clearAll() {
307 clearCache(CUSTOMER_CACHE.getName());
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;
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;