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