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 OnapR1VnfPackageBuilder {
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 OnapR1VnfPackageBuilder().covert(systemFunctions().in(), SupportedOnapPackageVersions.V1);
45 systemFunctions().out().write(covert);
49 * @param zip the original CBAM package
51 * @return the converted ONAP package
53 public byte[] covert(InputStream zip, SupportedOnapPackageVersions version) 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 = SupportedOnapPackageVersions.V2 == version ?
59 new OnapR2VnfdBuilder().toOnapVnfd(new String(cbamVnfdContent, StandardCharsets.UTF_8)) :
60 new OnapVnfdBuilder().toOnapVnfd(new String(cbamVnfdContent, StandardCharsets.UTF_8));
61 byte[] modifiedCbamPackage = new CbamVnfPackageBuilder().toModifiedCbamVnfPackage(cbamVnfPackage, vnfdLocation, new CbamVnfdBuilder().build(new String(cbamVnfdContent)));
62 return buildNewOnapPackage(modifiedCbamPackage, onapVnfd);
65 private byte[] buildNewOnapPackage(byte[] modifiedCbamPackage, String onapVnfd) throws IOException {
66 ByteArrayOutputStream result = new ByteArrayOutputStream();
67 ZipOutputStream out = new ZipOutputStream(result);
68 out.putNextEntry(new ZipEntry("Artifacts/Deployment/OTHER/cbam.package.zip"));
69 out.write(modifiedCbamPackage);
71 out.putNextEntry(new ZipEntry("TOSCA-Metadata/TOSCA.meta"));
72 out.write(systemFunctions().loadFile("TOSCA.meta"));
74 out.putNextEntry(new ZipEntry("MainServiceTemplate.yaml"));
75 out.write(onapVnfd.getBytes());
78 out.putNextEntry(new ZipEntry("MainServiceTemplate.mf"));
79 out.write(systemFunctions().loadFile("MainServiceTemplate.mf"));
82 return result.toByteArray();