146e173a2835b7776219861fd1afe8b40099a8c5
[sdc.git] /
1 package org.openecomp.core.tools.importinfo;
2
3 import static org.openecomp.core.tools.exportinfo.ExportDataCommand.NULL_REPRESENTATION;
4
5 import com.datastax.driver.core.BoundStatement;
6 import com.datastax.driver.core.DataType.Name;
7 import com.datastax.driver.core.PreparedStatement;
8 import com.datastax.driver.core.Session;
9 import com.google.common.collect.ImmutableMap;
10 import com.google.common.collect.ImmutableMap.Builder;
11 import com.google.common.collect.Sets;
12 import org.apache.commons.lang3.StringUtils;
13 import org.codehaus.jackson.map.ObjectMapper;
14 import org.openecomp.core.nosqldb.impl.cassandra.CassandraSessionFactory;
15 import org.openecomp.core.tools.exportinfo.ExportDataCommand;
16 import org.openecomp.core.tools.model.ColumnDefinition;
17 import org.openecomp.core.tools.model.TableData;
18 import org.openecomp.core.tools.util.Utils;
19 import org.openecomp.sdc.logging.api.Logger;
20 import org.openecomp.sdc.logging.api.LoggerFactory;
21
22 import java.io.IOException;
23 import java.nio.ByteBuffer;
24 import java.nio.file.Path;
25 import java.util.*;
26 import java.util.stream.Collectors;
27
28 public class ImportSingleTable {
29
30     private static final Logger logger = LoggerFactory.getLogger(ImportSingleTable.class);
31
32     private static final String INSERT_INTO = "INSERT INTO ";
33     private static final String VALUES = " VALUES ";
34     private static final Map<String, PreparedStatement> statementsCache = new HashMap<>();
35     
36     public static final ImmutableMap<String, Name> dataTypesMap;
37
38     public void importFile(Path file) {
39         try {
40             ObjectMapper objectMapper = new ObjectMapper();
41             TableData tableData = objectMapper.readValue(file.toFile(), TableData.class);
42             Session session = CassandraSessionFactory.getSession();
43             PreparedStatement ps = getPrepareStatement(tableData, session);
44             tableData.rows.forEach(row -> executeQuery(session, ps, tableData.definitions, row));
45         } catch (IOException e) {
46             Utils.logError(logger, e);
47         }
48
49     }
50
51     private PreparedStatement getPrepareStatement(TableData tableData, Session session) {
52         String query = createQuery(tableData);
53         if (statementsCache.containsKey(query)) {
54             return statementsCache.get(query);
55         }
56         PreparedStatement preparedStatement = session.prepare(query);
57         statementsCache.put(query, preparedStatement);
58         return preparedStatement;
59     }
60
61     private void executeQuery(Session session, PreparedStatement ps, List<ColumnDefinition> definitions, List<String> rows) {
62         BoundStatement bind = ps.bind();
63         for (int i = 0; i < definitions.size(); i++) {
64             ColumnDefinition columnDefinition = definitions.get(i);
65             String rowData = rows.get(i);
66             Name name = dataTypesMap.get(columnDefinition.getType());
67             handleByType(bind, i, rowData, name);
68         }
69         session.execute(bind);
70     }
71
72     private void handleByType(BoundStatement bind, int i, String rowData, Name name) {
73         switch (name) {
74             case VARCHAR:
75             case TEXT:
76             case ASCII:
77                 String string = new String(Base64.getDecoder().decode(rowData));
78                 bind.setString(i, NULL_REPRESENTATION.equals(string) ? null : string);
79                 break;
80             case BLOB:
81                 bind.setBytes(i, ByteBuffer.wrap(Base64.getDecoder().decode(rowData.getBytes())));
82                 break;
83             case TIMESTAMP:
84                 if (StringUtils.isEmpty(rowData)){
85                     bind.setTimestamp(i, null);
86                 } else {
87                     bind.setTimestamp(i, new Date(Long.parseLong(rowData)));
88                 }
89                 break;
90             case BOOLEAN:
91                 bind.setBool(i, Boolean.parseBoolean(rowData));
92                 break;
93             case COUNTER:
94                 bind.setLong(i, Long.parseLong(rowData));
95                 break;
96             case INT:
97                 bind.setInt(i, Integer.parseInt(rowData));
98                 break;
99             case FLOAT:
100                 bind.setFloat(i, Float.parseFloat(rowData));
101                 break;
102             case SET:
103                 byte[] decoded = Base64.getDecoder().decode(rowData);
104                 String decodedStr = new String(decoded);
105                 if (!StringUtils.isEmpty(decodedStr)) {
106                     String[] splitted = decodedStr.split(ExportDataCommand.JOIN_DELIMITER_SPLITTER);
107                     Set set = Sets.newHashSet(splitted);
108                     set.remove("");
109                     bind.setSet(i, set);
110                 } else {
111                     bind.setSet(i, null);
112                 }
113                 break;
114             case MAP:
115                 byte[] decodedMap = Base64.getDecoder().decode(rowData);
116                 String mapStr = new String(decodedMap);
117                 if (!StringUtils.isEmpty(mapStr)) {
118                     String[] splittedMap = mapStr.split(ExportDataCommand.JOIN_DELIMITER_SPLITTER);
119                     Map<String, String> map = new HashMap<>();
120                     for (String keyValue : splittedMap) {
121                         String[] split = keyValue.split(ExportDataCommand.MAP_DELIMITER_SPLITTER);
122                         map.put(split[0], split[1]);
123                     }
124                     bind.setMap(i, map);
125                 } else {
126                     bind.setMap(i, null);
127                 }
128                 break;
129             default:
130                 throw new UnsupportedOperationException("Name is not supported :" + name);
131
132         }
133     }
134
135     private String createQuery(TableData tableData) {
136         ColumnDefinition def = tableData.definitions.iterator().next();
137         StringBuilder sb = new StringBuilder(1024);
138         sb.append(INSERT_INTO).append(def.getKeyspace()).append(".").append(def.getTable());
139         sb.append(tableData.definitions.stream().map(ColumnDefinition::getName)
140                 .collect(Collectors.joining(" , ", " ( ", " ) ")));
141         sb.append(VALUES).append(tableData.definitions.stream().map(definition -> "?")
142                 .collect(Collectors.joining(" , ", " ( ", " ) "))).append(";");
143         return sb.toString();
144     }
145
146
147     static {
148         Builder<String, Name> builder = ImmutableMap.builder();
149         Name[] values = Name.values();
150         for (Name name : values) {
151             builder.put(name.name().toLowerCase(), name);
152         }
153         dataTypesMap = builder.build();
154     }
155
156 }