aboutsummaryrefslogtreecommitdiffstats
path: root/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/CloudRegionCacheServiceProviderImpl.java
blob: f15b389c626b8e35cb6d9148f51a7cc7e34fa611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */
package org.onap.so.aaisimulator.service.providers;

import static org.onap.so.aaisimulator.utils.CacheName.CLOUD_REGION_CACHE;
import static org.onap.so.aaisimulator.utils.Constants.BELONGS_TO;
import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION;
import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION_CLOUD_OWNER;
import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION_CLOUD_REGION_ID;
import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION_OWNER_DEFINED_TYPE;
import static org.onap.so.aaisimulator.utils.Constants.HOSTED_ON;
import static org.onap.so.aaisimulator.utils.Constants.K8S_RESOURCE;
import static org.onap.so.aaisimulator.utils.Constants.K8S_RESOURCE_ID;
import static org.onap.so.aaisimulator.utils.Constants.K8S_RESOURCE_NAME;
import static org.onap.so.aaisimulator.utils.Constants.LOCATED_IN;
import static org.onap.so.aaisimulator.utils.Constants.TENANT;
import static org.onap.so.aaisimulator.utils.Constants.TENANT_TENANT_ID;
import static org.onap.so.aaisimulator.utils.Constants.TENANT_TENANT_NAME;
import static org.onap.so.aaisimulator.utils.Constants.USES;
import static org.onap.so.aaisimulator.utils.Constants.VSERVER;
import static org.onap.so.aaisimulator.utils.Constants.VSERVER_VSERVER_ID;
import static org.onap.so.aaisimulator.utils.Constants.VSERVER_VSERVER_NAME;
import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getBiDirectionalRelationShipListRelatedLink;
import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getRelationShipListRelatedLink;
import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getTargetUrl;
import java.util.List;
import java.util.Optional;
import org.onap.aai.domain.yang.CloudRegion;
import org.onap.aai.domain.yang.EsrSystemInfo;
import org.onap.aai.domain.yang.EsrSystemInfoList;
import org.onap.aai.domain.yang.K8SResource;
import org.onap.aai.domain.yang.K8SResources;
import org.onap.aai.domain.yang.RelatedToProperty;
import org.onap.aai.domain.yang.Relationship;
import org.onap.aai.domain.yang.RelationshipData;
import org.onap.aai.domain.yang.RelationshipList;
import org.onap.aai.domain.yang.Tenant;
import org.onap.aai.domain.yang.Tenants;
import org.onap.aai.domain.yang.Vserver;
import org.onap.aai.domain.yang.Vservers;
import org.onap.so.aaisimulator.models.CloudRegionKey;
import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

/**
 * @author Waqas Ikram (waqas.ikram@est.tech)
 *
 */
