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=========================================================
 
  21 package org.onap.so.adapters.vnfmadapter.rest;
 
  23 import static org.onap.so.adapters.vnfmadapter.Constants.APPLICATION_ZIP;
 
  24 import static org.onap.so.adapters.vnfmadapter.Constants.PACKAGE_MANAGEMENT_BASE_URL;
 
  25 import static org.slf4j.LoggerFactory.getLogger;
 
  26 import java.util.Optional;
 
  27 import javax.ws.rs.core.MediaType;
 
  28 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.EtsiCatalogServiceProvider;
 
  29 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.ProblemDetails;
 
  30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2001;
 
  31 import org.slf4j.Logger;
 
  32 import org.springframework.beans.factory.annotation.Autowired;
 
  33 import org.springframework.http.HttpStatus;
 
  34 import org.springframework.http.ResponseEntity;
 
  35 import org.springframework.stereotype.Controller;
 
  36 import org.springframework.web.bind.annotation.GetMapping;
 
  37 import org.springframework.web.bind.annotation.PathVariable;
 
  38 import org.springframework.web.bind.annotation.RequestMapping;
 
  41  * Controller for handling the VNF Package Management. For further information please read:
 
  42  * https://www.etsi.org/deliver/etsi_gs/NFV-SOL/001_099/003/02.05.01_60/gs_nfv-sol003v020501p.pdf Use the section number
 
  43  * above each endpoint to find the corresponding section in the above document.
 
  45  * @author gareth.roper@est.tech
 
  48 @RequestMapping(value = PACKAGE_MANAGEMENT_BASE_URL, consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 
  49 public class Sol003PackageManagementController {
 
  51     private final EtsiCatalogServiceProvider etsiCatalogServiceProvider;
 
  52     private static final String LOG_REQUEST_RECEIVED = "VNF PackageManagement Controller: {} {} {} {}";
 
  53     private static final Logger logger = getLogger(Sol003PackageManagementController.class);
 
  56     Sol003PackageManagementController(final EtsiCatalogServiceProvider etsiCatalogServiceProvider) {
 
  57         this.etsiCatalogServiceProvider = etsiCatalogServiceProvider;
 
  61      * GET VNF packages information. Will return zero or more VNF package representations that match the attribute
 
  62      * filter. These representations will be in a list. Section Number: 10.4.2
 
  64      * @return An Array of all VNF packages. Object: InlineResponse2001[] Response Code: 200 OK
 
  66     @GetMapping(value = "/vnf_packages", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 
  67     public ResponseEntity<?> getVnfPackages() {
 
  68         logger.info(LOG_REQUEST_RECEIVED, "getVnfPackages.");
 
  69         final Optional<InlineResponse2001[]> response = etsiCatalogServiceProvider.getVnfPackages();
 
  70         if (response.isPresent()) {
 
  71             logger.info(LOG_REQUEST_RECEIVED, "getVnfPackages Response: ", HttpStatus.OK);
 
  72             return ResponseEntity.ok().body(response.get());
 
  74         final String errorMessage = "An error occurred, a null response was received by the\n"
 
  75                 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnf_packages\" \n"
 
  77         logger.error(errorMessage);
 
  78         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ProblemDetails().detail(errorMessage));
 
  82      * GET VNF package information. Will return a specific VNF package representation that match the attribute filter.
 
  83      * Section Number: 10.4.3
 
  85      * @param vnfPkgId The ID of the VNF Package that you want to query.
 
  86      * @return A VNF package based on vnfPkgId. Object: VnfPkgInfo Response Code: 200 OK
 
  88     @GetMapping(value = "/vnf_packages/{vnfPkgId}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 
  89     public ResponseEntity<?> getVnfPackage(@PathVariable("vnfPkgId") final String vnfPkgId) {
 
  90         logger.info(LOG_REQUEST_RECEIVED, "getVnfPackage: ", vnfPkgId);
 
  91         final Optional<InlineResponse2001> response = etsiCatalogServiceProvider.getVnfPackage(vnfPkgId);
 
  92         if (response.isPresent()) {
 
  93             logger.info(LOG_REQUEST_RECEIVED, "getVnfPackage Response: ", HttpStatus.OK);
 
  94             return ResponseEntity.ok().body(response.get());
 
  96         final String errorMessage = "An error occurred, a null response was received by the\n"
 
  97                 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnf_packages\" by vnfPkgId: \""
 
  98                 + vnfPkgId + "\" \n" + "endpoint.";
 
  99         logger.error(errorMessage);
 
 100         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ProblemDetails().detail(errorMessage));
 
 104      * GET VNFD, from VNF package. Will return a copy of the file representing the VNFD or a ZIP file that contains the
 
 105      * file/multiple files representing the VNFD specified. Section Number: 10.4.4
 
 107      * @param vnfPkgId The ID of the VNF Package that you want to retrieve the VNFD from.
 
 108      * @return The VNFD of a VNF Package as a single file or within a ZIP file. Object: byte[] Response Code: 200 OK
 
 110     @GetMapping(value = "/vnf_packages/{vnfPkgId}/vnfd",
 
 111             produces = {MediaType.TEXT_PLAIN, APPLICATION_ZIP, MediaType.APPLICATION_JSON})
 
 112     public ResponseEntity<?> getVnfPackageVnfd(@PathVariable("vnfPkgId") final String vnfPkgId) {
 
 113         logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageVnfd Endpoint Invoked with VNF Package ID: ", vnfPkgId);
 
 114         final Optional<byte[]> response = etsiCatalogServiceProvider.getVnfPackageVnfd(vnfPkgId);
 
 115         if (response.isPresent()) {
 
 116             logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageVnfd Response: ", HttpStatus.OK);
 
 117             return new ResponseEntity<>(response.get(), HttpStatus.OK);
 
 119         final String errorMessage = "An error occurred, a null response was received by the\n"
 
 120                 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnfd\" \n"
 
 123         logger.error(errorMessage);
 
 124         return new ResponseEntity<>(new ProblemDetails().detail(errorMessage), HttpStatus.INTERNAL_SERVER_ERROR);
 
 128      * GET Package Content, from VNF Package. Will return a copy of the VNF package file that you specified. Section
 
 131      * @param vnfPkgId The ID of the VNF Package that you want to retrieve the "package_content" from.
 
 132      * @return The Package Content of a VNF Package. Object: byte[] Response Code: 200 OK
 
 134     @GetMapping(value = "/vnf_packages/{vnfPkgId}/package_content",
 
 135             produces = {MediaType.APPLICATION_JSON, APPLICATION_ZIP, MediaType.APPLICATION_OCTET_STREAM})
 
 136     public ResponseEntity<?> getVnfPackageContent(@PathVariable("vnfPkgId") final String vnfPkgId) {
 
 137         logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageContent Endpoint Invoked with VNF Package ID: ", vnfPkgId);
 
 138         final Optional<byte[]> response = etsiCatalogServiceProvider.getVnfPackageContent(vnfPkgId);
 
 139         if (response.isPresent()) {
 
 140             logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageContent Response: ", HttpStatus.OK);
 
 141             return ResponseEntity.ok().body(response.get());
 
 143         final String errorMessage = "An error occurred, a null response was received by the\n"
 
 144                 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"package_content\" \n"
 
 146         logger.error(errorMessage);
 
 147         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ProblemDetails().detail(errorMessage));
 
 151      * GET Artifact, from VNF Package Will return a the content of the artifact that you specified. Section Number:
 
 154      * @param vnfPkgId The ID of the VNF Package that you want to retrieve an artifact from.
 
 155      * @param artifactPath The path of the artifact that you want to retrieve.
 
 156      * @return An Artifact from a VNF Package. Object: byte[] Response Code: 200 OK
 
 158     @GetMapping(value = "/vnf_packages/{vnfPkgId}/artifacts/{artifactPath}",
 
 159             produces = {MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_JSON})
 
 160     public ResponseEntity<?> getVnfPackageArtifact(@PathVariable("vnfPkgId") final String vnfPkgId,
 
 161             @PathVariable("artifactPath") final String artifactPath) {
 
 162         logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageArtifact: vnfPkgId= ", vnfPkgId, " artifactPath=",
 
 164         final Optional<byte[]> response = etsiCatalogServiceProvider.getVnfPackageArtifact(vnfPkgId, artifactPath);
 
 165         if (response.isPresent()) {
 
 166             logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageArtifact Response: ", HttpStatus.OK);
 
 167             return ResponseEntity.ok().body(response.get());
 
 169         final String errorMessage = "An error occurred, a null response was received by the\n"
 
 170                 + " Sol003PackageManagementController from the EtsiCatalogManager using the\n GET \"vnf_packages\" by vnfPkgId: \""
 
 171                 + vnfPkgId + "\" for artifactPath: \"" + artifactPath + "\"\n" + "endpoint.";
 
 172         logger.error(errorMessage);
 
 173         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ProblemDetails().detail(errorMessage));