15f8b02e2b6d021b58f3f8b00bef81a5f4f322e6
[sdc.git] /
1 package org.openecomp.core.tools.Commands.importdata;
2
3 import com.amdocs.zusammen.datatypes.SessionContext;
4 import org.openecomp.sdc.logging.api.Logger;
5 import org.openecomp.sdc.logging.api.LoggerFactory;
6
7 import javax.validation.constraints.Min;
8 import java.io.File;
9 import java.io.IOException;
10 import java.nio.file.Files;
11 import java.nio.file.Path;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.List;
15 import java.util.stream.Stream;
16
17 public class TreeWalker {
18     private static final Logger logger = LoggerFactory.getLogger(TreeWalker.class);
19
20     public static final void walkFiles(SessionContext sessionContext,Path rootDir, String filterItem) throws IOException {
21         try (Stream<Path> walk = Files.walk(rootDir)) {
22             walk.parallel().filter(path -> Files.isDirectory(path)).
23                     forEach(path -> handlePath(sessionContext,path, rootDir, filterItem));
24         }
25     }
26
27     private static final void handlePath(SessionContext sessionContext, Path path, Path root,String filterItem) {
28         String logicalPath = path.toString().replace(root.toString(), "");
29         if (logicalPath.startsWith(File.separator)){
30             logicalPath = logicalPath.substring(1);
31         }
32         String[] splitted = logicalPath.split(File.separator);
33         if(filterItem != null && splitted.length > 0 && !splitted[0].contains(filterItem)){
34             return;
35         }
36         switch (splitted.length) {
37             case 0:
38                 //root - ignore
39                 break;
40             case 1:     // handle Item
41                 new ItemImport().loadPath(sessionContext,path,splitted[splitted.length -1]);
42                 new VersionInfoImport().loadPath(sessionContext,path,splitted[splitted.length -1]);
43                 break;
44             case 2:
45                 //ignore this level
46                 break;
47             case 3: // handle version
48                 new VersionImport().loadPath(sessionContext,path,splitted[splitted.length -2]);
49                 break;
50             default:
51                 //handle elements
52                  new ElementImport().loadPath(sessionContext,path,splitted[splitted.length -1],splitted);
53                 break;
54         }
55
56     }
57
58 }