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