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.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;
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;
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;
43 * Responsible for providing the converter utilities for CBAM package format
46 @RequestMapping(value = BASE_URL)
47 public class ConverterApi {
48 private static Logger logger = getLogger(ConverterApi.class);
49 private OnapVnfPackageBuilder vnfPackageConverter = new OnapVnfPackageBuilder();
52 * Return the converted ONAP package
54 * @param httpResponse the HTTP response
55 * @return the converted ONAP package
57 @RequestMapping(value = "/convert", method = POST)
59 public void convert(HttpServletResponse httpResponse, HttpServletRequest request) throws IOException {
60 logger.info("REST: convert package");
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);
68 byte[] convertedPackage;
70 convertedPackage = vnfPackageConverter.covert(new ByteArrayInputStream(content));
71 } catch (Exception e) {
72 throw buildFatalFailure(logger, "Unable to convert VNF package", e);
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();
83 * Return the HTTP page to upload the package
84 * Can be removed after the generated swagger API in ONAP is fixed.
86 * @param httpResponse the HTTP response
88 @RequestMapping(value = "/convert", method = GET, produces = TEXT_HTML_VALUE)
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);