Removing jackson to mitigate cve-2017-4995
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / restapi / ConverterApi.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.restapi;
17
18 import com.google.common.io.ByteStreams;
19 import java.io.ByteArrayInputStream;
20 import java.io.IOException;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23 import javax.servlet.http.Part;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.OnapVnfPackageBuilder;
25 import org.slf4j.Logger;
26 import org.springframework.stereotype.Controller;
27 import org.springframework.web.bind.annotation.RequestMapping;
28 import org.springframework.web.bind.annotation.ResponseBody;
29
30 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
31 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
32 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties.BASE_URL;
33 import static org.slf4j.LoggerFactory.getLogger;
34 import static org.springframework.http.HttpHeaders.*;
35 import static org.springframework.http.HttpStatus.OK;
36 import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM;
37 import static org.springframework.http.MediaType.TEXT_HTML_VALUE;
38 import static org.springframework.web.bind.annotation.RequestMethod.GET;
39 import static org.springframework.web.bind.annotation.RequestMethod.POST;
40
41 /**
42  * Responsible for providing the converter utilities for CBAM package format
43  */
44 @Controller
45 @RequestMapping(value = BASE_URL)
46 public class ConverterApi {
47     private static Logger logger = getLogger(ConverterApi.class);
48     private OnapVnfPackageBuilder vnfPackageConverter = new OnapVnfPackageBuilder();
49
50     /**
51      * Return the converted ONAP package
52      *
53      * @param httpResponse the HTTP response
54      * @return the converted ONAP package
55      */
56     @RequestMapping(value = "/convert", method = POST)
57     @ResponseBody
58     public void convert(HttpServletResponse httpResponse, HttpServletRequest request) throws IOException {
59         logger.info("REST: convert package");
60         byte[] content;
61         try {
62             Part part = request.getParts().iterator().next();
63             content = ByteStreams.toByteArray(part.getInputStream());
64         } catch (Exception e) {
65             throw buildFatalFailure(logger, "Unable to extract package from REST parameters", e);
66         }
67         byte[] convertedPackage;
68         try {
69             convertedPackage = vnfPackageConverter.covert(new ByteArrayInputStream(content));
70         } catch (Exception e) {
71             throw buildFatalFailure(logger, "Unable to convert VNF package", e);
72         }
73         httpResponse.addHeader(CONTENT_TYPE, APPLICATION_OCTET_STREAM.toString());
74         httpResponse.setStatus(OK.value());
75         httpResponse.addHeader(CONTENT_LENGTH, Integer.toString(convertedPackage.length));
76         httpResponse.addHeader(CONTENT_DISPOSITION, "attachment; filename=\"" + "core.csar" + "\"");
77         httpResponse.getOutputStream().write(convertedPackage);
78         httpResponse.getOutputStream().flush();
79     }
80
81     /**
82      * Return the HTTP page to upload the package
83      * Can be removed after the generated swagger API in ONAP is fixed.
84      *
85      * @param httpResponse the HTTP response
86      */
87     @RequestMapping(value = "/convert", method = GET, produces = TEXT_HTML_VALUE)
88     @ResponseBody
89     public void getUploadPageForConvertingVnfd(HttpServletResponse httpResponse) throws IOException {
90         logger.info("REST: get converter main page");
91         byte[] bytes = systemFunctions().loadFile("upload.html");
92         httpResponse.addHeader(CONTENT_LENGTH, Integer.toString(bytes.length));
93         httpResponse.getOutputStream().write(bytes);
94     }
95 }