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.Collection;
25 import java.util.List;
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 com.datastax.driver.core.Cluster;
35 import com.datastax.driver.core.KeyspaceMetadata;
36 import com.datastax.driver.core.Metadata;
37 import com.datastax.driver.core.Row;
38 import com.datastax.driver.core.Session;
39 import com.datastax.driver.core.TableMetadata;
40 import com.datastax.driver.core.querybuilder.QueryBuilder;
41 import com.datastax.driver.core.querybuilder.Select;
42 import com.datastax.driver.core.querybuilder.Select.Where;
44 public final class CassandraUtils2 {
45 private static Logger logger = LoggerFactory.getLogger(CassandraUtils2.class.getName());
47 public static void truncateTable(String keyspace, String tableName) throws FileNotFoundException {
49 String cassandraHost = Utils.getConfig().getCassandraHost();
51 Cluster cluster = null;
55 Cluster.Builder clusterBuilder = Cluster.builder().addContactPoint(cassandraHost);
57 // clusterBuilder.withCredentials(username,password);
59 // System.setProperty("javax.net.ssl.trustStore",truststorePath);
60 // System.setProperty("javax.net.ssl.trustStorePassword",truststorePassword);
61 // clusterBuilder.withSSL();
62 cluster = clusterBuilder.build();
63 session = cluster.connect(keyspace);
64 if (session != null) {
65 session.execute(QueryBuilder.truncate(keyspace, tableName));
66 logger.debug("The table {}.{} was cleaned",keyspace,tableName);
68 throw new RuntimeException("Keyspace " + keyspace + " not connected");
71 if (cluster != null) {
77 public static void truncateAllKeyspaces() throws FileNotFoundException {
78 truncateAllTables(AuditingTypesConstants.ARTIFACT_KEYSPACE);
79 truncateAllTables(AuditingTypesConstants.AUDIT_KEYSPACE);
82 public static void truncateAllTables(String keyspace) throws FileNotFoundException {
83 String cassandraHost = Utils.getConfig().getCassandraHost();
85 Cluster cluster = null;
89 cluster = Cluster.builder().addContactPoint(cassandraHost).build();
90 session = cluster.connect(keyspace);
91 if (session != null) {
92 Metadata metadata = cluster.getMetadata();
93 KeyspaceMetadata keyspaceMetadata = metadata.getKeyspace(keyspace);
94 if (keyspaceMetadata != null) {
95 Collection<TableMetadata> tables = keyspaceMetadata.getTables();
96 tables.forEach(table -> {
97 session.execute(QueryBuilder.truncate(table));
98 logger.debug("Table trunceted - {}",table.getName());
102 throw new RuntimeException("Keyspace " + keyspace + " not connected");
106 if (cluster != null) {
112 public static List<Row> fetchFromTable(String keyspace, String tableName,
113 List<Pair<AuditingFieldsKeysEnum, String>> fields) throws FileNotFoundException {
115 // List<Pair<AuditingFieldsKeysEnum, String>>
116 // Map<AuditingFieldsKeysEnum, String>
118 Cluster cluster = null;
120 String cassandraHost = Utils.getConfig().getCassandraHost();
123 cluster = Cluster.builder().addContactPoint(cassandraHost).build();
124 session = cluster.connect(keyspace);
125 if (session != null) {
126 Select select = QueryBuilder.select().all().from(keyspace, tableName);
127 if (fields != null) {
128 // Set<Entry<AuditingFieldsKeysEnum, String>> entrySet =
129 // fields.entrySet();
131 boolean multiple = (fields.size() > 1) ? true : false;
135 for (Pair<AuditingFieldsKeysEnum, String> pair : fields) {
138 where = select.where(QueryBuilder.eq(pair.getValue0().getDisplayName(), pair.getValue1()));
140 where.and(QueryBuilder.eq(pair.getValue0().getDisplayName(), pair.getValue1()));
144 select.allowFiltering();
149 List<Row> rows = session.execute(select).all();
150 for (Row row : rows) {
151 logger.debug("{}", row);
156 if (cluster != null) {
163 // public static void main(String[] args) throws FileNotFoundException {
164 // Map<AuditingFieldsKeysEnum, String> map = new HashMap<>();
165 // map.put(AuditingFieldsKeysEnum.AUDIT_ACTION, "Access");
166 // map.put(AuditingFieldsKeysEnum.AUDIT_STATUS, "200");
167 // // CassandraUtils.truncateTable("sdcartifact", "resources");
168 //// CassandraUtils.truncateAllTables("sdcaudit");
169 // CassandraUtils.fetchFromTable("sdcaudit", "useraccessevent", map );