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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.aaisimulator.controller;
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;
51 * @author Waqas Ikram (waqas.ikram@est.tech)
55 @RequestMapping(path = CLOUD_REGIONS)
56 public class CloudRegionsController {
57 private static final Logger LOGGER = LoggerFactory.getLogger(CloudRegionsController.class);
59 private final CloudRegionCacheServiceProvider cacheServiceProvider;
62 public CloudRegionsController(final CloudRegionCacheServiceProvider cacheServiceProvider) {
63 this.cacheServiceProvider = cacheServiceProvider;
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) {
73 final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
76 LOGGER.info("Will add CloudRegion to cache with key 'key': {} ....", key);
77 if (cloudRegion.getResourceVersion() == null || cloudRegion.getResourceVersion().isEmpty()) {
78 cloudRegion.setResourceVersion(getResourceVersion());
80 cacheServiceProvider.putCloudRegion(key, cloudRegion);
81 return ResponseEntity.accepted().build();
84 LOGGER.error("Unable to add CloudRegion in cache because of invalid key {}", key);
85 return getRequestErrorResponseEntity(request, CLOUD_REGION);
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);
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);
103 LOGGER.error("Unable to find CloudRegion in cache using {}", key);
104 return getRequestErrorResponseEntity(request, CLOUD_REGION);
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());
115 final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
117 final Optional<Relationship> optional =
118 cacheServiceProvider.addRelationShip(key, relationship, request.getRequestURI());
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);
126 LOGGER.error("Couldn't add {} relationship for 'key': {} ...", relationship.getRelatedTo(), key);
127 return getRequestErrorResponseEntity(request, CLOUD_REGION);
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) {
139 final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
142 LOGGER.info("Will add Tenant to cache with key 'key': {} ....", key);
143 if (tenant.getResourceVersion() == null || tenant.getResourceVersion().isEmpty()) {
144 tenant.setResourceVersion(getResourceVersion());
146 if (cacheServiceProvider.putTenant(key, tenant)) {
147 return ResponseEntity.accepted().build();
151 LOGGER.error("Unable to add Tenant in cache using key {}", key);
152 return getRequestErrorResponseEntity(request, CLOUD_REGION);
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);
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);
170 LOGGER.error("Unable to find Tenant in cache key : {} and tenant-id:{} ...", key, tenantId);
171 return getRequestErrorResponseEntity(request, CLOUD_REGION);
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) {
182 final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
183 LOGGER.info("Will put RelationShip for key : {} and tenant-id:{} ...", key, tenantId);
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);
191 LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink());
192 return ResponseEntity.accepted().build();
196 LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
197 return getRequestErrorResponseEntity(request, CLOUD_REGION);