summaryrefslogtreecommitdiffstats
path: root/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/Commands/exportdata/ElementHandler.java
blob: 49eaaeaa76fca23617736c0e79daab75eae0c1e6 (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
package org.openecomp.core.tools.Commands.exportdata;


import org.openecomp.core.tools.store.ElementCassandraLoader;
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.nio.ByteBuffer;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.Set;

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

public class ElementHandler {

    private static final Logger logger = LoggerFactory.getLogger(ElementHandler.class);

    public ElementHandler() {
    }

    public void loadElements(Set<String> filteredItem) {
        ElementCassandraLoader elementCassandraLoader = new ElementCassandraLoader();
          elementCassandraLoader.list().forEach(elementEntity ->  handleElementEntity(elementEntity,filteredItem));
    }

    private void handleElementEntity(ElementEntity elementEntity, Set<String> filteredItem) {
        try {
            String itemId = elementEntity.getItemId();

            if (!filteredItem.isEmpty() && !filteredItem.contains(itemId)){
                return;
            }
            String versionId = elementEntity.getVersionId();
            String space = elementEntity.getSpace();
            String namespace = elementEntity.getNamespace();
            String elementId = elementEntity.getElement_id();

            String namespacePath = separator;
            if (!isNull(namespace)){
                namespacePath =  namespace.replace(ELEMENT_NAMESPACE_SPLITTER,separator)+separator;
            }
            Path elementDirectoryPath;
            if (!isNull(namespace)){
                elementDirectoryPath = Paths.get( ROOT_DIRECTORY + separator + itemId
                        + separator + versionId + separator + space + separator + namespacePath+ separator + elementId);
            } else {
                elementDirectoryPath = Paths.get( ROOT_DIRECTORY + separator + itemId
                        + separator + versionId + separator + space + separator + elementId);
              }

            if (notExists(elementDirectoryPath)) {
                 Path created = createDirectories(elementDirectoryPath);
             }

            String info = elementEntity.getInfo();
            if (!isNull(info)) {
                Path infoFilePath = Paths.get(elementDirectoryPath.toString() + separator + ELEMENT_INFO_PREFIX
                        + elementId + JSON_POSTFIX);
                write(infoFilePath, info.getBytes());
            }

            String relations = elementEntity.getRelations();
            if (!isNull(relations)) {
                Path realtionsFilePath = Paths.get(elementDirectoryPath.toString() + separator
                        + ELEMENT_RELATION_PREFIX + elementId + JSON_POSTFIX);
                write(realtionsFilePath, relations.getBytes());
            }

            ByteBuffer data = elementEntity.getData();
            if (!Objects.isNull(data)) {
                Path dataFilePath = Paths.get(elementDirectoryPath.toString() + separator
                        + ELEMENT_DATA_PREFIX + elementId + JSON_POSTFIX);
                write(dataFilePath, data.array());
            }

            ByteBuffer visualization = elementEntity.getVisualization();
            if (!Objects.isNull(visualization)) {
                Path visualFilePath = Paths.get(elementDirectoryPath.toString() + separator
                        + ELEMENT_VISUALIZATION_PREFIX + elementId );
                write(visualFilePath, visualization.array());
            }

            ByteBuffer searchableData = elementEntity.getSearchableData();
            if (!Objects.isNull(searchableData)) {
                Path searchableFilePath = Paths.get(elementDirectoryPath.toString() + separator
                        + ELEMENT_SEARCHABLE_PREFIX + elementId);
                write(searchableFilePath, searchableData.array());
            }

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            ex.printStackTrace();
        }

    }

    private boolean isNull(String inStr){
        if (Objects.isNull(inStr)){
            return true;
        }
        return inStr.trim().equalsIgnoreCase("null");
    }

}