Create based CSIT test for SO-CNFM - Simulator Changes.
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aaisimulator / controller / CloudRegionsController.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.controller;
21
22 import static org.onap.so.aaisimulator.utils.Constants.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL;
23 import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION;
24 import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGIONS;
25 import static org.onap.so.aaisimulator.utils.Constants.ESR_SYSTEM_INFO_LIST;
26 import static org.onap.so.aaisimulator.utils.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL;
27 import static org.onap.so.aaisimulator.utils.Constants.VSERVER;
28 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getHeaders;
29 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
30 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
31 import java.util.Optional;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.core.MediaType;
34 import org.onap.aai.domain.yang.CloudRegion;
35 import org.onap.aai.domain.yang.EsrSystemInfo;
36 import org.onap.aai.domain.yang.EsrSystemInfoList;
37 import org.onap.aai.domain.yang.K8SResource;
38 import org.onap.aai.domain.yang.Relationship;
39 import org.onap.aai.domain.yang.Tenant;
40 import org.onap.aai.domain.yang.Vserver;
41 import org.onap.so.aaisimulator.models.CloudRegionKey;
42 import org.onap.so.aaisimulator.service.providers.CloudRegionCacheServiceProvider;
43 import org.onap.so.aaisimulator.utils.HttpServiceUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.http.HttpHeaders;
48 import org.springframework.http.HttpStatus;
49 import org.springframework.http.ResponseEntity;
50 import org.springframework.stereotype.Controller;
51 import org.springframework.web.bind.annotation.DeleteMapping;
52 import org.springframework.web.bind.annotation.GetMapping;
53 import org.springframework.web.bind.annotation.PathVariable;
54 import org.springframework.web.bind.annotation.PutMapping;
55 import org.springframework.web.bind.annotation.RequestBody;
56 import org.springframework.web.bind.annotation.RequestMapping;
57 import org.springframework.web.bind.annotation.RequestParam;
58
59 /**
60  * @author Waqas Ikram (waqas.ikram@est.tech)
61  *
62  */
63 @Controller
64 @RequestMapping(path = CLOUD_REGIONS)
65 public class CloudRegionsController {
66     private static final Logger LOGGER = LoggerFactory.getLogger(CloudRegionsController.class);
67
68     private final CloudRegionCacheServiceProvider cacheServiceProvider;
69
70     @Autowired
71     public CloudRegionsController(final CloudRegionCacheServiceProvider cacheServiceProvider) {
72         this.cacheServiceProvider = cacheServiceProvider;
73     }
74
75     @PutMapping(value = "{cloud-owner}/{cloud-region-id}",
76             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
77             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
78     public ResponseEntity<?> putCloudRegion(@RequestBody final CloudRegion cloudRegion,
79             @PathVariable("cloud-owner") final String cloudOwner,
80             @PathVariable("cloud-region-id") final String cloudRegionId, final HttpServletRequest request) {
81
82         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
83
84         if (key.isValid()) {
85             LOGGER.info("Will add CloudRegion to cache with key 'key': {} ....", key);
86             if (cloudRegion.getResourceVersion() == null || cloudRegion.getResourceVersion().isEmpty()) {
87                 cloudRegion.setResourceVersion(getResourceVersion());
88             }
89             cacheServiceProvider.putCloudRegion(key, cloudRegion);
90             return ResponseEntity.accepted().build();
91         }
92
93         LOGGER.error("Unable to add CloudRegion in cache because of invalid key {}", key);
94         return getRequestErrorResponseEntity(request, CLOUD_REGION);
95     }
96
97     @GetMapping(value = "{cloud-owner}/{cloud-region-id}",
98             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
99     public ResponseEntity<?> getCloudRegion(@PathVariable("cloud-owner") final String cloudOwner,
100             @PathVariable("cloud-region-id") final String cloudRegionId,
101             @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) {
102         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
103         LOGGER.info("Retrieving CloudRegion using key : {} with depth: {}...", key, depth);
104         if (key.isValid()) {
105             final Optional<CloudRegion> optional = cacheServiceProvider.getCloudRegion(key);
106             if (optional.isPresent()) {
107                 final CloudRegion cloudRegion = optional.get();
108                 LOGGER.info("found CloudRegion {} in cache", cloudRegion);
109                 return ResponseEntity.ok(cloudRegion);
110             }
111         }
112         LOGGER.error("Unable to find CloudRegion in cache using {}", key);
113         return getRequestErrorResponseEntity(request, CLOUD_REGION);
114     }
115
116     @PutMapping(value = "{cloud-owner}/{cloud-region-id}" + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL,
117             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
118             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
119     public ResponseEntity<?> putRelationShip(@PathVariable("cloud-owner") final String cloudOwner,
120             @PathVariable("cloud-region-id") final String cloudRegionId, @RequestBody final Relationship relationship,
121             final HttpServletRequest request) {
122         LOGGER.info("Will add {} bi-directional relationship to : {} ...", relationship.getRelatedTo());
123
124         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
125
126         final Optional<Relationship> optional =
127                 cacheServiceProvider.addRelationShip(key, relationship, request.getRequestURI());
128
129         if (optional.isPresent()) {
130             final Relationship resultantRelationship = optional.get();
131             LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
132             return ResponseEntity.accepted().body(resultantRelationship);
133         }
134
135         LOGGER.error("Couldn't add {} relationship for 'key': {} ...", relationship.getRelatedTo(), key);
136         return getRequestErrorResponseEntity(request, VSERVER);
137
138     }
139
140     @PutMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}",
141             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
142             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
143     public ResponseEntity<?> putTenant(@RequestBody final Tenant tenant,
144             @PathVariable("cloud-owner") final String cloudOwner,
145             @PathVariable("cloud-region-id") final String cloudRegionId,
146             @PathVariable("tenant-id") final String tenantId, final HttpServletRequest request) {
147
148         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
149
150         if (key.isValid()) {
151             LOGGER.info("Will add Tenant to cache with key 'key': {} ....", key);
152             if (tenant.getResourceVersion() == null || tenant.getResourceVersion().isEmpty()) {
153                 tenant.setResourceVersion(getResourceVersion());
154             }
155             if (cacheServiceProvider.putTenant(key, tenantId, tenant)) {
156                 return ResponseEntity.accepted().build();
157             }
158         }
159
160         LOGGER.error("Unable to add Tenant in cache using key {}", key);
161         return getRequestErrorResponseEntity(request, CLOUD_REGION);
162     }
163
164     @GetMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}",
165             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
166     public ResponseEntity<?> getTenant(@PathVariable("cloud-owner") final String cloudOwner,
167             @PathVariable("cloud-region-id") final String cloudRegionId,
168             @PathVariable("tenant-id") final String tenantId, final HttpServletRequest request) {
169         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
170         LOGGER.info("Retrieving Tenant using key : {} and tenant-id:{} ...", key, tenantId);
171         if (key.isValid()) {
172             final Optional<Tenant> optional = cacheServiceProvider.getTenant(key, tenantId);
173             if (optional.isPresent()) {
174                 final Tenant tenant = optional.get();
175                 LOGGER.info("found Tenant {} in cache", tenant);
176                 return ResponseEntity.ok(tenant);
177             }
178         }
179         LOGGER.error("Unable to find Tenant in cache key : {} and tenant-id:{} ...", key, tenantId);
180         return getRequestErrorResponseEntity(request, CLOUD_REGION);
181     }
182
183     @PutMapping(
184             value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}" + RELATIONSHIP_LIST_RELATIONSHIP_URL,
185             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
186             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
187     public ResponseEntity<?> putRelationShip(@RequestBody final Relationship relationship,
188             @PathVariable("cloud-owner") final String cloudOwner,
189             @PathVariable("cloud-region-id") final String cloudRegionId,
190             @PathVariable("tenant-id") final String tenantId, final HttpServletRequest request) {
191
192         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
193         LOGGER.info("Will add {} relationship for key : {} and tenant-id:{} ...", relationship.getRelatedLink(), key,
194                 tenantId);
195
196         if (relationship.getRelatedLink() != null) {
197
198             final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString();
199             final HttpHeaders incomingHeader = getHeaders(request);
200             final boolean result = cacheServiceProvider.addRelationShip(incomingHeader, targetBaseUrl,
201                     request.getRequestURI(), key, tenantId, relationship);
202             if (result) {
203                 LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink());
204                 return ResponseEntity.accepted().build();
205             }
206
207         }
208         LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
209         return getRequestErrorResponseEntity(request, CLOUD_REGION);
210     }
211
212     @PutMapping(value = "{cloud-owner}/{cloud-region-id}/esr-system-info-list/esr-system-info/{esr-system-info-id}",
213             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
214             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
215     public ResponseEntity<?> putEsrSystemInfo(@RequestBody final EsrSystemInfo esrSystemInfo,
216             @PathVariable("esr-system-info-id") final String esrSystemInfoId,
217             @PathVariable("cloud-owner") final String cloudOwner,
218             @PathVariable("cloud-region-id") final String cloudRegionId, final HttpServletRequest request) {
219
220         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
221
222         LOGGER.info("Will put esrSystemInfo for 'key': {} ...", key);
223
224         if (esrSystemInfo.getResourceVersion() == null || esrSystemInfo.getResourceVersion().isEmpty()) {
225             esrSystemInfo.setResourceVersion(getResourceVersion());
226
227         }
228
229         if (cacheServiceProvider.putEsrSystemInfo(key, esrSystemInfoId, esrSystemInfo)) {
230             LOGGER.info("Successfully added EsrSystemInfo key : {}  ...", key, esrSystemInfo);
231             return ResponseEntity.accepted().build();
232         }
233         LOGGER.error("Unable to add EsrSystemInfo in cache for key : {} ...", key);
234
235         return getRequestErrorResponseEntity(request, ESR_SYSTEM_INFO_LIST);
236     }
237
238     @GetMapping(value = "{cloud-owner}/{cloud-region-id}/esr-system-info-list",
239             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
240     public ResponseEntity<?> getEsrSystemInfoList(@PathVariable("cloud-owner") final String cloudOwner,
241             @PathVariable("cloud-region-id") final String cloudRegionId, final HttpServletRequest request) {
242         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
243         LOGGER.info("Retrieving EsrSystemInfoList using key : {} ...", key);
244         if (key.isValid()) {
245             final Optional<EsrSystemInfoList> optional = cacheServiceProvider.getEsrSystemInfoList(key);
246             if (optional.isPresent()) {
247                 final EsrSystemInfoList esrSystemInfoList = optional.get();
248                 LOGGER.info("found EsrSystemInfoList {} in cache", esrSystemInfoList);
249                 return ResponseEntity.ok(esrSystemInfoList);
250             }
251         }
252         LOGGER.error("Unable to find EsrSystemInfoList in cache using key : {} ...", key);
253         return getRequestErrorResponseEntity(request, CLOUD_REGION);
254     }
255
256     @PutMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}",
257             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
258             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
259     public ResponseEntity<?> putVserver(@RequestBody final Vserver vServer,
260             @PathVariable("cloud-owner") final String cloudOwner,
261             @PathVariable("cloud-region-id") final String cloudRegionId,
262             @PathVariable("tenant-id") final String tenantId, @PathVariable("vserver-id") final String vServerId,
263             final HttpServletRequest request) {
264
265         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
266         if (vServer.getResourceVersion() == null || vServer.getResourceVersion().isEmpty()) {
267             vServer.setResourceVersion(getResourceVersion());
268         }
269         LOGGER.info("Will put Vserver in cache using using key: {}, tenantId: {}, vServerId: {} ...", key, tenantId,
270                 vServerId);
271
272         if (cacheServiceProvider.putVserver(key, tenantId, vServerId, vServer)) {
273
274             if (vServer.getRelationshipList() != null) {
275                 for (final Relationship relationship : vServer.getRelationshipList().getRelationship()) {
276                     if (relationship.getRelatedLink() != null) {
277                         final String requestUri = request.getRequestURI();
278                         final String targetBaseUrl =
279                                 HttpServiceUtils.getBaseUrl(request.getRequestURL(), requestUri).toString();
280                         final HttpHeaders incomingHeader = getHeaders(request);
281                         final boolean result = cacheServiceProvider.addVServerRelationShip(incomingHeader,
282                                 targetBaseUrl, requestUri, key, tenantId, vServerId, relationship);
283                         if (!result) {
284                             LOGGER.error(
285                                     "Unable to add Vserver relationship in cache using key: {}, tenantId: {}, vServerId: {}",
286                                     key, tenantId, vServerId);
287                             return getRequestErrorResponseEntity(request, CLOUD_REGION);
288                         }
289                         LOGGER.info("Successfully added relationship with {}", relationship.getRelatedLink());
290                     }
291                 }
292             }
293
294             LOGGER.info("Successfully added Vserver for key: {}, tenantId: {}, vServerId: {} ...", key, tenantId,
295                     vServerId);
296             return ResponseEntity.accepted().build();
297         }
298         LOGGER.error("Unable to add Vserver in cache using key: {}, tenantId: {}, vServerId: {}", key, tenantId,
299                 vServerId);
300         return getRequestErrorResponseEntity(request, CLOUD_REGION);
301     }
302
303     @GetMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}",
304             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
305     public ResponseEntity<?> getVserver(@PathVariable("cloud-owner") final String cloudOwner,
306             @PathVariable("cloud-region-id") final String cloudRegionId,
307             @PathVariable("tenant-id") final String tenantId, @PathVariable("vserver-id") final String vServerId,
308             final HttpServletRequest request) {
309
310         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
311         LOGGER.info("Retrieving Vserver using key: {}, tenant-id: {}  and vserver-id: {}...", key, tenantId, vServerId);
312         final Optional<Vserver> optional = cacheServiceProvider.getVserver(key, tenantId, vServerId);
313         if (optional.isPresent()) {
314             final Vserver vServer = optional.get();
315             LOGGER.info("found Vserver {} in cache", vServer);
316             return ResponseEntity.ok(vServer);
317         }
318         LOGGER.error("Unable to find Vserver in cache using key: {}, tenant-id: {}  and vserver-id: {}...", key,
319                 tenantId, vServerId);
320         return getRequestErrorResponseEntity(request, CLOUD_REGION);
321     }
322
323
324     @DeleteMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}",
325             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
326     public ResponseEntity<?> deleteVserver(@PathVariable("cloud-owner") final String cloudOwner,
327             @PathVariable("cloud-region-id") final String cloudRegionId,
328             @PathVariable("tenant-id") final String tenantId, @PathVariable("vserver-id") final String vServerId,
329             @RequestParam(name = "resource-version") final String resourceVersion, final HttpServletRequest request) {
330
331         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
332         LOGGER.info("Will delete Vserver using key: {}, tenant-id: {}, vserver-id: {} and resource-version: {}...", key,
333                 tenantId, vServerId, resourceVersion);
334
335
336         if (cacheServiceProvider.deleteVserver(key, tenantId, vServerId, resourceVersion)) {
337             LOGGER.info(
338                     "Successfully delete Vserver from cache for key: {}, tenant-id: {}, vserver-id: {} and resource-version: {}",
339                     key, tenantId, vServerId, resourceVersion);
340             return ResponseEntity.noContent().build();
341         }
342
343         LOGGER.error(
344                 "Unable to delete Vserver from cache using key: {}, tenant-id: {}, vserver-id: {} and resource-version: {} ...",
345                 key, tenantId, vServerId, resourceVersion);
346         return getRequestErrorResponseEntity(request, CLOUD_REGION);
347     }
348
349     @PutMapping(
350             value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}"
351                     + RELATIONSHIP_LIST_RELATIONSHIP_URL,
352             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
353             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
354     public ResponseEntity<?> putVserverRelationShip(@PathVariable("cloud-owner") final String cloudOwner,
355             @PathVariable("cloud-region-id") final String cloudRegionId,
356             @PathVariable("tenant-id") final String tenantId, @PathVariable("vserver-id") final String vServerId,
357             @RequestBody final Relationship relationship, final HttpServletRequest request) {
358         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
359
360         LOGGER.info("Will add {} relationship for key : {}, tenant-id: {} and vserver-id: {}...",
361                 relationship.getRelatedLink(), key, tenantId, vServerId);
362
363         if (relationship.getRelatedLink() != null) {
364             final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString();
365             final HttpHeaders incomingHeader = getHeaders(request);
366             final boolean result = cacheServiceProvider.addVServerRelationShip(incomingHeader, targetBaseUrl,
367                     request.getRequestURI(), key, tenantId, vServerId, relationship);
368             if (result) {
369                 LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink());
370                 return ResponseEntity.accepted().build();
371             }
372         }
373         LOGGER.error("Couldn't add {} relationship for 'key': {} ...", relationship.getRelatedLink(), key);
374         return getRequestErrorResponseEntity(request, CLOUD_REGION);
375
376     }
377
378     @PutMapping(
379             value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}"
380                     + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL,
381             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
382             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
383     public ResponseEntity<?> putBiDirectionalVServerRelationShip(@PathVariable("cloud-owner") final String cloudOwner,
384             @PathVariable("cloud-region-id") final String cloudRegionId,
385             @PathVariable("tenant-id") final String tenantId, @PathVariable("vserver-id") final String vServerId,
386             @RequestBody final Relationship relationship, final HttpServletRequest request) {
387         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
388         LOGGER.info("Will add {} bi-directional relationship to : {} ...", relationship.getRelatedTo());
389
390         final Optional<Relationship> optional = cacheServiceProvider.addvServerRelationShip(key, tenantId, vServerId,
391                 relationship, request.getRequestURI());
392
393         if (optional.isPresent()) {
394             final Relationship resultantRelationship = optional.get();
395             LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
396             return ResponseEntity.accepted().body(resultantRelationship);
397         }
398         LOGGER.error("Couldn't add {} relationship for 'key': {} ...", relationship.getRelatedTo(), key);
399         return getRequestErrorResponseEntity(request, CLOUD_REGION);
400
401     }
402
403
404     @PutMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/k8s-resources/k8s-resource/{id}",
405             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
406             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
407     public ResponseEntity<?> putK8sResource(@RequestBody final K8SResource k8sResource,
408             @PathVariable("cloud-owner") final String cloudOwner,
409             @PathVariable("cloud-region-id") final String cloudRegionId,
410             @PathVariable("tenant-id") final String tenantId, @PathVariable("id") final String id,
411             final HttpServletRequest request) {
412
413         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
414
415         if (key.isValid()) {
416             LOGGER.info("Will add K8s Resource to cache with key 'key': {} ....", key);
417             if (k8sResource.getResourceVersion() == null || k8sResource.getResourceVersion().isEmpty()) {
418                 k8sResource.setResourceVersion(getResourceVersion());
419             }
420             if (cacheServiceProvider.putK8sResource(key, tenantId, id, k8sResource)) {
421                 return ResponseEntity.accepted().build();
422             }
423         }
424
425         LOGGER.error("Unable to add K8s Resource in cache using key {}", key);
426         return getRequestErrorResponseEntity(request, CLOUD_REGION);
427     }
428
429     @GetMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/k8s-resources/k8s-resource/{id}",
430             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
431     public ResponseEntity<?> getK8sResource(@PathVariable("cloud-owner") final String cloudOwner,
432             @PathVariable("cloud-region-id") final String cloudRegionId,
433             @PathVariable("tenant-id") final String tenantId, @PathVariable("id") final String id,
434             final HttpServletRequest request) {
435         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
436         LOGGER.info("Retrieving K8SResource using key : {}, tenant-id:{} and id: {}...", key, tenantId, id);
437         if (key.isValid()) {
438             final Optional<K8SResource> optional = cacheServiceProvider.getK8sResource(key, tenantId, id);
439             if (optional.isPresent()) {
440                 final K8SResource tenant = optional.get();
441                 LOGGER.info("found K8SResource {} in cache", tenant);
442                 return ResponseEntity.ok(tenant);
443             }
444         }
445         LOGGER.error("Unable to find K8SResource in cache key : {},tenant-id:{} and id: {}...", key, tenantId, id);
446         return getRequestErrorResponseEntity(request, CLOUD_REGION);
447     }
448
449     @PutMapping(
450             value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/k8s-resources/k8s-resource/{id}"
451                     + RELATIONSHIP_LIST_RELATIONSHIP_URL,
452             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
453             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
454     public ResponseEntity<?> putK8sResourceRelationShip(@PathVariable("cloud-owner") final String cloudOwner,
455             @PathVariable("cloud-region-id") final String cloudRegionId,
456             @PathVariable("tenant-id") final String tenantId, @PathVariable("id") final String id,
457             @RequestBody final Relationship relationship, final HttpServletRequest request) {
458         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
459         LOGGER.info("Will add {} relationship for key : {}, tenant-id: {} and id: {}...", relationship.getRelatedLink(),
460                 key, tenantId, id);
461
462         if (relationship.getRelatedLink() != null) {
463             final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString();
464             final HttpHeaders incomingHeader = getHeaders(request);
465             final boolean result = cacheServiceProvider.addK8sResourceRelationShip(incomingHeader, targetBaseUrl,
466                     request.getRequestURI(), key, tenantId, id, relationship);
467             if (result) {
468                 LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink());
469                 return ResponseEntity.accepted().build();
470             }
471         }
472         LOGGER.error("Couldn't add {} relationship for 'key': {} ...", relationship.getRelatedLink(), key);
473         return getRequestErrorResponseEntity(request, CLOUD_REGION);
474
475     }
476
477     @DeleteMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/k8s-resources/k8s-resource/{id}",
478         produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
479     public ResponseEntity<?> deleteK8sResource(@PathVariable("cloud-owner") final String cloudOwner,
480                                                @PathVariable("cloud-region-id") final String cloudRegionId,
481                                                @PathVariable("tenant-id") final String tenantId, @PathVariable("id") final String id,
482                                                @RequestParam(name = "resource-version") final String resourceVersion,
483                                                final HttpServletRequest request) {
484         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
485         LOGGER.info("Removing K8SResource using key : {}, tenant-id:{} and id: {}...", key, tenantId, id);
486         if (key.isValid()) {
487             final boolean result = cacheServiceProvider.deleteK8sResource(key, tenantId, id, resourceVersion);
488             if (result) {
489                 LOGGER.info("Deleted K8SResource from cache related to tenant ID: {}", tenantId);
490                 return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
491             }  else {
492                 return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
493             }
494         }
495         LOGGER.error("Unable to find K8SResource in cache key : {},tenant-id:{} and id: {}...", key, tenantId, id);
496         return getRequestErrorResponseEntity(request, CLOUD_REGION);
497     }
498
499 }