aboutsummaryrefslogtreecommitdiffstats
path: root/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/utils/DbUtils.java
blob: 5ba7062f3b08ff8e83b06f882337574ad6021cf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.ci.tests.utils;

import com.google.gson.*;
import org.janusgraph.core.JanusGraphEdge;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphVertex;
import fj.data.Either;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Element;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
import org.openecomp.sdc.ci.tests.api.Urls;
import org.openecomp.sdc.ci.tests.config.Config;
import org.openecomp.sdc.ci.tests.datatypes.http.HttpRequest;
import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
import org.openecomp.sdc.ci.tests.users.UserAuditJavaObject;
import org.openecomp.sdc.ci.tests.utils.cassandra.CassandraUtils;

import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;

public class DbUtils {

	private static String janusGraphConfigFilePath;
	private static JanusGraph janusGraph;

	
	public static void cleanAllAudits() throws IOException {
		CassandraUtils.truncateAllTables("sdcaudit");
	}

	public static RestResponse deleteFromEsDbByPattern(String patternToDelete) throws IOException {
		Config config = Utils.getConfig();
		String url = String.format(Urls.GET_SEARCH_DATA_FROM_ES, config.getEsHost(), config.getEsPort(),
				patternToDelete);
		HttpRequest httpRequest = new HttpRequest();
		RestResponse restResponse = httpRequest.httpSendDelete(url, null);
		restResponse.getErrorCode();
		cleanAllAudits();

		return restResponse;
	}

	public static RestResponse getFromEsByPattern(String patternToGet) throws IOException {
		Config config = Utils.getConfig();
		String url = String.format(Urls.GET_SEARCH_DATA_FROM_ES, config.getEsHost(), config.getEsPort(), patternToGet);
		HttpRequest httpRequest = new HttpRequest();
		RestResponse restResponse = httpRequest.httpSendGet(url, null);
		restResponse.getErrorCode();

		return restResponse;
	}

	public Either<Vertex, Boolean> getVertexByUId(String uid) {
		JanusGraph janusGraph = getJanusGraph();
		Either<Vertex, Boolean> result = Either.right(false);
		// Iterator<Vertex> vertexItr = janusGraph.getVertices().iterator();

		Iterator<JanusGraphVertex> vertexItr = janusGraph.query().vertices().iterator();
		while (vertexItr.hasNext()) {
			Vertex vertex = vertexItr.next();
			// String uidFoundVal = vertex.getProperty("uid");
			String uidFoundVal = vertex.value("uid");
			if (uid.equals(uidFoundVal)) {
				result = Either.left(vertex);
			}
		}
		return result;
	}

	public static JanusGraphState getCurrentJanusGraphState() {
		JanusGraph janusGraph = getJanusGraph();
		List<Vertex> vertices = new ArrayList<>();
		List<Edge> edges = new ArrayList<>();
		// Iterator<Edge> edgesItr = janusGraph.getEdges().iterator();
		Iterator<JanusGraphEdge> edgesItr = janusGraph.query().edges().iterator();
		// Iterator<Vertex> verticesItr = janusGraph.getVertices().iterator();
		Iterator<JanusGraphVertex> verticesItr = janusGraph.query().vertices().iterator();
		while (edgesItr.hasNext()) {
			edges.add(edgesItr.next());
		}
		while (verticesItr.hasNext()) {
			vertices.add(verticesItr.next());
		}

		JanusGraphState currState = new JanusGraphState(edges, vertices);
		return currState;

	}

	//
	private static JanusGraph getJanusGraph() {
		if (janusGraph == null) {
			janusGraph = JanusGraphFactory.open(janusGraphConfigFilePath);
		}
		return janusGraph;
	}

	public void restoreToJanusGraphState(JanusGraphState janusGraphStateToRestoreTo) {
		List<Vertex> verticesToRemove = new ArrayList<>(), verticesToAdd = new ArrayList<>();
		List<Edge> edgesToRemove = new ArrayList<>(), edgesToAdd = new ArrayList<>();

		JanusGraphState currentJanusGraphState = getCurrentJanusGraphState();

		List<Edge> joinedEdges = new ArrayList<>();
		joinedEdges.addAll(janusGraphStateToRestoreTo.edges);
		joinedEdges.retainAll(currentJanusGraphState.edges);

		List<Vertex> joinedVertices = new ArrayList<>();
		joinedVertices.addAll(janusGraphStateToRestoreTo.vertices);
		joinedVertices.retainAll(currentJanusGraphState.vertices);

		edgesToRemove.addAll(currentJanusGraphState.edges);
		edgesToRemove.removeAll(joinedEdges);

		verticesToRemove.addAll(currentJanusGraphState.vertices);
		verticesToRemove.removeAll(joinedVertices);

		edgesToAdd.addAll(janusGraphStateToRestoreTo.edges);
		edgesToAdd.removeAll(joinedEdges);

		verticesToAdd.addAll(janusGraphStateToRestoreTo.vertices);
		verticesToAdd.removeAll(joinedVertices);

		modifyGraphAccordingToDelta(verticesToRemove, verticesToAdd, edgesToRemove, edgesToAdd);

	}

