5f9f634136122fcdba368633285454247a9fc80f
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / packagetransformer / CbamVnfPackageBuilder.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.util.zip.ZipEntry;
25 import java.util.zip.ZipInputStream;
26 import java.util.zip.ZipOutputStream;
27
28 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
29
30 /**
31  * Builds a CBAM VNF package capable to be deployed on ONAP from a CBAM package
32  */
33 public class CbamVnfPackageBuilder {
34
35     /**
36      * @param originalCbamVnfPackage  the original CBAM VNF package
37      * @param vnfdLocation            the location of the VNFD within the CBAM VNF package
38      * @param modifiedCbamVnfdContent the modified CBAM VNFD content
39      * @return the mod
40      */
41     public byte[] toModifiedCbamVnfPackage(byte[] originalCbamVnfPackage, String vnfdLocation, String modifiedCbamVnfdContent) throws IOException {
42         ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(originalCbamVnfPackage));
43         ByteArrayOutputStream result = new ByteArrayOutputStream();
44         ZipOutputStream out = new ZipOutputStream(result);
45         ZipEntry zipEntry;
46         while ((zipEntry = zipInputStream.getNextEntry()) != null) {
47             if (zipEntry.getName().matches(vnfdLocation)) {
48                 out.putNextEntry(new ZipEntry(vnfdLocation));
49                 out.write(modifiedCbamVnfdContent.getBytes());
50                 out.closeEntry();
51             } else {
52                 out.putNextEntry(new ZipEntry(zipEntry.getName()));
53                 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
54                 ByteStreams.copy(zipInputStream, byteArrayOutputStream);
55                 out.write(byteArrayOutputStream.toByteArray());
56                 out.closeEntry();
57             }
58         }
59         out.putNextEntry(new ZipEntry("javascript/cbam.pre.collectConnectionPoints.js"));
60         out.write(systemFunctions().loadFile("cbam.pre.collectConnectionPoints.js"));
61         out.closeEntry();
62         out.putNextEntry(new ZipEntry("javascript/cbam.collectConnectionPoints.js"));
63         out.write(systemFunctions().loadFile("cbam.collectConnectionPoints.js"));
64         out.closeEntry();
65         out.putNextEntry(new ZipEntry("javascript/cbam.post.collectConnectionPoints.js"));
66         out.write(systemFunctions().loadFile("cbam.post.collectConnectionPoints.js"));
67         out.closeEntry();
68         out.close();
69         zipInputStream.close();
70         return result.toByteArray();
71     }
72 }