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_ID;
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 static org.onap.so.aaisimulator.utils.HttpServiceUtils.getBiDirectionalRelationShipListRelatedLink;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.Optional;
35 import java.util.stream.Collectors;
36 import org.onap.aai.domain.yang.Customer;
37 import org.onap.aai.domain.yang.RelatedToProperty;
38 import org.onap.aai.domain.yang.Relationship;
39 import org.onap.aai.domain.yang.RelationshipData;
40 import org.onap.aai.domain.yang.RelationshipList;
41 import org.onap.aai.domain.yang.ServiceInstance;
42 import org.onap.aai.domain.yang.ServiceInstances;
43 import org.onap.aai.domain.yang.ServiceSubscription;
44 import org.onap.aai.domain.yang.ServiceSubscriptions;
45 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.cache.Cache;
50 import org.springframework.cache.CacheManager;
51 import org.springframework.stereotype.Service;
54 * @author waqas.ikram@ericsson.com
58 public class CustomerCacheServiceProviderImpl extends AbstractCacheServiceProvider
59 implements CustomerCacheServiceProvider {
60 private static final Logger LOGGER = LoggerFactory.getLogger(CustomerCacheServiceProviderImpl.class);
63 public CustomerCacheServiceProviderImpl(final CacheManager cacheManager) {
68 public Optional<Customer> getCustomer(final String globalCustomerId) {
69 LOGGER.info("getting customer from cache using key: {}", globalCustomerId);
70 final Cache cache = getCache(CUSTOMER_CACHE.getName());
71 final Customer value = cache.get(globalCustomerId, Customer.class);
73 return Optional.of(value);
75 return Optional.empty();
79 public void putCustomer(final String globalCustomerId, final Customer customer) {
80 LOGGER.info("Adding customer: {} with key: {} in cache ...", customer, globalCustomerId);
81 final Cache cache = getCache(CUSTOMER_CACHE.getName());
83 cache.put(globalCustomerId, customer);
87 public Optional<ServiceSubscription> getServiceSubscription(final String globalCustomerId,
88 final String serviceType) {
89 LOGGER.info("getting service subscription from cache for globalCustomerId: {} and serviceType: {}",
90 globalCustomerId, serviceType);
92 final Cache cache = getCache(CUSTOMER_CACHE.getName());
94 final Customer value = cache.get(globalCustomerId, Customer.class);
97 return Optional.ofNullable(value.getServiceSubscriptions().getServiceSubscription().stream()
98 .filter(s -> serviceType.equals(s.getServiceType())).findFirst().orElse(null));
100 return Optional.empty();
105 public Optional<ServiceInstances> getServiceInstances(final String globalCustomerId, final String serviceType,
106 final String serviceInstanceName) {
108 final Cache cache = getCache(CUSTOMER_CACHE.getName());
109 final Customer value = cache.get(globalCustomerId, Customer.class);
112 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
113 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
115 if (serviceSubscription.isPresent()) {
116 LOGGER.info("Found service subscription ...");
117 final ServiceInstances serviceInstances = serviceSubscription.get().getServiceInstances();
118 if (serviceInstances != null) {
119 final List<ServiceInstance> serviceInstancesList =
120 serviceInstances.getServiceInstance().stream()
121 .filter(serviceInstance -> serviceInstanceName
122 .equals(serviceInstance.getServiceInstanceName()))
123 .collect(Collectors.toList());
124 if (serviceInstancesList != null && !serviceInstancesList.isEmpty()) {
125 LOGGER.info("Found {} service instances ", serviceInstancesList.size());
126 final ServiceInstances result = new ServiceInstances();
127 result.getServiceInstance().addAll(serviceInstancesList);
128 return Optional.of(result);
134 return Optional.empty();
138 public Optional<ServiceInstance> getServiceInstance(final String globalCustomerId, final String serviceType,
139 final String serviceInstanceId) {
140 final Cache cache = getCache(CUSTOMER_CACHE.getName());
141 final Customer value = cache.get(globalCustomerId, Customer.class);
144 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
145 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
147 if (serviceSubscription.isPresent()) {
148 LOGGER.info("Found service subscription ...");
149 final ServiceInstances serviceInstances = serviceSubscription.get().getServiceInstances();
150 if (serviceInstances != null) {
151 return Optional.ofNullable(serviceInstances.getServiceInstance().stream()
152 .filter(serviceInstance -> serviceInstanceId.equals(serviceInstance.getServiceInstanceId()))
153 .findFirst().orElse(null));
159 "Unable to find ServiceInstance using globalCustomerId: {}, serviceType: {} and serviceInstanceId: {} ...",
160 globalCustomerId, serviceType, serviceInstanceId);
161 return Optional.empty();
165 public boolean putServiceInstance(final String globalCustomerId, final String serviceType,
166 final String serviceInstanceId, final ServiceInstance serviceInstance) {
167 LOGGER.info("Adding serviceInstance: {} in cache ...", serviceInstance, globalCustomerId);
169 final Cache cache = getCache(CUSTOMER_CACHE.getName());
170 final Customer value = cache.get(globalCustomerId, Customer.class);
173 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
174 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
176 if (serviceSubscription.isPresent()) {
177 final ServiceInstances serviceInstances = getServiceInstances(serviceSubscription);
180 if (!serviceInstances.getServiceInstance().stream()
181 .filter(existing -> serviceInstanceId.equals(existing.getServiceInstanceId())).findFirst()
183 return serviceInstances.getServiceInstance().add(serviceInstance);
185 LOGGER.error("Service {} already exists ....", serviceInstanceId);
188 LOGGER.error("Couldn't find service subscription with serviceType: {} in cache ", serviceType);
191 LOGGER.error("Couldn't find Customer with key: {} in cache ", globalCustomerId);
196 public boolean putServiceSubscription(final String globalCustomerId, final String serviceType,
197 final ServiceSubscription serviceSubscription) {
199 final Optional<Customer> customerOptional = getCustomer(globalCustomerId);
201 if (customerOptional.isPresent()) {
202 final Customer customer = customerOptional.get();
203 if (customer.getServiceSubscriptions() == null) {
204 final ServiceSubscriptions serviceSubscriptions = new ServiceSubscriptions();
205 customer.setServiceSubscriptions(serviceSubscriptions);
206 return serviceSubscriptions.getServiceSubscription().add(serviceSubscription);
209 final Optional<ServiceSubscription> serviceSubscriptionOptional = customer.getServiceSubscriptions()
210 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
212 if (!serviceSubscriptionOptional.isPresent()) {
213 return customer.getServiceSubscriptions().getServiceSubscription().add(serviceSubscription);
215 LOGGER.error("ServiceSubscription already exists {}", serviceSubscriptionOptional.get().getServiceType());
218 LOGGER.error("Unable to add ServiceSubscription to cache becuase customer does not exits ...");
223 public boolean patchServiceInstance(final String globalCustomerId, final String serviceType,
224 final String serviceInstanceId, final ServiceInstance serviceInstance) {
225 final Optional<ServiceInstance> instance = getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
226 if (instance.isPresent()) {
227 final ServiceInstance cachedServiceInstance = instance.get();
228 LOGGER.info("Changing OrchestrationStatus from {} to {} ", cachedServiceInstance.getOrchestrationStatus(),
229 serviceInstance.getOrchestrationStatus());
230 cachedServiceInstance.setOrchestrationStatus(serviceInstance.getOrchestrationStatus());
233 LOGGER.error("Unable to find ServiceInstance ...");
238 public boolean deleteSericeInstance(final String globalCustomerId, final String serviceType,
239 final String serviceInstanceId, final String resourceVersion) {
240 final Cache cache = getCache(CUSTOMER_CACHE.getName());
241 final Customer value = cache.get(globalCustomerId, Customer.class);
244 final Optional<ServiceSubscription> serviceSubscription = value.getServiceSubscriptions()
245 .getServiceSubscription().stream().filter(s -> serviceType.equals(s.getServiceType())).findFirst();
247 if (serviceSubscription.isPresent()) {
248 LOGGER.info("Found service subscription ...");
249 final ServiceInstances serviceInstances = serviceSubscription.get().getServiceInstances();
250 if (serviceInstances != null) {
252 serviceInstances.getServiceInstance().removeIf(serviceInstance -> {
253 final String existingServiceInstanceId = serviceInstance.getServiceInstanceId();
254 final String existingResourceVersion = serviceInstance.getResourceVersion();
255 if (existingServiceInstanceId != null && existingServiceInstanceId.equals(serviceInstanceId)
256 && existingResourceVersion != null && existingResourceVersion.equals(resourceVersion)) {
257 LOGGER.info("Removing ServiceInstance with serviceInstanceId: {} and resourceVersion: {}",
258 existingServiceInstanceId, existingResourceVersion);
273 private ServiceInstances getServiceInstances(final Optional<ServiceSubscription> optional) {
274 final ServiceSubscription serviceSubscription = optional.get();
275 final ServiceInstances serviceInstances = serviceSubscription.getServiceInstances();
276 if (serviceInstances == null) {
277 final ServiceInstances instances = new ServiceInstances();
278 serviceSubscription.setServiceInstances(instances);
281 return serviceInstances;
285 public List<String> getRelatedToVnfIds(final String globalCustomerId, final String serviceType,
286 final String serviceInstanceId, final String vnfName) {
287 final Optional<ServiceInstance> optional = getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
289 if (optional.isPresent()) {
290 LOGGER.info("Found service instance ...");
291 final ServiceInstance serviceInstance = optional.get();
292 final RelationshipList relationshipList = serviceInstance.getRelationshipList();
294 if (relationshipList != null) {
295 final List<Relationship> relationships = relationshipList.getRelationship().stream().filter(
296 relationShip -> relationShip.getRelatedToProperty().stream().filter(relatedToProperty -> {
297 final String propertyKey = relatedToProperty.getPropertyKey();
298 final String propertyValue = relatedToProperty.getPropertyValue();
299 return GENERIC_VNF_VNF_NAME.equals(propertyKey) && propertyValue != null
300 && propertyValue.equals(vnfName);
301 }).findFirst().isPresent()).collect(Collectors.toList());
302 LOGGER.info("Found relationships {} for vnf-name: {}", relationships, vnfName);
303 return getGenericVnfIdsIfPresent(relationships);
305 LOGGER.warn("Relationship list is nulll ...");
307 LOGGER.error("Unable to find generic-vnf relationships with property value: {}... ", vnfName);
308 return Collections.emptyList();
312 public List<String> getRelatedToVnfIds(final String globalCustomerId, final String serviceType,
313 final String serviceInstanceId) {
314 final Optional<ServiceInstance> optional = getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
316 if (optional.isPresent()) {
317 LOGGER.info("Found service instance ...");
318 final ServiceInstance serviceInstance = optional.get();
319 final RelationshipList relationshipList = serviceInstance.getRelationshipList();
321 if (relationshipList != null) {
322 final List<Relationship> relationships = relationshipList.getRelationship();
323 LOGGER.info("Relationships found {}", relationships);
324 return getGenericVnfIdsIfPresent(relationships);
326 LOGGER.warn("Relationship list is nulll ...");
328 LOGGER.error("Unable to find generic-vnf relationships ... ");
329 return Collections.emptyList();
332 private List<String> getGenericVnfIdsIfPresent(final List<Relationship> relationships) {
333 final List<String> vnfIdsFound = new ArrayList<>();
334 relationships.stream().forEach(relationship -> {
335 relationship.getRelationshipData().stream()
336 .filter(existing -> GENERIC_VNF_VNF_ID.equals(existing.getRelationshipKey())).findFirst()
337 .ifPresent(consume -> {
338 vnfIdsFound.add(consume.getRelationshipValue());
345 public Optional<Relationship> addRelationShip(final String globalCustomerId, final String serviceType,
346 final String serviceInstanceId, final Relationship relationship, final String requestUri) {
347 final Optional<ServiceInstance> optional = getServiceInstance(globalCustomerId, serviceType, serviceInstanceId);
348 if (optional.isPresent()) {
349 final ServiceInstance serviceInstance = optional.get();
350 RelationshipList relationshipList = serviceInstance.getRelationshipList();
351 if (relationshipList == null) {
352 relationshipList = new RelationshipList();
353 serviceInstance.setRelationshipList(relationshipList);
355 relationshipList.getRelationship().add(relationship);
357 LOGGER.info("Successfully added relation to ServiceInstance");
359 final Relationship resultantRelationship = new Relationship();
360 resultantRelationship.setRelatedTo(GENERIC_VNF);
361 resultantRelationship.setRelationshipLabel(relationship.getRelationshipLabel());
362 resultantRelationship.setRelatedLink(getBiDirectionalRelationShipListRelatedLink(requestUri));
364 final List<RelationshipData> relationshipDataList = resultantRelationship.getRelationshipData();
365 relationshipDataList.add(getRelationshipData(CUSTOMER_GLOBAL_CUSTOMER_ID, globalCustomerId));
366 relationshipDataList.add(getRelationshipData(SERVICE_SUBSCRIPTION_SERVICE_TYPE, serviceType));
367 relationshipDataList.add(getRelationshipData(SERVICE_INSTANCE_SERVICE_INSTANCE_ID, serviceInstanceId));
369 final List<RelatedToProperty> relatedToProperty = resultantRelationship.getRelatedToProperty();
370 relatedToProperty.add(getRelatedToProperty(SERVICE_INSTANCE_SERVICE_INSTANCE_NAME,
371 serviceInstance.getServiceInstanceName()));
373 return Optional.of(resultantRelationship);
376 LOGGER.error("Unable to find ServiceInstance ...");
377 return Optional.empty();
381 public void clearAll() {
382 clearCache(CUSTOMER_CACHE.getName());
385 private RelatedToProperty getRelatedToProperty(final String key, final String value) {
386 final RelatedToProperty relatedToProperty = new RelatedToProperty();
387 relatedToProperty.setPropertyKey(key);
388 relatedToProperty.setPropertyValue(value);
389 return relatedToProperty;
392 private RelationshipData getRelationshipData(final String key, final String value) {
393 final RelationshipData relationshipData = new RelationshipData();
394 relationshipData.setRelationshipKey(key);
395 relationshipData.setRelationshipValue(value);
396 return relationshipData;