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.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;
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;
52 import fj.data.Either;
54 public class DbUtils {
56 private static String titanConfigFilePath;
57 private static TitanGraph titanGraph;
59 public static void cleanAllAudits() throws IOException {
60 CassandraUtils.truncateAllTables("sdcaudit");
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(),
67 HttpRequest httpRequest = new HttpRequest();
68 RestResponse restResponse = httpRequest.httpSendDelete(url, null);
69 restResponse.getErrorCode();
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();
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();
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);
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());
113 while (verticesItr.hasNext()) {
114 vertices.add(verticesItr.next());
117 TitanState currState = new TitanState(edges, vertices);
122 private static TitanGraph getTitanGraph() {
123 if (titanGraph == null) {
124 titanGraph = TitanFactory.open(titanConfigFilePath);
129 public void restoreToTitanState(TitanState titanStateToRestoreTo) {
130 List<Vertex> verticesToRemove = new ArrayList<>(), verticesToAdd = new ArrayList<>();
131 List<Edge> edgesToRemove = new ArrayList<>(), edgesToAdd = new ArrayList<>();
133 TitanState currentTitanState = getCurrentTitanState();
135 List<Edge> joinedEdges = new ArrayList<>();
136 joinedEdges.addAll(titanStateToRestoreTo.edges);
137 joinedEdges.retainAll(currentTitanState.edges);
139 List<Vertex> joinedVertices = new ArrayList<>();
140 joinedVertices.addAll(titanStateToRestoreTo.vertices);
141 joinedVertices.retainAll(currentTitanState.vertices);
143 edgesToRemove.addAll(currentTitanState.edges);
144 edgesToRemove.removeAll(joinedEdges);
146 verticesToRemove.addAll(currentTitanState.vertices);
147 verticesToRemove.removeAll(joinedVertices);
149 edgesToAdd.addAll(titanStateToRestoreTo.edges);
150 edgesToAdd.removeAll(joinedEdges);
152 verticesToAdd.addAll(titanStateToRestoreTo.vertices);
153 verticesToAdd.removeAll(joinedVertices);
155 modifyGraphAccordingToDelta(verticesToRemove, verticesToAdd, edgesToRemove, edgesToAdd);
159 private void modifyGraphAccordingToDelta(List<Vertex> verticesToRemove, List<Vertex> verticesToAdd,
160 List<Edge> edgesToRemove, List<Edge> edgesToAdd) {
162 TitanGraph titanGraph = getTitanGraph();
164 for (Vertex vertex : verticesToRemove) {
165 // titanGraph.removeVertex(vertex);
168 for (Vertex vertex : verticesToAdd) {
169 TitanVertex titanVertex = titanGraph.addVertex();
170 copyProperties(vertex, titanVertex);
173 for (Edge edge : edgesToRemove) {
174 // titanGraph.removeEdge(edge);
178 for (Edge edge : edgesToAdd) {
179 // Element addedEdge = titanGraph.addEdge(edge.getId(),
180 // edge.getVertex(Direction.OUT), edge.getVertex(Direction.IN),
183 // Edge edge = tGraph.addEdge(null, fromV.left().value(),
184 // toV.left().value(), type);
186 Element addedEdge = edge.outVertex().addEdge(edge.label(), edge.inVertex());
188 copyProperties(edge, addedEdge);
192 // titanGraph.commit();
193 titanGraph.tx().commit();
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));
208 public static class TitanState {
209 private List<Edge> edges;
210 private List<Vertex> vertices;
212 private TitanState(List<Edge> edges, List<Vertex> vertices) {
214 this.vertices = vertices;
218 public String toString() {
219 return "TitanState [edges=" + edges.size() + ", vertices=" + vertices.size() + "]";
224 public void shutDowntitan() {
225 if (titanGraph != null) {
226 // titanGraph.shutdown();
231 public static void setProperties(Element element, Map<String, Object> properties) {
233 if (properties != null && false == properties.isEmpty()) {
235 Object[] propertyKeyValues = new Object[properties.size() * 2];
237 for (Entry<String, Object> entry : properties.entrySet()) {
238 propertyKeyValues[i++] = entry.getKey();
239 propertyKeyValues[i++] = entry.getValue();
242 ElementHelper.attachProperties(element, propertyKeyValues);
248 public static UserAuditJavaObject parseAuditRespByAction(String action) throws Exception {
250 // String index = "auditingevents*";
251 // String type = "useradminevent";
252 // String pattern = "/_search?q=action:\""+action+"\"";
253 // String auditingMessage = retrieveAuditMessageByIndexType(index, type,
255 UserAuditJavaObject auditParsedResp = new UserAuditJavaObject();
256 Gson gson = new Gson();
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;
268 JsonObject jHitObject = (JsonObject) hitsArray.get(0);
269 JsonObject jSourceObject = (JsonObject) jHitObject.get("_source");
271 auditParsedResp = gson.fromJson(jSourceObject, UserAuditJavaObject.class);
273 return auditParsedResp;
277 public static String retrieveAuditMessagesByPattern(String pattern) throws IOException {
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);
284 return restResponse.getResponse();