2 * Copyright © 2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.core.nosqldb.impl.cassandra;
19 import com.datastax.driver.core.BoundStatement;
20 import com.datastax.driver.core.Host;
21 import com.datastax.driver.core.PreparedStatement;
22 import com.datastax.driver.core.ResultSet;
23 import com.datastax.driver.core.Session;
24 import com.datastax.driver.mapping.MappingManager;
25 import org.openecomp.core.nosqldb.api.NoSqlDb;
26 import org.openecomp.core.nosqldb.util.CassandraUtils;
27 import org.openecomp.core.utilities.CommonMethods;
28 import org.openecomp.sdc.common.errors.CoreException;
29 import org.openecomp.sdc.common.errors.ErrorCategory;
30 import org.openecomp.sdc.common.errors.ErrorCode;
31 import org.openecomp.sdc.logging.api.Logger;
32 import org.openecomp.sdc.logging.api.LoggerFactory;
35 import java.util.stream.Collectors;
37 class CassandraNoSqlDbImpl implements NoSqlDb {
39 private final Session session;
40 private final String keySpace;
41 private final MappingManager mappingManager;
43 private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
46 public CassandraNoSqlDbImpl(Session session) {
47 this.session = session;
48 this.keySpace = this.session.getLoggedKeyspace();
49 this.mappingManager = new MappingManager(this.session);
54 public void insert(String tableName, String[] colNames, Object[] values) {
55 if (colNames.length != values.length) {
56 throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(
57 "number of colmuns[" + colNames.length + "] is not equal to the number of values["
58 + values.length + "].").withId("E0005").withCategory(ErrorCategory.APPLICATION)
62 StringBuilder sb = new StringBuilder();
63 sb.append("insert into ")
66 .append(CommonMethods.arrayToCommaSeparatedString(colNames))
68 .append(CommonMethods.duplicateStringWithDelimiter("?", ',', values.length))
70 log.info(sb.toString());
71 PreparedStatement prepared = session.prepare(sb.toString());
74 bound = prepared.bind(values);
75 session.execute(bound);
80 public ResultSet execute(String statement) {
81 return session.execute(statement);
85 public ResultSet execute(String statementName, Object... values) {
87 String statement = CassandraUtils.getStatement(statementName);
88 if (statement == null) {
89 statement = statementName;
92 PreparedStatement prepared = session.prepare(statement);
95 bound = prepared.bind(values);
96 return session.execute(bound);
98 return session.execute(statement);
104 public MappingManager getMappingManager() {
105 return mappingManager;
109 public String getVersion() {
111 Set<Host> allHosts = this.session.getCluster().getMetadata().getAllHosts();
112 Set<String> versions = allHosts.stream().map(host -> host.getCassandraVersion().toString())
113 .collect(Collectors.toSet());
114 return versions.stream().collect(Collectors.joining(","));
115 } catch (Exception e){
116 log.error("Failed to retrieve version", e);
117 return "Failed to retrieve version";