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 com.google.common.base.Charsets;
19 import com.google.common.io.ByteStreams;
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24 import javax.servlet.http.Part;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.OnapVnfPackageBuilder;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.SupportedOnapPackageVersions;
27 import org.slf4j.Logger;
28 import org.springframework.stereotype.Controller;
29 import org.springframework.web.bind.annotation.RequestMapping;
30 import org.springframework.web.bind.annotation.ResponseBody;
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.Constants.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.APPLICATION_OCTET_STREAM;
39 import static org.springframework.http.MediaType.TEXT_HTML_VALUE;
40 import static org.springframework.web.bind.annotation.RequestMethod.GET;
41 import static org.springframework.web.bind.annotation.RequestMethod.POST;
44 * Responsible for providing the converter utilities for CBAM package format
47 @RequestMapping(value = BASE_URL)
48 public class ConverterApi {
49 private static Logger logger = getLogger(ConverterApi.class);
50 private OnapVnfPackageBuilder vnfPackageConverter = new OnapVnfPackageBuilder();
53 * Return the converted ONAP package
55 * @param httpResponse the HTTP response
56 * @return the converted ONAP package
58 @RequestMapping(value = "/convert", method = POST)
60 public void convert(HttpServletResponse httpResponse, HttpServletRequest request) throws IOException {
61 logger.info("REST: convert package");
62 SupportedOnapPackageVersions version;
64 request.getPart("version");
65 version = SupportedOnapPackageVersions.valueOf(new String(ByteStreams.toByteArray(request.getPart("version").getInputStream()), Charsets.UTF_8));
66 } catch (Exception e) {
67 throw buildFatalFailure(logger, "Unable to determine the desired ONAP package version", e);
71 Part uploadedFile = request.getPart("fileToUpload");
72 content = ByteStreams.toByteArray(uploadedFile.getInputStream());
73 } catch (Exception e) {
74 throw buildFatalFailure(logger, "Unable to extract package from REST parameters", e);
76 byte[] convertedPackage;
78 convertedPackage = vnfPackageConverter.covert(new ByteArrayInputStream(content), version);
79 } catch (Exception e) {
80 throw buildFatalFailure(logger, "Unable to convert VNF package", e);
82 httpResponse.addHeader(CONTENT_TYPE, APPLICATION_OCTET_STREAM.toString());
83 httpResponse.setStatus(OK.value());
84 httpResponse.addHeader(CONTENT_LENGTH, Integer.toString(convertedPackage.length));
85 httpResponse.addHeader(CONTENT_DISPOSITION, "attachment; filename=\"" + "core.csar" + "\"");
86 httpResponse.getOutputStream().write(convertedPackage);
87 httpResponse.getOutputStream().flush();
91 * Return the HTTP page to upload the package
92 * Can be removed after the generated swagger API in ONAP is fixed.
94 * @param httpResponse the HTTP response
96 @RequestMapping(value = "/convert", method = GET, produces = TEXT_HTML_VALUE)
98 public void getUploadPageForConvertingVnfd(HttpServletResponse httpResponse) throws IOException {
99 logger.info("REST: get converter main page");
100 byte[] bytes = systemFunctions().loadFile("upload.html");
101 httpResponse.addHeader(CONTENT_LENGTH, Integer.toString(bytes.length));
102 httpResponse.getOutputStream().write(bytes);