2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.utils;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
28 import java.util.Map.Entry;
31 import org.apache.tinkerpop.gremlin.structure.Edge;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
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;
44 import fj.data.Either;
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;
59 public class DbUtils {
61 private static String titanConfigFilePath;
62 private static TitanGraph titanGraph;
65 public static void cleanAllAudits() throws IOException {
66 CassandraUtils.truncateAllTables("sdcaudit");
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(),
73 HttpRequest httpRequest = new HttpRequest();
74 RestResponse restResponse = httpRequest.httpSendDelete(url, null);
75 restResponse.getErrorCode();
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();
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();
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);
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());
119 while (verticesItr.hasNext()) {
120 vertices.add(verticesItr.next());
123 TitanState currState = new TitanState(edges, vertices);
129 private static TitanGraph getTitanGraph() {
130 if (titanGraph == null) {
131 titanGraph = TitanFactory.open(titanConfigFilePath);
136 public void restoreToTitanState(TitanState titanStateToRestoreTo) {
137 List<Vertex> verticesToRemove = new ArrayList<>(), verticesToAdd = new ArrayList<>();
138 List<Edge> edgesToRemove = new ArrayList<>(), edgesToAdd = new ArrayList<>();
140 TitanState currentTitanState = getCurrentTitanState();
142 List<Edge> joinedEdges = new ArrayList<>();
143 joinedEdges.addAll(titanStateToRestoreTo.edges);
144 joinedEdges.retainAll(currentTitanState.edges);
146 List<Vertex> joinedVertices = new ArrayList<>();
147 joinedVertices.addAll(titanStateToRestoreTo.vertices);
148 joinedVertices.retainAll(currentTitanState.vertices);
150 edgesToRemove.addAll(currentTitanState.edges);
151 edgesToRemove.removeAll(joinedEdges);
153 verticesToRemove.addAll(currentTitanState.vertices);
154 verticesToRemove.removeAll(joinedVertices);
156 edgesToAdd.addAll(titanStateToRestoreTo.edges);
157 edgesToAdd.removeAll(joinedEdges);
159 verticesToAdd.addAll(titanStateToRestoreTo.vertices);
160 verticesToAdd.removeAll(joinedVertices);
162 modifyGraphAccordingToDelta(verticesToRemove, verticesToAdd, edgesToRemove, edgesToAdd);
166 private void modifyGraphAccordingToDelta(List<Vertex> verticesToRemove, List<Vertex> verticesToAdd,
167 List<Edge> edgesToRemove, List<Edge> edgesToAdd) {
169 TitanGraph titanGraph = getTitanGraph();
171 for (Vertex vertex : verticesToRemove) {
172 // titanGraph.removeVertex(vertex);
175 for (Vertex vertex : verticesToAdd) {
176 TitanVertex titanVertex = titanGraph.addVertex();
177 copyProperties(vertex, titanVertex);
180 for (Edge edge : edgesToRemove) {
181 // titanGraph.removeEdge(edge);
185 for (Edge edge : edgesToAdd) {
186 // Element addedEdge = titanGraph.addEdge(edge.getId(),
187 // edge.getVertex(Direction.OUT), edge.getVertex(Direction.IN),
190 // Edge edge = tGraph.addEdge(null, fromV.left().value(),
191 // toV.left().value(), type);
193 Element addedEdge = edge.outVertex().addEdge(edge.label(), edge.inVertex());
195 copyProperties(edge, addedEdge);
199 // titanGraph.commit();
200 titanGraph.tx().commit();
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));
215 public static class TitanState {
216 private List<Edge> edges;
217 private List<Vertex> vertices;
219 private TitanState(List<Edge> edges, List<Vertex> vertices) {
221 this.vertices = vertices;
225 public String toString() {
226 return "TitanState [edges=" + edges.size() + ", vertices=" + vertices.size() + "]";
231 public void shutDowntitan() {
232 if (titanGraph != null) {
233 // titanGraph.shutdown();
238 public static void setProperties(Element element, Map<String, Object> properties) {
240 if (properties != null && false == properties.isEmpty()) {
242 Object[] propertyKeyValues = new Object[properties.size() * 2];
244 for (Entry<String, Object> entry : properties.entrySet()) {
245 propertyKeyValues[i++] = entry.getKey();
246 propertyKeyValues[i++] = entry.getValue();
249 ElementHelper.attachProperties(element, propertyKeyValues);
255 public static UserAuditJavaObject parseAuditRespByAction(String action) throws Exception {
257 // String index = "auditingevents*";
258 // String type = "useradminevent";
259 // String pattern = "/_search?q=action:\""+action+"\"";
260 // String auditingMessage = retrieveAuditMessageByIndexType(index, type,
262 UserAuditJavaObject auditParsedResp = new UserAuditJavaObject();
263 Gson gson = new Gson();
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;
275 JsonObject jHitObject = (JsonObject) hitsArray.get(0);
276 JsonObject jSourceObject = (JsonObject) jHitObject.get("_source");
278 auditParsedResp = gson.fromJson(jSourceObject, UserAuditJavaObject.class);
280 return auditParsedResp;
284 public static String retrieveAuditMessagesByPattern(String pattern) throws IOException {
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);
291 return restResponse.getResponse();