Merge "Updating Nokia driver"
[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 org.apache.http.entity.ContentType;
19 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.OnapVnfPackageBuilder;
20 import org.slf4j.Logger;
21 import org.springframework.http.HttpHeaders;
22 import org.springframework.http.HttpStatus;
23 import org.springframework.stereotype.Controller;
24 import org.springframework.web.bind.annotation.RequestMapping;
25 import org.springframework.web.bind.annotation.ResponseBody;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.servlet.http.Part;
30
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.MediaType.TEXT_HTML_VALUE;
35 import static org.springframework.web.bind.annotation.RequestMethod.GET;
36 import static org.springframework.web.bind.annotation.RequestMethod.POST;
37
38 /**
39  * Responsible for providing the converter utilities for CBAM package format
40  */
41 @Controller
42 @RequestMapping(value = BASE_URL)
43 public class ConverterApi {
44     private static Logger logger = getLogger(ConverterApi.class);
45     private OnapVnfPackageBuilder vnfPackageConverter = new OnapVnfPackageBuilder();
46
47     /**
48      * Return the converted ONAP package
49      *
50      * @param httpResponse the HTTP response
51      * @return the converted ONAP package
52      */
53     @RequestMapping(value = "/convert", method = POST)
54     @ResponseBody
55     public void convert(HttpServletResponse httpResponse, HttpServletRequest request) throws Exception {
56         logger.info("REST: convert package");
57         Part part = request.getParts().iterator().next();
58         byte[] bytes = vnfPackageConverter.covert(part.getInputStream());
59         httpResponse.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_OCTET_STREAM.getMimeType());
60         httpResponse.setStatus(HttpStatus.OK.value());
61         httpResponse.addHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(bytes.length));
62         httpResponse.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + "core.csar" + "\"");
63         httpResponse.getOutputStream().write(bytes);
64         httpResponse.getOutputStream().flush();
65     }
66
67     /**
68      * Return the HTTP page to upload the package
69      * Can be removed after the generated swagger API in ONAP is fixed.
70      *
71      * @param httpResponse the HTTP response
72      */
73     @RequestMapping(value = "/convert", method = GET, produces = TEXT_HTML_VALUE)
74     @ResponseBody
75     public void getUploadPageForConvertingVnfd(HttpServletResponse httpResponse) throws Exception {
76         logger.info("REST: get converter main page");
77         byte[] bytes = systemFunctions().loadFile("upload.html");
78         httpResponse.addHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(bytes.length));
79         httpResponse.getOutputStream().write(bytes);
80     }
81 }