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(buildProblemDetails(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(buildProblemDetails(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: ", vnfPkgId);
114 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
118 * GET Package Content, from VNF Package. Will return a copy of the VNF package file that you specified. Section
121 * @param vnfPkgId The ID of the VNF Package that you want to retrieve the "package_content" from.
122 * @return The Package Content of a VNF Package. Object: byte[] Response Code: 200 OK
124 @GetMapping(value = "/vnf_packages/{vnfPkgId}/package_content",
125 produces = {MediaType.APPLICATION_JSON, APPLICATION_ZIP, MediaType.APPLICATION_OCTET_STREAM})
126 public ResponseEntity<?> getVnfPackageContent(@PathVariable("vnfPkgId") final String vnfPkgId) {
127 logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageContent Endpoint Invoked with VNF Package ID: ", vnfPkgId);
128 final Optional<byte[]> response = etsiCatalogServiceProvider.getVnfPackageContent(vnfPkgId);
129 if (response.isPresent()) {
130 logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageContent Response: ", HttpStatus.OK);
131 return ResponseEntity.ok().body(response.get());
133 final String errorMessage = "An error occurred, a null response was received by the\n"
134 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"package_content\" \n"
136 logger.error(errorMessage);
137 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(buildProblemDetails(errorMessage));
141 * GET Artifact, from VNF Package Will return a the content of the artifact that you specified. Section Number:
144 * @param vnfPkgId The ID of the VNF Package that you want to retrieve an artifact from.
145 * @param artifactPath The path of the artifact that you want to retrieve.
146 * @return An Artifact from a VNF Package. Object: byte[] Response Code: 200 OK
148 @GetMapping(value = "/vnf_packages/{vnfPkgId}/artifacts/{artifactPath}",
149 produces = {MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_JSON})
150 public ResponseEntity<?> getVnfPackageArtifact(@PathVariable("vnfPkgId") final String vnfPkgId,
151 @PathVariable("artifactPath") final String artifactPath) {
152 logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageArtifact: vnfPkgId= ", vnfPkgId, " artifactPath=",
154 final Optional<byte[]> response = etsiCatalogServiceProvider.getVnfPackageArtifact(vnfPkgId, artifactPath);
155 if (response.isPresent()) {
156 logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageArtifact Response: ", HttpStatus.OK);
157 return ResponseEntity.ok().body(response.get());
159 final String errorMessage = "An error occurred, a null response was received by the\n"
160 + " Sol003PackageManagementController from the EtsiCatalogManager using the\n GET \"vnf_packages\" by vnfPkgId: \""
161 + vnfPkgId + "\" for artifactPath: \"" + artifactPath + "\"\n" + "endpoint.";
162 logger.error(errorMessage);
163 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(buildProblemDetails(errorMessage));
167 * Builds the ProblemDetails Object, using the provided error message.
169 * @param detail The error message retrieved from the exception thrown.
170 * @return ProblemDetails Object, containing error information.
172 private ProblemDetails buildProblemDetails(final String detail) {
173 final ProblemDetails problemDetails = new ProblemDetails();
174 problemDetails.setDetail(detail);
175 return problemDetails;