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 com.datastax.driver.core.*;
24 import com.datastax.driver.core.querybuilder.QueryBuilder;
25 import com.datastax.driver.core.querybuilder.Select;
26 import com.datastax.driver.core.querybuilder.Select.Where;
27 import org.javatuples.Pair;
28 import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants;
29 import org.openecomp.sdc.ci.tests.utils.Utils;
30 import org.openecomp.sdc.common.datastructure.AuditingFieldsKeysEnum;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import java.io.FileNotFoundException;
35 import java.util.Collection;
36 import java.util.List;
38 public final class CassandraUtils2 {
39 private static Logger logger = LoggerFactory.getLogger(CassandraUtils2.class.getName());
41 public static void truncateTable(String keyspace, String tableName) throws FileNotFoundException {
43 String cassandraHost = Utils.getConfig().getCassandraHost();
45 Cluster cluster = null;
49 Cluster.Builder clusterBuilder = Cluster.builder().addContactPoint(cassandraHost);
51 // clusterBuilder.withCredentials(username,password);
53 // System.setProperty("javax.net.ssl.trustStore",truststorePath);
54 // System.setProperty("javax.net.ssl.trustStorePassword",truststorePassword);
55 // clusterBuilder.withSSL();
56 cluster = clusterBuilder.build();
57 session = cluster.connect(keyspace);
58 if (session != null) {
59 session.execute(QueryBuilder.truncate(keyspace, tableName));
60 logger.debug("The table {}.{} was cleaned",keyspace,tableName);
62 throw new RuntimeException("Keyspace " + keyspace + " not connected");
65 if (cluster != null) {
71 public static void truncateAllKeyspaces() throws FileNotFoundException {
72 truncateAllTables(AuditingTypesConstants.ARTIFACT_KEYSPACE);
73 truncateAllTables(AuditingTypesConstants.AUDIT_KEYSPACE);
76 public static void truncateAllTables(String keyspace) throws FileNotFoundException {
77 String cassandraHost = Utils.getConfig().getCassandraHost();
79 Cluster cluster = null;
83 cluster = Cluster.builder().addContactPoint(cassandraHost).build();
84 session = cluster.connect(keyspace);
85 if (session != null) {
86 Metadata metadata = cluster.getMetadata();
87 KeyspaceMetadata keyspaceMetadata = metadata.getKeyspace(keyspace);
88 if (keyspaceMetadata != null) {
89 Collection<TableMetadata> tables = keyspaceMetadata.getTables();
90 tables.forEach(table -> {
91 session.execute(QueryBuilder.truncate(table));
92 logger.debug("Table trunceted - {}",table.getName());
96 throw new RuntimeException("Keyspace " + keyspace + " not connected");
100 if (cluster != null) {
106 public static List<Row> fetchFromTable(String keyspace, String tableName,
107 List<Pair<AuditingFieldsKeysEnum, String>> fields) throws FileNotFoundException {
109 // List<Pair<AuditingFieldsKeysEnum, String>>
110 // Map<AuditingFieldsKeysEnum, String>
112 Cluster cluster = null;
114 String cassandraHost = Utils.getConfig().getCassandraHost();
117 cluster = Cluster.builder().addContactPoint(cassandraHost).build();
118 session = cluster.connect(keyspace);
119 if (session != null) {
120 Select select = QueryBuilder.select().all().from(keyspace, tableName);
121 if (fields != null) {
122 // Set<Entry<AuditingFieldsKeysEnum, String>> entrySet =
123 // fields.entrySet();
125 boolean multiple = (fields.size() > 1) ? true : false;
129 for (Pair<AuditingFieldsKeysEnum, String> pair : fields) {
132 where = select.where(QueryBuilder.eq(pair.getValue0().getDisplayName(), pair.getValue1()));
134 where.and(QueryBuilder.eq(pair.getValue0().getDisplayName(), pair.getValue1()));
138 select.allowFiltering();
143 List<Row> rows = session.execute(select).all();
144 for (Row row : rows) {
145 logger.debug("{}", row);
150 if (cluster != null) {
157 // public static void main(String[] args) throws FileNotFoundException {
158 // Map<AuditingFieldsKeysEnum, String> map = new HashMap<>();
159 // map.put(AuditingFieldsKeysEnum.AUDIT_ACTION, "Access");
160 // map.put(AuditingFieldsKeysEnum.AUDIT_STATUS, "200");
161 // // CassandraUtils.truncateTable("sdcartifact", "resources");
162 //// CassandraUtils.truncateAllTables("sdcaudit");
163 // CassandraUtils.fetchFromTable("sdcaudit", "useraccessevent", map );