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 java.io.IOException;
13 import java.nio.ByteBuffer;
14 import java.nio.file.Path;
15 import java.util.Base64;
16 import java.util.Date;
17 import java.util.HashMap;
18 import java.util.List;
21 import java.util.stream.Collectors;
22 import org.apache.commons.lang3.StringUtils;
23 import org.codehaus.jackson.map.ObjectMapper;
24 import org.openecomp.core.nosqldb.impl.cassandra.CassandraSessionFactory;
25 import org.openecomp.core.tools.exportinfo.ExportDataCommand;
26 import org.openecomp.core.tools.model.ColumnDefinition;
27 import org.openecomp.core.tools.model.TableData;
28 import org.openecomp.core.tools.util.Utils;
29 import org.openecomp.sdc.logging.api.Logger;
30 import org.openecomp.sdc.logging.api.LoggerFactory;
32 public class ImportSingleTable {
34 private static final Logger logger = LoggerFactory.getLogger(ImportSingleTable.class);
36 private static final String INSERT_INTO = "INSERT INTO ";
37 private static final String VALUES = " VALUES ";
38 private static final Map<String, PreparedStatement> statementsCache = new HashMap<>();
40 public void importFile(Path file) {
42 ObjectMapper objectMapper = new ObjectMapper();
43 TableData tableData = objectMapper.readValue(file.toFile(), TableData.class);
44 Session session = CassandraSessionFactory.getSession();
45 PreparedStatement ps = getPrepareStatement(tableData, session);
46 tableData.rows.forEach(row -> executeQuery(session, ps, tableData.definitions, row));
47 } catch (IOException e) {
48 Utils.logError(logger, e);
53 private PreparedStatement getPrepareStatement(TableData tableData, Session session) {
54 String query = createQuery(tableData);
55 if (statementsCache.containsKey(query)) {
56 return statementsCache.get(query);
58 PreparedStatement preparedStatement = session.prepare(query);
59 statementsCache.put(query, preparedStatement);
60 return preparedStatement;
63 private void executeQuery(Session session, PreparedStatement ps, List<ColumnDefinition> definitions, List<String> rows) {
64 BoundStatement bind = ps.bind();
65 for (int i = 0; i < definitions.size(); i++) {
66 ColumnDefinition columnDefinition = definitions.get(i);
67 String rowData = rows.get(i);
68 Name name = dataTypesMap.get(columnDefinition.getType());
69 handleByType(bind, i, rowData, name);
71 session.execute(bind);
74 private void handleByType(BoundStatement bind, int i, String rowData, Name name) {
79 String string = new String(Base64.getDecoder().decode(rowData));
80 bind.setString(i, NULL_REPRESENTATION.equals(string) ? null : string);
83 bind.setBytes(i, ByteBuffer.wrap(Base64.getDecoder().decode(rowData.getBytes())));
86 if (StringUtils.isEmpty(rowData)){
87 bind.setTimestamp(i, null);
89 bind.setTimestamp(i, new Date(Long.parseLong(rowData)));
93 bind.setBool(i, Boolean.parseBoolean(rowData));
96 bind.setLong(i, Long.parseLong(rowData));
99 bind.setInt(i, Integer.parseInt(rowData));
102 bind.setFloat(i, Float.parseFloat(rowData));
105 byte[] decoded = Base64.getDecoder().decode(rowData);
106 String decodedStr = new String(decoded);
107 if (!StringUtils.isEmpty(decodedStr)) {
108 String[] splitted = decodedStr.split(ExportDataCommand.JOIN_DELIMITER_SPLITTER);
109 Set set = Sets.newHashSet(splitted);
113 bind.setSet(i, null);
117 byte[] decodedMap = Base64.getDecoder().decode(rowData);
118 String mapStr = new String(decodedMap);
119 if (!StringUtils.isEmpty(mapStr)) {
120 String[] splittedMap = mapStr.split(ExportDataCommand.JOIN_DELIMITER_SPLITTER);
121 Map<String, String> map = new HashMap<>();
122 for (String keyValue : splittedMap) {
123 String[] split = keyValue.split(ExportDataCommand.MAP_DELIMITER_SPLITTER);
124 map.put(split[0], split[1]);
128 bind.setMap(i, null);
132 throw new UnsupportedOperationException("Name is not supported :" + name);
137 private String createQuery(TableData tableData) {
138 ColumnDefinition def = tableData.definitions.iterator().next();
139 StringBuilder sb = new StringBuilder();
140 sb.append(INSERT_INTO).append(def.getKeyspace()).append(".").append(def.getTable());
141 sb.append(tableData.definitions.stream().map(ColumnDefinition::getName).collect(Collectors.joining(" , ", " ( ", " ) ")));
142 sb.append(VALUES).append(tableData.definitions.stream().map(definition -> "?").collect(Collectors.joining(" , ", " ( ", " ) "))).append(";");
143 return sb.toString();
146 public static final ImmutableMap<String, Name> dataTypesMap;
149 Builder<String, Name> builder = ImmutableMap.builder();
150 Name[] values = Name.values();
151 for (Name name : values) {
152 builder.put(name.name().toLowerCase(), name);
154 dataTypesMap = builder.build();