	private void modifyGraphAccordingToDelta(List<Vertex> verticesToRemove, List<Vertex> verticesToAdd,
			List<Edge> edgesToRemove, List<Edge> edgesToAdd) {

		JanusGraph janusGraph = getJanusGraph();

		for (Vertex vertex : verticesToRemove) {
			// janusGraph.removeVertex(vertex);
			vertex.remove();
		}
		for (Vertex vertex : verticesToAdd) {
			JanusGraphVertex janusGraphVertex = janusGraph.addVertex();
			copyProperties(vertex, janusGraphVertex);
		}

		for (Edge edge : edgesToRemove) {
			// janusGraph.removeEdge(edge);
			edge.remove();
		}

		for (Edge edge : edgesToAdd) {
			// Element addedEdge = janusGraph.addEdge(edge.getId(),
			// edge.getVertex(Direction.OUT), edge.getVertex(Direction.IN),
			// edge.getLabel());

			// Edge edge = tGraph.addEdge(null, fromV.left().value(),
			// toV.left().value(), type);

			Element addedEdge = edge.outVertex().addEdge(edge.label(), edge.inVertex());

			copyProperties(edge, addedEdge);

		}

		// janusGraph.commit();
		janusGraph.tx().commit();

	}

	private void copyProperties(Element copyFrom, Element copyTo) {
		// Set<String> properties = copyFrom.getPropertyKeys();
		Set<String> properties = copyFrom.keys();
		for (String propertyKey : properties) {
			// copyTo.setProperty(propertyKey,
			// copyFrom.getProperty(propertyKey));
			copyTo.property(propertyKey, copyFrom.value(propertyKey));
		}

	}

	public static class JanusGraphState {
		private List<Edge> edges;
		private List<Vertex> vertices;

		private JanusGraphState(List<Edge> edges, List<Vertex> vertices) {
			this.edges = edges;
			this.vertices = vertices;
		}

		@Override
		public String toString() {
			return "JanusGraphState [edges=" + edges.size() + ", vertices=" + vertices.size() + "]";
		}

	}

	public void shutDownJanusGraph() {
		if (janusGraph != null) {
			// janusGraph.shutdown();
			janusGraph.close();
		}
	}

	public static void setProperties(Element element, Map<String, Object> properties) {

		if (properties != null && false == properties.isEmpty()) {

			Object[] propertyKeyValues = new Object[properties.size() * 2];
			int i = 0;
			for (Entry<String, Object> entry : properties.entrySet()) {
				propertyKeyValues[i++] = entry.getKey();
				propertyKeyValues[i++] = entry.getValue();
			}

			ElementHelper.attachProperties(element, propertyKeyValues);

		}

	}

	public static UserAuditJavaObject parseAuditRespByAction(String action) throws Exception {

		// String index = "auditingevents*";
		// String type = "useradminevent";
		// String pattern = "/_search?q=action:\""+action+"\"";
		// String auditingMessage = retrieveAuditMessageByIndexType(index, type,
		// pattern);
		UserAuditJavaObject auditParsedResp = new UserAuditJavaObject();
		Gson gson = new Gson();

		String pattern = "/_search?q=ACTION:\"" + action + "\"";
		String auditingMessage = retrieveAuditMessagesByPattern(pattern);
		JsonElement jElement = new JsonParser().parse(auditingMessage);
		JsonObject jObject = jElement.getAsJsonObject();
		JsonObject hitsObject = (JsonObject) jObject.get("hits");
		JsonArray hitsArray = (JsonArray) hitsObject.get("hits");
		// for (int i = 0; i < hitsArray.size();){
		if (hitsArray.size() == 0) {
			return auditParsedResp;
		}
		JsonObject jHitObject = (JsonObject) hitsArray.get(0);
		JsonObject jSourceObject = (JsonObject) jHitObject.get("_source");

		auditParsedResp = gson.fromJson(jSourceObject, UserAuditJavaObject.class);		

		return auditParsedResp;

	}

	public static String retrieveAuditMessagesByPattern(String pattern) throws IOException {

		Config config = Utils.getConfig();
		HttpRequest getAuditingMessage = new HttpRequest();
		String url = String.format(Urls.GET_SEARCH_DATA_FROM_ES, config.getEsHost(), config.getEsPort(), pattern);
		RestResponse restResponse = getAuditingMessage.httpSendGet(url, null);

		return restResponse.getResponse();
	}
}