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