Add SO APIs to Nokia VNFM adapter
[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.Map;
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(), SupportedOnapPackageVersions.V1TOSCA);
46         systemFunctions().out().write(covert);
47     }
48
49     /**
50      * @param zip     the original CBAM package
51      * @param version
52      * @return the converted ONAP package
53      */
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)));
60         switch (version){
61             case V1TOSCA:
62             case V2TOSCA:
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);
67             case V2HEAT:
68             default:
69                 Map<String, String> files = new OnapR2HeatPackageBuilder().processVnfd(new String(cbamVnfdContent));
70                 return buildHeatOnapPackage(modifiedCbamPackage, files);
71         }
72
73     }
74
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);
80         out.closeEntry();
81         for (Map.Entry<String, String> file : heatFiles.entrySet()) {
82             out.putNextEntry(new ZipEntry(file.getKey()));
83             out.write(file.getValue().getBytes());
84             out.closeEntry();
85         }
86         out.close();
87         return result.toByteArray();
88     }
89
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);
95         out.closeEntry();
96         out.putNextEntry(new ZipEntry("TOSCA-Metadata/TOSCA.meta"));
97         out.write(systemFunctions().loadFile("TOSCA.meta"));
98         out.closeEntry();
99         out.putNextEntry(new ZipEntry("MainServiceTemplate.yaml"));
100         out.write(onapVnfd.getBytes());
101         out.closeEntry();
102         out.closeEntry();
103         out.putNextEntry(new ZipEntry("MainServiceTemplate.mf"));
104         out.write(systemFunctions().loadFile("MainServiceTemplate.mf"));
105         out.closeEntry();
106         out.close();
107         return result.toByteArray();
108     }
109 }