2 * Copyright 2016-2017, Nokia Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.restapi;
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;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.servlet.http.Part;
31 import java.io.IOException;
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;
46 * Responsible for providing the converter utilities for CBAM package format
49 @RequestMapping(value = BASE_URL)
50 public class ConverterApi {
51 private static Logger logger = getLogger(ConverterApi.class);
52 private OnapVnfPackageBuilder vnfPackageConverter = new OnapVnfPackageBuilder();
55 * Return the converted ONAP package
57 * @param httpResponse the HTTP response
58 * @return the converted ONAP package
60 @RequestMapping(value = "/convert", method = POST)
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();
75 * Return the HTTP page to upload the package
76 * Can be removed after the generated swagger API in ONAP is fixed.
78 * @param httpResponse the HTTP response
80 @RequestMapping(value = "/convert", method = GET, produces = TEXT_HTML_VALUE)
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);