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.aai.domain.yang.Tenant;
36 import org.onap.aai.domain.yang.Tenants;
37 import org.onap.so.aaisimulator.models.CloudRegionKey;
38 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.cache.Cache;
43 import org.springframework.cache.CacheManager;
44 import org.springframework.stereotype.Service;
47 * @author Waqas Ikram (waqas.ikram@est.tech)
51 public class CloudRegionCacheServiceProviderImpl extends AbstractCacheServiceProvider
52 implements CloudRegionCacheServiceProvider {
54 private static final Logger LOGGER = LoggerFactory.getLogger(CloudRegionCacheServiceProviderImpl.class);
58 public CloudRegionCacheServiceProviderImpl(final CacheManager cacheManager) {
63 public void putCloudRegion(final CloudRegionKey cloudRegionKey, final CloudRegion cloudRegion) {
64 LOGGER.info("Adding CloudRegion to cache with key: {} ...", cloudRegionKey);
65 final Cache cache = getCache(CLOUD_REGION_CACHE.getName());
66 cache.put(cloudRegionKey, cloudRegion);
71 public Optional<CloudRegion> getCloudRegion(final CloudRegionKey cloudRegionKey) {
72 LOGGER.info("getting CloudRegion from cache using key: {}", cloudRegionKey);
73 final Cache cache = getCache(CLOUD_REGION_CACHE.getName());
74 final CloudRegion value = cache.get(cloudRegionKey, CloudRegion.class);
76 return Optional.of(value);
78 LOGGER.error("Unable to find CloudRegion in cache using key:{} ", cloudRegionKey);
79 return Optional.empty();
83 public Optional<Relationship> addRelationShip(final CloudRegionKey key, final Relationship relationship,
84 final String requestUri) {
85 final Optional<CloudRegion> optional = getCloudRegion(key);
86 if (optional.isPresent()) {
87 final CloudRegion cloudRegion = optional.get();
88 RelationshipList relationshipList = cloudRegion.getRelationshipList();
89 if (relationshipList == null) {
90 relationshipList = new RelationshipList();
91 cloudRegion.setRelationshipList(relationshipList);
93 relationshipList.getRelationship().add(relationship);
95 LOGGER.info("Successfully added relation to CloudRegion with key: {}", key);
98 final Relationship resultantRelationship = new Relationship();
99 resultantRelationship.setRelatedTo(CLOUD_REGION);
100 resultantRelationship.setRelationshipLabel(LOCATED_IN);
101 resultantRelationship.setRelatedLink(requestUri);
103 final List<RelationshipData> relationshipDataList = resultantRelationship.getRelationshipData();
104 relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_OWNER, cloudRegion.getCloudOwner()));
105 relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_REGION_ID, cloudRegion.getCloudRegionId()));
107 final List<RelatedToProperty> relatedToPropertyList = resultantRelationship.getRelatedToProperty();
109 final RelatedToProperty relatedToProperty = new RelatedToProperty();
110 relatedToProperty.setPropertyKey(CLOUD_REGION_OWNER_DEFINED_TYPE);
111 relatedToProperty.setPropertyValue(cloudRegion.getOwnerDefinedType());
112 relatedToPropertyList.add(relatedToProperty);
114 return Optional.of(resultantRelationship);
117 LOGGER.error("Unable to find CloudRegion using key: {} ...", key);
118 return Optional.empty();
122 public boolean putTenant(final CloudRegionKey key, final Tenant tenant) {
123 final Optional<CloudRegion> optional = getCloudRegion(key);
124 if (optional.isPresent()) {
125 final CloudRegion cloudRegion = optional.get();
126 Tenants tenants = cloudRegion.getTenants();
127 if (tenants == null) {
128 tenants = new Tenants();
129 cloudRegion.setTenants(tenants);
132 final Optional<Tenant> existingTenantOptional = tenants.getTenant().stream()
133 .filter(existing -> existing.getTenantId().equals(tenant.getTenantId())).findFirst();
135 if (!existingTenantOptional.isPresent()) {
136 return tenants.getTenant().add(tenant);
138 LOGGER.warn("Tenant already exists ...");
141 LOGGER.error("Unable to add Tenant using key: {} ...", key);
146 public Optional<Tenant> getTenant(final CloudRegionKey key, final String tenantId) {
147 final Optional<CloudRegion> optional = getCloudRegion(key);
148 if (optional.isPresent()) {
149 final CloudRegion cloudRegion = optional.get();
150 final Tenants tenants = cloudRegion.getTenants();
151 if (tenants != null) {
152 return tenants.getTenant().stream().filter(existing -> existing.getTenantId().equals(tenantId))
157 LOGGER.error("Unable to find Tenant using key: {} and tenantId: {} ...", key, tenantId);
158 return Optional.empty();
161 private RelationshipData getRelationshipData(final String key, final String value) {
162 final RelationshipData relationshipData = new RelationshipData();
163 relationshipData.setRelationshipKey(key);
164 relationshipData.setRelationshipValue(value);
165 return relationshipData;
169 public void clearAll() {
170 clearCahce(CLOUD_REGION_CACHE.getName());