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.BELONGS_TO;
24 import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION;
25 import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION_CLOUD_OWNER;
26 import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION_CLOUD_REGION_ID;
27 import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION_OWNER_DEFINED_TYPE;
28 import static org.onap.so.aaisimulator.utils.Constants.LOCATED_IN;
29 import static org.onap.so.aaisimulator.utils.Constants.TENANT;
30 import static org.onap.so.aaisimulator.utils.Constants.TENANT_TENANT_ID;
31 import static org.onap.so.aaisimulator.utils.Constants.TENANT_TENANT_NAME;
32 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getBiDirectionalRelationShipListRelatedLink;
33 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getRelationShipListRelatedLink;
34 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getTargetUrl;
35 import java.util.List;
36 import java.util.Optional;
37 import org.onap.aai.domain.yang.CloudRegion;
38 import org.onap.aai.domain.yang.EsrSystemInfo;
39 import org.onap.aai.domain.yang.EsrSystemInfoList;
40 import org.onap.aai.domain.yang.RelatedToProperty;
41 import org.onap.aai.domain.yang.Relationship;
42 import org.onap.aai.domain.yang.RelationshipData;
43 import org.onap.aai.domain.yang.RelationshipList;
44 import org.onap.aai.domain.yang.Tenant;
45 import org.onap.aai.domain.yang.Tenants;
46 import org.onap.aai.domain.yang.Vserver;
47 import org.onap.aai.domain.yang.Vservers;
48 import org.onap.so.aaisimulator.models.CloudRegionKey;
49 import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.cache.Cache;
54 import org.springframework.cache.CacheManager;
55 import org.springframework.http.HttpHeaders;
56 import org.springframework.stereotype.Service;
59 * @author Waqas Ikram (waqas.ikram@est.tech)
63 public class CloudRegionCacheServiceProviderImpl extends AbstractCacheServiceProvider
64 implements CloudRegionCacheServiceProvider {
66 private static final Logger LOGGER = LoggerFactory.getLogger(CloudRegionCacheServiceProviderImpl.class);
68 private final HttpRestServiceProvider httpRestServiceProvider;
71 public CloudRegionCacheServiceProviderImpl(final CacheManager cacheManager,
72 final HttpRestServiceProvider httpRestServiceProvider) {
74 this.httpRestServiceProvider = httpRestServiceProvider;
78 public void putCloudRegion(final CloudRegionKey cloudRegionKey, final CloudRegion cloudRegion) {
79 LOGGER.info("Adding CloudRegion to cache with key: {} ...", cloudRegionKey);
80 final Cache cache = getCache(CLOUD_REGION_CACHE.getName());
81 cache.put(cloudRegionKey, cloudRegion);
86 public Optional<CloudRegion> getCloudRegion(final CloudRegionKey cloudRegionKey) {
87 LOGGER.info("getting CloudRegion from cache using key: {}", cloudRegionKey);
88 final Cache cache = getCache(CLOUD_REGION_CACHE.getName());
89 final CloudRegion value = cache.get(cloudRegionKey, CloudRegion.class);
91 return Optional.of(value);
93 LOGGER.error("Unable to find CloudRegion in cache using key:{} ", cloudRegionKey);
94 return Optional.empty();
98 public Optional<Relationship> addRelationShip(final CloudRegionKey key, final Relationship relationship,
99 final String requestUri) {
100 final Optional<CloudRegion> optional = getCloudRegion(key);
101 if (optional.isPresent()) {
102 final CloudRegion cloudRegion = optional.get();
103 RelationshipList relationshipList = cloudRegion.getRelationshipList();
104 if (relationshipList == null) {
105 relationshipList = new RelationshipList();
106 cloudRegion.setRelationshipList(relationshipList);
108 relationshipList.getRelationship().add(relationship);
110 LOGGER.info("Successfully added relation to CloudRegion with key: {}", key);
113 final Relationship resultantRelationship = new Relationship();
114 resultantRelationship.setRelatedTo(CLOUD_REGION);
115 resultantRelationship.setRelationshipLabel(LOCATED_IN);
116 resultantRelationship.setRelatedLink(getBiDirectionalRelationShipListRelatedLink(requestUri));
118 final List<RelationshipData> relationshipDataList = resultantRelationship.getRelationshipData();
119 relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_OWNER, cloudRegion.getCloudOwner()));
120 relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_REGION_ID, cloudRegion.getCloudRegionId()));
122 final List<RelatedToProperty> relatedToPropertyList = resultantRelationship.getRelatedToProperty();
124 final RelatedToProperty relatedToProperty = new RelatedToProperty();
125 relatedToProperty.setPropertyKey(CLOUD_REGION_OWNER_DEFINED_TYPE);
126 relatedToProperty.setPropertyValue(cloudRegion.getOwnerDefinedType());
127 relatedToPropertyList.add(relatedToProperty);
129 return Optional.of(resultantRelationship);
132 LOGGER.error("Unable to find CloudRegion using key: {} ...", key);
133 return Optional.empty();
137 public boolean putTenant(final CloudRegionKey key, final String tenantId, final Tenant tenant) {
138 final Optional<CloudRegion> optional = getCloudRegion(key);
139 if (optional.isPresent()) {
140 final CloudRegion cloudRegion = optional.get();
141 Tenants tenants = cloudRegion.getTenants();
142 if (tenants == null) {
143 tenants = new Tenants();
144 cloudRegion.setTenants(tenants);
147 final Optional<Tenant> existingTenantOptional = tenants.getTenant().stream()
148 .filter(existing -> existing.getTenantId() != null && existing.getTenantId().equals(tenantId))
151 if (!existingTenantOptional.isPresent()) {
152 return tenants.getTenant().add(tenant);
155 LOGGER.warn("Tenant already exists ...");
158 LOGGER.error("Unable to add Tenant using key: {} ...", key);
163 public Optional<Tenant> getTenant(final CloudRegionKey key, final String tenantId) {
164 final Optional<CloudRegion> optional = getCloudRegion(key);
165 if (optional.isPresent()) {
166 final CloudRegion cloudRegion = optional.get();
167 final Tenants tenants = cloudRegion.getTenants();
168 if (tenants != null) {
169 return tenants.getTenant().stream().filter(existing -> existing.getTenantId().equals(tenantId))
174 LOGGER.error("Unable to find Tenant using key: {} and tenantId: {} ...", key, tenantId);
175 return Optional.empty();
179 public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
180 final String requestUriString, final CloudRegionKey key, final String tenantId,
181 final Relationship relationship) {
183 final Optional<Tenant> optional = getTenant(key, tenantId);
184 if (optional.isPresent()) {
185 final Tenant tenant = optional.get();
186 final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
188 final Relationship outGoingRelationShip = getRelationship(requestUriString, key, tenant);
189 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
190 outGoingRelationShip, targetUrl, Relationship.class);
192 if (optionalRelationship.isPresent()) {
193 final Relationship resultantRelationship = optionalRelationship.get();
194 RelationshipList relationshipList = tenant.getRelationshipList();
195 if (relationshipList == null) {
196 relationshipList = new RelationshipList();
197 tenant.setRelationshipList(relationshipList);
200 if (relationshipList.getRelationship().add(resultantRelationship)) {
201 LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
208 } catch (final Exception exception) {
209 LOGGER.error("Unable to add two-way relationship for CloudRegion: {} and tenant: {}", key, tenantId,
212 LOGGER.error("Unable to add relationship in cache for CloudRegion: {} and tenant: {}", key, tenantId);
217 public Optional<EsrSystemInfoList> getEsrSystemInfoList(final CloudRegionKey key) {
218 final Optional<CloudRegion> optional = getCloudRegion(key);
219 if (optional.isPresent()) {
220 final CloudRegion cloudRegion = optional.get();
221 final EsrSystemInfoList esrSystemInfoList = cloudRegion.getEsrSystemInfoList();
222 if (esrSystemInfoList != null) {
223 return Optional.of(esrSystemInfoList);
226 LOGGER.error("Unable to find EsrSystemInfoList in cache for CloudRegion: {} ", key);
228 return Optional.empty();
232 public boolean putEsrSystemInfo(final CloudRegionKey key, final String esrSystemInfoId,
233 final EsrSystemInfo esrSystemInfo) {
234 final Optional<CloudRegion> optional = getCloudRegion(key);
235 if (optional.isPresent()) {
236 final CloudRegion cloudRegion = optional.get();
237 final List<EsrSystemInfo> esrSystemInfoList = getEsrSystemInfoList(cloudRegion);
239 final Optional<EsrSystemInfo> existingEsrSystemInfo =
240 esrSystemInfoList.stream().filter(existing -> existing.getEsrSystemInfoId() != null
241 && existing.getEsrSystemInfoId().equals(esrSystemInfoId)).findFirst();
242 if (existingEsrSystemInfo.isPresent()) {
243 LOGGER.error("EsrSystemInfo already exists {}", existingEsrSystemInfo.get());
247 return esrSystemInfoList.add(esrSystemInfo);
254 public boolean putVserver(final CloudRegionKey key, final String tenantId, final String vServerId,
255 final Vserver vServer) {
256 final Optional<Tenant> optional = getTenant(key, tenantId);
257 if (optional.isPresent()) {
258 final Tenant tenant = optional.get();
259 Vservers vServers = tenant.getVservers();
260 if (vServers == null) {
261 vServers = new Vservers();
262 tenant.setVservers(vServers);
264 final List<Vserver> vServerList = vServers.getVserver();
266 final Optional<Vserver> existingVserver = vServerList.stream()
267 .filter(existing -> existing.getVserverId() != null && existing.getVserverId().equals(vServerId))
270 if (existingVserver.isPresent()) {
271 LOGGER.error("Vserver already exists {}", existingVserver.get());
274 return vServerList.add(vServer);
281 public Optional<Vserver> getVserver(final CloudRegionKey key, final String tenantId, final String vServerId) {
282 final Optional<Tenant> optional = getTenant(key, tenantId);
283 if (optional.isPresent()) {
284 final Tenant tenant = optional.get();
285 Vservers vServers = tenant.getVservers();
286 if (vServers != null) {
287 return vServers.getVserver().stream()
288 .filter(vServer -> vServer.getVserverId() != null && vServer.getVserverId().equals(vServerId))
292 LOGGER.error("Unable to find vServer in cache ... ");
293 return Optional.empty();
296 private List<EsrSystemInfo> getEsrSystemInfoList(final CloudRegion cloudRegion) {
297 EsrSystemInfoList esrSystemInfoList = cloudRegion.getEsrSystemInfoList();
298 if (esrSystemInfoList == null) {
299 esrSystemInfoList = new EsrSystemInfoList();
300 cloudRegion.setEsrSystemInfoList(esrSystemInfoList);
302 return esrSystemInfoList.getEsrSystemInfo();
305 private Relationship getRelationship(final String requestUriString, final CloudRegionKey cloudRegionKey,
306 final Tenant tenant) {
307 final Relationship relationShip = new Relationship();
308 relationShip.setRelatedTo(TENANT);
309 relationShip.setRelationshipLabel(BELONGS_TO);
310 relationShip.setRelatedLink(getRelationShipListRelatedLink(requestUriString));
313 final List<RelationshipData> relationshipDataList = relationShip.getRelationshipData();
314 relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_OWNER, cloudRegionKey.getCloudOwner()));
315 relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_REGION_ID, cloudRegionKey.getCloudRegionId()));
316 relationshipDataList.add(getRelationshipData(TENANT_TENANT_ID, tenant.getTenantId()));
319 final RelatedToProperty relatedToProperty = new RelatedToProperty();
320 relatedToProperty.setPropertyKey(TENANT_TENANT_NAME);
321 relatedToProperty.setPropertyValue(tenant.getTenantName());
322 relationShip.getRelatedToProperty().add(relatedToProperty);
326 private RelationshipData getRelationshipData(final String key, final String value) {
327 final RelationshipData relationshipData = new RelationshipData();
328 relationshipData.setRelationshipKey(key);
329 relationshipData.setRelationshipValue(value);
330 return relationshipData;
334 public void clearAll() {
335 clearCache(CLOUD_REGION_CACHE.getName());