Adding cloud region endpoints
[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.CLOUD_REGION;
23 import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGIONS;
24 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
25 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
26 import java.util.Optional;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.ws.rs.core.MediaType;
29 import org.onap.aai.domain.yang.CloudRegion;
30 import org.onap.aai.domain.yang.Relationship;
31 import org.onap.so.aaisimulator.models.CloudRegionKey;
32 import org.onap.so.aaisimulator.service.providers.CloudRegionCacheServiceProvider;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.stereotype.Controller;
38 import org.springframework.web.bind.annotation.GetMapping;
39 import org.springframework.web.bind.annotation.PathVariable;
40 import org.springframework.web.bind.annotation.PutMapping;
41 import org.springframework.web.bind.annotation.RequestBody;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.bind.annotation.RequestParam;
44
45 /**
46  * @author Waqas Ikram (waqas.ikram@est.tech)
47  *
48  */
49 @Controller
50 @RequestMapping(path = CLOUD_REGIONS)
51 public class CloudRegionsController {
52     private static final Logger LOGGER = LoggerFactory.getLogger(CloudRegionsController.class);
53
54     private final CloudRegionCacheServiceProvider cacheServiceProvider;
55
56     @Autowired
57     public CloudRegionsController(final CloudRegionCacheServiceProvider cacheServiceProvider) {
58         this.cacheServiceProvider = cacheServiceProvider;
59     }
60
61     @PutMapping(value = "{cloud-owner}/{cloud-region-id}",
62             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
63             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
64     public ResponseEntity<?> putCloudRegion(@RequestBody final CloudRegion cloudRegion,
65             @PathVariable("cloud-owner") final String cloudOwner,
66             @PathVariable("cloud-region-id") final String cloudRegionId, final HttpServletRequest request) {
67
68         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
69
70         if (key.isValid()) {
71             LOGGER.info("Will add CloudRegion to cache with key 'key': {} ....", key);
72             if (cloudRegion.getResourceVersion() == null || cloudRegion.getResourceVersion().isEmpty()) {
73                 cloudRegion.setResourceVersion(getResourceVersion());
74             }
75             cacheServiceProvider.putCloudRegion(key, cloudRegion);
76             return ResponseEntity.accepted().build();
77         }
78
79         LOGGER.error("Unable to add CloudRegion in cache because of invalid key {}", key);
80         return getRequestErrorResponseEntity(request, CLOUD_REGION);
81     }
82
83     @GetMapping(value = "{cloud-owner}/{cloud-region-id}",
84             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
85     public ResponseEntity<?> getCloudRegion(@PathVariable("cloud-owner") final String cloudOwner,
86             @PathVariable("cloud-region-id") final String cloudRegionId,
87             @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) {
88         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
89         LOGGER.info("Retrieving CloudRegion using key : {} with depth: {}...", key, depth);
90         if (key.isValid()) {
91             final Optional<CloudRegion> optional = cacheServiceProvider.getCloudRegion(key);
92             if (optional.isPresent()) {
93                 final CloudRegion cloudRegion = optional.get();
94                 LOGGER.info("found CloudRegion {} in cache", cloudRegion);
95                 return ResponseEntity.ok(cloudRegion);
96             }
97         }
98         LOGGER.error("Unable to find CloudRegion in cache using {}", key);
99         return getRequestErrorResponseEntity(request, CLOUD_REGION);
100     }
101
102     @PutMapping(value = "{cloud-owner}/{cloud-region-id}/relationship-list/relationship",
103             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
104             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
105     public ResponseEntity<?> putRelationShip(@PathVariable("cloud-owner") final String cloudOwner,
106             @PathVariable("cloud-region-id") final String cloudRegionId, @RequestBody final Relationship relationship,
107             final HttpServletRequest request) {
108         LOGGER.info("Will add {} relationship to : {} ...", relationship.getRelatedTo());
109
110         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
111
112         final Optional<Relationship> optional =
113                 cacheServiceProvider.addRelationShip(key, relationship, request.getRequestURI());
114
115         if (optional.isPresent()) {
116             final Relationship resultantRelationship = optional.get();
117             LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
118             return ResponseEntity.accepted().body(resultantRelationship);
119         }
120
121         LOGGER.error("Couldn't add {} relationship for 'key': {} ...", relationship.getRelatedTo(), key);
122
123         return getRequestErrorResponseEntity(request, CLOUD_REGION);
124
125     }
126
127
128 }