9fe4a298160bf459d6e4aaa465fed5282c1b459d
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / packagetransformer / OnapVnfPackageBuilder.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer;
18
19 import com.google.common.io.ByteStreams;
20
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;
28
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;
32
33 /**
34  * Transforms a CBAM package into an ONAP package
35  */
36
37 public class OnapVnfPackageBuilder {
38
39     /**
40      * Entry point for the command line package transformer
41      *
42      * @param args not used (required due to signature)
43      */
44     public static void main(String[] args) throws Exception {
45         byte[] covert = new OnapVnfPackageBuilder().covert(systemFunctions().in());
46         systemFunctions().out().write(covert);
47     }
48
49     /**
50      * @param zip the original CBAM package
51      * @return the converted ONAP package
52      */
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);
61     }
62
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);
68         out.closeEntry();
69         out.putNextEntry(new ZipEntry("TOSCA-Metadata/TOSCA.meta"));
70         out.write(systemFunctions().loadFile("TOSCA.meta"));
71         out.closeEntry();
72         out.putNextEntry(new ZipEntry("MainServiceTemplate.yaml"));
73         out.write(onapVnfd.getBytes());
74         out.closeEntry();
75         out.putNextEntry(new ZipEntry("Definitions/MainServiceTemplate.yaml"));
76         out.write(onapVnfd.getBytes());
77         out.closeEntry();
78         out.putNextEntry(new ZipEntry("MainServiceTemplate.meta"));
79         out.write(systemFunctions().loadFile("MainServiceTemplate.meta"));
80         out.closeEntry();
81         out.close();
82         return result.toByteArray();
83     }
84
85
86 }