Create onboarding validator for ASD VSPs
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / backend / ci / tests / utils / general / FileHandling.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2021 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.sdc.backend.ci.tests.utils.general;
23
24 import static org.testng.AssertJUnit.assertTrue;
25
26 import com.aventstack.extentreports.Status;
27 import java.io.BufferedWriter;
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FileWriter;
31 import java.io.FilenameFilter;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.nio.file.Paths;
35 import java.util.ArrayList;
36 import java.util.EnumMap;
37 import java.util.Enumeration;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Properties;
42 import java.util.zip.ZipEntry;
43 import java.util.zip.ZipException;
44 import java.util.zip.ZipFile;
45 import org.apache.commons.io.FileUtils;
46 import org.onap.sdc.backend.ci.tests.api.ComponentBaseTest;
47 import org.onap.sdc.backend.ci.tests.datatypes.enums.PackageTypeEnum;
48 import org.openecomp.sdc.be.model.DataTypeDefinition;
49 import org.openecomp.sdc.common.util.GeneralUtility;
50 import org.yaml.snakeyaml.Yaml;
51
52 public class FileHandling {
53
54 //      ------------------yaml parser methods----------------------------
55         public static Map<?, ?> parseYamlFile(String filePath) throws Exception {
56                 Yaml yaml = new Yaml();
57                 File file = new File(filePath);
58                 InputStream inputStream = new FileInputStream(file);
59                 Map<?, ?> map = (Map<?, ?>) yaml.load(inputStream);
60                 return map;
61         }
62
63         /**
64          * The method return map fetched objects by pattern from yaml file
65          * @param yamlFile
66          * @param pattern
67          * @return
68          * @throws Exception
69          */
70         public static Map<String, Object> parseYamlFileToMapByPattern(File yamlFile, String pattern) throws Exception {
71                 Map<?, ?> yamlFileToMap = FileHandling.parseYamlFile(yamlFile.toString());
72                 Map<String, Object> objectMap = getObjectMapByPattern(yamlFileToMap, pattern);
73                 return objectMap;
74         }
75
76         @SuppressWarnings("unchecked")
77         public static Map<String, Object> getObjectMapByPattern(Map<?, ?> parseUpdetedEnvFile, String pattern) {
78                 Map<String, Object> objectMap = null;
79
80                 Object objectUpdetedEnvFile = parseUpdetedEnvFile.get(pattern);
81                 if(objectUpdetedEnvFile instanceof HashMap){
82                         objectMap = (Map<String, Object>) objectUpdetedEnvFile;
83                 }
84                 return objectMap;
85         }
86
87
88         public static Map<String, DataTypeDefinition> parseDataTypesYaml(String filePath) throws Exception {
89                 @SuppressWarnings("unchecked")
90                 Map<String, DataTypeDefinition> dataTypesMap = (Map<String, DataTypeDefinition>) parseYamlFile(filePath);
91                 return dataTypesMap;
92         }
93 //      -------------------------------------------------------------------------------------------------
94
95         /**
96          * @param folder, folder name under "Files" folder
97          * @return path to given folder from perspective of working directory or sdc-vnfs repository
98          */
99         public static String getFilePath(String folder) {
100                 // return folder from perspective of working directory ( in general for nightly run from Linux, should already contain "Files" directory )
101                 return FileHandling.getBasePath() + "src/test/resources/Files" + File.separator + folder + File.separator;
102         }
103
104         public static String getBasePath() {
105                 return System.getProperty("user.dir") + File.separator;
106         }
107
108         public static String getSdcVnfsPath() {
109                 return  getBasePath() + Paths.get("..", "..", "sdc-vnfs").toString();
110         }
111
112         public static String getDriversPath() {
113                 return getBasePath() + "src" + File.separator + "test" + File.separator + "resources"
114                                 + File.separator + "ci" + File.separator + "drivers" + File.separator;
115         }
116
117         public static String getResourcesFilesPath() {
118                 return getSdcVnfsPath()+ File.separator + "ui-tests" + File.separator + "Files" + File.separator;
119         }
120
121         public static String getResourcesEnvFilesPath() {
122                 return getBasePath() + File.separator + "src" + File.separator + "test" + File.separator + "resources"
123                                 + File.separator + "Files" + File.separator + "ResourcesEnvFiles" +File.separator;
124         }
125
126         public static String getCiFilesPath() {
127                 return getBasePath() + "src" + File.separator + "test" + File.separator + "resources"
128                                 + File.separator + "ci";
129         }
130
131         public static String getConfFilesPath() {
132                 return getCiFilesPath() + File.separator + "conf" + File.separator;
133         }
134
135         private static final EnumMap<PackageTypeEnum, String> PACKAGE_REPOSITORY_PATHS_MAP = new EnumMap<>(Map.of(
136                 PackageTypeEnum.PNF, getPnfRepositoryPath(),
137                 PackageTypeEnum.CNF, getCnfRepositoryPath(),
138                 PackageTypeEnum.CNF_HELM, getCnfRepositoryForHelmValidatorPath(),
139                 PackageTypeEnum.VNF, getVnfRepositoryPath(),
140                 PackageTypeEnum.ETSI, getEtsiRepositoryPath(),
141                 PackageTypeEnum.VFC, getVfcRepositoryPath(),
142                 PackageTypeEnum.ASD, getAsdRepositoryPath()
143         ));
144
145         public static String getVnfRepositoryPath() {
146                 return getFilePath("VNFs");
147         }
148
149         private static String getPnfRepositoryPath() {
150                 return getFilePath("PNFs");
151         }
152
153         private static String getCnfRepositoryPath() {
154                 return getFilePath("CNFs");
155         }
156
157         private static String getCnfRepositoryForHelmValidatorPath() {
158                 return getFilePath("CNFs/helm_validator");
159         }
160
161         private static String getEtsiRepositoryPath() { return getFilePath("ETSI"); }
162
163         private static String getAsdRepositoryPath() { return getFilePath("ASDs"); }
164
165         private static String getVfcRepositoryPath() { return getFilePath("VFCs"); }
166
167         public static String getPackageRepositoryPath(PackageTypeEnum packageTypeEnum) {
168                 return PACKAGE_REPOSITORY_PATHS_MAP.get(packageTypeEnum);
169         }
170
171         public static String getPortMirroringRepositoryPath() {
172                 return getFilePath("PortMirroring");
173         }
174
175         public static File getConfigFile(String configFileName) throws Exception {
176                 File configFile = new File(FileHandling.getBasePath() + File.separator + "conf" + File.separator + configFileName);
177                 if (!configFile.exists()) {
178                         configFile = new File(FileHandling.getConfFilesPath() + configFileName);
179                 }
180                 return configFile;
181         }
182
183         public static Object[] filterFileNamesFromFolder(String filepath, String extension) {
184                 try {
185                         File dir = new File(filepath);
186                         List<String> filenames = new ArrayList<String>();
187
188                         FilenameFilter extensionFilter = new FilenameFilter() {
189                                 public boolean accept(File dir, String name) {
190                                         return name.endsWith(extension);
191                                 }
192                         };
193
194                         if (dir.isDirectory()) {
195                                 for (File file : dir.listFiles(extensionFilter)) {
196                                         filenames.add(file.getName());
197                                 }
198                                 return filenames.toArray();
199                         }
200
201                 } catch (Exception e) {
202                         e.printStackTrace();
203                 }
204                 return null;
205         }
206
207         public static List<String> filterFileNamesListFromFolder(String filepath, String extension) {
208                 List<String> filenames = new ArrayList<String>();
209                 try {
210                         File dir = new File(filepath);
211
212                         FilenameFilter extensionFilter = new FilenameFilter() {
213                                 public boolean accept(File dir, String name) {
214                                         return name.endsWith(extension);
215                                 }
216                         };
217
218                         if (dir.isDirectory()) {
219                                 for (File file : dir.listFiles(extensionFilter)) {
220                                         filenames.add(file.getName());
221                                 }
222                                 return filenames;
223                         }
224
225                 } catch (Exception e) {
226                         e.printStackTrace();
227                 }
228                 return filenames;
229         }
230
231         public static String[] getArtifactsFromZip(String filepath, String zipFilename){
232                 try {
233                         ZipFile zipFile = new ZipFile(filepath + File.separator + zipFilename);
234                         Enumeration<? extends ZipEntry> entries = zipFile.entries();
235
236                         String[] artifactNames = new String[zipFile.size() - 1];
237
238                         int i = 0;
239                         while(entries.hasMoreElements()){
240                                 ZipEntry nextElement = entries.nextElement();
241                                 if (!nextElement.isDirectory()){
242                                         if (!nextElement.getName().equals("MANIFEST.json")){
243                                                 String name = nextElement.getName();
244                                                 artifactNames[i++] = name;
245                                         }
246                                 }
247                         }
248                         zipFile.close();
249                         return artifactNames;
250                 } catch(ZipException zipEx) {
251                         System.err.println("Error in zip file named : " +  zipFilename);
252                         zipEx.printStackTrace();
253                 } catch (IOException e) {
254                         System.err.println("Unhandled exception : ");
255                         e.printStackTrace();
256                 }
257
258                 return null;
259
260         }
261
262         public static List<String> getFileNamesFromZip(String zipFileLocation){
263                 try{
264                         ZipFile zipFile = new ZipFile(zipFileLocation);
265                         Enumeration<? extends ZipEntry> entries = zipFile.entries();
266
267                         List<String> artifactNames = new ArrayList<>();
268
269                         int i = 0;
270                         while(entries.hasMoreElements()){
271                                 ZipEntry nextElement = entries.nextElement();
272                                 if (!nextElement.isDirectory()){
273                                         String name = nextElement.getName();
274                                         artifactNames.add(name);
275                                 }
276                         }
277                         zipFile.close();
278                         return artifactNames;
279                 }
280                 catch(ZipException zipEx){
281                         System.err.println("Error in zip file named : " +  zipFileLocation);
282                         zipEx.printStackTrace();
283                 } catch (IOException e) {
284                         System.err.println("Unhandled exception : ");
285                         e.printStackTrace();
286                 }
287                 return null;
288         }
289
290         public static List<String> getZipFileNamesFromFolder(String filepath) {
291                 List<String> fileNamesListFromFolder = filterFileNamesListFromFolder(filepath, ".zip");
292                 fileNamesListFromFolder.addAll(filterFileNamesListFromFolder(filepath, ".csar"));
293                 return fileNamesListFromFolder;
294         }
295
296         public static int countFilesInZipFile(String[] artifactsArr, String reqExtension){
297                 int fileCounter = 0;
298                 for (String artifact : artifactsArr){
299                         String extensionFile = artifact.substring(artifact.lastIndexOf(".") + 1 , artifact.length());
300                         if (extensionFile.equals(reqExtension)){
301                                 fileCounter++;
302                         }
303                 }
304                 return fileCounter;
305         }
306
307         /**
308          * @param dirPath
309          * @return last modified file name from dirPath directory
310          */
311         public static synchronized File getLastModifiedFileNameFromDir(String dirPath){
312             File dir = new File(dirPath);
313             File[] files = dir.listFiles();
314             if (files == null) {
315                 assertTrue("File not found under directory " + dirPath, false);
316                 return null;
317             }
318
319             File lastModifiedFile = files[0];
320             for (int i = 1; i < files.length; i++) {
321                 if(files[i].isDirectory()) {
322                         continue;
323                 }
324                 if (lastModifiedFile.lastModified()  < files[i].lastModified()) {
325                    lastModifiedFile = files[i];
326                 }
327             }
328             return lastModifiedFile;
329         }
330
331         public static void deleteDirectory(String directoryPath) {
332                 File dir = new File(directoryPath);
333                 try {
334                         FileUtils.cleanDirectory(dir);
335                 } catch (IllegalArgumentException e) {
336                         System.out.println("Failed to clean " + dir);
337                 } catch (IOException e) {
338                         System.out.println("Failed to clean " + dir);
339                 }
340         }
341
342         public static void createDirectory(String directoryPath) {
343                 File directory = new File(String.valueOf(directoryPath));
344             if (! directory.exists()){
345                 directory.mkdirs();
346             }
347         }
348
349
350         /**
351          * The method append data to existing file, if file not exists - create it
352          * @param pathToFile
353          * @param text
354          * @param leftSpaceCount
355          * @throws IOException
356          */
357         public static synchronized void writeToFile(File pathToFile, Object text, Integer leftSpaceCount) throws IOException{
358
359                 BufferedWriter bw = null;
360                 FileWriter fw = null;
361                 if(!pathToFile.exists()){
362                         createEmptyFile(pathToFile);
363                 }
364                 try {
365                         fw = new FileWriter(pathToFile, true);
366                         bw = new BufferedWriter(fw);
367                         StringBuilder sb = new StringBuilder();
368                         if(leftSpaceCount > 0 ){
369                                 for(int i = 0; i < leftSpaceCount; i++){
370                                         sb.append(" ");
371                                 }
372                         }
373                         bw.write(sb.toString() + text);
374                         bw.newLine();
375                         bw.close();
376                         fw.close();
377                 } catch (Exception e) {
378                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Unable to write to flie " + pathToFile);
379                 }
380         }
381
382         public static String getCreateDirByName(String dirName) {
383                 File dir = new File(dirName);
384                 dir.mkdir();
385                 if(!dir.exists()) {
386                 }
387
388                 return dir.getPath();
389         }
390
391         public static boolean isFileDownloaded(String downloadPath, String fileName) {
392                 boolean flag = false;
393                 File dir = new File(downloadPath);
394                 File[] dir_contents = dir.listFiles();
395                 for (int i = 0; i < dir_contents.length; i++) {
396                         if (dir_contents[i].getName().equals(fileName))
397                                 return flag = true;
398                 }
399                 return flag;
400         }
401
402         public static String getMD5OfFile(File file) throws IOException {
403                 String content = FileUtils.readFileToString(file);
404                 String md5 = GeneralUtility.calculateMD5Base64EncodedByString(content);
405                 return md5;
406         }
407
408         public static File createEmptyFile(String fileToCreate) {
409                 File file= new File(fileToCreate);
410                 try {
411                         if(file.exists()){
412                                 deleteFile(file);
413                         }
414                         file.createNewFile();
415                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Create file " + fileToCreate);
416                 } catch (IOException e) {
417                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to create file " + fileToCreate);
418                         e.printStackTrace();
419                 }
420                 return file;
421         }
422
423         public static File createEmptyFile(File fileToCreate) {
424                 try {
425                         if(fileToCreate.exists()){
426                                 deleteFile(fileToCreate);
427                         }
428                         fileToCreate.createNewFile();
429                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Create file " + fileToCreate);
430                 } catch (IOException e) {
431                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to create file " + fileToCreate);
432                         e.printStackTrace();
433                 }
434                 return fileToCreate;
435         }
436
437         public static void deleteFile(File file){
438
439                 try{
440                 if(file.exists()){
441                         file.deleteOnExit();
442                         ComponentBaseTest.getExtendTest().log(Status.INFO, "File " + file.getName() + "has been deleted");
443                 }else{
444                         ComponentBaseTest.getExtendTest().log(Status.INFO, "Failed to delete file " + file.getName());
445                 }
446         }catch(Exception e){
447                 e.printStackTrace();
448         }
449
450         }
451
452
453         /**
454          * get file list from directory by extension array
455          * @param directory
456          * @param okFileExtensions
457          * @return
458          */
459         public static List<File> getHeatAndHeatEnvArtifactsFromZip(File directory, String[] okFileExtensions){
460
461                         List<File> fileList = new ArrayList<>();
462                         File[] files = directory.listFiles();
463
464                         for (String extension : okFileExtensions){
465                                 for(File file : files){
466                                         if (file.getName().toLowerCase().endsWith(extension)){
467                                                 fileList.add(file);
468                                         }
469                                 }
470                         }
471                         return fileList;
472         }
473
474     public static String getKeyByValueFromPropertyFormatFile(String fullPath, String key) {
475                 Properties prop = new Properties();
476                 InputStream input = null;
477                 String value = null;
478                 try {
479                         input = new FileInputStream(fullPath);
480                         prop.load(input);
481                         value = (prop.getProperty(key));
482
483                 } catch (IOException ex) {
484                         ex.printStackTrace();
485                 } finally {
486                         if (input != null) {
487                                 try {
488                                         input.close();
489                                 } catch (IOException e) {
490                                         e.printStackTrace();
491                                 }
492                         }
493                 }
494
495                 return value.replaceAll("\"","");
496         }
497
498     public static void overWriteExistindDir(String outputCsar) throws IOException {
499                 String basePath = getBasePath();
500                 String csarDir = FileHandling.getCreateDirByName("target/outputCsar");
501                 FileUtils.cleanDirectory(new File(csarDir));
502     }
503 }