93410bc58133ecf1a1e138fb45a56878f0fe8fd5
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.core.nosqldb.impl.cassandra;
22
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;
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 = (Logger) 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         System.out.println(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.debug("",e);
117             return "Failed to retrieve version";
118         }
119     }
120 }