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;
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(), SupportedOnapPackageVersions.V1TOSCA);
46 systemFunctions().out().write(covert);
50 * @param zip the original CBAM package
52 * @return the converted ONAP package
54 public byte[] covert(InputStream zip, SupportedOnapPackageVersions version) throws IOException {
55 byte[] cbamVnfPackage = ByteStreams.toByteArray(zip);
56 String vnfdLocation = getVnfdLocation(new ByteArrayInputStream(cbamVnfPackage));
57 ByteArrayOutputStream vnfdContent = getFileInZip(new ByteArrayInputStream(cbamVnfPackage), vnfdLocation);
58 byte[] cbamVnfdContent = vnfdContent.toByteArray();
59 byte[] modifiedCbamPackage = new CbamVnfPackageBuilder().toModifiedCbamVnfPackage(cbamVnfPackage, vnfdLocation, new CbamVnfdBuilder().build(new String(cbamVnfdContent)));
63 String onapVnfd = SupportedOnapPackageVersions.V2TOSCA == version ?
64 new OnapR2VnfdBuilder().toOnapVnfd(new String(cbamVnfdContent, StandardCharsets.UTF_8)) :
65 new OnapR1VnfdBuilder().toOnapVnfd(new String(cbamVnfdContent, StandardCharsets.UTF_8));
66 return buildNewOnapPackage(modifiedCbamPackage, onapVnfd);
69 Map<String, String> files = new OnapR2HeatPackageBuilder().processVnfd(new String(cbamVnfdContent));
70 return buildHeatOnapPackage(modifiedCbamPackage, files);
75 private byte [] buildHeatOnapPackage(byte [] modifiedCbamPackage, Map<String, String> heatFiles) throws IOException {
76 ByteArrayOutputStream result = new ByteArrayOutputStream();
77 ZipOutputStream out = new ZipOutputStream(result);
78 out.putNextEntry(new ZipEntry("Artifacts/Deployment/OTHER/cbam.package.zip"));
79 out.write(modifiedCbamPackage);
81 for (Map.Entry<String, String> file : heatFiles.entrySet()) {
82 out.putNextEntry(new ZipEntry(file.getKey()));
83 out.write(file.getValue().getBytes());
87 return result.toByteArray();
90 private byte[] buildNewOnapPackage(byte[] modifiedCbamPackage, String onapVnfd) throws IOException {
91 ByteArrayOutputStream result = new ByteArrayOutputStream();
92 ZipOutputStream out = new ZipOutputStream(result);
93 out.putNextEntry(new ZipEntry("Artifacts/Deployment/OTHER/cbam.package.zip"));
94 out.write(modifiedCbamPackage);
96 out.putNextEntry(new ZipEntry("TOSCA-Metadata/TOSCA.meta"));
97 out.write(systemFunctions().loadFile("TOSCA.meta"));
99 out.putNextEntry(new ZipEntry("MainServiceTemplate.yaml"));
100 out.write(onapVnfd.getBytes());
103 out.putNextEntry(new ZipEntry("MainServiceTemplate.mf"));
104 out.write(systemFunctions().loadFile("MainServiceTemplate.mf"));
107 return result.toByteArray();