2 * ============LICENSE_START===================================================
3 * Copyright (c) 2017 Cloudify.co. All rights reserved.
4 * ===================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6 * use this file except in compliance with the License. You may obtain a copy
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
16 * ============LICENSE_END====================================================
18 package org.openecomp.mso.aria;
20 import java.io.ByteArrayOutputStream;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.OutputStream;
26 import java.io.PrintStream;
28 import java.util.List;
29 import java.util.zip.ZipEntry;
30 import java.util.zip.ZipOutputStream;
32 import org.openecomp.mso.adapters.vdu.VduModelInfo;
33 import org.openecomp.mso.adapters.vdu.VduArtifact;
34 import org.openecomp.mso.adapters.vdu.VduArtifact.ArtifactType;
36 import com.google.common.io.Files;
39 * The purpose of this class is to create a CSAR byte array from Vdu inputs for the purpose
40 * of forwarding to a TOSCA orchestrator.
46 private static final String MANIFEST_FILENAME = "MANIFEST.MF";
47 private VduModelInfo vduModel;
49 public CSAR(VduModelInfo model){
50 this.vduModel = model;
54 * Creates a byte array representation of a CSAR corresponding to the VduBlueprint arg in the
59 public byte[] create() {
60 File dir = Files.createTempDir();
65 File metadir = new File(dir.getAbsolutePath() + "/" + "TOSCA-Metadata");
66 if (!metadir.mkdir()) {
67 throw new RuntimeException("CSAR TOSCA-Metadata directory create failed");
71 * Organize model info for consumption
73 VduArtifact mainTemplate = null;
74 List<VduArtifact> extraFiles = null;
75 for(VduArtifact artifact: vduModel.getArtifacts()){
76 if( artifact.getType() == ArtifactType.MAIN_TEMPLATE ){
77 mainTemplate = artifact;
80 extraFiles.add(artifact);
85 * Write template files
87 OutputStream ofs = null;
89 ofs = new FileOutputStream(new File(dir, mainTemplate.getName()));
90 ofs.write(mainTemplate.getContent());
96 if (extraFiles != null) {
97 for (VduArtifact artifact: extraFiles){
98 ofs = new FileOutputStream(new File(dir, artifact.getName()));
99 ofs.write(artifact.getContent());
108 PrintStream mfstream = new PrintStream(new File(metadir.getAbsolutePath() + "/" + MANIFEST_FILENAME));
109 mfstream.println("TOSCA-Meta-File-Version: 1.0");
110 mfstream.println("CSAR-Version: 1.1");
111 mfstream.println("Created-by: ONAP");
112 mfstream.println("Entry-Definitions: " + mainTemplate.getName());
118 ByteArrayOutputStream zipbytes = new ByteArrayOutputStream();
119 ZipOutputStream zos = new ZipOutputStream(zipbytes);
120 compressTree(zos, "", dir, dir);
122 return zipbytes.toByteArray();
124 } catch (Exception e) {
125 throw new RuntimeException("Failed to create CSAR: " + e.getMessage());
131 deleteDirectory(dir);
140 * Compresses (ZIPs) a directory tree
143 * @throws IOException
145 private void compressTree(ZipOutputStream zos, String path, File basedir, File dir) throws IOException {
146 if (!dir.isDirectory())
149 for (File f : dir.listFiles()) {
150 if (f.isDirectory()) {
151 String newpath = path + f.getName() + "/";
152 ZipEntry entry = new ZipEntry(newpath);
153 zos.putNextEntry(entry);
155 compressTree(zos, newpath, basedir, f);
157 ZipEntry ze = new ZipEntry(
158 f.getAbsolutePath().substring(basedir.getAbsolutePath().length() + 1).replaceAll("\\\\", "/"));
159 zos.putNextEntry(ze);
160 // read the file and write to ZipOutputStream
161 FileInputStream fis = new FileInputStream(f);
162 byte[] buffer = new byte[1024];
164 while ((len = fis.read(buffer)) > 0) {
165 zos.write(buffer, 0, len);
173 private boolean deleteDirectory(File directory) {
174 if (directory.exists()) {
175 File[] files = directory.listFiles();
177 for (int i = 0; i < files.length; i++) {
178 if (files[i].isDirectory()) {
179 deleteDirectory(files[i]);
186 return (directory.delete());