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