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.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL;
23 import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGION;
24 import static org.onap.so.aaisimulator.utils.Constants.CLOUD_REGIONS;
25 import static org.onap.so.aaisimulator.utils.Constants.ESR_SYSTEM_INFO_LIST;
26 import static org.onap.so.aaisimulator.utils.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL;
27 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getHeaders;
28 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
29 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
30 import java.util.Optional;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.ws.rs.core.MediaType;
33 import org.onap.aai.domain.yang.CloudRegion;
34 import org.onap.aai.domain.yang.EsrSystemInfo;
35 import org.onap.aai.domain.yang.EsrSystemInfoList;
36 import org.onap.aai.domain.yang.Relationship;
37 import org.onap.aai.domain.yang.Tenant;
38 import org.onap.aai.domain.yang.Vserver;
39 import org.onap.so.aaisimulator.models.CloudRegionKey;
40 import org.onap.so.aaisimulator.service.providers.CloudRegionCacheServiceProvider;
41 import org.onap.so.aaisimulator.utils.HttpServiceUtils;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.http.HttpHeaders;
46 import org.springframework.http.ResponseEntity;
47 import org.springframework.stereotype.Controller;
48 import org.springframework.web.bind.annotation.GetMapping;
49 import org.springframework.web.bind.annotation.PathVariable;
50 import org.springframework.web.bind.annotation.PutMapping;
51 import org.springframework.web.bind.annotation.RequestBody;
52 import org.springframework.web.bind.annotation.RequestMapping;
53 import org.springframework.web.bind.annotation.RequestParam;
56 * @author Waqas Ikram (waqas.ikram@est.tech)
60 @RequestMapping(path = CLOUD_REGIONS)
61 public class CloudRegionsController {
62 private static final Logger LOGGER = LoggerFactory.getLogger(CloudRegionsController.class);
64 private final CloudRegionCacheServiceProvider cacheServiceProvider;
67 public CloudRegionsController(final CloudRegionCacheServiceProvider cacheServiceProvider) {
68 this.cacheServiceProvider = cacheServiceProvider;
71 @PutMapping(value = "{cloud-owner}/{cloud-region-id}",
72 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
73 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
74 public ResponseEntity<?> putCloudRegion(@RequestBody final CloudRegion cloudRegion,
75 @PathVariable("cloud-owner") final String cloudOwner,
76 @PathVariable("cloud-region-id") final String cloudRegionId, final HttpServletRequest request) {
78 final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
81 LOGGER.info("Will add CloudRegion to cache with key 'key': {} ....", key);
82 if (cloudRegion.getResourceVersion() == null || cloudRegion.getResourceVersion().isEmpty()) {
83 cloudRegion.setResourceVersion(getResourceVersion());
85 cacheServiceProvider.putCloudRegion(key, cloudRegion);
86 return ResponseEntity.accepted().build();
89 LOGGER.error("Unable to add CloudRegion in cache because of invalid key {}", key);
90 return getRequestErrorResponseEntity(request, CLOUD_REGION);
93 @GetMapping(value = "{cloud-owner}/{cloud-region-id}",
94 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
95 public ResponseEntity<?> getCloudRegion(@PathVariable("cloud-owner") final String cloudOwner,
96 @PathVariable("cloud-region-id") final String cloudRegionId,
97 @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) {
98 final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
99 LOGGER.info("Retrieving CloudRegion using key : {} with depth: {}...", key, depth);
101 final Optional<CloudRegion> optional = cacheServiceProvider.getCloudRegion(key);
102 if (optional.isPresent()) {
103 final CloudRegion cloudRegion = optional.get();
104 LOGGER.info("found CloudRegion {} in cache", cloudRegion);
105 return ResponseEntity.ok(cloudRegion);
108 LOGGER.error("Unable to find CloudRegion in cache using {}", key);
109 return getRequestErrorResponseEntity(request, CLOUD_REGION);
112 @PutMapping(value = "{cloud-owner}/{cloud-region-id}" + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL,
113 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
114 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
115 public ResponseEntity<?> putRelationShip(@PathVariable("cloud-owner") final String cloudOwner,
116 @PathVariable("cloud-region-id") final String cloudRegionId, @RequestBody final Relationship relationship,
117 final HttpServletRequest request) {
118 LOGGER.info("Will add {} relationship to : {} ...", relationship.getRelatedTo());
120 final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
122 final Optional<Relationship> optional =
123 cacheServiceProvider.addRelationShip(key, relationship, request.getRequestURI());
125 if (optional.isPresent()) {
126 final Relationship resultantRelationship = optional.get();
127 LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
128 return ResponseEntity.accepted().body(resultantRelationship);
131 LOGGER.error("Couldn't add {} relationship for 'key': {} ...", relationship.getRelatedTo(), key);
132 return getRequestErrorResponseEntity(request, CLOUD_REGION);
136 @PutMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}",
137 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
138 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
139 public ResponseEntity<?> putTenant(@RequestBody final Tenant tenant,
140 @PathVariable("cloud-owner") final String cloudOwner,
141 @PathVariable("cloud-region-id") final String cloudRegionId,
142 @PathVariable("tenant-id") final String tenantId, final HttpServletRequest request) {
144 final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
147 LOGGER.info("Will add Tenant to cache with key 'key': {} ....", key);
148 if (tenant.getResourceVersion() == null || tenant.getResourceVersion().isEmpty()) {
149 tenant.setResourceVersion(getResourceVersion());
151 if (cacheServiceProvider.putTenant(key, tenantId, tenant)) {
152 return ResponseEntity.accepted().build();
156 LOGGER.error("Unable to add Tenant in cache using key {}", key);
157 return getRequestErrorResponseEntity(request, CLOUD_REGION);
160 @GetMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}",
161 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
162 public ResponseEntity<?> getTenant(@PathVariable("cloud-owner") final String cloudOwner,
163 @PathVariable("cloud-region-id") final String cloudRegionId,
164 @PathVariable("tenant-id") final String tenantId, final HttpServletRequest request) {
165 final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
166 LOGGER.info("Retrieving Tenant using key : {} and tenant-id:{} ...", key, tenantId);
168 final Optional<Tenant> optional = cacheServiceProvider.getTenant(key, tenantId);
169 if (optional.isPresent()) {
170 final Tenant tenant = optional.get();
171 LOGGER.info("found Tenant {} in cache", tenant);
172 return ResponseEntity.ok(tenant);
175 LOGGER.error("Unable to find Tenant in cache key : {} and tenant-id:{} ...", key, tenantId);
176 return getRequestErrorResponseEntity(request, CLOUD_REGION);
180 value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}" + RELATIONSHIP_LIST_RELATIONSHIP_URL,
181 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
182 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
183 public ResponseEntity<?> putRelationShip(@RequestBody final Relationship relationship,
184 @PathVariable("cloud-owner") final String cloudOwner,
185 @PathVariable("cloud-region-id") final String cloudRegionId,
186 @PathVariable("tenant-id") final String tenantId, final HttpServletRequest request) {
188 final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
189 LOGGER.info("Will put RelationShip for key : {} and tenant-id:{} ...", key, tenantId);
191 if (relationship.getRelatedLink() != null) {
192 final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString();
193 final HttpHeaders incomingHeader = getHeaders(request);
194 final boolean result = cacheServiceProvider.addRelationShip(incomingHeader, targetBaseUrl,
195 request.getRequestURI(), key, tenantId, relationship);
197 LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink());
198 return ResponseEntity.accepted().build();
202 LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
203 return getRequestErrorResponseEntity(request, CLOUD_REGION);
206 @PutMapping(value = "{cloud-owner}/{cloud-region-id}/esr-system-info-list/esr-system-info/{esr-system-info-id}",
207 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
208 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
209 public ResponseEntity<?> putEsrSystemInfo(@RequestBody final EsrSystemInfo esrSystemInfo,
210 @PathVariable("esr-system-info-id") final String esrSystemInfoId,
211 @PathVariable("cloud-owner") final String cloudOwner,
212 @PathVariable("cloud-region-id") final String cloudRegionId, final HttpServletRequest request) {
214 final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
216 LOGGER.info("Will put esrSystemInfo for 'key': {} ...", key);
218 if (esrSystemInfo.getResourceVersion() == null || esrSystemInfo.getResourceVersion().isEmpty()) {
219 esrSystemInfo.setResourceVersion(getResourceVersion());
223 if (cacheServiceProvider.putEsrSystemInfo(key, esrSystemInfoId, esrSystemInfo)) {
224 LOGGER.info("Successfully added EsrSystemInfo key : {} ...", key, esrSystemInfo);
225 return ResponseEntity.accepted().build();
227 LOGGER.error("Unable to add EsrSystemInfo in cache for key : {} ...", key);
229 return getRequestErrorResponseEntity(request, ESR_SYSTEM_INFO_LIST);
232 @GetMapping(value = "{cloud-owner}/{cloud-region-id}/esr-system-info-list",
233 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
234 public ResponseEntity<?> getEsrSystemInfoList(@PathVariable("cloud-owner") final String cloudOwner,
235 @PathVariable("cloud-region-id") final String cloudRegionId, final HttpServletRequest request) {
236 final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
237 LOGGER.info("Retrieving EsrSystemInfoList using key : {} ...", key);
239 final Optional<EsrSystemInfoList> optional = cacheServiceProvider.getEsrSystemInfoList(key);
240 if (optional.isPresent()) {
241 final EsrSystemInfoList esrSystemInfoList = optional.get();
242 LOGGER.info("found EsrSystemInfoList {} in cache", esrSystemInfoList);
243 return ResponseEntity.ok(esrSystemInfoList);
246 LOGGER.error("Unable to find EsrSystemInfoList in cache using key : {} ...", key);
247 return getRequestErrorResponseEntity(request, CLOUD_REGION);
250 @PutMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}",
251 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
252 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
253 public ResponseEntity<?> putVserver(@RequestBody final Vserver vServer,
254 @PathVariable("cloud-owner") final String cloudOwner,
255 @PathVariable("cloud-region-id") final String cloudRegionId,
256 @PathVariable("tenant-id") final String tenantId, @PathVariable("vserver-id") final String vServerId,
257 final HttpServletRequest request) {
259 final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
260 if (vServer.getResourceVersion() == null || vServer.getResourceVersion().isEmpty()) {
261 vServer.setResourceVersion(getResourceVersion());
263 LOGGER.info("Will put Vserver in cache using using key: {}, tenantId: {}, vServerId: {} ...", key, tenantId,
266 if (cacheServiceProvider.putVserver(key, tenantId, vServerId, vServer)) {
267 LOGGER.info("Successfully added Vserver for key: {}, tenantId: {}, vServerId: {} ...", key, tenantId,
269 return ResponseEntity.accepted().build();
271 LOGGER.error("Unable to add Vserver in cache using key: {}, tenantId: {}, vServerId: {}", key, tenantId,
273 return getRequestErrorResponseEntity(request, CLOUD_REGION);
276 @GetMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}",
277 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
278 public ResponseEntity<?> getVserver(@PathVariable("cloud-owner") final String cloudOwner,
279 @PathVariable("cloud-region-id") final String cloudRegionId,
280 @PathVariable("tenant-id") final String tenantId, @PathVariable("vserver-id") final String vServerId,
281 final HttpServletRequest request) {
283 final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
284 LOGGER.info("Retrieving Vserver using key: {}, tenant-id: {} and vserver-id: {}...", key, tenantId, vServerId);
285 final Optional<Vserver> optional = cacheServiceProvider.getVserver(key, tenantId, vServerId);
286 if (optional.isPresent()) {
287 final Vserver vServer = optional.get();
288 LOGGER.info("found Vserver {} in cache", vServer);
289 return ResponseEntity.ok(vServer);
291 LOGGER.error("Unable to find Vserver in cache using key: {}, tenant-id: {} and vserver-id: {}...", key,
292 tenantId, vServerId);
293 return getRequestErrorResponseEntity(request, CLOUD_REGION);