Refactor SOL003 Adapter to organize its modules
[so.git] / adapters / etsi-sol003-adapter / etsi-sol003-package-management / etsi-sol003-package-management-adapter / src / main / java / org / onap / so / adapters / etsi / sol003 / adapter / packagemanagement / rest / Sol003PackageManagementController.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.rest;
22
23 import static org.onap.so.adapters.etsi.sol003.adapter.common.CommonConstants.PACKAGE_MANAGEMENT_BASE_URL;
24 import static org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.PackageManagementConstants.APPLICATION_ZIP;
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.etsi.sol003.adapter.packagemanagement.extclients.etsicatalog.model.ProblemDetails;
29 import org.onap.so.adapters.etsi.sol003.adapter.packagemanagement.extclients.etsicatalog.EtsiCatalogServiceProvider;
30 import org.onap.so.adapters.etsi.sol003.adapter.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;
39
40 /**
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.
44  *
45  * @author gareth.roper@est.tech
46  */
47 @Controller
48 @RequestMapping(value = PACKAGE_MANAGEMENT_BASE_URL, consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
49 public class Sol003PackageManagementController {
50
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);
54
55     @Autowired
56     Sol003PackageManagementController(final EtsiCatalogServiceProvider etsiCatalogServiceProvider) {
57         this.etsiCatalogServiceProvider = etsiCatalogServiceProvider;
58     }
59
60     /**
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
63      * 
64      * @return An Array of all VNF packages. Object: InlineResponse2001[] Response Code: 200 OK
65      */
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());
73         }
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"
76                 + "endpoint.";
77         logger.error(errorMessage);
78         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ProblemDetails().detail(errorMessage));
79     }
80
81     /**
82      * GET VNF package information. Will return a specific VNF package representation that match the attribute filter.
83      * Section Number: 10.4.3
84      *
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
87      */
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());
95         }
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));
101     }
102
103     /**
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
106      *
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
109      */
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);
118         }
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"
121                 + "endpoint.";
122
123         logger.error(errorMessage);
124         return new ResponseEntity<>(new ProblemDetails().detail(errorMessage), HttpStatus.INTERNAL_SERVER_ERROR);
125     }
126
127     /**
128      * GET Package Content, from VNF Package. Will return a copy of the VNF package file that you specified. Section
129      * Number: 10.4.5
130      * 
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
133      */
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());
142         }
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"
145                 + "endpoint.";
146         logger.error(errorMessage);
147         return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ProblemDetails().detail(errorMessage));
148     }
149
150     /**
151      * GET Artifact, from VNF Package Will return a the content of the artifact that you specified. Section Number:
152      * 10.4.6
153      * 
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
157      */
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=",
163                 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());
168         }
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));
174     }
175
176 }