1 package org.openecomp.core.tools.Commands.importdata;
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;
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;
22 import static java.io.File.separator;
23 import static org.openecomp.core.tools.Commands.exportdata.ImportProperties.*;
25 public class ElementImport {
26 private static final Logger logger = LoggerFactory.getLogger(ElementImport.class);
27 public static final String ROOT_ITEM = Id.ZERO.getValue();
29 private ElementCassandraLoader elementCassandraLoader = new ElementCassandraLoader();
30 private ElementNamespaceHandler cassandraElementRepository = new ElementNamespaceHandler();
31 private VersionCassandraLoader versionCassandraLoader = new VersionCassandraLoader();
33 public void loadPath(SessionContext sessionContext, Path elementDir, String elementId, String[]
36 if (!Files.isDirectory(elementDir)){
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);
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);
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);
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);
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);
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);
97 private String getParentId(String[] pathObjects) {
99 if (pathObjects[pathObjects.length - 1].equals(ROOT_ITEM)) {
102 if (pathObjects.length == 4) {
105 return pathObjects[pathObjects.length - 2];
108 private Set<String> getAllSubElementsIds(Path root, String elementId) throws IOException {
109 if (elementId.equals(ROOT_ITEM)) {
110 root = root.getParent();
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());
120 retVal.remove(ROOT_ITEM);
125 private String getNameSpace(String[] pathObjects) {
126 if (pathObjects.length <= 4) {
129 if (pathObjects.length == 5) {
130 return pathObjects[3];
132 return Arrays.stream(pathObjects, 3, pathObjects.length - 1)
133 .reduce("", (s1, s2) -> s1 + File.separator + s2);