2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.core.nosqldb.impl.cassandra;
23 import com.datastax.driver.core.*;
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 = (Logger) 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 System.out.println(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){
117 return "Failed to retrieve version";