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.sdc.ci.tests.utils.cassandra;
23 import java.io.FileNotFoundException;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
28 import org.javatuples.Pair;
29 import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants;
30 import org.openecomp.sdc.ci.tests.utils.Utils;
31 import org.openecomp.sdc.common.datastructure.AuditingFieldsKeysEnum;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.datastax.driver.core.Cluster;
36 import com.datastax.driver.core.KeyspaceMetadata;
37 import com.datastax.driver.core.Metadata;
38 import com.datastax.driver.core.Row;
39 import com.datastax.driver.core.Session;
40 import com.datastax.driver.core.TableMetadata;
41 import com.datastax.driver.core.querybuilder.QueryBuilder;
42 import com.datastax.driver.core.querybuilder.Select;
43 import com.datastax.driver.core.querybuilder.Select.Where;
45 public final class CassandraUtils {
46 private static Logger logger = LoggerFactory.getLogger(CassandraUtils.class.getName());
48 protected static Cluster cluster = null;
49 protected static Session session;
51 protected static void initConnection(String keyspace) throws FileNotFoundException {
53 String cassandraHost = Utils.getConfig().getCassandraHost();
54 Boolean cassandraAuthenticate = Utils.getConfig().getCassandraAuthenticate();
55 String cassandraUsername = Utils.getConfig().getCassandraUsername();
56 String cassandraPassword = Utils.getConfig().getCassandraPassword();
57 Boolean cassandraSsl = Utils.getConfig().getCassandraSsl();
58 String cassandraTruststorePath = Utils.getConfig().getCassandraTruststorePath();
59 String cassandraTruststorePassword = Utils.getConfig().getCassandraTruststorePassword();
61 * String cassandraAuditKeySpace=
62 * Utils.getConfig().getCassandraAuditKeySpace(); String
63 * cassandraArtifactKeySpace=
64 * Utils.getConfig().getCassandraArtifactKeySpace();
67 Cluster.Builder clusterBuilder = Cluster.builder().addContactPoint(cassandraHost);
68 if (cassandraAuthenticate) {
70 clusterBuilder.withCredentials(cassandraUsername, cassandraPassword);
75 System.setProperty("javax.net.ssl.trustStore", cassandraTruststorePath);
76 System.setProperty("javax.net.ssl.trustStorePassword", cassandraTruststorePassword);
77 clusterBuilder.withSSL();
80 cluster = clusterBuilder.build();
81 session = cluster.connect(keyspace);
85 public static void truncateTable(String keyspace, String tableName) throws FileNotFoundException {
87 if (session == null || session.isClosed()) {
88 initConnection(keyspace);
93 if (session != null) {
94 session.execute(QueryBuilder.truncate(keyspace, tableName));
95 logger.debug("The table {}.{} was cleaned",keyspace,tableName);
97 throw new RuntimeException("Keyspace " + keyspace + " not connected");
100 // if (cluster != null) {
106 public static void close() {
107 if (cluster != null) {
112 public static void truncateAllKeyspaces() throws FileNotFoundException {
113 // truncateAllTables(AuditingTypesConstants.ARTIFACT_KEYSPACE);
114 truncateAllTables(AuditingTypesConstants.AUDIT_KEYSPACE);
117 public static void truncateAllTables(String keyspace) throws FileNotFoundException {
119 if (session == null || session.isClosed()) {
120 initConnection(keyspace);
124 if (session != null) {
125 Metadata metadata = cluster.getMetadata();
126 KeyspaceMetadata keyspaceMetadata = metadata.getKeyspace(keyspace);
127 if (keyspaceMetadata != null) {
128 Collection<TableMetadata> tables = keyspaceMetadata.getTables();
129 tables.forEach(table -> {
130 session.execute(QueryBuilder.truncate(table));
131 logger.debug("Table trunceted - {}",table.getName());
135 throw new RuntimeException("Keyspace " + keyspace + " not connected");
139 // if (cluster != null) {
145 public static List<Row> fetchFromTable(String keyspace, String tableName,
146 List<Pair<AuditingFieldsKeysEnum, String>> fields) throws FileNotFoundException {
148 List<Pair<String, String>> fieldsConverted = new ArrayList<>();
150 // fields.forEach(pair -> {
151 // Pair<String, String> newPair = new Pair(pair.getValue0().getDisplayName(), pair.getValue1());
152 // fieldsConverted.add(newPair);
155 fields.forEach(pair ->{
156 Pair<String, String> newPair;
157 if(pair.getValue0() == AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_RESOURCE_URL ){
158 newPair = new Pair<String, String>("RESOURE_URL", pair.getValue1());
161 newPair = new Pair<String, String>(pair.getValue0().getDisplayName(), pair.getValue1());
163 fieldsConverted.add(newPair);
167 return fetchFromTableQuery(keyspace, tableName, fieldsConverted);
170 public static List<Row> fetchFromTableQuery(String keyspace, String tableName, List<Pair<String, String>> fields)
171 throws FileNotFoundException {
173 if (session == null || session.isClosed()) {
174 initConnection(keyspace);
178 if (session != null) {
179 Select select = QueryBuilder.select().all().from(keyspace, tableName);
180 if (fields != null) {
181 // Set<Entry<AuditingFieldsKeysEnum, String>> entrySet =
182 // fields.entrySet();
184 boolean multiple = (fields.size() > 1) ? true : false;
188 for (Pair<String, String> pair : fields) {
191 where = select.where(QueryBuilder.eq(pair.getValue0(), pair.getValue1()));
193 where.and(QueryBuilder.eq(pair.getValue0(), pair.getValue1()));
197 select.allowFiltering();
202 List<Row> rows = session.execute(select).all();
203 for (Row row : rows) {
204 logger.debug("{}", row);
209 // if (cluster != null) {
216 // public static void main(String[] args) throws FileNotFoundException {
217 // Map<AuditingFieldsKeysEnum, String> map = new HashMap<>();
218 // map.put(AuditingFieldsKeysEnum.AUDIT_ACTION, "Access");
219 // map.put(AuditingFieldsKeysEnum.AUDIT_STATUS, "200");
220 // // CassandraUtils.truncateTable("sdcartifact", "resources");
221 //// CassandraUtils.truncateAllTables("sdcaudit");
222 // CassandraUtils.fetchFromTable("sdcaudit", "useraccessevent", map );