Merge "Add resource sequence if config file not defined"
[so.git] / adapters / mso-adapter-utils / src / main / java / org / openecomp / mso / aria / CSAR.java
1 /*
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
7  * of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
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
15  * the License.
16  * ============LICENSE_END====================================================
17 */
18 package org.openecomp.mso.aria;
19
20 import java.io.ByteArrayOutputStream;
21 import java.io.File;
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;
27 import java.util.Map;
28 import java.util.List;
29 import java.util.zip.ZipEntry;
30 import java.util.zip.ZipOutputStream;
31
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;
35
36 import com.google.common.io.Files;
37
38 /**
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.
41  * 
42  * @author DeWayne
43  *
44  */
45 public class CSAR {
46         private static final String MANIFEST_FILENAME = "MANIFEST.MF";
47         private VduModelInfo vduModel;
48
49         public CSAR(VduModelInfo model){
50            this.vduModel = model;
51         }
52
53         /**
54          * Creates a byte array representation of a CSAR corresponding to the VduBlueprint arg in the
55          * constructor.  
56          * 
57          * @return
58          */
59         public byte[] create() {
60                 File dir = Files.createTempDir();
61
62                 /**
63                  * Create subdir
64                  */
65                 File metadir = new File(dir.getAbsolutePath() + "/" + "TOSCA-Metadata");
66                 if (!metadir.mkdir()) {
67                         throw new RuntimeException("CSAR TOSCA-Metadata directory create failed");
68                 }
69
70                 /**
71                  * Organize model info for consumption
72                  */
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;
78                                   }
79                                   else{
80                                     extraFiles.add(artifact);
81                                   }
82                 }         
83
84                 /**
85                  * Write template files
86                  */
87                 OutputStream ofs = null;
88                 try {
89                         ofs = new FileOutputStream(new File(dir, mainTemplate.getName()));
90                         ofs.write(mainTemplate.getContent());
91                         ofs.close();
92
93                         /**
94                          * Write other files
95                          */
96                         if (extraFiles != null) {
97                                 for (VduArtifact artifact: extraFiles){
98                                         ofs = new FileOutputStream(new File(dir, artifact.getName()));
99                                         ofs.write(artifact.getContent());
100                                         ofs.close();
101                                 }
102                         }
103
104
105                         /**
106                          * Create manifest
107                          */
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());
113                         mfstream.close();
114
115                         /**
116                          * ZIP it up
117                          */
118                         ByteArrayOutputStream zipbytes = new ByteArrayOutputStream();
119                         ZipOutputStream zos = new ZipOutputStream(zipbytes);
120                         compressTree(zos, "", dir, dir);
121                         zos.close();
122                         return zipbytes.toByteArray();
123
124                 } catch (Exception e) {
125                         throw new RuntimeException("Failed to create CSAR: " + e.getMessage());
126                 } finally {
127
128                         /**
129                          * Clean up tmpdir
130                          */
131                         deleteDirectory(dir);
132                 }
133         }
134
135         /**
136          * Private methods
137          */
138
139         /**
140          * Compresses (ZIPs) a directory tree
141          * 
142          * @param dir
143          * @throws IOException
144          */
145         private void compressTree(ZipOutputStream zos, String path, File basedir, File dir) throws IOException {
146                 if (!dir.isDirectory())
147                         return;
148
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);
154                                 zos.closeEntry();
155                                 compressTree(zos, newpath, basedir, f);
156                         } else {
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];
163                                 int len;
164                                 while ((len = fis.read(buffer)) > 0) {
165                                         zos.write(buffer, 0, len);
166                                 }
167                                 zos.closeEntry();
168                                 fis.close();
169                         }
170                 }
171         }
172
173         private boolean deleteDirectory(File directory) {
174                 if (directory.exists()) {
175                         File[] files = directory.listFiles();
176                         if (null != files) {
177                                 for (int i = 0; i < files.length; i++) {
178                                         if (files[i].isDirectory()) {
179                                                 deleteDirectory(files[i]);
180                                         } else {
181                                                 files[i].delete();
182                                         }
183                                 }
184                         }
185                 }
186                 return (directory.delete());
187         }
188 }