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.ESR_SYSTEM_INFO;
 
  23 import static org.onap.so.aaisimulator.utils.Constants.ESR_SYSTEM_INFO_LIST;
 
  24 import static org.onap.so.aaisimulator.utils.Constants.ESR_VNFM;
 
  25 import static org.onap.so.aaisimulator.utils.Constants.EXTERNAL_SYSTEM_ESR_VNFM_LIST_URL;
 
  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.List;
 
  31 import java.util.Optional;
 
  32 import javax.servlet.http.HttpServletRequest;
 
  33 import javax.ws.rs.core.MediaType;
 
  34 import org.onap.aai.domain.yang.EsrSystemInfo;
 
  35 import org.onap.aai.domain.yang.EsrSystemInfoList;
 
  36 import org.onap.aai.domain.yang.EsrVnfm;
 
  37 import org.onap.aai.domain.yang.EsrVnfmList;
 
  38 import org.onap.aai.domain.yang.Relationship;
 
  39 import org.onap.so.aaisimulator.service.providers.ExternalSystemCacheServiceProvider;
 
  40 import org.onap.so.aaisimulator.utils.HttpServiceUtils;
 
  41 import org.onap.so.aaisimulator.utils.RequestErrorResponseUtils;
 
  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 = EXTERNAL_SYSTEM_ESR_VNFM_LIST_URL)
 
  61 public class ExternalSystemEsrController {
 
  62     private static final Logger LOGGER = LoggerFactory.getLogger(ExternalSystemEsrController.class);
 
  64     private final ExternalSystemCacheServiceProvider cacheServiceProvider;
 
  67     public ExternalSystemEsrController(final ExternalSystemCacheServiceProvider cacheServiceProvider) {
 
  68         this.cacheServiceProvider = cacheServiceProvider;
 
  71     @PutMapping(value = "/esr-vnfm/{vnfm-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
 
  72             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 
  73     public ResponseEntity<?> putEsrVnfm(@RequestBody final EsrVnfm esrVnfm,
 
  74             @PathVariable("vnfm-id") final String vnfmId, final HttpServletRequest request) {
 
  75         LOGGER.info("Will put esr-vnfm to cache for 'vnfm id': {} ...", esrVnfm.getVnfmId());
 
  77         if (esrVnfm.getResourceVersion() == null || esrVnfm.getResourceVersion().isEmpty()) {
 
  78             esrVnfm.setResourceVersion(getResourceVersion());
 
  81         cacheServiceProvider.putEsrVnfm(vnfmId, esrVnfm);
 
  82         return ResponseEntity.accepted().build();
 
  85     @GetMapping(value = "/esr-vnfm/{vnfm-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 
  86     public ResponseEntity<?> getEsrVnfm(@PathVariable("vnfm-id") final String vnfmId,
 
  87             @RequestParam(name = "depth", required = false) final Integer depth, final HttpServletRequest request) {
 
  88         LOGGER.info("Will retrieve ESR VNFM for 'vnfm id': {} with depth: {}...", vnfmId, depth);
 
  90         final Optional<EsrVnfm> optional = cacheServiceProvider.getEsrVnfm(vnfmId);
 
  91         if (optional.isPresent()) {
 
  92             final EsrVnfm esrVnfm = optional.get();
 
  93             LOGGER.info("found esrVnfm {} in cache", esrVnfm);
 
  94             return ResponseEntity.ok(esrVnfm);
 
  97         LOGGER.error("Couldn't Esr Vnfm for 'vnfm id': {} with depth: {}...", vnfmId, depth);
 
  98         return getRequestErrorResponseEntity(request, ESR_VNFM);
 
 101     @GetMapping(produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 
 102     public ResponseEntity<?> getEsrVnfmList(final HttpServletRequest request) {
 
 103         LOGGER.info("Will retrieve a list of all ESR VNFMs");
 
 105         final List<EsrVnfm> esrVnfms = cacheServiceProvider.getAllEsrVnfm();
 
 106         LOGGER.info("found {} Esr Vnfms in cache", esrVnfms.size());
 
 108         final EsrVnfmList esrVnfmList = new EsrVnfmList();
 
 109         esrVnfmList.getEsrVnfm().addAll(esrVnfms);
 
 111         return ResponseEntity.ok(esrVnfmList);
 
 114     @PutMapping(value = "/esr-vnfm/{vnfm-id}/esr-system-info-list/esr-system-info/{esr-system-info-id}",
 
 115             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
 
 116             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 
 117     public ResponseEntity<?> putEsrSystemInfo(@RequestBody final EsrSystemInfo esrSystemInfo,
 
 118             @PathVariable("vnfm-id") final String vnfmId,
 
 119             @PathVariable("esr-system-info-id") final String esrSystemInfoId, final HttpServletRequest request) {
 
 120         LOGGER.info("Will put esrSystemInfo for 'vnfm id': {} and 'esr-system-info-id': {} ...", vnfmId, esrSystemInfo);
 
 122         if (esrSystemInfo.getResourceVersion() == null || esrSystemInfo.getResourceVersion().isEmpty()) {
 
 123             esrSystemInfo.setResourceVersion(getResourceVersion());
 
 127         if (cacheServiceProvider.putEsrSystemInfo(vnfmId, esrSystemInfoId, esrSystemInfo)) {
 
 128             LOGGER.info("Successfully added EsrSystemInfo for 'vnfm id': {} and 'esr-system-info-id': {} ...", vnfmId,
 
 130             return ResponseEntity.accepted().build();
 
 132         LOGGER.error("Unable to add esrSystemInfo for 'vnfm id': {} and 'esr-system-info-id': {} ...", vnfmId,
 
 134         return getRequestErrorResponseEntity(request, ESR_SYSTEM_INFO_LIST);
 
 137     @GetMapping(value = "/esr-vnfm/{vnfm-id}/esr-system-info-list",
 
 138             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 
 139     public ResponseEntity<?> getEsrSystemInfoList(@PathVariable("vnfm-id") final String vnfmId,
 
 140             final HttpServletRequest request) {
 
 141         LOGGER.info("Will retrieve esrSystemInfoList for 'vnfm id': {} ...", vnfmId);
 
 143         final Optional<EsrSystemInfoList> optional = cacheServiceProvider.getEsrSystemInfoList(vnfmId);
 
 144         if (optional.isPresent()) {
 
 145             final EsrSystemInfoList esrSystemInfoList = optional.get();
 
 146             LOGGER.info("found esrSystemInfoList {} in cache", esrSystemInfoList);
 
 147             return ResponseEntity.ok(esrSystemInfoList);
 
 150         LOGGER.error("Couldn't find esrSystemInfoList for 'vnfm id': {} ...", vnfmId);
 
 151         return getRequestErrorResponseEntity(request, ESR_SYSTEM_INFO);
 
 154     @PutMapping(value = "/esr-vnfm/{vnfm-id}" + RELATIONSHIP_LIST_RELATIONSHIP_URL,
 
 155             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
 
 156             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 
 157     public ResponseEntity<?> putEsrVnfmRelationShip(@RequestBody final Relationship relationship,
 
 158             @PathVariable("vnfm-id") final String vnfmId, final HttpServletRequest request) {
 
 159         LOGGER.info("Will put RelationShip for 'vnfm-id': {} ...", vnfmId);
 
 161         if (relationship.getRelatedLink() != null) {
 
 162             final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString();
 
 163             final HttpHeaders incomingHeader = getHeaders(request);
 
 164             final boolean result = cacheServiceProvider.addRelationShip(incomingHeader, targetBaseUrl,
 
 165                     request.getRequestURI(), vnfmId, relationship);
 
 167                 LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink());
 
 168                 return ResponseEntity.accepted().build();
 
 171         LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
 
 172         return RequestErrorResponseUtils.getRequestErrorResponseEntity(request, ESR_VNFM);