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