1 package org.openecomp.core.tools.importinfo;
3 import static org.openecomp.core.tools.exportinfo.ExportDataCommand.NULL_REPRESENTATION;
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;
22 import java.io.IOException;
23 import java.nio.ByteBuffer;
24 import java.nio.file.Path;
26 import java.util.stream.Collectors;
28 public class ImportSingleTable {
30 private static final Logger logger = LoggerFactory.getLogger(ImportSingleTable.class);
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<>();
36 public static final ImmutableMap<String, Name> dataTypesMap;
38 public void importFile(Path file) {
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);
51 private PreparedStatement getPrepareStatement(TableData tableData, Session session) {
52 String query = createQuery(tableData);
53 if (statementsCache.containsKey(query)) {
54 return statementsCache.get(query);
56 PreparedStatement preparedStatement = session.prepare(query);
57 statementsCache.put(query, preparedStatement);
58 return preparedStatement;
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);
69 session.execute(bind);
72 private void handleByType(BoundStatement bind, int i, String rowData, Name name) {
77 String string = new String(Base64.getDecoder().decode(rowData));
78 bind.setString(i, NULL_REPRESENTATION.equals(string) ? null : string);
81 bind.setBytes(i, ByteBuffer.wrap(Base64.getDecoder().decode(rowData.getBytes())));
84 if (StringUtils.isEmpty(rowData)){
85 bind.setTimestamp(i, null);
87 bind.setTimestamp(i, new Date(Long.parseLong(rowData)));
91 bind.setBool(i, Boolean.parseBoolean(rowData));
94 bind.setLong(i, Long.parseLong(rowData));
97 bind.setInt(i, Integer.parseInt(rowData));
100 bind.setFloat(i, Float.parseFloat(rowData));
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);
111 bind.setSet(i, null);
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]);
126 bind.setMap(i, null);
130 throw new UnsupportedOperationException("Name is not supported :" + name);
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();
148 Builder<String, Name> builder = ImmutableMap.builder();
149 Name[] values = Name.values();
150 for (Name name : values) {
151 builder.put(name.name().toLowerCase(), name);
153 dataTypesMap = builder.build();