summaryrefslogtreecommitdiffstats
path: root/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/Commands/importdata/ElementImport.java
blob: 7ba830906cb656def130188e130c8df587c8009d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package org.openecomp.core.tools.Commands.importdata;

import com.amdocs.zusammen.datatypes.SessionContext;
import org.openecomp.core.tools.store.ElementCassandraLoader;
import org.openecomp.core.tools.store.ElementNamespaceHandler;
import org.openecomp.core.tools.store.VersionCassandraLoader;
import org.openecomp.core.tools.store.zusammen.datatypes.ElementEntity;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.io.File.separator;
import static org.openecomp.core.tools.Commands.exportdata.ImportProperties.*;

public class ElementImport {
    private static final Logger logger = LoggerFactory.getLogger(ElementImport.class);
    private ElementCassandraLoader elementCassandraLoader = new ElementCassandraLoader();
    private ElementNamespaceHandler cassandraElementRepository = new ElementNamespaceHandler();
    private VersionCassandraLoader versionCassandraLoader = new VersionCassandraLoader();

    public void loadPath(SessionContext sessionContext, Path elementDir, String elementId, String[] pathObjects) {
        try {
            // load info file
            ElementEntity elementEntity = new ElementEntity();
            Path infoFilePath = Paths.get(elementDir.toString() + separator + ELEMENT_INFO_PREFIX
                    + elementId + JSON_POSTFIX);
            if (Files.exists(infoFilePath)) {
                String info = new String(Files.readAllBytes(infoFilePath));
                elementEntity.setInfo(info);
            }

            // load relation file
            Path realtionsFilePath = Paths.get(elementDir.toString() + separator
                    + ELEMENT_RELATION_PREFIX + elementId + JSON_POSTFIX);
            if (Files.exists(realtionsFilePath)) {
                String relations = new String(Files.readAllBytes(realtionsFilePath));
                elementEntity.setRelations(relations);
            }

            //load entity data
            Path dataFilePath = Paths.get(elementDir.toString() + separator
                    + ELEMENT_DATA_PREFIX + elementId + JSON_POSTFIX);
            if (Files.exists(dataFilePath)) {
                byte[] bytes = Files.readAllBytes(dataFilePath);
                ByteBuffer data = ByteBuffer.wrap(bytes);
                elementEntity.setData(data);
            }

            //load visualization
            Path visualFilePath = Paths.get(elementDir.toString() + separator
                    + ELEMENT_VISUALIZATION_PREFIX + elementId );
            if (Files.exists(visualFilePath)) {
                byte[] bytes = Files.readAllBytes(visualFilePath);
                ByteBuffer visualization = ByteBuffer.wrap(bytes);
                elementEntity.setVisualization(visualization);
            }

            //load searchable
            Path searchableFilePath = Paths.get(elementDir.toString() + separator
                    + ELEMENT_SEARCHABLE_PREFIX + elementId );
            if (Files.exists(searchableFilePath)) {
                byte[] bytes = Files.readAllBytes(searchableFilePath);
                ByteBuffer searchable = ByteBuffer.wrap(bytes);
                elementEntity.setSearchableData(searchable);
            }

            elementEntity.setSpace(pathObjects[2]);
            elementEntity.setItemId(pathObjects[0]);
            elementEntity.setVersionId(pathObjects[1]);
            elementEntity.setElement_id(pathObjects[pathObjects.length - 1]);
            elementEntity.setNamespace(getNameSpace(pathObjects));
            elementEntity.setParentId(getParentId(pathObjects));
            elementEntity.setSubElementIds(getAllSubElementsIds(elementDir));
            elementCassandraLoader.createEntity(elementEntity);
            cassandraElementRepository.createElementNamespace(elementEntity);
            versionCassandraLoader.insertElementToVersion(elementEntity);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }

    private String getParentId(String[] pathObjects) {
        if (pathObjects.length <= 4) {
            return null;
        }
        return pathObjects[pathObjects.length - 2];
    }

    private Set<String> getAllSubElementsIds(Path root) throws IOException {
        try (Stream<Path> walk = Files.walk(root)) {
           return  walk.filter(path -> Files.isDirectory(path))
                   .map(path -> path.toFile().getName() ).collect(Collectors.toSet());
        }
    }

    private String getNameSpace(String[] pathObjects) {
        if (pathObjects.length <= 4) {
            return null;
        }
        if (pathObjects.length == 5) {
            return pathObjects[3];
        }
        return Arrays.stream(pathObjects, 3, pathObjects.length - 1)
                .reduce("", (s1, s2) -> s1 + File.separator + s2);
    }
}