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));
156 "Unable to find ServiceInstance using globalCustomerId: {}, serviceType: {} and serviceInstanceId: {} ...",
157 globalCustomerId, serviceType, serviceInstanceId);
158 return Optional.empty();
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);
166 final Cache cache = getCache(CUSTOMER_CACHE.getName());
167 final Customer value = cache.get(globalCustomerId, Customer.class);
170 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
171 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
173 if (serviceSubscription.isPresent()) {
174 final ServiceInstances serviceInstances = getServiceInstances(serviceSubscription);
177 if (!serviceInstances.getServiceInstance().stream()
178 .filter(existing -> serviceInstanceId.equals(existing.getServiceInstanceId())).findFirst()
180 return serviceInstances.getServiceInstance().add(serviceInstance);
182 LOGGER.error("Service {} already exists ....", serviceInstanceId);
185 LOGGER.error("Couldn't find service subscription with serviceType: {} in cache ", serviceType);
188 LOGGER.error("Couldn't find Customer with key: {} in cache ", globalCustomerId);
193 public boolean putServiceSubscription(final String globalCustomerId, final String serviceType,
194 final ServiceSubscription serviceSubscription) {
196 final Optional<Customer> customerOptional = getCustomer(globalCustomerId);
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);
206 final Optional<ServiceSubscription> serviceSubscriptionOptional = customer.getServiceSubscriptions()
207 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
209 if (!serviceSubscriptionOptional.isPresent()) {
210 return customer.getServiceSubscriptions().getServiceSubscription().add(serviceSubscription);
212 LOGGER.error("ServiceSubscription already exists {}", serviceSubscriptionOptional.get().getServiceType());
215 LOGGER.error("Unable to add ServiceSubscription to cache becuase customer does not exits ...");
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());
230 LOGGER.error("Unable to find ServiceInstance ...");
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);
241 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
242 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
244 if (serviceSubscription.isPresent()) {
245 LOGGER.info("Found service subscription ...");
246 final ServiceInstances serviceInstances = serviceSubscription.get().getServiceInstances();
247 if (serviceInstances != null) {
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);
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);
278 return serviceInstances;
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);
286 if (optional.isPresent()) {
287 LOGGER.info("Found service instance ...");
288 final ServiceInstance serviceInstance = optional.get();
289 final RelationshipList relationshipList = serviceInstance.getRelationshipList();
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();
301 LOGGER.warn("Relationship list is nulll ...");
303 LOGGER.error("Unable to RelationShip with property value: {}... ", vnfName);
305 return Optional.empty();
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);
319 relationshipList.getRelationship().add(relationship);
321 LOGGER.info("Successfully added relation to ServiceInstance");
323 final Relationship resultantRelationship = new Relationship();
324 resultantRelationship.setRelatedTo(GENERIC_VNF);
325 resultantRelationship.setRelationshipLabel(relationship.getRelationshipLabel());
326 resultantRelationship.setRelatedLink(getBiDirectionalRelationShipListRelatedLink(requestUri));
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));
333 final List<RelatedToProperty> relatedToProperty = resultantRelationship.getRelatedToProperty();
334 relatedToProperty.add(getRelatedToProperty(SERVICE_INSTANCE_SERVICE_INSTANCE_NAME,
335 serviceInstance.getServiceInstanceName()));
337 return Optional.of(resultantRelationship);
340 LOGGER.error("Unable to find ServiceInstance ...");
341 return Optional.empty();
345 public void clearAll() {
346 clearCache(CUSTOMER_CACHE.getName());
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;
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;