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.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer;
19 import com.google.common.io.ByteStreams;
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.nio.charset.StandardCharsets;
26 import java.util.zip.ZipEntry;
27 import java.util.zip.ZipOutputStream;
29 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
30 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip;
31 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getVnfdLocation;
34 * Transforms a CBAM package into an ONAP package
37 public class OnapVnfPackageBuilder {
40 * Entry point for the command line package transformer
42 * @param args not used (required due to signature)
44 public static void main(String[] args) throws Exception {
45 byte[] covert = new OnapVnfPackageBuilder().covert(systemFunctions().in());
46 systemFunctions().out().write(covert);
50 * @param zip the original CBAM package
51 * @return the converted ONAP package
53 public byte[] covert(InputStream zip) throws IOException {
54 byte[] cbamVnfPackage = ByteStreams.toByteArray(zip);
55 String vnfdLocation = getVnfdLocation(new ByteArrayInputStream(cbamVnfPackage));
56 ByteArrayOutputStream vnfdContent = getFileInZip(new ByteArrayInputStream(cbamVnfPackage), vnfdLocation);
57 byte[] cbamVnfdContent = vnfdContent.toByteArray();
58 String onapVnfd = new OnapVnfdBuilder().toOnapVnfd(new String(cbamVnfdContent, StandardCharsets.UTF_8));
59 byte[] modifiedCbamPackage = new CbamVnfPackageBuilder().toModifiedCbamVnfPackage(cbamVnfPackage, vnfdLocation, new CbamVnfdBuilder().build(new String(cbamVnfdContent)));
60 return buildNewOnapPackage(modifiedCbamPackage, onapVnfd);
63 private byte[] buildNewOnapPackage(byte[] modifiedCbamPackage, String onapVnfd) throws IOException {
64 ByteArrayOutputStream result = new ByteArrayOutputStream();
65 ZipOutputStream out = new ZipOutputStream(result);
66 out.putNextEntry(new ZipEntry("Artifacts/Deployment/OTHER/cbam.package.zip"));
67 out.write(modifiedCbamPackage);
69 out.putNextEntry(new ZipEntry("TOSCA-Metadata/TOSCA.meta"));
70 out.write(systemFunctions().loadFile("TOSCA.meta"));
72 out.putNextEntry(new ZipEntry("MainServiceTemplate.yaml"));
73 out.write(onapVnfd.getBytes());
75 out.putNextEntry(new ZipEntry("Definitions/MainServiceTemplate.yaml"));
76 out.write(onapVnfd.getBytes());
78 out.putNextEntry(new ZipEntry("MainServiceTemplate.meta"));
79 out.write(systemFunctions().loadFile("MainServiceTemplate.meta"));
82 return result.toByteArray();