fix incorrect dependency
[sdc.git] / openecomp-be / tools / zusammen-tools / src / main / java / org / openecomp / core / tools / Commands / importdata / ElementImport.java
1 package org.openecomp.core.tools.Commands.importdata;
2
3 import com.amdocs.zusammen.datatypes.Id;
4 import com.amdocs.zusammen.datatypes.SessionContext;
5 import org.openecomp.core.tools.store.ElementCassandraLoader;
6 import org.openecomp.core.tools.store.ElementNamespaceHandler;
7 import org.openecomp.core.tools.store.VersionCassandraLoader;
8 import org.openecomp.core.tools.store.zusammen.datatypes.ElementEntity;
9 import org.openecomp.sdc.logging.api.Logger;
10 import org.openecomp.sdc.logging.api.LoggerFactory;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.nio.ByteBuffer;
15 import java.nio.file.Files;
16 import java.nio.file.Path;
17 import java.nio.file.Paths;
18 import java.util.Arrays;
19 import java.util.HashSet;
20 import java.util.Set;
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     public static final String ROOT_ITEM = Id.ZERO.getValue();
28
29     private ElementCassandraLoader elementCassandraLoader = new ElementCassandraLoader();
30     private ElementNamespaceHandler cassandraElementRepository = new ElementNamespaceHandler();
31     private VersionCassandraLoader versionCassandraLoader = new VersionCassandraLoader();
32
33     public void loadPath(SessionContext sessionContext, Path elementDir, String elementId, String[]
34             pathObjects) {
35         try {
36             if (!Files.isDirectory(elementDir)){
37                 return;
38             }
39             // load info file
40             ElementEntity elementEntity = new ElementEntity();
41             Path infoFilePath = Paths.get(elementDir.toString() + separator + ELEMENT_INFO_PREFIX
42                     + elementId + JSON_POSTFIX);
43             if (Files.exists(infoFilePath)) {
44                 String info = new String(Files.readAllBytes(infoFilePath));
45                 elementEntity.setInfo(info);
46             }
47             // load relation file
48             Path realtionsFilePath = Paths.get(elementDir.toString() + separator
49                     + ELEMENT_RELATION_PREFIX + elementId + JSON_POSTFIX);
50             if (Files.exists(realtionsFilePath)) {
51                 String relations = new String(Files.readAllBytes(realtionsFilePath));
52                 elementEntity.setRelations(relations);
53             }
54
55             //load entity data
56             Path dataFilePath = Paths.get(elementDir.toString() + separator
57                     + ELEMENT_DATA_PREFIX + elementId + JSON_POSTFIX);
58             if (Files.exists(dataFilePath)) {
59                 byte[] bytes = Files.readAllBytes(dataFilePath);
60                 ByteBuffer data = ByteBuffer.wrap(bytes);
61                 elementEntity.setData(data);
62             }
63
64             //load visualization
65             Path visualFilePath = Paths.get(elementDir.toString() + separator
66                     + ELEMENT_VISUALIZATION_PREFIX + elementId);
67             if (Files.exists(visualFilePath)) {
68                 byte[] bytes = Files.readAllBytes(visualFilePath);
69                 ByteBuffer visualization = ByteBuffer.wrap(bytes);
70                 elementEntity.setVisualization(visualization);
71             }
72
73             //load searchable
74             Path searchableFilePath = Paths.get(elementDir.toString() + separator
75                     + ELEMENT_SEARCHABLE_PREFIX + elementId);
76             if (Files.exists(searchableFilePath)) {
77                 byte[] bytes = Files.readAllBytes(searchableFilePath);
78                 ByteBuffer searchable = ByteBuffer.wrap(bytes);
79                 elementEntity.setSearchableData(searchable);
80             }
81             String element_Id = pathObjects[pathObjects.length - 1];
82             elementEntity.setSpace(pathObjects[2]);
83             elementEntity.setItemId(pathObjects[0]);
84             elementEntity.setVersionId(pathObjects[1]);
85             elementEntity.setElement_id(element_Id);
86             elementEntity.setNamespace(getNameSpace(pathObjects));
87             elementEntity.setParentId(getParentId(pathObjects));
88             elementEntity.setSubElementIds(getAllSubElementsIds(elementDir, element_Id));
89             elementCassandraLoader.createEntity(elementEntity);
90             cassandraElementRepository.createElementNamespace(elementEntity);
91             versionCassandraLoader.insertElementToVersion(elementEntity);
92         } catch (Exception ex) {
93             logger.error(ex.getMessage(), ex);
94         }
95     }
96
97     private String getParentId(String[] pathObjects) {
98
99         if (pathObjects[pathObjects.length - 1].equals(ROOT_ITEM)) {
100             return null;
101         }
102         if (pathObjects.length == 4) {
103             return ROOT_ITEM;
104         }
105         return pathObjects[pathObjects.length - 2];
106     }
107
108     private Set<String> getAllSubElementsIds(Path root, String elementId) throws IOException {
109         if (elementId.equals(ROOT_ITEM)) {
110             root = root.getParent();
111         }
112         File file = root.toFile();
113         Set<String> retVal = new HashSet<>();
114         File[] files = file.listFiles();
115         for (File f : files){
116             if (f.isDirectory()){
117                 retVal.add(f.getName());
118             }
119         }
120         retVal.remove(ROOT_ITEM);
121         return retVal;
122
123     }
124
125     private String getNameSpace(String[] pathObjects) {
126         if (pathObjects.length <= 4) {
127             return "";
128         }
129         if (pathObjects.length == 5) {
130             return pathObjects[3];
131         }
132         return Arrays.stream(pathObjects, 3, pathObjects.length - 1)
133                 .reduce("", (s1, s2) -> s1 + File.separator + s2);
134     }
135 }