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;
33 import java.util.stream.Collectors;
35 class CassandraNoSqlDbImpl implements NoSqlDb {
37 private final Session session;
38 private final String keySpace;
39 private final MappingManager mappingManager;
42 public CassandraNoSqlDbImpl(Session session) {
43 this.session = session;
44 this.keySpace = this.session.getLoggedKeyspace();
45 this.mappingManager = new MappingManager(this.session);
50 public void insert(String tableName, String[] colNames, Object[] values) {
51 if (colNames.length != values.length) {
52 throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(
53 "number of colmuns[" + colNames.length + "] is not equal to the number of values["
54 + values.length + "].").withId("E0005").withCategory(ErrorCategory.APPLICATION)
58 StringBuilder sb = new StringBuilder();
59 sb.append("insert into ")
62 .append(CommonMethods.arrayToCommaSeparatedString(colNames))
64 .append(CommonMethods.duplicateStringWithDelimiter("?", ',', values.length))
66 System.out.println(sb.toString());
67 PreparedStatement prepared = session.prepare(sb.toString());
70 bound = prepared.bind(values);
71 session.execute(bound);
76 public ResultSet execute(String statement) {
77 return session.execute(statement);
81 public ResultSet execute(String statementName, Object... values) {
83 String statement = CassandraUtils.getStatement(statementName);
84 if (statement == null) {
85 statement = statementName;
88 PreparedStatement prepared = session.prepare(statement);
91 bound = prepared.bind(values);
92 return session.execute(bound);
94 return session.execute(statement);
100 public MappingManager getMappingManager() {
101 return mappingManager;
105 public String getVersion() {
107 Set<Host> allHosts = this.session.getCluster().getMetadata().getAllHosts();
108 Set<String> versions = allHosts.stream().map(host -> host.getCassandraVersion().toString())
109 .collect(Collectors.toSet());
110 return versions.stream().collect(Collectors.joining(","));
111 } catch (Exception e){
112 return "Failed to retrieve version";