Add sdc startup in IT
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / backend / ci / tests / execute / devCI / ArtifactFromCsar.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.sdc.backend.ci.tests.execute.devCI;
22
23 import org.apache.commons.codec.digest.DigestUtils;
24 import org.apache.commons.io.FileUtils;
25 import org.apache.commons.io.IOUtils;
26 import org.onap.sdc.backend.ci.tests.datatypes.GroupHeatMetaDefinition;
27 import org.onap.sdc.backend.ci.tests.datatypes.HeatMetaFirstLevelDefinition;
28 import org.onap.sdc.backend.ci.tests.datatypes.TypeHeatMetaDefinition;
29 import org.onap.sdc.backend.ci.tests.utils.CsarParserUtils;
30
31 import java.io.File;
32 import java.io.FileInputStream;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
35 import java.nio.file.DirectoryStream;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.*;
40 import java.util.stream.Collectors;
41 import java.util.zip.ZipEntry;
42 import java.util.zip.ZipInputStream;
43
44 public class ArtifactFromCsar {
45
46         public static Map<String, Object> combineHeatArtifacstWithFolderArtifacsToMap(String pathToCsar) throws Exception {
47                 return combineHeatArtifacstWithFolderArtifacsToMap(pathToCsar, "output");
48         }
49         
50         public static Map<String, Object> combineHeatArtifacstWithFolderArtifacsToMap(String pathToCsar, String outputCsar) throws Exception {
51                 File csarFile = new File(pathToCsar);
52                 
53                 
54                 File dir = new File(csarFile.getParent() + File.separator + outputCsar);
55                         dir.mkdir();
56                         if(!dir.exists()) {
57                 }
58
59                 String outputFolder = dir.getPath();
60                 unZip(pathToCsar, outputFolder);
61                 File directory = new File(outputFolder + File.separator + "Artifacts" + File.separator );
62                 
63                 Map<String, Object> artifactsMap = combineHeatArtifacstWithFolderArtifacsToMap(getMapArtifactFromFolderStructure(directory), getDeploymentArtifactListFromHeatMeta(csarFile, directory));
64                 FileUtils.cleanDirectory(new File(outputFolder));
65                 
66                 return artifactsMap;
67         }
68         
69         public static Map<String, Object> getVFCArtifacts(String pathToCsar) throws Exception{
70                 String outputFolder = unzipCsarFile(pathToCsar);
71                 File directory = new File(outputFolder + File.separator + "Artifacts" + File.separator );
72                 Map<String, Object> artifactsMap = getMapArtifactFromFolderStructure(directory);
73                 cleanFolders(outputFolder);
74                 
75                 return artifactsMap;
76         }
77         
78         private static Map<String, Object> combineHeatArtifacstWithFolderArtifacsToMap(Map<String, Object> map, List<HeatMetaFirstLevelDefinition> rlist) {
79                 if(map.get("Deployment") != null) {
80                         rlist.addAll((Collection<? extends HeatMetaFirstLevelDefinition>) map.get("Deployment"));
81                 }
82                 map.put("Deployment", rlist);
83                 return map;
84         }
85         
86         private static List<HeatMetaFirstLevelDefinition> getDeploymentArtifactListFromHeatMeta(File pathToCsar, File directory) throws Exception {
87                 List<HeatMetaFirstLevelDefinition> artifactList = new LinkedList<HeatMetaFirstLevelDefinition>();
88                 
89                 List<TypeHeatMetaDefinition> listTypeHeatMetaDefinition = CsarParserUtils.getListTypeHeatMetaDefinition(pathToCsar);
90
91             for(TypeHeatMetaDefinition typeHeatMetaDefinition : listTypeHeatMetaDefinition){
92                 for(GroupHeatMetaDefinition groupHeatMetaDefinition : typeHeatMetaDefinition.getGroupHeatMetaDefinition()){
93                         artifactList.addAll(groupHeatMetaDefinition.getArtifactList());
94                 }
95             }
96                 
97             List<HeatMetaFirstLevelDefinition> listArtifactWithTypesByList = getListArtifactWithTypesByList(directory, artifactList);
98                 return listArtifactWithTypesByList;
99 //          return artifactList;
100                 
101         }
102         
103         private static Map<String, Object> getMapArtifactFromFolderStructure(File pathToArtifactFolder) throws IOException {
104                 
105                 Map<String, Object> map = new HashMap<String, Object>();
106                 
107                 
108                 final Path dir = Paths.get(pathToArtifactFolder.getPath());
109                 final DirectoryStream<Path> dirStream = Files.newDirectoryStream(dir);
110                 
111                 dirStream.forEach(currFile -> {
112                         File file = currFile.toFile();
113                                 if (file.isDirectory()) {
114                                         System.out.println(file.getName());
115                                         if(file.getName().toLowerCase().equals("deployment") || file.getName().toLowerCase().equals("informational")) {
116                                                 map.put(file.getName(), getListArtifactWithTypes(file));
117                                         } else {
118                                                 try {
119                                                         map.put(file.getName(), getMapArtifactFromFolderStructure(file));
120                                                 } catch (IOException e) {
121                                                         e.printStackTrace();
122                                                 }
123                                         }
124                                 }
125                 });
126                 
127                 dirStream.close();
128                 
129
130                 
131 //              
132 //              File[] fileList = pathToArtifactFolder.listFiles();
133 //              for(File file: fileList) {
134 //                      if (file.isDirectory()) {
135 //                              
136 //                              System.out.println(file.getName());
137 //                              if(file.getName().equals("Deployment") || file.getName().equals("Informational")) {
138 //                                      map.put(file.getName(), getListArtifactWithTypes(file));
139 //                              } else {
140 //                                      map.put(file.getName(), getMapArtifactFromFolderStructure(file));
141 //                              }
142 //                      }
143 //              }
144                 return map;
145         }
146         
147         
148         
149         private static List<HeatMetaFirstLevelDefinition> getListArtifactWithTypes(File folderPath) {
150                 List<HeatMetaFirstLevelDefinition> artifactList = new LinkedList<HeatMetaFirstLevelDefinition>();
151                 
152                 File[] fileList = folderPath.listFiles();
153                 
154                 for(File file: fileList) {
155                         File[] artifacts = file.listFiles();
156                         
157                         for(File artifact: artifacts) {
158 //                              HeatMetaFirstLevelDefinition heatMetaFirstLevelDefinition = new HeatMetaFirstLevelDefinition(file.getName(), artifact.getName());
159                                 HeatMetaFirstLevelDefinition heatMetaFirstLevelDefinition = new HeatMetaFirstLevelDefinition(artifact.getName(), file.getName(), crunchifyGetMd5ForFile(artifact));
160                                 artifactList.add(heatMetaFirstLevelDefinition);
161                         }
162                 }
163                 
164                 return artifactList;
165         }
166         
167         private static List<HeatMetaFirstLevelDefinition> getListArtifactWithTypesByList(File folderPath, List<HeatMetaFirstLevelDefinition> artifactLogicList) {
168                                 
169                 
170                 File[] fileList = folderPath.listFiles();
171                 
172                 
173                 
174                 for (HeatMetaFirstLevelDefinition heatMetaFirstLevelDefinition : artifactLogicList) {
175                         
176                         String fileName = heatMetaFirstLevelDefinition.getFileName();
177                         
178                         for (File fileFromFolder : fileList) {
179                                 if ( fileFromFolder.getName().equals(fileName)){
180                                         heatMetaFirstLevelDefinition.setCheckSum(crunchifyGetMd5ForFile(fileFromFolder));
181                                 }
182                                 
183                         }
184                  }
185
186                 return artifactLogicList;
187         }
188         
189         public static String crunchifyGetMd5ForFile(File crunchifyFile) {
190                 String crunchifyValue = null;
191                 FileInputStream crunchifyInputStream = null;
192                 try {
193                         crunchifyInputStream = new FileInputStream(crunchifyFile);
194  
195                         // md5Hex converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
196                         // The returned array will be double the length of the passed array, as it takes two characters to represent any given byte.
197                         crunchifyValue = DigestUtils.md5Hex(IOUtils.toByteArray(crunchifyInputStream));
198                 } catch (IOException e) {
199                         e.printStackTrace();
200                 } finally {
201                         IOUtils.closeQuietly(crunchifyInputStream);
202                 }
203                 return crunchifyValue;
204         }
205         
206         public static void unZip(String zipFile, String outputFolder) {
207                 byte[] buffer = new byte[1024];
208
209              try{
210                 File folder = new File(outputFolder);
211                 
212                 if(!folder.exists()){
213                         folder.mkdir();
214                 }
215
216                 ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
217                 ZipEntry ze = zis.getNextEntry();
218
219                 while(ze!=null){
220
221                    String fileName = ze.getName();
222                    File newFile = new File(outputFolder + File.separator + fileName);
223                    
224                    if(ze.isDirectory()) {
225                            newFile.mkdir();
226                            ze = zis.getNextEntry();
227                            continue;
228                    }
229
230                     new File(newFile.getParent()).mkdirs();
231                     FileOutputStream fos = new FileOutputStream(newFile);
232
233                     int len;
234                     while ((len = zis.read(buffer)) > 0) {
235                         fos.write(buffer, 0, len);
236                     }
237
238                     fos.close();
239                     ze = zis.getNextEntry();
240                 }
241
242                 zis.closeEntry();
243                 zis.close();
244
245             } catch (IOException ex) {
246                ex.printStackTrace();
247             }
248
249         }
250         
251     private static void cleanFolders(String outputFolder) throws IOException {
252                 System.gc();
253                 FileUtils.cleanDirectory(new File(outputFolder));
254                 FileUtils.deleteDirectory(new File(outputFolder));
255         }
256
257     private static String unzipCsarFile(String pathToCsar) {
258                 File csarFile = new File(pathToCsar);
259                 
260                 
261                 File dir = new File(csarFile.getParent() + File.separator + "output-" + UUID.randomUUID());
262                 if(!dir.exists()) {
263                         dir.mkdirs();
264                 }
265
266                 String outputFolder = dir.getPath();
267                 ArtifactFromCsar.unZip(pathToCsar, outputFolder);
268                 return outputFolder;
269         }
270
271         public static String[] getArtifactNamesFromCsar(String path, String csarFile) throws Exception {
272                 Map<String, Object> combinedMap = combineHeatArtifacstWithFolderArtifacsToMap(path + csarFile);
273                 LinkedList<HeatMetaFirstLevelDefinition> deploymentArtifacts = ((LinkedList<HeatMetaFirstLevelDefinition>) combinedMap.get("Deployment"));
274                 List<String> artifactNamesList = deploymentArtifacts.stream().map(e -> e.getFileName()).collect(Collectors.toList());
275                 Object[] artifactNamesObjectArr = artifactNamesList.toArray();
276                 String[] artifactNamesFromFile = Arrays.copyOf(artifactNamesObjectArr, artifactNamesObjectArr.length, String[].class);
277                 return artifactNamesFromFile;
278         }
279
280 }