7ba830906cb656def130188e130c8df587c8009d
[sdc.git] /
1 package org.openecomp.core.tools.Commands.importdata;
2
3 import com.amdocs.zusammen.datatypes.SessionContext;
4 import org.openecomp.core.tools.store.ElementCassandraLoader;
5 import org.openecomp.core.tools.store.ElementNamespaceHandler;
6 import org.openecomp.core.tools.store.VersionCassandraLoader;
7 import org.openecomp.core.tools.store.zusammen.datatypes.ElementEntity;
8 import org.openecomp.sdc.logging.api.Logger;
9 import org.openecomp.sdc.logging.api.LoggerFactory;
10
11 import java.io.File;
12 import java.io.IOException;
13 import java.nio.ByteBuffer;
14 import java.nio.file.Files;
15 import java.nio.file.Path;
16 import java.nio.file.Paths;
17 import java.util.Arrays;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21
22 import static java.io.File.separator;
23 import static org.openecomp.core.tools.Commands.exportdata.ImportProperties.*;
24
25 public class ElementImport {
26     private static final Logger logger = LoggerFactory.getLogger(ElementImport.class);
27     private ElementCassandraLoader elementCassandraLoader = new ElementCassandraLoader();
28     private ElementNamespaceHandler cassandraElementRepository = new ElementNamespaceHandler();
29     private VersionCassandraLoader versionCassandraLoader = new VersionCassandraLoader();
30
31     public void loadPath(SessionContext sessionContext, Path elementDir, String elementId, String[] pathObjects) {
32         try {
33             // load info file
34             ElementEntity elementEntity = new ElementEntity();
35             Path infoFilePath = Paths.get(elementDir.toString() + separator + ELEMENT_INFO_PREFIX
36                     + elementId + JSON_POSTFIX);
37             if (Files.exists(infoFilePath)) {
38                 String info = new String(Files.readAllBytes(infoFilePath));
39                 elementEntity.setInfo(info);
40             }
41
42             // load relation file
43             Path realtionsFilePath = Paths.get(elementDir.toString() + separator
44                     + ELEMENT_RELATION_PREFIX + elementId + JSON_POSTFIX);
45             if (Files.exists(realtionsFilePath)) {
46                 String relations = new String(Files.readAllBytes(realtionsFilePath));
47                 elementEntity.setRelations(relations);
48             }
49
50             //load entity data
51             Path dataFilePath = Paths.get(elementDir.toString() + separator
52                     + ELEMENT_DATA_PREFIX + elementId + JSON_POSTFIX);
53             if (Files.exists(dataFilePath)) {
54                 byte[] bytes = Files.readAllBytes(dataFilePath);
55                 ByteBuffer data = ByteBuffer.wrap(bytes);
56                 elementEntity.setData(data);
57             }
58
59             //load visualization
60             Path visualFilePath = Paths.get(elementDir.toString() + separator
61                     + ELEMENT_VISUALIZATION_PREFIX + elementId );
62             if (Files.exists(visualFilePath)) {
63                 byte[] bytes = Files.readAllBytes(visualFilePath);
64                 ByteBuffer visualization = ByteBuffer.wrap(bytes);
65                 elementEntity.setVisualization(visualization);
66             }
67
68             //load searchable
69             Path searchableFilePath = Paths.get(elementDir.toString() + separator
70                     + ELEMENT_SEARCHABLE_PREFIX + elementId );
71             if (Files.exists(searchableFilePath)) {
72                 byte[] bytes = Files.readAllBytes(searchableFilePath);
73                 ByteBuffer searchable = ByteBuffer.wrap(bytes);
74                 elementEntity.setSearchableData(searchable);
75             }
76
77             elementEntity.setSpace(pathObjects[2]);
78             elementEntity.setItemId(pathObjects[0]);
79             elementEntity.setVersionId(pathObjects[1]);
80             elementEntity.setElement_id(pathObjects[pathObjects.length - 1]);
81             elementEntity.setNamespace(getNameSpace(pathObjects));
82             elementEntity.setParentId(getParentId(pathObjects));
83             elementEntity.setSubElementIds(getAllSubElementsIds(elementDir));
84             elementCassandraLoader.createEntity(elementEntity);
85             cassandraElementRepository.createElementNamespace(elementEntity);
86             versionCassandraLoader.insertElementToVersion(elementEntity);
87         } catch (Exception ex) {
88             logger.error(ex.getMessage(), ex);
89         }
90     }
91
92     private String getParentId(String[] pathObjects) {
93         if (pathObjects.length <= 4) {
94             return null;
95         }
96         return pathObjects[pathObjects.length - 2];
97     }
98
99     private Set<String> getAllSubElementsIds(Path root) throws IOException {
100         try (Stream<Path> walk = Files.walk(root)) {
101            return  walk.filter(path -> Files.isDirectory(path))
102                    .map(path -> path.toFile().getName() ).collect(Collectors.toSet());
103         }
104     }
105
106     private String getNameSpace(String[] pathObjects) {
107         if (pathObjects.length <= 4) {
108             return null;
109         }
110         if (pathObjects.length == 5) {
111             return pathObjects[3];
112         }
113         return Arrays.stream(pathObjects, 3, pathObjects.length - 1)
114                 .reduce("", (s1, s2) -> s1 + File.separator + s2);
115     }
116 }