2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.adapters.vnf;
23 import java.io.ByteArrayOutputStream;
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.OutputStream;
29 import java.io.PrintStream;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.zip.ZipEntry;
33 import java.util.zip.ZipOutputStream;
35 import org.onap.so.adapters.vdu.VduArtifact;
36 import org.onap.so.adapters.vdu.VduArtifact.ArtifactType;
37 import org.onap.so.adapters.vdu.VduModelInfo;
38 import org.onap.so.adapters.vnf.exceptions.VnfException;
40 import com.google.common.io.Files;
43 * The purpose of this class is to create a CSAR byte array from Vdu inputs for the purpose
44 * of forwarding to a TOSCA orchestrator.
50 private static final String MANIFEST_FILENAME = "MANIFEST.MF";
51 private VduModelInfo vduModel;
53 public CSAR(VduModelInfo model){
54 this.vduModel = model;
58 * Creates a byte array representation of a CSAR corresponding to the VduBlueprint arg in the
62 * @throws VnfException
64 public byte[] create() {
65 File dir = Files.createTempDir();
70 File metadir = new File(dir.getAbsolutePath() + "/TOSCA-Metadata");
71 if (!metadir.mkdir()) {
72 throw new RuntimeException("CSAR TOSCA-Metadata directory create failed");
76 * Organize model info for consumption
78 VduArtifact mainTemplate = null;
79 List<VduArtifact> extraFiles = new ArrayList<>();
80 for(VduArtifact artifact: vduModel.getArtifacts()) {
81 if(artifact.getType() == ArtifactType.MAIN_TEMPLATE ) {
82 mainTemplate = artifact;
84 extraFiles.add(artifact);
88 if (mainTemplate == null) { // make a dummy to avoid null pointers
89 mainTemplate = new VduArtifact("", new byte[0], null);
93 * Write template files
95 try (OutputStream ofs = new FileOutputStream(new File(dir, mainTemplate.getName()));
96 PrintStream mfstream = new PrintStream(new File(metadir.getAbsolutePath() + '/' + MANIFEST_FILENAME));
98 ofs.write(mainTemplate.getContent());
103 if (!extraFiles.isEmpty()) {
104 for (VduArtifact artifact: extraFiles){
105 try (OutputStream out = new FileOutputStream(new File(dir, artifact.getName()));) {
106 out.write(artifact.getContent());
115 mfstream.println("TOSCA-Meta-File-Version: 1.0");
116 mfstream.println("CSAR-Version: 1.1");
117 mfstream.println("Created-by: ONAP");
118 mfstream.println("Entry-Definitions: " + mainTemplate.getName());
123 ByteArrayOutputStream zipbytes = new ByteArrayOutputStream();
124 ZipOutputStream zos = new ZipOutputStream(zipbytes);
125 compressTree(zos, "", dir, dir);
127 return zipbytes.toByteArray();
129 } catch (Exception e) {
130 throw new RuntimeException("Failed to create CSAR: " + e.getMessage());
135 deleteDirectory(dir);
144 * Compresses (ZIPs) a directory tree
147 * @throws IOException
149 private void compressTree(ZipOutputStream zos, String path, File basedir, File dir) throws IOException {
150 if (!dir.isDirectory())
153 for (File f : dir.listFiles()) {
154 if (f.isDirectory()) {
155 String newpath = path + f.getName() + '/';
156 ZipEntry entry = new ZipEntry(newpath);
157 zos.putNextEntry(entry);
159 compressTree(zos, newpath, basedir, f);
161 ZipEntry ze = new ZipEntry(
162 f.getAbsolutePath().substring(basedir.getAbsolutePath().length() + 1).replaceAll("\\\\", "/"));
163 zos.putNextEntry(ze);
164 // read the file and write to ZipOutputStream
165 try (FileInputStream fis = new FileInputStream(f);) {
166 byte[] buffer = new byte[1024];
168 while ((len = fis.read(buffer)) > 0) {
169 zos.write(buffer, 0, len);
177 private boolean deleteDirectory(File directory) {
178 if (directory.exists()) {
179 File[] files = directory.listFiles();
181 for (int i = 0; i < files.length; i++) {
182 if (files[i].isDirectory()) {
183 deleteDirectory(files[i]);
190 return (directory.delete());