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;
34 import org.onap.so.adapters.vdu.VduArtifact;
35 import org.onap.so.adapters.vdu.VduArtifact.ArtifactType;
36 import org.onap.so.adapters.vdu.VduModelInfo;
37 import org.onap.so.adapters.vnf.exceptions.VnfException;
38 import com.google.common.io.Files;
41 * The purpose of this class is to create a CSAR byte array from Vdu inputs for the purpose of forwarding to a TOSCA
48 private static final String MANIFEST_FILENAME = "MANIFEST.MF";
49 private VduModelInfo vduModel;
51 public CSAR(VduModelInfo model) {
52 this.vduModel = model;
56 * Creates a byte array representation of a CSAR corresponding to the VduBlueprint arg in the constructor.
59 * @throws VnfException
61 public byte[] create() {
62 File dir = Files.createTempDir();
67 File metadir = new File(dir.getAbsolutePath() + "/TOSCA-Metadata");
68 if (!metadir.mkdir()) {
69 throw new RuntimeException("CSAR TOSCA-Metadata directory create failed");
73 * Organize model info for consumption
75 VduArtifact mainTemplate = null;
76 List<VduArtifact> extraFiles = new ArrayList<>();
77 for (VduArtifact artifact : vduModel.getArtifacts()) {
78 if (artifact.getType() == ArtifactType.MAIN_TEMPLATE) {
79 mainTemplate = artifact;
81 extraFiles.add(artifact);
85 if (mainTemplate == null) { // make a dummy to avoid null pointers
86 mainTemplate = new VduArtifact("", new byte[0], null);
90 * Write template files
92 try (OutputStream ofs = new FileOutputStream(new File(dir, mainTemplate.getName()));
93 PrintStream mfstream =
94 new PrintStream(new File(metadir.getAbsolutePath() + '/' + MANIFEST_FILENAME));) {
95 ofs.write(mainTemplate.getContent());
100 if (!extraFiles.isEmpty()) {
101 for (VduArtifact artifact : extraFiles) {
102 try (OutputStream out = new FileOutputStream(new File(dir, artifact.getName()));) {
103 out.write(artifact.getContent());
112 mfstream.println("TOSCA-Meta-File-Version: 1.0");
113 mfstream.println("CSAR-Version: 1.1");
114 mfstream.println("Created-by: ONAP");
115 mfstream.println("Entry-Definitions: " + mainTemplate.getName());
120 ByteArrayOutputStream zipbytes = new ByteArrayOutputStream();
121 ZipOutputStream zos = new ZipOutputStream(zipbytes);
122 compressTree(zos, "", dir, dir);
124 return zipbytes.toByteArray();
126 } catch (Exception e) {
127 throw new RuntimeException("Failed to create CSAR: " + e.getMessage());
132 deleteDirectory(dir);
141 * Compresses (ZIPs) a directory tree
144 * @throws IOException
146 private void compressTree(ZipOutputStream zos, String path, File basedir, File dir) throws IOException {
147 if (!dir.isDirectory())
150 for (File f : dir.listFiles()) {
151 if (f.isDirectory()) {
152 String newpath = path + f.getName() + '/';
153 ZipEntry entry = new ZipEntry(newpath);
154 zos.putNextEntry(entry);
156 compressTree(zos, newpath, basedir, f);
158 ZipEntry ze = new ZipEntry(
159 f.getAbsolutePath().substring(basedir.getAbsolutePath().length() + 1).replaceAll("\\\\", "/"));
160 zos.putNextEntry(ze);
161 // read the file and write to ZipOutputStream
162 try (FileInputStream fis = new FileInputStream(f);) {
163 byte[] buffer = new byte[1024];
165 while ((len = fis.read(buffer)) > 0) {
166 zos.write(buffer, 0, len);
174 private boolean deleteDirectory(File directory) {
175 if (directory.exists()) {
176 File[] files = directory.listFiles();
178 for (int i = 0; i < files.length; i++) {
179 if (files[i].isDirectory()) {
180 deleteDirectory(files[i]);
187 return (directory.delete());