@Service
public class CloudRegionCacheServiceProviderImpl extends AbstractCacheServiceProvider
        implements CloudRegionCacheServiceProvider {

    private static final Logger LOGGER = LoggerFactory.getLogger(CloudRegionCacheServiceProviderImpl.class);

    private final HttpRestServiceProvider httpRestServiceProvider;

    @Autowired
    public CloudRegionCacheServiceProviderImpl(final CacheManager cacheManager,
            final HttpRestServiceProvider httpRestServiceProvider) {
        super(cacheManager);
        this.httpRestServiceProvider = httpRestServiceProvider;
    }

    @Override
    public void putCloudRegion(final CloudRegionKey cloudRegionKey, final CloudRegion cloudRegion) {
        LOGGER.info("Adding CloudRegion to cache with key: {} ...", cloudRegionKey);
        final Cache cache = getCache(CLOUD_REGION_CACHE.getName());
        cache.put(cloudRegionKey, cloudRegion);

    }

    @Override
    public Optional<CloudRegion> getCloudRegion(final CloudRegionKey cloudRegionKey) {
        LOGGER.info("getting CloudRegion from cache using key: {}", cloudRegionKey);
        final Cache cache = getCache(CLOUD_REGION_CACHE.getName());
        final CloudRegion value = cache.get(cloudRegionKey, CloudRegion.class);
        if (value != null) {
            return Optional.of(value);
        }
        LOGGER.error("Unable to find CloudRegion in cache using key:{} ", cloudRegionKey);
        return Optional.empty();
    }

    @Override
    public Optional<Relationship> addRelationShip(final CloudRegionKey key, final Relationship relationship,
            final String requestUri) {
        final Optional<CloudRegion> optional = getCloudRegion(key);
        if (optional.isPresent()) {
            final CloudRegion cloudRegion = optional.get();
            RelationshipList relationshipList = cloudRegion.getRelationshipList();
            if (relationshipList == null) {
                relationshipList = new RelationshipList();
                cloudRegion.setRelationshipList(relationshipList);
            }
            relationshipList.getRelationship().add(relationship);

            LOGGER.info("Successfully added relation to CloudRegion with key: {}", key);


            final Relationship resultantRelationship = new Relationship();
            resultantRelationship.setRelatedTo(CLOUD_REGION);
            resultantRelationship.setRelationshipLabel(LOCATED_IN);
            resultantRelationship.setRelatedLink(getBiDirectionalRelationShipListRelatedLink(requestUri));

            final List<RelationshipData> relationshipDataList = resultantRelationship.getRelationshipData();
            relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_OWNER, cloudRegion.getCloudOwner()));
            relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_REGION_ID, cloudRegion.getCloudRegionId()));

            final List<RelatedToProperty> relatedToPropertyList = resultantRelationship.getRelatedToProperty();

            final RelatedToProperty relatedToProperty = new RelatedToProperty();
            relatedToProperty.setPropertyKey(CLOUD_REGION_OWNER_DEFINED_TYPE);
            relatedToProperty.setPropertyValue(cloudRegion.getOwnerDefinedType());
            relatedToPropertyList.add(relatedToProperty);

            return Optional.of(resultantRelationship);

        }
        LOGGER.error("Unable to find CloudRegion using key: {} ...", key);
        return Optional.empty();
    }

    @Override
    public boolean putTenant(final CloudRegionKey key, final String tenantId, final Tenant tenant) {
        final Optional<CloudRegion> optional = getCloudRegion(key);
        if (optional.isPresent()) {
            final CloudRegion cloudRegion = optional.get();
            Tenants tenants = cloudRegion.getTenants();
            if (tenants == null) {
                tenants = new Tenants();
                cloudRegion.setTenants(tenants);
            }

            final Optional<Tenant> existingTenantOptional = tenants.getTenant().stream()
                    .filter(existing -> existing.getTenantId() != null && existing.getTenantId().equals(tenantId))
                    .findFirst();

            if (!existingTenantOptional.isPresent()) {
                return tenants.getTenant().add(tenant);
            }

            LOGGER.warn("Tenant already exists ...");
            return false;
        }
        LOGGER.error("Unable to add Tenant using key: {} ...", key);
        return false;
    }

    @Override
    public Optional<Tenant> getTenant(final CloudRegionKey key, final String tenantId) {
        final Optional<CloudRegion> optional = getCloudRegion(key);
        if (optional.isPresent()) {
            final CloudRegion cloudRegion = optional.get();
            final Tenants tenants = cloudRegion.getTenants();
            if (tenants != null) {
                return tenants.getTenant().stream().filter(existing -> existing.getTenantId().equals(tenantId))
                        .findFirst();
            }
        }

        LOGGER.error("Unable to find Tenant using key: {} and tenantId: {} ...", key, tenantId);
        return Optional.empty();
    }

    @Override
    public boolean addRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
            final String requestUriString, final CloudRegionKey key, final String tenantId,
            final Relationship relationship) {
        try {
            final Optional<Tenant> optional = getTenant(key, tenantId);
            if (optional.isPresent()) {
                final Tenant tenant = optional.get();
                final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());

                final Relationship outGoingRelationShip = getRelationship(requestUriString, key, tenant);
                final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
                        outGoingRelationShip, targetUrl, Relationship.class);

                if (optionalRelationship.isPresent()) {
                    final Relationship resultantRelationship = optionalRelationship.get();
                    RelationshipList relationshipList = tenant.getRelationshipList();
                    if (relationshipList == null) {
                        relationshipList = new RelationshipList();
                        tenant.setRelationshipList(relationshipList);
                    }

                    if (relationshipList.getRelationship().add(resultantRelationship)) {
                        LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
                        return true;
                    }
                }


            }
        } catch (final Exception exception) {
            LOGGER.error("Unable to add two-way relationship for CloudRegion: {} and tenant: {}", key, tenantId,
                    exception);
        }
        LOGGER.error("Unable to add relationship in cache for CloudRegion: {} and tenant: {}", key, tenantId);
        return false;
    }

    @Override
    public Optional<EsrSystemInfoList> getEsrSystemInfoList(final CloudRegionKey key) {
        final Optional<CloudRegion> optional = getCloudRegion(key);
        if (optional.isPresent()) {
            final CloudRegion cloudRegion = optional.get();
            final EsrSystemInfoList esrSystemInfoList = cloudRegion.getEsrSystemInfoList();
            if (esrSystemInfoList != null) {
                return Optional.of(esrSystemInfoList);
            }
        }
        LOGGER.error("Unable to find EsrSystemInfoList in cache for CloudRegion: {} ", key);

        return Optional.empty();
    }

    @Override
    public boolean putEsrSystemInfo(final CloudRegionKey key, final String esrSystemInfoId,
            final EsrSystemInfo esrSystemInfo) {
        final Optional<CloudRegion> optional = getCloudRegion(key);
        if (optional.isPresent()) {
            final CloudRegion cloudRegion = optional.get();
            final List<EsrSystemInfo> esrSystemInfoList = getEsrSystemInfoList(cloudRegion);

            final Optional<EsrSystemInfo> existingEsrSystemInfo =
                    esrSystemInfoList.stream().filter(existing -> existing.getEsrSystemInfoId() != null
                            && existing.getEsrSystemInfoId().equals(esrSystemInfoId)).findFirst();
            if (existingEsrSystemInfo.isPresent()) {
                LOGGER.error("EsrSystemInfo already exists {}", existingEsrSystemInfo.get());
                return false;
            }

            return esrSystemInfoList.add(esrSystemInfo);

        }
        return false;
    }

    @Override
    public boolean putVserver(final CloudRegionKey key, final String tenantId, final String vServerId,
            final Vserver vServer) {
        final Optional<Tenant> optional = getTenant(key, tenantId);
        if (optional.isPresent()) {
            final Tenant tenant = optional.get();
            Vservers vServers = tenant.getVservers();
            if (vServers == null) {
                vServers = new Vservers();
                tenant.setVservers(vServers);
            }
            final List<Vserver> vServerList = vServers.getVserver();

            final Optional<Vserver> existingVserver = vServerList.stream()
                    .filter(existing -> existing.getVserverId() != null && existing.getVserverId().equals(vServerId))
                    .findFirst();

            if (existingVserver.isPresent()) {
                LOGGER.error("Vserver already exists {}", existingVserver.get());
                return false;
            }
            return vServerList.add(vServer);

        }
        return false;
    }

    @Override
    public Optional<Vserver> getVserver(final CloudRegionKey key, final String tenantId, final String vServerId) {
        final Optional<Tenant> optional = getTenant(key, tenantId);
        if (optional.isPresent()) {
            final Tenant tenant = optional.get();
            final Vservers vServers = tenant.getVservers();
            if (vServers != null) {
                return vServers.getVserver().stream()
                        .filter(vServer -> vServer.getVserverId() != null && vServer.getVserverId().equals(vServerId))
                        .findFirst();
            }
        }
        LOGGER.error("Unable to find vServer in cache ... ");
        return Optional.empty();
    }

    @Override
    public boolean deleteVserver(final CloudRegionKey key, final String tenantId, final String vServerId,
            final String resourceVersion) {
        final Optional<Vserver> optional = getVserver(key, tenantId, vServerId);
        if (optional.isPresent()) {
            final Optional<Tenant> tenantOptional = getTenant(key, tenantId);
            if (tenantOptional.isPresent()) {
                final Tenant tenant = tenantOptional.get();
                final Vservers vServers = tenant.getVservers();
                if (vServers != null) {
                    return vServers.getVserver().removeIf(vServer -> {
                        if (vServer.getVserverId() != null && vServer.getVserverId().equals(vServerId)
                                && vServer.getResourceVersion() != null
                                && vServer.getResourceVersion().equals(resourceVersion)) {
                            LOGGER.info("Will remove Vserver from cache with vServerId: {} and resource-version: {} ",
                                    vServerId, vServer.getResourceVersion());
                            return true;
                        }
                        return false;
                    });
                }

            }

        }
        LOGGER.error(
                "Unable to find Vserver for using key: {}, tenant-id: {}, vserver-id: {} and resource-version: {} ...",
                key, tenantId, vServerId, resourceVersion);

        return false;
    }

    @Override
    public boolean putK8sResource(final CloudRegionKey key, final String tenantId, final String id,
            final K8SResource k8sResource) {
        final Optional<Tenant> optional = getTenant(key, tenantId);
        if (optional.isPresent()) {
            final Tenant tenant = optional.get();
            K8SResources k8sResources = tenant.getK8SResources();
            if (k8sResources == null) {
                k8sResources = new K8SResources();
                tenant.setK8SResources(k8sResources);
            }


            final Optional<K8SResource> existingK8sResource = k8sResources.getK8SResource().stream()
                    .filter(entry -> entry.getId() != null && entry.getId().equalsIgnoreCase(id)).findFirst();

            if (existingK8sResource.isEmpty()) {
                LOGGER.info("Adding k8sResources to cache...");
                return k8sResources.getK8SResource().add(k8sResource);
            }

            LOGGER.warn("K8SResource already exists existingK8sResource: {}...", existingK8sResource.get());
            return false;
        }

        LOGGER.error("Unable to add K8s Resource using key: {}, tenantId: {} and id: {}...", key, tenantId, id);
        return false;
    }

    @Override
    public Optional<K8SResource> getK8sResource(final CloudRegionKey key, final String tenantId, final String id) {
        final Optional<Tenant> optional = getTenant(key, tenantId);

        if (optional.isPresent()) {

            final Tenant tenant = optional.get();
            final K8SResources k8sResources = tenant.getK8SResources();
            if (k8sResources != null) {
                return k8sResources.getK8SResource().stream()
                        .filter(entry -> entry.getId() != null && entry.getId().equalsIgnoreCase(id)).findFirst();
            }

        }
        LOGGER.error("Unable to find K8sResource using key: {}, tenantId: {} and id: {}...", key, tenantId, id);
        return Optional.empty();
    }

    @Override
    public boolean addK8sResourceRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
            final String requestUriString, final CloudRegionKey key, final String tenantId, final String id,
            final Relationship relationship) {
        try {
            final Optional<K8SResource> optional = getK8sResource(key, tenantId, id);
            if (optional.isPresent()) {
                final K8SResource k8sResource = optional.get();
                final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
                final Relationship outGoingRelationShip =
                        getRelationship(key, tenantId, k8sResource, getRelationShipListRelatedLink(requestUriString));
                final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
                        outGoingRelationShip, targetUrl, Relationship.class);

                if (optionalRelationship.isPresent()) {
                    final Relationship resultantRelationship = optionalRelationship.get();

                    RelationshipList relationshipList = k8sResource.getRelationshipList();
                    if (relationshipList == null) {
                        relationshipList = new RelationshipList();
                        k8sResource.setRelationshipList(relationshipList);
                    }
                    if (relationshipList.getRelationship().add(resultantRelationship)) {
                        LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
                        return true;
                    }
                }

            }
        } catch (final Exception exception) {
            LOGGER.error("Unable to add two-way relationship for key: {}, tenantId: {} and id: {}", key, tenantId, id,
                    exception);
        }
        LOGGER.error("Unable to add K8sResource relationship for key: {}, tenantId: {} and id: {}...", key, tenantId,
                id);
        return false;
    }

    private Relationship getRelationship(final CloudRegionKey key, final String tenantId, final K8SResource k8sResource,
            final String relatedLink) {
        final Relationship relationShip = new Relationship();
        relationShip.setRelatedTo(K8S_RESOURCE);
        relationShip.setRelationshipLabel(USES);
        relationShip.setRelatedLink(relatedLink);

        final List<RelationshipData> relationshipDataList = relationShip.getRelationshipData();
        relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_OWNER, key.getCloudOwner()));
        relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_REGION_ID, key.getCloudRegionId()));
        relationshipDataList.add(getRelationshipData(TENANT_TENANT_ID, tenantId));
        relationshipDataList.add(getRelationshipData(K8S_RESOURCE_ID, k8sResource.getId()));

        final RelatedToProperty relatedToProperty = new RelatedToProperty();
        relatedToProperty.setPropertyKey(K8S_RESOURCE_NAME);
        relatedToProperty.setPropertyValue(k8sResource.getName());
        relationShip.getRelatedToProperty().add(relatedToProperty);

        return relationShip;
    }

    @Override
    public Optional<Relationship> addvServerRelationShip(final CloudRegionKey key, final String tenantId,
            final String vServerId, final Relationship relationship, final String requestUri) {
        final Optional<Vserver> optional = getVserver(key, tenantId, vServerId);
        if (optional.isPresent()) {
            final Vserver vServer = optional.get();
            RelationshipList relationshipList = vServer.getRelationshipList();
            if (relationshipList == null) {
                relationshipList = new RelationshipList();
                vServer.setRelationshipList(relationshipList);
            }
            relationshipList.getRelationship().add(relationship);
            LOGGER.info("Successfully added relation to Vserver with key: {}, tenantId: {} and vServerId: {}", key,
                    tenantId, vServerId);
            final String relatedLink = getBiDirectionalRelationShipListRelatedLink(requestUri);

            final Relationship resultantRelationship = getVserverRelationship(key, tenantId, vServer, relatedLink);

            return Optional.of(resultantRelationship);
        }

        LOGGER.error("Unable to find Vserver using key: {}, tenantId: {} and vServerId: {}...", key, tenantId,
                vServerId);
        return Optional.empty();
    }

    private Relationship getVserverRelationship(final CloudRegionKey key, final String tenantId, final Vserver vServer,
            final String relatedLink) {
        final Relationship resultantRelationship = new Relationship();
        resultantRelationship.setRelatedTo(VSERVER);
        resultantRelationship.setRelationshipLabel(HOSTED_ON);
        resultantRelationship.setRelatedLink(relatedLink);

        final List<RelationshipData> relationshipDataList = resultantRelationship.getRelationshipData();
        relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_OWNER, key.getCloudOwner()));
        relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_REGION_ID, key.getCloudRegionId()));
        relationshipDataList.add(getRelationshipData(TENANT_TENANT_ID, tenantId));
        relationshipDataList.add(getRelationshipData(VSERVER_VSERVER_ID, vServer.getVserverId()));

        final List<RelatedToProperty> relatedToPropertyList = resultantRelationship.getRelatedToProperty();

        final RelatedToProperty relatedToProperty = new RelatedToProperty();
        relatedToProperty.setPropertyKey(VSERVER_VSERVER_NAME);
        relatedToProperty.setPropertyValue(vServer.getVserverName());
        relatedToPropertyList.add(relatedToProperty);
        return resultantRelationship;
    }

    @Override
    public boolean addVServerRelationShip(final HttpHeaders incomingHeader, final String targetBaseUrl,
            final String requestUriString, final CloudRegionKey key, final String tenantId, final String vServerId,
            final Relationship relationship) {
        try {
            final Optional<Vserver> optional = getVserver(key, tenantId, vServerId);
            if (optional.isPresent()) {
                final Vserver vServer = optional.get();
                final String targetUrl = getTargetUrl(targetBaseUrl, relationship.getRelatedLink());
                final Relationship outGoingRelationShip = getVserverRelationship(key, tenantId, vServer,
                        getRelationShipListRelatedLink(requestUriString));
                final Optional<Relationship> optionalRelationship = httpRestServiceProvider.put(incomingHeader,
                        outGoingRelationShip, targetUrl, Relationship.class);
                if (optionalRelationship.isPresent()) {
                    final Relationship resultantRelationship = optionalRelationship.get();

                    RelationshipList relationshipList = vServer.getRelationshipList();
                    if (relationshipList == null) {
                        relationshipList = new RelationshipList();
                        vServer.setRelationshipList(relationshipList);
                    }

                    final Optional<Relationship> relationShipExists = relationshipList.getRelationship().stream()
                            .filter(relation -> relation.getRelatedTo().equals(resultantRelationship.getRelatedTo())
                                    && relation.getRelatedLink().equals(resultantRelationship.getRelatedLink()))
                            .findAny();

                    if (relationShipExists.isPresent()) {
                        LOGGER.info("relationship {} already exists in cache ", resultantRelationship);
                        return true;
                    }

                    LOGGER.info("added relationship {} in cache successfully", resultantRelationship);
                    return relationshipList.getRelationship().add(resultantRelationship);
                }

            }
        } catch (final Exception exception) {
            LOGGER.error("Unable to add two-way relationship for key: {}, tenantId: {} and vServerId: {}", key,
                    tenantId, vServerId, exception);
        }
        LOGGER.error("Unable to add Vserver relationship for key: {}, tenantId: {} and vServerId: {}...", key, tenantId,
                vServerId);
        return false;
    }

    private List<EsrSystemInfo> getEsrSystemInfoList(final CloudRegion cloudRegion) {
        EsrSystemInfoList esrSystemInfoList = cloudRegion.getEsrSystemInfoList();
        if (esrSystemInfoList == null) {
            esrSystemInfoList = new EsrSystemInfoList();
            cloudRegion.setEsrSystemInfoList(esrSystemInfoList);
        }
        return esrSystemInfoList.getEsrSystemInfo();
    }

    private Relationship getRelationship(final String requestUriString, final CloudRegionKey cloudRegionKey,
            final Tenant tenant) {
        final Relationship relationShip = new Relationship();
        relationShip.setRelatedTo(TENANT);
        relationShip.setRelationshipLabel(BELONGS_TO);
        relationShip.setRelatedLink(getRelationShipListRelatedLink(requestUriString));


        final List<RelationshipData> relationshipDataList = relationShip.getRelationshipData();
        relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_OWNER, cloudRegionKey.getCloudOwner()));
        relationshipDataList.add(getRelationshipData(CLOUD_REGION_CLOUD_REGION_ID, cloudRegionKey.getCloudRegionId()));
        relationshipDataList.add(getRelationshipData(TENANT_TENANT_ID, tenant.getTenantId()));


        final RelatedToProperty relatedToProperty = new RelatedToProperty();
        relatedToProperty.setPropertyKey(TENANT_TENANT_NAME);
        relatedToProperty.setPropertyValue(tenant.getTenantName());
        relationShip.getRelatedToProperty().add(relatedToProperty);
        return relationShip;
    }

    private RelationshipData getRelationshipData(final String key, final String value) {
        final RelationshipData relationshipData = new RelationshipData();
        relationshipData.setRelationshipKey(key);
        relationshipData.setRelationshipValue(value);
        return relationshipData;
    }

    @Override
    public void clearAll() {
        clearCache(CLOUD_REGION_CACHE.getName());

    }

    @Override
    public boolean deleteK8sResource(final CloudRegionKey key, final String tenantId, final String id,
                                     final String resourceVersion) {
        final Optional<Tenant> optional = getTenant(key, tenantId);
        if (optional.isPresent()) {
            final Tenant tenant = optional.get();
            K8SResources k8sResources = tenant.getK8SResources();
            if (k8sResources != null) {
                final Optional<K8SResource> existingK8sResource = k8sResources.getK8SResource().stream()
                    .filter(entry -> entry.getId() != null && entry.getId().equalsIgnoreCase(id)).findFirst();

                if (existingK8sResource.isPresent()
                    && existingK8sResource.get().getResourceVersion().equals(resourceVersion)) {
                    LOGGER.info("k8sResources found in cache and removing the same.");
                    return k8sResources.getK8SResource().remove(existingK8sResource.get());
                }
            }
            else {
                return true;
            }
        }

        LOGGER.error("Unable to add K8s Resource using key: {}, tenantId: {} and id: {}...", key, tenantId, id);
        return false;
    }
}