2172e1bc2f0f83c8502d62fc3eec6da3196a1f3c
[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.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;
37
38 import java.util.Set;
39 import java.util.stream.Collectors;
40
41 class CassandraNoSqlDbImpl implements NoSqlDb {
42
43     private final Session session;
44     private final String keySpace;
45     private final MappingManager mappingManager;
46
47     private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
48
49
50     public CassandraNoSqlDbImpl(Session session) {
51         this.session = session;
52         this.keySpace = this.session.getLoggedKeyspace();
53         this.mappingManager = new MappingManager(this.session);
54
55     }
56
57     @Override
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)
63                     .build());
64         }
65
66         StringBuilder sb = new StringBuilder();
67         sb.append("insert into ")
68                 .append(tableName)
69                 .append(" (")
70                 .append(CommonMethods.arrayToCommaSeparatedString(colNames))
71                 .append(") values (")
72                 .append(CommonMethods.duplicateStringWithDelimiter("?", ',', values.length))
73                 .append(")");
74         System.out.println(sb.toString());
75         PreparedStatement prepared = session.prepare(sb.toString());
76
77         BoundStatement bound;
78         bound = prepared.bind(values);
79         session.execute(bound);
80
81     }
82
83     @Override
84     public ResultSet execute(String statement) {
85         return session.execute(statement);
86     }
87
88     @Override
89     public ResultSet execute(String statementName, Object... values) {
90
91         String statement = CassandraUtils.getStatement(statementName);
92         if (statement == null) {
93             statement = statementName;
94         }
95         if (values != null) {
96             PreparedStatement prepared = session.prepare(statement);
97
98             BoundStatement bound;
99             bound = prepared.bind(values);
100             return session.execute(bound);
101         } else {
102             return session.execute(statement);
103         }
104
105     }
106
107     @Override
108     public MappingManager getMappingManager() {
109         return mappingManager;
110     }
111
112     @Override
113     public String getVersion() {
114         try {
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){
120             log.debug("",e);
121             return "Failed to retrieve version";
122         }
123     }
124 }