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.mapping.MappingManager;
20 import com.datastax.driver.core.BoundStatement;
21 import com.datastax.driver.core.Host;
22 import com.datastax.driver.core.PreparedStatement;
23 import com.datastax.driver.core.ResultSet;
24 import com.datastax.driver.core.Session;
27 import org.openecomp.core.nosqldb.api.NoSqlDb;
28 import org.openecomp.core.nosqldb.util.CassandraUtils;
29 import org.openecomp.core.utilities.CommonMethods;
30 import org.openecomp.sdc.common.errors.CoreException;
31 import org.openecomp.sdc.common.errors.ErrorCategory;
32 import org.openecomp.sdc.common.errors.ErrorCode;
33 import org.openecomp.sdc.logging.api.Logger;
34 import org.openecomp.sdc.logging.api.LoggerFactory;
37 import java.util.stream.Collectors;
39 class CassandraNoSqlDbImpl implements NoSqlDb {
41 private final Session session;
42 private final String keySpace;
43 private final MappingManager mappingManager;
45 private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
48 public CassandraNoSqlDbImpl(Session session) {
49 this.session = session;
50 this.keySpace = this.session.getLoggedKeyspace();
51 this.mappingManager = new MappingManager(this.session);
56 public void insert(String tableName, String[] colNames, Object[] values) {
57 if (colNames.length != values.length) {
58 throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(
59 "number of colmuns[" + colNames.length + "] is not equal to the number of values["
60 + values.length + "].").withId("E0005").withCategory(ErrorCategory.APPLICATION)
64 StringBuilder sb = new StringBuilder();
65 sb.append("insert into ")
68 .append(CommonMethods.arrayToCommaSeparatedString(colNames))
70 .append(CommonMethods.duplicateStringWithDelimiter("?", ',', values.length))
72 log.info(sb.toString());
73 PreparedStatement prepared = session.prepare(sb.toString());
76 bound = prepared.bind(values);
77 session.execute(bound);
82 public ResultSet execute(String statement) {
83 return session.execute(statement);
87 public ResultSet execute(String statementName, Object... values) {
89 String statement = CassandraUtils.getStatement(statementName);
90 if (statement == null) {
91 statement = statementName;
94 PreparedStatement prepared = session.prepare(statement);
97 bound = prepared.bind(values);
98 return session.execute(bound);
100 return session.execute(statement);
106 public MappingManager getMappingManager() {
107 return mappingManager;
111 public String getVersion() {
113 Set<Host> allHosts = this.session.getCluster().getMetadata().getAllHosts();
114 Set<String> versions = allHosts.stream().map(host -> host.getCassandraVersion().toString())
115 .collect(Collectors.toSet());
116 return versions.stream().collect(Collectors.joining(","));
117 } catch (Exception e){
118 log.error("Failed to retrieve version", e);
119 return "Failed to retrieve version";