1 package org.openecomp.core.tools.Commands.importdata;
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;
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;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
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 private ElementCassandraLoader elementCassandraLoader = new ElementCassandraLoader();
28 private ElementNamespaceHandler cassandraElementRepository = new ElementNamespaceHandler();
29 private VersionCassandraLoader versionCassandraLoader = new VersionCassandraLoader();
31 public void loadPath(SessionContext sessionContext, Path elementDir, String elementId, String[] pathObjects) {
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);
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);
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);
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);
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);
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);
92 private String getParentId(String[] pathObjects) {
93 if (pathObjects.length <= 4) {
96 return pathObjects[pathObjects.length - 2];
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());
106 private String getNameSpace(String[] pathObjects) {
107 if (pathObjects.length <= 4) {
110 if (pathObjects.length == 5) {
111 return pathObjects[3];
113 return Arrays.stream(pathObjects, 3, pathObjects.length - 1)
114 .reduce("", (s1, s2) -> s1 + File.separator + s2);