41422c409c327c97f3c44f2df10fd08489155e20
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aaisimulator / service / providers / CloudRegionCacheServiceProviderImpl.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.aaisimulator.service.providers;
21
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;
43
44 /**
45  * @author Waqas Ikram (waqas.ikram@est.tech)
46  *
47  */
48 @Service
49 public class CloudRegionCacheServiceProviderImpl extends AbstractCacheServiceProvider
50         implements CloudRegionCacheServiceProvider {
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(CloudRegionCacheServiceProviderImpl.class);
53
54
55     @Autowired
56     public CloudRegionCacheServiceProviderImpl(final CacheManager cacheManager) {
57         super(cacheManager);
58     }
59
60     @Override
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);
65
66     }
67
68     @Override
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);
73         if (value != null) {
74             return Optional.of(value);
75         }
76         LOGGER.error("Unable to find CloudRegion in cache using key:{} ", cloudRegionKey);
77         return Optional.empty();
78     }
79
80     @Override
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);
90             }
91             relationshipList.getRelationship().add(relationship);
92
93             LOGGER.info("Successfully added relation to CloudRegion with key: {}", key);
94
95
96             final Relationship resultantRelationship = new Relationship();
97             resultantRelationship.setRelatedTo(CLOUD_REGION);
98             resultantRelationship.setRelationshipLabel(LOCATED_IN);
99             resultantRelationship.setRelatedLink(requestUri);
100
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()));
104
105             final List<RelatedToProperty> relatedToPropertyList = resultantRelationship.getRelatedToProperty();
106
107             final RelatedToProperty relatedToProperty = new RelatedToProperty();
108             relatedToProperty.setPropertyKey(CLOUD_REGION_OWNER_DEFINED_TYPE);
109             relatedToProperty.setPropertyValue(cloudRegion.getOwnerDefinedType());
110             relatedToPropertyList.add(relatedToProperty);
111
112             return Optional.of(resultantRelationship);
113
114         }
115         LOGGER.error("Unable to find CloudRegion using key: {} ...", key);
116         return Optional.empty();
117     }
118
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;
124     }
125
126     @Override
127     public void clearAll() {
128         clearCahce(CLOUD_REGION_CACHE.getName());
129
130     }
131
132 }