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.CLOUD_REGION_CACHE;
23 import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION;
24 import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION_CLOUD_OWNER;
25 import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION_CLOUD_REGION_ID;
26 import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION_OWNER_DEFINED_TYPE;
27 import static org.onap.so.aaisimulator.utils.Constants.LOCATED_IN;
28 import java.util.List;
29 import java.util.Optional;
30 import org.onap.aai.domain.yang.CloudRegion;
31 import org.onap.aai.domain.yang.RelatedToProperty;
32 import org.onap.aai.domain.yang.Relationship;
33 import org.onap.aai.domain.yang.RelationshipData;
34 import org.onap.aai.domain.yang.RelationshipList;
35 import org.onap.so.aaisimulator.models.CloudRegionKey;
36 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.cache.Cache;
41 import org.springframework.cache.CacheManager;
42 import org.springframework.stereotype.Service;
45 * @author Waqas Ikram (waqas.ikram@est.tech)
49 public class CloudRegionCacheServiceProviderImpl extends AbstractCacheServiceProvider
50 implements CloudRegionCacheServiceProvider {
52 private static final Logger LOGGER = LoggerFactory.getLogger(CloudRegionCacheServiceProviderImpl.class);
56 public CloudRegionCacheServiceProviderImpl(final CacheManager cacheManager) {
61 public void putCloudRegion(final CloudRegionKey cloudRegionKey, final CloudRegion cloudRegion) {
62 LOGGER.info("Adding CloudRegion to cache with key: {} ...", cloudRegionKey);
63 final Cache cache = getCache(CLOUD_REGION_CACHE.getName());
64 cache.put(cloudRegionKey, cloudRegion);
69 public Optional<CloudRegion> getCloudRegion(final CloudRegionKey cloudRegionKey) {
70 LOGGER.info("getting CloudRegion from cache using key: {}", cloudRegionKey);
71 final Cache cache = getCache(CLOUD_REGION_CACHE.getName());
72 final CloudRegion value = cache.get(cloudRegionKey, CloudRegion.class);
74 return Optional.of(value);
76 LOGGER.error("Unable to find CloudRegion in cache using key:{} ", cloudRegionKey);
77 return Optional.empty();
81 public Optional<Relationship> addRelationShip(final CloudRegionKey key, final Relationship relationship,
82 final String requestUri) {
83 final Optional<CloudRegion> optional = getCloudRegion(key);
84 if (optional.isPresent()) {
85 final CloudRegion cloudRegion = optional.get();
86 RelationshipList relationshipList = cloudRegion.getRelationshipList();
87 if (relationshipList == null) {
88 relationshipList = new RelationshipList();
89 cloudRegion.setRelationshipList(relationshipList);
91 relationshipList.getRelationship().add(relationship);
93 LOGGER.info("Successfully added relation to CloudRegion with key: {}", key);
96 final Relationship resultantRelationship = new Relationship();
97 resultantRelationship.setRelatedTo(CLOUD_REGION);
98 resultantRelationship.setRelationshipLabel(LOCATED_IN);
99 resultantRelationship.setRelatedLink(requestUri);
101 final List<RelationshipData> relationshipDataList = resultantRelationship.getRelationshipData();
102 relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_OWNER, cloudRegion.getCloudOwner()));
103 relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_REGION_ID, cloudRegion.getCloudRegionId()));
105 final List<RelatedToProperty> relatedToPropertyList = resultantRelationship.getRelatedToProperty();
107 final RelatedToProperty relatedToProperty = new RelatedToProperty();
108 relatedToProperty.setPropertyKey(CLOUD_REGION_OWNER_DEFINED_TYPE);
109 relatedToProperty.setPropertyValue(cloudRegion.getOwnerDefinedType());
110 relatedToPropertyList.add(relatedToProperty);
112 return Optional.of(resultantRelationship);
115 LOGGER.error("Unable to find CloudRegion using key: {} ...", key);
116 return Optional.empty();
119 private RelationshipData getRelationshipData(final String key, final String value) {
120 final RelationshipData relationshipData = new RelationshipData();
121 relationshipData.setRelationshipKey(key);
122 relationshipData.setRelationshipValue(value);
123 return relationshipData;
127 public void clearAll() {
128 clearCahce(CLOUD_REGION_CACHE.getName());