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