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.PLATFORM;
 
  23 import static org.onap.so.aaisimulator.utils.Constants.PLATFORMS_URL;
 
  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.Platform;
 
  31 import org.onap.aai.domain.yang.Relationship;
 
  32 import org.onap.so.aaisimulator.service.providers.PlatformCacheServiceProvider;
 
  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;
 
  45  * @author Waqas Ikram (waqas.ikram@est.tech)
 
  49 @RequestMapping(path = PLATFORMS_URL)
 
  50 public class PlatformController {
 
  51     private static final Logger LOGGER = LoggerFactory.getLogger(PlatformController.class);
 
  53     private final PlatformCacheServiceProvider cacheServiceProvider;
 
  56     public PlatformController(final PlatformCacheServiceProvider cacheServiceProvider) {
 
  57         this.cacheServiceProvider = cacheServiceProvider;
 
  60     @PutMapping(value = "{platform-name}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
 
  61             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 
  62     public ResponseEntity<?> putPlatform(@RequestBody final Platform platform,
 
  63             @PathVariable("platform-name") final String platformName, final HttpServletRequest request) {
 
  64         LOGGER.info("Will add Platform to cache with key 'platform-name': {} ...", platform.getPlatformName());
 
  66         if (platform.getResourceVersion() == null || platform.getResourceVersion().isEmpty()) {
 
  67             platform.setResourceVersion(getResourceVersion());
 
  70         cacheServiceProvider.putPlatform(platformName, platform);
 
  71         return ResponseEntity.accepted().build();
 
  74     @GetMapping(value = "/{platform-name}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 
  75     public ResponseEntity<?> getPlatform(@PathVariable("platform-name") final String platformName,
 
  76             final HttpServletRequest request) {
 
  77         LOGGER.info("retrieving Platform for 'platform-name': {} ...", platformName);
 
  78         final Optional<Platform> optional = cacheServiceProvider.getPlatform(platformName);
 
  79         if (optional.isPresent()) {
 
  80             final Platform platform = optional.get();
 
  81             LOGGER.info("found Platform {} in cache", platform);
 
  82             return ResponseEntity.ok(platform);
 
  84         LOGGER.error("Unable to find Platform in cahce using {}", platformName);
 
  85         return getRequestErrorResponseEntity(request, PLATFORM);
 
  88     @PutMapping(value = "/{platform-name}" + BI_DIRECTIONAL_RELATIONSHIP_LIST_URL,
 
  89             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
 
  90             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 
  91     public ResponseEntity<?> putRelationShip(@PathVariable("platform-name") final String platformName,
 
  92             @RequestBody final Relationship relationship, final HttpServletRequest request) {
 
  93         LOGGER.info("Will add {} relationship to : {} ...", relationship.getRelatedTo());
 
  95         final Optional<Relationship> optional =
 
  96                 cacheServiceProvider.addRelationShip(platformName, relationship, request.getRequestURI());
 
  98         if (optional.isPresent()) {
 
  99             final Relationship resultantRelationship = optional.get();
 
 100             LOGGER.info("Relationship add, sending resultant relationship: {} in response ...", resultantRelationship);
 
 101             return ResponseEntity.accepted().body(resultantRelationship);
 
 104         LOGGER.error("Couldn't add {} relationship for 'platform-name': {} ...", relationship.getRelatedTo(),
 
 107         return getRequestErrorResponseEntity(request, PLATFORM);