ebce90fb4a7c51e72b57e8db3203e6dd334eb45a
[sdc.git] /
1 package org.openecomp.core.tools.Commands.exportdata;
2
3
4 import org.openecomp.core.tools.store.ElementCassandraLoader;
5 import org.openecomp.core.tools.store.zusammen.datatypes.ElementEntity;
6 import org.openecomp.sdc.logging.api.Logger;
7 import org.openecomp.sdc.logging.api.LoggerFactory;
8
9 import java.nio.ByteBuffer;
10 import java.nio.file.Path;
11 import java.nio.file.Paths;
12 import java.util.Objects;
13
14 import static java.io.File.separator;
15 import static java.nio.file.Files.*;
16 import static org.openecomp.core.tools.Commands.exportdata.ImportProperties.*;
17
18 public class ElementHandler {
19
20     private static final Logger logger = LoggerFactory.getLogger(ElementHandler.class);
21
22     public ElementHandler() {
23     }
24
25     public void loadElements(String filteredItem) {
26         ElementCassandraLoader elementCassandraLoader = new ElementCassandraLoader();
27         elementCassandraLoader.list().forEach(elementEntity ->  handleElementEntity(elementEntity,filteredItem));
28     }
29
30     private void handleElementEntity(ElementEntity elementEntity, String filteredItem) {
31         try {
32             String itemId = elementEntity.getItemId();
33             if (filteredItem != null && !itemId.contains(filteredItem)){
34                 return;
35             }
36             String versionId = elementEntity.getVersionId();
37             String space = elementEntity.getSpace();
38             String namespace = elementEntity.getNamespace();
39             String elementId = elementEntity.getElement_id();
40
41             String namespacePath = separator;
42             if (!isNull(namespace)){
43                 namespacePath =  namespace.replace(ELEMENT_NAMESPACE_SPLITTER,separator)+separator;
44             }
45             Path elementDirectoryPath = Paths.get( ROOT_DIRECTORY + separator + itemId
46                     + separator + versionId + separator + space + separator + namespacePath+ separator + elementId);
47             if (notExists(elementDirectoryPath)) {
48                 createDirectories(elementDirectoryPath);
49             }
50
51             String info = elementEntity.getInfo();
52             if (!isNull(info)) {
53                 Path infoFilePath = Paths.get(elementDirectoryPath.toString() + separator + ELEMENT_INFO_PREFIX
54                         + elementId + JSON_POSTFIX);
55                 write(infoFilePath, info.getBytes());
56             }
57
58             String relations = elementEntity.getRelations();
59             if (!isNull(relations)) {
60                 Path realtionsFilePath = Paths.get(elementDirectoryPath.toString() + separator
61                         + ELEMENT_RELATION_PREFIX + elementId + JSON_POSTFIX);
62                 write(realtionsFilePath, relations.getBytes());
63             }
64
65             ByteBuffer data = elementEntity.getData();
66             if (!Objects.isNull(data)) {
67                 Path dataFilePath = Paths.get(elementDirectoryPath.toString() + separator
68                         + ELEMENT_DATA_PREFIX + elementId + JSON_POSTFIX);
69                 write(dataFilePath, data.array());
70             }
71
72             ByteBuffer visualization = elementEntity.getVisualization();
73             if (!Objects.isNull(visualization)) {
74                 Path visualFilePath = Paths.get(elementDirectoryPath.toString() + separator
75                         + ELEMENT_VISUALIZATION_PREFIX + elementId );
76                 write(visualFilePath, visualization.array());
77             }
78
79             ByteBuffer searchableData = elementEntity.getSearchableData();
80             if (!Objects.isNull(searchableData)) {
81                 Path searchableFilePath = Paths.get(elementDirectoryPath.toString() + separator
82                         + ELEMENT_SEARCHABLE_PREFIX + elementId);
83                 write(searchableFilePath, searchableData.array());
84             }
85
86         } catch (Exception ex) {
87             logger.error(ex.getMessage(), ex);
88             ex.printStackTrace();
89         }
90
91     }
92
93     private boolean isNull(String inStr){
94         if (Objects.isNull(inStr)){
95             return true;
96         }
97         return inStr.trim().equalsIgnoreCase("null");
98     }
99
100 }