Fix template generator for R2
[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 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;
27
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;
31
32 /**
33  * Transforms a CBAM package into an ONAP package
34  */
35
36 public class OnapVnfPackageBuilder {
37
38     /**
39      * Entry point for the command line package transformer
40      *
41      * @param args not used (required due to signature)
42      */
43     public static void main(String[] args) throws Exception {
44         byte[] covert = new OnapVnfPackageBuilder().covert(systemFunctions().in(), SupportedOnapPackageVersions.V1);
45         systemFunctions().out().write(covert);
46     }
47
48     /**
49      * @param zip     the original CBAM package
50      * @param version
51      * @return the converted ONAP package
52      */
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 OnapR1VnfdBuilder().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);
63     }
64
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);
70         out.closeEntry();
71         out.putNextEntry(new ZipEntry("TOSCA-Metadata/TOSCA.meta"));
72         out.write(systemFunctions().loadFile("TOSCA.meta"));
73         out.closeEntry();
74         out.putNextEntry(new ZipEntry("MainServiceTemplate.yaml"));
75         out.write(onapVnfd.getBytes());
76         out.closeEntry();
77         out.closeEntry();
78         out.putNextEntry(new ZipEntry("MainServiceTemplate.mf"));
79         out.write(systemFunctions().loadFile("MainServiceTemplate.mf"));
80         out.closeEntry();
81         out.close();
82         return result.toByteArray();
83     }
84 }