aboutsummaryrefslogtreecommitdiffstats
path: root/so-etsi-sol003-adapter-pkgm/so-etsi-sol003-adapter-pkgm-service/src/main/java/org/onap/so/adapters/etsisol003adapter/pkgm/rest/Sol003PackageManagementController.java
blob: 6c1e5e5506e830bdc62ffbbbe488345c63d00b3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.so.adapters.etsisol003adapter.pkgm.rest;

import static org.onap.so.adapters.etsi.sol003.adapter.common.CommonConstants.PACKAGE_MANAGEMENT_BASE_URL;
import static org.onap.so.adapters.etsisol003adapter.pkgm.PackageManagementConstants.APPLICATION_ZIP;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.Optional;
import javax.ws.rs.core.MediaType;
import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.EtsiCatalogServiceProvider;
import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.model.ProblemDetails;
import org.onap.so.adapters.etsisol003adapter.pkgm.model.InlineResponse2001;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Controller for handling the VNF Package Management. For further information please read:
 * https://www.etsi.org/deliver/etsi_gs/NFV-SOL/001_099/003/02.05.01_60/gs_nfv-sol003v020501p.pdf Use the section number
 * above each endpoint to find the corresponding section in the above document.
 *
 * @author gareth.roper@est.tech
 */
@Controller
@RequestMapping(value = PACKAGE_MANAGEMENT_BASE_URL, consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public class Sol003PackageManagementController {

    private final EtsiCatalogServiceProvider etsiCatalogServiceProvider;
    private static final String LOG_REQUEST_RECEIVED = "VNF PackageManagement Controller: {} {} {} {}";
    private static final Logger logger = getLogger(Sol003PackageManagementController.class);

    @Autowired
    Sol003PackageManagementController(final EtsiCatalogServiceProvider etsiCatalogServiceProvider) {
        this.etsiCatalogServiceProvider = etsiCatalogServiceProvider;
    }

    /**
     * GET VNF packages information. Will return zero or more VNF package representations that match the attribute
     * filter. These representations will be in a list. Section Number: 10.4.2
     * 
     * @return An Array of all VNF packages. Object: InlineResponse2001[] Response Code: 200 OK
     */
    @GetMapping(value = "/vnf_packages", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public ResponseEntity<?> getVnfPackages() {
        logger.info(LOG_REQUEST_RECEIVED, "getVnfPackages.");
        final Optional<InlineResponse2001[]> response = etsiCatalogServiceProvider.getVnfPackages();
        if (response.isPresent()) {
            logger.info(LOG_REQUEST_RECEIVED, "getVnfPackages Response: ", HttpStatus.OK);
            return ResponseEntity.ok().body(response.get());
        }
        final String errorMessage = "An error occurred, a null response was received by the\n"
                + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnf_packages\" \n"
                + "endpoint.";
        logger.error(errorMessage);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ProblemDetails().detail(errorMessage));
    }

    /**
     * GET VNF package information. Will return a specific VNF package representation that match the attribute filter.
     * Section Number: 10.4.3
     *
     * @param vnfPkgId The ID of the VNF Package that you want to query.
     * @return A VNF package based on vnfPkgId. Object: VnfPkgInfo Response Code: 200 OK
     */
    @GetMapping(value = "/vnf_packages/{vnfPkgId}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public ResponseEntity<?> getVnfPackage(@PathVariable("vnfPkgId") final String vnfPkgId) {
        logger.info(LOG_REQUEST_RECEIVED, "getVnfPackage: ", vnfPkgId);
        final Optional<InlineResponse2001> response = etsiCatalogServiceProvider.getVnfPackage(vnfPkgId);
        if (response.isPresent()) {
            logger.info(LOG_REQUEST_RECEIVED, "getVnfPackage Response: ", HttpStatus.OK);
            return ResponseEntity.ok().body(response.get());
        }
        final String errorMessage = "An error occurred, a null response was received by the\n"
                + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnf_packages\" by vnfPkgId: \""
                + vnfPkgId + "\" \n" + "endpoint.";
        logger.error(errorMessage);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ProblemDetails().detail(errorMessage));
    }

    /**
     * GET VNFD, from VNF package. Will return a copy of the file representing the VNFD or a ZIP file that contains the
     * file/multiple files representing the VNFD specified. Section Number: 10.4.4
     *
     * @param vnfPkgId The ID of the VNF Package that you want to retrieve the VNFD from.
     * @return The VNFD of a VNF Package as a single file or within a ZIP file. Object: byte[] Response Code: 200 OK
     */
    @GetMapping(value = "/vnf_packages/{vnfPkgId}/vnfd",
            produces = {MediaType.TEXT_PLAIN, APPLICATION_ZIP, MediaType.APPLICATION_JSON})
    public ResponseEntity<?> getVnfPackageVnfd(@PathVariable("vnfPkgId") final String vnfPkgId) {
        logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageVnfd Endpoint Invoked with VNF Package ID: ", vnfPkgId);
        final Optional<byte[]> response = etsiCatalogServiceProvider.getVnfPackageVnfd(vnfPkgId);
        if (response.isPresent()) {
            logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageVnfd Response: ", HttpStatus.OK);
            return new ResponseEntity<>(response.get(), HttpStatus.OK);
        }
        final String errorMessage = "An error occurred, a null response was received by the\n"
                + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnfd\" \n"
                + "endpoint.";

        logger.error(errorMessage);
        return new ResponseEntity<>(new ProblemDetails().detail(errorMessage), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * GET Package Content, from VNF Package. Will return a copy of the VNF package file that you specified. Section
     * Number: 10.4.5
     * 
     * @param vnfPkgId The ID of the VNF Package that you want to retrieve the "package_content" from.
     * @return The Package Content of a VNF Package. Object: byte[] Response Code: 200 OK
     */
    @GetMapping(value = "/vnf_packages/{vnfPkgId}/package_content",
            produces = {MediaType.APPLICATION_JSON, APPLICATION_ZIP, MediaType.APPLICATION_OCTET_STREAM})
    public ResponseEntity<?> getVnfPackageContent(@PathVariable("vnfPkgId") final String vnfPkgId) {
        logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageContent Endpoint Invoked with VNF Package ID: ", vnfPkgId);
        final Optional<byte[]> response = etsiCatalogServiceProvider.getVnfPackageContent(vnfPkgId);
        if (response.isPresent()) {
            logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageContent Response: ", HttpStatus.OK);
            return ResponseEntity.ok().body(response.get());
        }
        final String errorMessage = "An error occurred, a null response was received by the\n"
                + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"package_content\" \n"
                + "endpoint.";
        logger.error(errorMessage);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ProblemDetails().detail(errorMessage));
    }

    /**
     * GET Artifact, from VNF Package Will return a the content of the artifact that you specified. Section Number:
     * 10.4.6
     * 
     * @param vnfPkgId The ID of the VNF Package that you want to retrieve an artifact from.
     * @param artifactPath The path of the artifact that you want to retrieve.
     * @return An Artifact from a VNF Package. Object: byte[] Response Code: 200 OK
     */
    @GetMapping(value = "/vnf_packages/{vnfPkgId}/artifacts/{artifactPath}",
            produces = {MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_JSON})
    public ResponseEntity<?> getVnfPackageArtifact(@PathVariable("vnfPkgId") final String vnfPkgId,
            @PathVariable("artifactPath") final String artifactPath) {
        logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageArtifact: vnfPkgId= ", vnfPkgId, " artifactPath=",
                artifactPath);
        final Optional<byte[]> response = etsiCatalogServiceProvider.getVnfPackageArtifact(vnfPkgId, artifactPath);
        if (response.isPresent()) {
            logger.info(LOG_REQUEST_RECEIVED, "getVnfPackageArtifact Response: ", HttpStatus.OK);
            return ResponseEntity.ok().body(response.get());
        }
        final String errorMessage = "An error occurred, a null response was received by the\n"
                + " Sol003PackageManagementController from the EtsiCatalogManager using the\n GET \"vnf_packages\" by vnfPkgId: \""
                + vnfPkgId + "\" for artifactPath: \"" + artifactPath + "\"\n" + "endpoint.";
        logger.error(errorMessage);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ProblemDetails().detail(errorMessage));
    }

}