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