5024cc7907d38835037bfd21a6b857d9c4d4329b
[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.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;
57
58 /**
59  * @author Waqas Ikram (waqas.ikram@est.tech)
60  *
61  */
62 @Service
63 public class CloudRegionCacheServiceProviderImpl extends AbstractCacheServiceProvider
64         implements CloudRegionCacheServiceProvider {
65
66     private static final Logger LOGGER = LoggerFactory.getLogger(CloudRegionCacheServiceProviderImpl.class);
67
68     private final HttpRestServiceProvider httpRestServiceProvider;
69
70     @Autowired
71     public CloudRegionCacheServiceProviderImpl(final CacheManager cacheManager,
72             final HttpRestServiceProvider httpRestServiceProvider) {
73         super(cacheManager);
74         this.httpRestServiceProvider = httpRestServiceProvider;
75     }
76
77     @Override
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);
82
83     }
84
85     @Override
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);
90         if (value != null) {
91             return Optional.of(value);
92         }
93         LOGGER.error("Unable to find CloudRegion in cache using key:{} ", cloudRegionKey);
94         return Optional.empty();
95     }
96
97     @Override
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);
107             }
108             relationshipList.getRelationship().add(relationship);
109
110             LOGGER.info("Successfully added relation to CloudRegion with key: {}", key);
111
112
113             final Relationship resultantRelationship = new Relationship();
114             resultantRelationship.setRelatedTo(CLOUD_REGION);
115             resultantRelationship.setRelationshipLabel(LOCATED_IN);
116             resultantRelationship.setRelatedLink(getBiDirectionalRelationShipListRelatedLink(requestUri));
117
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()));
121
122             final List<RelatedToProperty> relatedToPropertyList = resultantRelationship.getRelatedToProperty();
123
124             final RelatedToProperty relatedToProperty = new RelatedToProperty();
125             relatedToProperty.setPropertyKey(CLOUD_REGION_OWNER_DEFINED_TYPE);
126             relatedToProperty.setPropertyValue(cloudRegion.getOwnerDefinedType());
127             relatedToPropertyList.add(relatedToProperty);
128
129             return Optional.of(resultantRelationship);
130
131         }
132         LOGGER.error("Unable to find CloudRegion using key: {} ...", key);
133         return Optional.empty();
134     }
135
136     @Override
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);
145             }
146
147             final Optional<Tenant> existingTenantOptional = tenants.getTenant().stream()
148                     .filter(existing -> existing.getTenantId() != null && existing.getTenantId().equals(tenantId))
149                     .findFirst();
150
151             if (!existingTenantOptional.isPresent()) {
152                 return tenants.getTenant().add(tenant);
153             }
154
155             LOGGER.warn("Tenant already exists ...");
156             return false;
157         }
158         LOGGER.error("Unable to add Tenant using key: {} ...", key);
159         return false;
160     }
161
162     @Override
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))
170                         .findFirst();
171             }
172         }
173
174         LOGGER.error("Unable to find Tenant using key: {} and tenantId: {} ...", key, tenantId);
175         return Optional.empty();
176     }
177
178     @Override
179     public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
180             final String requestUriString, final CloudRegionKey key, final String tenantId,
181             final Relationship relationship) {
182         try {
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());
187
188                 final Relationship outGoingRelationShip = getRelationship(requestUriString, key, tenant);
189                 final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
190                         outGoingRelationShip, targetUrl, Relationship.class);
191
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);
198                     }
199
200                     if (relationshipList.getRelationship().add(resultantRelationship)) {
201                         LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
202                         return true;
203                     }
204                 }
205
206
207             }
208         } catch (final Exception exception) {
209             LOGGER.error("Unable to add two-way relationship for CloudRegion: {} and tenant: {}", key, tenantId,
210                     exception);
211         }
212         LOGGER.error("Unable to add relationship in cache for CloudRegion: {} and tenant: {}", key, tenantId);
213         return false;
214     }
215
216     @Override
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);
224             }
225         }
226         LOGGER.error("Unable to find EsrSystemInfoList in cache for CloudRegion: {} ", key);
227
228         return Optional.empty();
229     }
230
231     @Override
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);
238
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());
244                 return false;
245             }
246
247             return esrSystemInfoList.add(esrSystemInfo);
248
249         }
250         return false;
251     }
252
253     @Override
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);
263             }
264             final List<Vserver> vServerList = vServers.getVserver();
265
266             final Optional<Vserver> existingVserver = vServerList.stream()
267                     .filter(existing -> existing.getVserverId() != null && existing.getVserverId().equals(vServerId))
268                     .findFirst();
269
270             if (existingVserver.isPresent()) {
271                 LOGGER.error("Vserver already exists {}", existingVserver.get());
272                 return false;
273             }
274             return vServerList.add(vServer);
275
276         }
277         return false;
278     }
279
280     @Override
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))
289                         .findFirst();
290             }
291         }
292         LOGGER.error("Unable to find vServer in cache ... ");
293         return Optional.empty();
294     }
295
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);
301         }
302         return esrSystemInfoList.getEsrSystemInfo();
303     }
304
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));
311
312
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()));
317
318
319         final RelatedToProperty relatedToProperty = new RelatedToProperty();
320         relatedToProperty.setPropertyKey(TENANT_TENANT_NAME);
321         relatedToProperty.setPropertyValue(tenant.getTenantName());
322         relationShip.getRelatedToProperty().add(relatedToProperty);
323         return relationShip;
324     }
325
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;
331     }
332
333     @Override
334     public void clearAll() {
335         clearCache(CLOUD_REGION_CACHE.getName());
336
337     }
338
339 }