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.Constants.BI_DIRECTIONAL_RELATIONSHIP_LIST_URL;
 
  25 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
 
  26 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
 
  27 import java.util.Optional;
 
  28 import javax.servlet.http.HttpServletRequest;
 
  29 import javax.ws.rs.core.MediaType;
 
  30 import org.onap.aai.domain.yang.CloudRegion;
 
  31 import org.onap.aai.domain.yang.Relationship;
 
  32 import org.onap.aai.domain.yang.Tenant;
 
  33 import org.onap.so.aaisimulator.models.CloudRegionKey;
 
  34 import org.onap.so.aaisimulator.service.providers.CloudRegionCacheServiceProvider;
 
  35 import org.slf4j.Logger;
 
  36 import org.slf4j.LoggerFactory;
 
  37 import org.springframework.beans.factory.annotation.Autowired;
 
  38 import org.springframework.http.ResponseEntity;
 
  39 import org.springframework.stereotype.Controller;
 
  40 import org.springframework.web.bind.annotation.GetMapping;
 
  41 import org.springframework.web.bind.annotation.PathVariable;
 
  42 import org.springframework.web.bind.annotation.PutMapping;
 
  43 import org.springframework.web.bind.annotation.RequestBody;
 
  44 import org.springframework.web.bind.annotation.RequestMapping;
 
  45 import org.springframework.web.bind.annotation.RequestParam;
 
  48  * @author Waqas Ikram (waqas.ikram@est.tech)
 
  52 @RequestMapping(path = CLOUD_REGIONS)
 
  53 public class CloudRegionsController {
 
  54     private static final Logger LOGGER = LoggerFactory.getLogger(CloudRegionsController.class);
 
  56     private final CloudRegionCacheServiceProvider cacheServiceProvider;
 
  59     public CloudRegionsController(final CloudRegionCacheServiceProvider cacheServiceProvider) {
 
  60         this.cacheServiceProvider = cacheServiceProvider;
 
  63     @PutMapping(value = "{cloud-owner}/{cloud-region-id}",
 
  64             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
 
  65             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 
  66     public ResponseEntity<?> putCloudRegion(@RequestBody final CloudRegion cloudRegion,
 
  67             @PathVariable("cloud-owner") final String cloudOwner,
 
  68             @PathVariable("cloud-region-id") final String cloudRegionId, final HttpServletRequest request) {
 
  70         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
 
  73             LOGGER.info("Will add CloudRegion to cache with key 'key': {} ....", key);
 
  74             if (cloudRegion.getResourceVersion() == null || cloudRegion.getResourceVersion().isEmpty()) {
 
  75                 cloudRegion.setResourceVersion(getResourceVersion());
 
  77             cacheServiceProvider.putCloudRegion(key, cloudRegion);
 
  78             return ResponseEntity.accepted().build();
 
  81         LOGGER.error("Unable to add CloudRegion in cache because of invalid key {}", key);
 
  82         return getRequestErrorResponseEntity(request, CLOUD_REGION);
 
  85     @GetMapping(value = "{cloud-owner}/{cloud-region-id}",
 
  86             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 
  87     public ResponseEntity<?> getCloudRegion(@PathVariable("cloud-owner") final String cloudOwner,
 
  88             @PathVariable("cloud-region-id") final String cloudRegionId,
 
  89             @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) {
 
  90         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
 
  91         LOGGER.info("Retrieving CloudRegion using key : {} with depth: {}...", key, depth);
 
  93             final Optional<CloudRegion> optional = cacheServiceProvider.getCloudRegion(key);
 
  94             if (optional.isPresent()) {
 
  95                 final CloudRegion cloudRegion = optional.get();
 
  96                 LOGGER.info("found CloudRegion {} in cache", cloudRegion);
 
  97                 return ResponseEntity.ok(cloudRegion);
 
 100         LOGGER.error("Unable to find CloudRegion in cache using {}", key);
 
 101         return getRequestErrorResponseEntity(request, CLOUD_REGION);
 
 104     @PutMapping(value = "{cloud-owner}/{cloud-region-id}" + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL,
 
 105             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
 
 106             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 
 107     public ResponseEntity<?> putRelationShip(@PathVariable("cloud-owner") final String cloudOwner,
 
 108             @PathVariable("cloud-region-id") final String cloudRegionId, @RequestBody final Relationship relationship,
 
 109             final HttpServletRequest request) {
 
 110         LOGGER.info("Will add {} relationship to : {} ...", relationship.getRelatedTo());
 
 112         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
 
 114         final Optional<Relationship> optional =
 
 115                 cacheServiceProvider.addRelationShip(key, relationship, request.getRequestURI());
 
 117         if (optional.isPresent()) {
 
 118             final Relationship resultantRelationship = optional.get();
 
 119             LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
 
 120             return ResponseEntity.accepted().body(resultantRelationship);
 
 123         LOGGER.error("Couldn't add {} relationship for 'key': {} ...", relationship.getRelatedTo(), key);
 
 125         return getRequestErrorResponseEntity(request, CLOUD_REGION);
 
 129     @PutMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}",
 
 130             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
 
 131             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 
 132     public ResponseEntity<?> putTenant(@RequestBody final Tenant tenant,
 
 133             @PathVariable("cloud-owner") final String cloudOwner,
 
 134             @PathVariable("cloud-region-id") final String cloudRegionId,
 
 135             @PathVariable("tenant-id") final String tenantId, final HttpServletRequest request) {
 
 137         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
 
 140             LOGGER.info("Will add Tenant to cache with key 'key': {} ....", key);
 
 141             if (tenant.getResourceVersion() == null || tenant.getResourceVersion().isEmpty()) {
 
 142                 tenant.setResourceVersion(getResourceVersion());
 
 144             if (cacheServiceProvider.putTenant(key, tenant)) {
 
 145                 return ResponseEntity.accepted().build();
 
 149         LOGGER.error("Unable to add Tenant in cache using key {}", key);
 
 150         return getRequestErrorResponseEntity(request, CLOUD_REGION);
 
 153     @GetMapping(value = "{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}",
 
 154             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 
 155     public ResponseEntity<?> getTenant(@PathVariable("cloud-owner") final String cloudOwner,
 
 156             @PathVariable("cloud-region-id") final String cloudRegionId,
 
 157             @PathVariable("tenant-id") final String tenantId, final HttpServletRequest request) {
 
 158         final CloudRegionKey key = new CloudRegionKey(cloudOwner, cloudRegionId);
 
 159         LOGGER.info("Retrieving Tenant using key : {} and tenant-id:{} ...", key, tenantId);
 
 161             final Optional<Tenant> optional = cacheServiceProvider.getTenant(key, tenantId);
 
 162             if (optional.isPresent()) {
 
 163                 final Tenant tenant = optional.get();
 
 164                 LOGGER.info("found Tenant {} in cache", tenant);
 
 165                 return ResponseEntity.ok(tenant);
 
 168         LOGGER.error("Unable to find Tenant in cache key : {} and tenant-id:{} ...", key, tenantId);
 
 169         return getRequestErrorResponseEntity(request, CLOUD_REGION);