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