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 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;
40 import java.io.IOException;
42 import java.util.Map.Entry;
44 public class DbUtils {
46 private static String titanConfigFilePath;
47 private static TitanGraph titanGraph;
50 public static void cleanAllAudits() throws IOException {
51 CassandraUtils.truncateAllTables("sdcaudit");
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(),
58 HttpRequest httpRequest = new HttpRequest();
59 RestResponse restResponse = httpRequest.httpSendDelete(url, null);
60 restResponse.getErrorCode();
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();
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();
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);
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());
104 while (verticesItr.hasNext()) {
105 vertices.add(verticesItr.next());
108 TitanState currState = new TitanState(edges, vertices);
114 private static TitanGraph getTitanGraph() {
115 if (titanGraph == null) {
116 titanGraph = TitanFactory.open(titanConfigFilePath);
121 public void restoreToTitanState(TitanState titanStateToRestoreTo) {
122 List<Vertex> verticesToRemove = new ArrayList<>(), verticesToAdd = new ArrayList<>();
123 List<Edge> edgesToRemove = new ArrayList<>(), edgesToAdd = new ArrayList<>();
125 TitanState currentTitanState = getCurrentTitanState();
127 List<Edge> joinedEdges = new ArrayList<>();
128 joinedEdges.addAll(titanStateToRestoreTo.edges);
129 joinedEdges.retainAll(currentTitanState.edges);
131 List<Vertex> joinedVertices = new ArrayList<>();
132 joinedVertices.addAll(titanStateToRestoreTo.vertices);
133 joinedVertices.retainAll(currentTitanState.vertices);
135 edgesToRemove.addAll(currentTitanState.edges);
136 edgesToRemove.removeAll(joinedEdges);
138 verticesToRemove.addAll(currentTitanState.vertices);
139 verticesToRemove.removeAll(joinedVertices);
141 edgesToAdd.addAll(titanStateToRestoreTo.edges);
142 edgesToAdd.removeAll(joinedEdges);
144 verticesToAdd.addAll(titanStateToRestoreTo.vertices);
145 verticesToAdd.removeAll(joinedVertices);
147 modifyGraphAccordingToDelta(verticesToRemove, verticesToAdd, edgesToRemove, edgesToAdd);
151 private void modifyGraphAccordingToDelta(List<Vertex> verticesToRemove, List<Vertex> verticesToAdd,
152 List<Edge> edgesToRemove, List<Edge> edgesToAdd) {
154 TitanGraph titanGraph = getTitanGraph();
156 for (Vertex vertex : verticesToRemove) {
157 // titanGraph.removeVertex(vertex);
160 for (Vertex vertex : verticesToAdd) {
161 TitanVertex titanVertex = titanGraph.addVertex();
162 copyProperties(vertex, titanVertex);
165 for (Edge edge : edgesToRemove) {
166 // titanGraph.removeEdge(edge);
170 for (Edge edge : edgesToAdd) {
171 // Element addedEdge = titanGraph.addEdge(edge.getId(),
172 // edge.getVertex(Direction.OUT), edge.getVertex(Direction.IN),
175 // Edge edge = tGraph.addEdge(null, fromV.left().value(),
176 // toV.left().value(), type);
178 Element addedEdge = edge.outVertex().addEdge(edge.label(), edge.inVertex());
180 copyProperties(edge, addedEdge);
184 // titanGraph.commit();
185 titanGraph.tx().commit();
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));
200 public static class TitanState {
201 private List<Edge> edges;
202 private List<Vertex> vertices;
204 private TitanState(List<Edge> edges, List<Vertex> vertices) {
206 this.vertices = vertices;
210 public String toString() {
211 return "TitanState [edges=" + edges.size() + ", vertices=" + vertices.size() + "]";
216 public void shutDowntitan() {
217 if (titanGraph != null) {
218 // titanGraph.shutdown();
223 public static void setProperties(Element element, Map<String, Object> properties) {
225 if (properties != null && false == properties.isEmpty()) {
227 Object[] propertyKeyValues = new Object[properties.size() * 2];
229 for (Entry<String, Object> entry : properties.entrySet()) {
230 propertyKeyValues[i++] = entry.getKey();
231 propertyKeyValues[i++] = entry.getValue();
234 ElementHelper.attachProperties(element, propertyKeyValues);
240 public static UserAuditJavaObject parseAuditRespByAction(String action) throws Exception {
242 // String index = "auditingevents*";
243 // String type = "useradminevent";
244 // String pattern = "/_search?q=action:\""+action+"\"";
245 // String auditingMessage = retrieveAuditMessageByIndexType(index, type,
247 UserAuditJavaObject auditParsedResp = new UserAuditJavaObject();
248 Gson gson = new Gson();
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;
260 JsonObject jHitObject = (JsonObject) hitsArray.get(0);
261 JsonObject jSourceObject = (JsonObject) jHitObject.get("_source");
263 auditParsedResp = gson.fromJson(jSourceObject, UserAuditJavaObject.class);
265 return auditParsedResp;
269 public static String retrieveAuditMessagesByPattern(String pattern) throws IOException {
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);
276 return restResponse.getResponse();