Fix for radio buttons
[sdc.git] / asdc-tests / 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 java.io.FileNotFoundException;
24 import java.util.Collection;
25 import java.util.List;
26
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 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;
43
44 public final class CassandraUtils2 {
45         private static Logger logger = LoggerFactory.getLogger(CassandraUtils2.class.getName());
46
47         public static void truncateTable(String keyspace, String tableName) throws FileNotFoundException {
48
49                 String cassandraHost = Utils.getConfig().getCassandraHost();
50
51                 Cluster cluster = null;
52                 Session session;
53
54                 try {
55                         Cluster.Builder clusterBuilder = Cluster.builder().addContactPoint(cassandraHost);
56                         // authantication
57                         // clusterBuilder.withCredentials(username,password);
58                         // ssl
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);
67                         } else {
68                                 throw new RuntimeException("Keyspace " + keyspace + " not connected");
69                         }
70                 } finally {
71                         if (cluster != null) {
72                                 cluster.close();
73                         }
74                 }
75         }
76
77         public static void truncateAllKeyspaces() throws FileNotFoundException {
78                 truncateAllTables(AuditingTypesConstants.ARTIFACT_KEYSPACE);
79                 truncateAllTables(AuditingTypesConstants.AUDIT_KEYSPACE);
80         }
81
82         public static void truncateAllTables(String keyspace) throws FileNotFoundException {
83                 String cassandraHost = Utils.getConfig().getCassandraHost();
84
85                 Cluster cluster = null;
86                 Session session;
87
88                 try {
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());
99                                         });
100                                 }
101                         } else {
102                                 throw new RuntimeException("Keyspace " + keyspace + " not connected");
103                         }
104
105                 } finally {
106                         if (cluster != null) {
107                                 cluster.close();
108                         }
109                 }
110         }
111
112         public static List<Row> fetchFromTable(String keyspace, String tableName,
113                         List<Pair<AuditingFieldsKeysEnum, String>> fields) throws FileNotFoundException {
114
115                 // List<Pair<AuditingFieldsKeysEnum, String>>
116                 // Map<AuditingFieldsKeysEnum, String>
117
118                 Cluster cluster = null;
119                 Session session;
120                 String cassandraHost = Utils.getConfig().getCassandraHost();
121
122                 try {
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();
130                                         // fields.
131                                         boolean multiple = (fields.size() > 1) ? true : false;
132                                         Where where = null;
133                                         int size = 0;
134
135                                         for (Pair<AuditingFieldsKeysEnum, String> pair : fields) {
136                                                 ++size;
137                                                 if (size == 1) {
138                                                         where = select.where(QueryBuilder.eq(pair.getValue0().getDisplayName(), pair.getValue1()));
139                                                 } else {
140                                                         where.and(QueryBuilder.eq(pair.getValue0().getDisplayName(), pair.getValue1()));
141                                                 }
142                                         }
143                                         if (multiple) {
144                                                 select.allowFiltering();
145                                         }
146
147                                 }
148
149                                 List<Row> rows = session.execute(select).all();
150                                 for (Row row : rows) {
151                                         logger.debug("{}", row);
152                                 }
153                                 return rows;
154                         }
155                 } finally {
156                         if (cluster != null) {
157                                 cluster.close();
158                         }
159                 }
160                 return null;
161         }
162         //
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 );
170         // }
171
172 }