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