Fix for radio buttons
[sdc.git] / asdc-tests / src / main / java / org / openecomp / sdc / ci / tests / utils / cassandra / CassandraUtils.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.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27
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;
34
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;
44
45 public final class CassandraUtils {
46         private static Logger logger = LoggerFactory.getLogger(CassandraUtils.class.getName());
47
48         protected static Cluster cluster = null;
49         protected static Session session;
50
51         protected static void initConnection(String keyspace) throws FileNotFoundException {
52
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();
60                 /*
61                  * String cassandraAuditKeySpace=
62                  * Utils.getConfig().getCassandraAuditKeySpace(); String
63                  * cassandraArtifactKeySpace=
64                  * Utils.getConfig().getCassandraArtifactKeySpace();
65                  */
66
67                 Cluster.Builder clusterBuilder = Cluster.builder().addContactPoint(cassandraHost);
68                 if (cassandraAuthenticate) {
69                         // authantication
70                         clusterBuilder.withCredentials(cassandraUsername, cassandraPassword);
71                 }
72
73                 if (cassandraSsl) {
74                         // ssl
75                         System.setProperty("javax.net.ssl.trustStore", cassandraTruststorePath);
76                         System.setProperty("javax.net.ssl.trustStorePassword", cassandraTruststorePassword);
77                         clusterBuilder.withSSL();
78                 }
79
80                 cluster = clusterBuilder.build();
81                 session = cluster.connect(keyspace);
82
83         }
84
85         public static void truncateTable(String keyspace, String tableName) throws FileNotFoundException {
86
87                 if (session == null || session.isClosed()) {
88                         initConnection(keyspace);
89                 }
90
91                 try {
92
93                         if (session != null) {
94                                 session.execute(QueryBuilder.truncate(keyspace, tableName));
95                                 logger.debug("The table {}.{} was cleaned",keyspace,tableName);
96                         } else {
97                                 throw new RuntimeException("Keyspace " + keyspace + " not connected");
98                         }
99                 } finally {
100                         // if (cluster != null) {
101                         // cluster.close();
102                         // }
103                 }
104         }
105
106         public static void close() {
107                 if (cluster != null) {
108                         cluster.close();
109                 }
110         }
111
112         public static void truncateAllKeyspaces() throws FileNotFoundException {
113                 // truncateAllTables(AuditingTypesConstants.ARTIFACT_KEYSPACE);
114                 truncateAllTables(AuditingTypesConstants.AUDIT_KEYSPACE);
115         }
116
117         public static void truncateAllTables(String keyspace) throws FileNotFoundException {
118
119                 if (session == null || session.isClosed()) {
120                         initConnection(keyspace);
121                 }
122                 try {
123
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());
132                                         });
133                                 }
134                         } else {
135                                 throw new RuntimeException("Keyspace " + keyspace + " not connected");
136                         }
137
138                 } finally {
139                         // if (cluster != null) {
140                         // cluster.close();
141                         // }
142                 }
143         }
144
145         public static List<Row> fetchFromTable(String keyspace, String tableName,
146                         List<Pair<AuditingFieldsKeysEnum, String>> fields) throws FileNotFoundException {
147
148                 List<Pair<String, String>> fieldsConverted = new ArrayList<>();
149
150 //              fields.forEach(pair -> {
151 //                      Pair<String, String> newPair = new Pair(pair.getValue0().getDisplayName(), pair.getValue1());
152 //                      fieldsConverted.add(newPair);
153 //              });
154                 
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());
159                         
160                 }else{
161                         newPair = new Pair<String, String>(pair.getValue0().getDisplayName(), pair.getValue1());
162                 }
163                 fieldsConverted.add(newPair);
164                         
165                 });
166
167                 return fetchFromTableQuery(keyspace, tableName, fieldsConverted);
168         }
169
170         public static List<Row> fetchFromTableQuery(String keyspace, String tableName, List<Pair<String, String>> fields)
171                         throws FileNotFoundException {
172
173                 if (session == null || session.isClosed()) {
174                         initConnection(keyspace);
175                 }
176                 try {
177
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();
183                                         // fields.
184                                         boolean multiple = (fields.size() > 1) ? true : false;
185                                         Where where = null;
186                                         int size = 0;
187
188                                         for (Pair<String, String> pair : fields) {
189                                                 ++size;
190                                                 if (size == 1) {
191                                                         where = select.where(QueryBuilder.eq(pair.getValue0(), pair.getValue1()));
192                                                 } else {
193                                                         where.and(QueryBuilder.eq(pair.getValue0(), pair.getValue1()));
194                                                 }
195                                         }
196                                         if (multiple) {
197                                                 select.allowFiltering();
198                                         }
199
200                                 }
201
202                                 List<Row> rows = session.execute(select).all();
203                                 for (Row row : rows) {
204                                         logger.debug("{}", row);
205                                 }
206                                 return rows;
207                         }
208                 } finally {
209                         // if (cluster != null) {
210                         // cluster.close();
211                         // }
212                 }
213                 return null;
214         }
215         //
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 );
223         // }
224
225 }