71bc146bacfacd7b4a99c613edfd881a95646482
[sdc.git] /
1 /*
2  * Copyright © 2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15 */
16
17 package org.openecomp.core.nosqldb.impl.cassandra;
18
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;
33
34 import java.util.Set;
35 import java.util.stream.Collectors;
36
37 class CassandraNoSqlDbImpl implements NoSqlDb {
38
39     private final Session session;
40     private final String keySpace;
41     private final MappingManager mappingManager;
42
43     private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
44
45
46     public CassandraNoSqlDbImpl(Session session) {
47         this.session = session;
48         this.keySpace = this.session.getLoggedKeyspace();
49         this.mappingManager = new MappingManager(this.session);
50
51     }
52
53     @Override
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)
59                     .build());
60         }
61
62         StringBuilder sb = new StringBuilder();
63         sb.append("insert into ")
64                 .append(tableName)
65                 .append(" (")
66                 .append(CommonMethods.arrayToCommaSeparatedString(colNames))
67                 .append(") values (")
68                 .append(CommonMethods.duplicateStringWithDelimiter("?", ',', values.length))
69                 .append(")");
70         log.info(sb.toString());
71         PreparedStatement prepared = session.prepare(sb.toString());
72
73         BoundStatement bound;
74         bound = prepared.bind(values);
75         session.execute(bound);
76
77     }
78
79     @Override
80     public ResultSet execute(String statement) {
81         return session.execute(statement);
82     }
83
84     @Override
85     public ResultSet execute(String statementName, Object... values) {
86
87         String statement = CassandraUtils.getStatement(statementName);
88         if (statement == null) {
89             statement = statementName;
90         }
91         if (values != null) {
92             PreparedStatement prepared = session.prepare(statement);
93
94             BoundStatement bound;
95             bound = prepared.bind(values);
96             return session.execute(bound);
97         } else {
98             return session.execute(statement);
99         }
100
101     }
102
103     @Override
104     public MappingManager getMappingManager() {
105         return mappingManager;
106     }
107
108     @Override
109     public String getVersion() {
110         try {
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";
118         }
119     }
120 }