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.RequestErrorResponseUtils.getRequestErrorResponseEntity;
25 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
26 import java.util.Optional;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.ws.rs.core.MediaType;
29 import org.onap.aai.domain.yang.CloudRegion;
30 import org.onap.aai.domain.yang.Relationship;
31 import org.onap.so.aaisimulator.models.CloudRegionKey;
32 import org.onap.so.aaisimulator.service.providers.CloudRegionCacheServiceProvider;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.stereotype.Controller;
38 import org.springframework.web.bind.annotation.GetMapping;
39 import org.springframework.web.bind.annotation.PathVariable;
40 import org.springframework.web.bind.annotation.PutMapping;
41 import org.springframework.web.bind.annotation.RequestBody;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.bind.annotation.RequestParam;
46 * @author Waqas Ikram (waqas.ikram@est.tech)
50 @RequestMapping(path = CLOUD_REGIONS)
51 public class CloudRegionsController {
52 private static final Logger LOGGER = LoggerFactory.getLogger(CloudRegionsController.class);
54 private final CloudRegionCacheServiceProvider cacheServiceProvider;
57 public CloudRegionsController(final CloudRegionCacheServiceProvider cacheServiceProvider) {
58 this.cacheServiceProvider = cacheServiceProvider;
61 @PutMapping(value = "{cloud-owner}/{cloud-region-id}",
62 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
63 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
64 public ResponseEntity<?> putCloudRegion(@RequestBody final CloudRegion cloudRegion,
65 @PathVariable("cloud-owner") final String cloudOwner,
66 @PathVariable("cloud-region-id") final String cloudRegionId, final HttpServletRequest request) {
68 final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
71 LOGGER.info("Will add CloudRegion to cache with key 'key': {} ....", key);
72 if (cloudRegion.getResourceVersion() == null || cloudRegion.getResourceVersion().isEmpty()) {
73 cloudRegion.setResourceVersion(getResourceVersion());
75 cacheServiceProvider.putCloudRegion(key, cloudRegion);
76 return ResponseEntity.accepted().build();
79 LOGGER.error("Unable to add CloudRegion in cache because of invalid key {}", key);
80 return getRequestErrorResponseEntity(request, CLOUD_REGION);
83 @GetMapping(value = "{cloud-owner}/{cloud-region-id}",
84 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
85 public ResponseEntity<?> getCloudRegion(@PathVariable("cloud-owner") final String cloudOwner,
86 @PathVariable("cloud-region-id") final String cloudRegionId,
87 @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) {
88 final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
89 LOGGER.info("Retrieving CloudRegion using key : {} with depth: {}...", key, depth);
91 final Optional<CloudRegion> optional = cacheServiceProvider.getCloudRegion(key);
92 if (optional.isPresent()) {
93 final CloudRegion cloudRegion = optional.get();
94 LOGGER.info("found CloudRegion {} in cache", cloudRegion);
95 return ResponseEntity.ok(cloudRegion);
98 LOGGER.error("Unable to find CloudRegion in cache using {}", key);
99 return getRequestErrorResponseEntity(request, CLOUD_REGION);
102 @PutMapping(value = "{cloud-owner}/{cloud-region-id}/relationship-list/relationship",
103 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
104 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
105 public ResponseEntity<?> putRelationShip(@PathVariable("cloud-owner") final String cloudOwner,
106 @PathVariable("cloud-region-id") final String cloudRegionId, @RequestBody final Relationship relationship,
107 final HttpServletRequest request) {
108 LOGGER.info("Will add {} relationship to : {} ...", relationship.getRelatedTo());
110 final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
112 final Optional<Relationship> optional =
113 cacheServiceProvider.addRelationShip(key, relationship, request.getRequestURI());
115 if (optional.isPresent()) {
116 final Relationship resultantRelationship = optional.get();
117 LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
118 return ResponseEntity.accepted().body(resultantRelationship);
121 LOGGER.error("Couldn't add {} relationship for 'key': {} ...", relationship.getRelatedTo(), key);
123 return getRequestErrorResponseEntity(request, CLOUD_REGION);