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.BoundStatement;
24 import com.datastax.driver.core.Host;
25 import com.datastax.driver.core.PreparedStatement;
26 import com.datastax.driver.core.ResultSet;
27 import com.datastax.driver.core.Session;
28 import com.datastax.driver.mapping.MappingManager;
29 import org.openecomp.core.nosqldb.api.NoSqlDb;
30 import org.openecomp.core.nosqldb.util.CassandraUtils;
31 import org.openecomp.core.utilities.CommonMethods;
32 import org.openecomp.sdc.common.errors.CoreException;
33 import org.openecomp.sdc.common.errors.ErrorCategory;
34 import org.openecomp.sdc.common.errors.ErrorCode;
35 import org.openecomp.sdc.logging.api.Logger;
36 import org.openecomp.sdc.logging.api.LoggerFactory;
39 import java.util.stream.Collectors;
41 class CassandraNoSqlDbImpl implements NoSqlDb {
43 private final Session session;
44 private final String keySpace;
45 private final MappingManager mappingManager;
47 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
50 public CassandraNoSqlDbImpl(Session session) {
51 this.session = session;
52 this.keySpace = this.session.getLoggedKeyspace();
53 this.mappingManager = new MappingManager(this.session);
58 public void insert(String tableName, String[] colNames, Object[] values) {
59 if (colNames.length != values.length) {
60 throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(
61 "number of colmuns[" + colNames.length + "] is not equal to the number of values["
62 + values.length + "].").withId("E0005").withCategory(ErrorCategory.APPLICATION)
66 StringBuilder sb = new StringBuilder();
67 sb.append("insert into ")
70 .append(CommonMethods.arrayToCommaSeparatedString(colNames))
72 .append(CommonMethods.duplicateStringWithDelimiter("?", ',', values.length))
74 System.out.println(sb.toString());
75 PreparedStatement prepared = session.prepare(sb.toString());
78 bound = prepared.bind(values);
79 session.execute(bound);
84 public ResultSet execute(String statement) {
85 return session.execute(statement);
89 public ResultSet execute(String statementName, Object... values) {
91 String statement = CassandraUtils.getStatement(statementName);
92 if (statement == null) {
93 statement = statementName;
96 PreparedStatement prepared = session.prepare(statement);
99 bound = prepared.bind(values);
100 return session.execute(bound);
102 return session.execute(statement);
108 public MappingManager getMappingManager() {
109 return mappingManager;
113 public String getVersion() {
115 Set<Host> allHosts = this.session.getCluster().getMetadata().getAllHosts();
116 Set<String> versions = allHosts.stream().map(host -> host.getCassandraVersion().toString())
117 .collect(Collectors.toSet());
118 return versions.stream().collect(Collectors.joining(","));
119 } catch (Exception e){
121 return "Failed to retrieve version";