re base code
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-nosqldb-lib / openecomp-nosqldb-core / src / main / java / org / openecomp / core / nosqldb / impl / cassandra / CassandraNoSqlDbImpl.java
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.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;
25
26
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;
35
36 import java.util.Set;
37 import java.util.stream.Collectors;
38
39 class CassandraNoSqlDbImpl implements NoSqlDb {
40
41     private final Session session;
42     private final String keySpace;
43     private final MappingManager mappingManager;
44
45     private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
46
47
48     public CassandraNoSqlDbImpl(Session session) {
49         this.session = session;
50         this.keySpace = this.session.getLoggedKeyspace();
51         this.mappingManager = new MappingManager(this.session);
52
53     }
54
55     @Override
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)
61                     .build());
62         }
63
64         StringBuilder sb = new StringBuilder();
65         sb.append("insert into ")
66                 .append(tableName)
67                 .append(" (")
68                 .append(CommonMethods.arrayToCommaSeparatedString(colNames))
69                 .append(") values (")
70                 .append(CommonMethods.duplicateStringWithDelimiter("?", ',', values.length))
71                 .append(")");
72         log.info(sb.toString());
73         PreparedStatement prepared = session.prepare(sb.toString());
74
75         BoundStatement bound;
76         bound = prepared.bind(values);
77         session.execute(bound);
78
79     }
80
81     @Override
82     public ResultSet execute(String statement) {
83         return session.execute(statement);
84     }
85
86     @Override
87     public ResultSet execute(String statementName, Object... values) {
88
89         String statement = CassandraUtils.getStatement(statementName);
90         if (statement == null) {
91             statement = statementName;
92         }
93         if (values != null) {
94             PreparedStatement prepared = session.prepare(statement);
95
96             BoundStatement bound;
97             bound = prepared.bind(values);
98             return session.execute(bound);
99         } else {
100             return session.execute(statement);
101         }
102
103     }
104
105     @Override
106     public MappingManager getMappingManager() {
107         return mappingManager;
108     }
109
110     @Override
111     public String getVersion() {
112         try {
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";
120         }
121     }
122 }