Fix for radio buttons
[sdc.git] / asdc-tests / src / main / java / org / openecomp / sdc / ci / tests / utils / DbUtils.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;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import java.util.Set;
30
31 import org.apache.tinkerpop.gremlin.structure.Edge;
32 import org.apache.tinkerpop.gremlin.structure.Element;
33 import org.apache.tinkerpop.gremlin.structure.Vertex;
34 import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
35 import org.openecomp.sdc.ci.tests.api.Urls;
36 import org.openecomp.sdc.ci.tests.config.Config;
37 import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
38 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
39 import org.openecomp.sdc.ci.tests.users.UserAuditJavaObject;
40 import org.openecomp.sdc.ci.tests.utils.cassandra.CassandraUtils;
41
42 import com.google.gson.Gson;
43 import com.google.gson.JsonArray;
44 import com.google.gson.JsonElement;
45 import com.google.gson.JsonObject;
46 import com.google.gson.JsonParser;
47 import com.thinkaurelius.titan.core.TitanEdge;
48 import com.thinkaurelius.titan.core.TitanFactory;
49 import com.thinkaurelius.titan.core.TitanGraph;
50 import com.thinkaurelius.titan.core.TitanVertex;
51
52 import fj.data.Either;
53
54 public class DbUtils {
55
56         private static String titanConfigFilePath;
57         private static TitanGraph titanGraph;
58
59         public static void cleanAllAudits() throws IOException {
60                 CassandraUtils.truncateAllTables("sdcaudit");
61         }
62
63         public static RestResponse deleteFromEsDbByPattern(String patternToDelete) throws IOException {
64                 Config config = Utils.getConfig();
65                 String url = String.format(Urls.GET_SEARCH_DATA_FROM_ES, config.getEsHost(), config.getEsPort(),
66                                 patternToDelete);
67                 HttpRequest httpRequest = new HttpRequest();
68                 RestResponse restResponse = httpRequest.httpSendDelete(url, null);
69                 restResponse.getErrorCode();
70                 cleanAllAudits();
71
72                 return restResponse;
73         }
74
75         public static RestResponse getFromEsByPattern(String patternToGet) throws IOException {
76                 Config config = Utils.getConfig();
77                 String url = String.format(Urls.GET_SEARCH_DATA_FROM_ES, config.getEsHost(), config.getEsPort(), patternToGet);
78                 HttpRequest httpRequest = new HttpRequest();
79                 RestResponse restResponse = httpRequest.httpSendGet(url, null);
80                 restResponse.getErrorCode();
81                 
82                 return restResponse;
83         }
84
85         public Either<Vertex, Boolean> getVertexByUId(String uid) {
86                 TitanGraph titanGraph = getTitanGraph();
87                 Either<Vertex, Boolean> result = Either.right(false);
88                 // Iterator<Vertex> vertexItr = titanGraph.getVertices().iterator();
89
90                 Iterator<TitanVertex> vertexItr = titanGraph.query().vertices().iterator();
91                 while (vertexItr.hasNext()) {
92                         Vertex vertex = vertexItr.next();
93                         // String uidFoundVal = vertex.getProperty("uid");
94                         String uidFoundVal = vertex.value("uid");
95                         if (uid.equals(uidFoundVal)) {
96                                 result = Either.left(vertex);
97                         }
98                 }
99                 return result;
100         }
101
102         public static TitanState getCurrentTitanState() {
103                 TitanGraph titanGraph = getTitanGraph();
104                 List<Vertex> vertices = new ArrayList<>();
105                 List<Edge> edges = new ArrayList<>();
106                 // Iterator<Edge> edgesItr = titanGraph.getEdges().iterator();
107                 Iterator<TitanEdge> edgesItr = titanGraph.query().edges().iterator();
108                 // Iterator<Vertex> verticesItr = titanGraph.getVertices().iterator();
109                 Iterator<TitanVertex> verticesItr = titanGraph.query().vertices().iterator();
110                 while (edgesItr.hasNext()) {
111                         edges.add(edgesItr.next());
112                 }
113                 while (verticesItr.hasNext()) {
114                         vertices.add(verticesItr.next());
115                 }
116
117                 TitanState currState = new TitanState(edges, vertices);
118                 return currState;
119
120         }
121
122         private static TitanGraph getTitanGraph() {
123                 if (titanGraph == null) {
124                         titanGraph = TitanFactory.open(titanConfigFilePath);
125                 }
126                 return titanGraph;
127         }
128
129         public void restoreToTitanState(TitanState titanStateToRestoreTo) {
130                 List<Vertex> verticesToRemove = new ArrayList<>(), verticesToAdd = new ArrayList<>();
131                 List<Edge> edgesToRemove = new ArrayList<>(), edgesToAdd = new ArrayList<>();
132
133                 TitanState currentTitanState = getCurrentTitanState();
134
135                 List<Edge> joinedEdges = new ArrayList<>();
136                 joinedEdges.addAll(titanStateToRestoreTo.edges);
137                 joinedEdges.retainAll(currentTitanState.edges);
138
139                 List<Vertex> joinedVertices = new ArrayList<>();
140                 joinedVertices.addAll(titanStateToRestoreTo.vertices);
141                 joinedVertices.retainAll(currentTitanState.vertices);
142
143                 edgesToRemove.addAll(currentTitanState.edges);
144                 edgesToRemove.removeAll(joinedEdges);
145
146                 verticesToRemove.addAll(currentTitanState.vertices);
147                 verticesToRemove.removeAll(joinedVertices);
148
149                 edgesToAdd.addAll(titanStateToRestoreTo.edges);
150                 edgesToAdd.removeAll(joinedEdges);
151
152                 verticesToAdd.addAll(titanStateToRestoreTo.vertices);
153                 verticesToAdd.removeAll(joinedVertices);
154
155                 modifyGraphAccordingToDelta(verticesToRemove, verticesToAdd, edgesToRemove, edgesToAdd);
156
157         }
158
159         private void modifyGraphAccordingToDelta(List<Vertex> verticesToRemove, List<Vertex> verticesToAdd,
160                         List<Edge> edgesToRemove, List<Edge> edgesToAdd) {
161
162                 TitanGraph titanGraph = getTitanGraph();
163
164                 for (Vertex vertex : verticesToRemove) {
165                         // titanGraph.removeVertex(vertex);
166                         vertex.remove();
167                 }
168                 for (Vertex vertex : verticesToAdd) {
169                         TitanVertex titanVertex = titanGraph.addVertex();
170                         copyProperties(vertex, titanVertex);
171                 }
172
173                 for (Edge edge : edgesToRemove) {
174                         // titanGraph.removeEdge(edge);
175                         edge.remove();
176                 }
177
178                 for (Edge edge : edgesToAdd) {
179                         // Element addedEdge = titanGraph.addEdge(edge.getId(),
180                         // edge.getVertex(Direction.OUT), edge.getVertex(Direction.IN),
181                         // edge.getLabel());
182
183                         // Edge edge = tGraph.addEdge(null, fromV.left().value(),
184                         // toV.left().value(), type);
185
186                         Element addedEdge = edge.outVertex().addEdge(edge.label(), edge.inVertex());
187
188                         copyProperties(edge, addedEdge);
189
190                 }
191
192                 // titanGraph.commit();
193                 titanGraph.tx().commit();
194
195         }
196
197         private void copyProperties(Element copyFrom, Element copyTo) {
198                 // Set<String> properties = copyFrom.getPropertyKeys();
199                 Set<String> properties = copyFrom.keys();
200                 for (String propertyKey : properties) {
201                         // copyTo.setProperty(propertyKey,
202                         // copyFrom.getProperty(propertyKey));
203                         copyTo.property(propertyKey, copyFrom.value(propertyKey));
204                 }
205
206         }
207
208         public static class TitanState {
209                 private List<Edge> edges;
210                 private List<Vertex> vertices;
211
212                 private TitanState(List<Edge> edges, List<Vertex> vertices) {
213                         this.edges = edges;
214                         this.vertices = vertices;
215                 }
216
217                 @Override
218                 public String toString() {
219                         return "TitanState [edges=" + edges.size() + ", vertices=" + vertices.size() + "]";
220                 }
221
222         }
223
224         public void shutDowntitan() {
225                 if (titanGraph != null) {
226                         // titanGraph.shutdown();
227                         titanGraph.close();
228                 }
229         }
230
231         public static void setProperties(Element element, Map<String, Object> properties) {
232
233                 if (properties != null && false == properties.isEmpty()) {
234
235                         Object[] propertyKeyValues = new Object[properties.size() * 2];
236                         int i = 0;
237                         for (Entry<String, Object> entry : properties.entrySet()) {
238                                 propertyKeyValues[i++] = entry.getKey();
239                                 propertyKeyValues[i++] = entry.getValue();
240                         }
241
242                         ElementHelper.attachProperties(element, propertyKeyValues);
243
244                 }
245
246         }
247
248         public static UserAuditJavaObject parseAuditRespByAction(String action) throws Exception {
249
250                 // String index = "auditingevents*";
251                 // String type = "useradminevent";
252                 // String pattern = "/_search?q=action:\""+action+"\"";
253                 // String auditingMessage = retrieveAuditMessageByIndexType(index, type,
254                 // pattern);
255                 UserAuditJavaObject auditParsedResp = new UserAuditJavaObject();
256                 Gson gson = new Gson();
257
258                 String pattern = "/_search?q=ACTION:\"" + action + "\"";
259                 String auditingMessage = retrieveAuditMessagesByPattern(pattern);
260                 JsonElement jElement = new JsonParser().parse(auditingMessage);
261                 JsonObject jObject = jElement.getAsJsonObject();
262                 JsonObject hitsObject = (JsonObject) jObject.get("hits");
263                 JsonArray hitsArray = (JsonArray) hitsObject.get("hits");
264                 // for (int i = 0; i < hitsArray.size();){
265                 if (hitsArray.size() == 0) {
266                         return auditParsedResp;
267                 }
268                 JsonObject jHitObject = (JsonObject) hitsArray.get(0);
269                 JsonObject jSourceObject = (JsonObject) jHitObject.get("_source");
270
271                 auditParsedResp = gson.fromJson(jSourceObject, UserAuditJavaObject.class);
272                 
273                 return auditParsedResp;
274
275         }
276
277         public static String retrieveAuditMessagesByPattern(String pattern) throws IOException {
278
279                 Config config = Utils.getConfig();
280                 HttpRequest getAuditingMessage = new HttpRequest();
281                 String url = String.format(Urls.GET_SEARCH_DATA_FROM_ES, config.getEsHost(), config.getEsPort(), pattern);
282                 RestResponse restResponse = getAuditingMessage.httpSendGet(url, null);
283
284                 return restResponse.getResponse();
285         }
286 }