[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / cassandra / CassandraUtils2.java
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.sdc.ci.tests.utils.cassandra;
22
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;
33
34 import java.io.FileNotFoundException;
35 import java.util.Collection;
36 import java.util.List;
37
38 public final class CassandraUtils2 {
39         private static Logger logger = LoggerFactory.getLogger(CassandraUtils2.class.getName());
40
41         public static void truncateTable(String keyspace, String tableName) throws FileNotFoundException {
42
43                 String cassandraHost = Utils.getConfig().getCassandraHost();
44
45                 Cluster cluster = null;
46                 Session session;
47
48                 try {
49                         Cluster.Builder clusterBuilder = Cluster.builder().addContactPoint(cassandraHost);
50                         // authantication
51                         // clusterBuilder.withCredentials(username,password);
52                         // ssl
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);
61                         } else {
62                                 throw new RuntimeException("Keyspace " + keyspace + " not connected");
63                         }
64                 } finally {
65                         if (cluster != null) {
66                                 cluster.close();
67                         }
68                 }
69         }
70
71         public static void truncateAllKeyspaces() throws FileNotFoundException {
72                 truncateAllTables(AuditingTypesConstants.ARTIFACT_KEYSPACE);
73                 truncateAllTables(AuditingTypesConstants.AUDIT_KEYSPACE);
74         }
75
76         public static void truncateAllTables(String keyspace) throws FileNotFoundException {
77                 String cassandraHost = Utils.getConfig().getCassandraHost();
78
79                 Cluster cluster = null;
80                 Session session;
81
82                 try {
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());
93                                         });
94                                 }
95                         } else {
96                                 throw new RuntimeException("Keyspace " + keyspace + " not connected");
97                         }
98
99                 } finally {
100                         if (cluster != null) {
101                                 cluster.close();
102                         }
103                 }
104         }
105
106         public static List<Row> fetchFromTable(String keyspace, String tableName,
107                         List<Pair<AuditingFieldsKeysEnum, String>> fields) throws FileNotFoundException {
108
109                 // List<Pair<AuditingFieldsKeysEnum, String>>
110                 // Map<AuditingFieldsKeysEnum, String>
111
112                 Cluster cluster = null;
113                 Session session;
114                 String cassandraHost = Utils.getConfig().getCassandraHost();
115
116                 try {
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();
124                                         // fields.
125                                         boolean multiple = (fields.size() > 1) ? true : false;
126                                         Where where = null;
127                                         int size = 0;
128
129                                         for (Pair<AuditingFieldsKeysEnum, String> pair : fields) {
130                                                 ++size;
131                                                 if (size == 1) {
132                                                         where = select.where(QueryBuilder.eq(pair.getValue0().getDisplayName(), pair.getValue1()));
133                                                 } else {
134                                                         where.and(QueryBuilder.eq(pair.getValue0().getDisplayName(), pair.getValue1()));
135                                                 }
136                                         }
137                                         if (multiple) {
138                                                 select.allowFiltering();
139                                         }
140
141                                 }
142
143                                 List<Row> rows = session.execute(select).all();
144                                 for (Row row : rows) {
145                                         logger.debug("{}", row);
146                                 }
147                                 return rows;
148                         }
149                 } finally {
150                         if (cluster != null) {
151                                 cluster.close();
152                         }
153                 }
154                 return null;
155         }
156         //
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 );
164         // }
165
166 }