2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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.onap.aai.serialization.queryformats;
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.OutputStream;
31 import java.util.HashMap;
32 import java.util.Iterator;
34 import java.util.Optional;
36 import org.apache.tinkerpop.gremlin.structure.Direction;
37 import org.apache.tinkerpop.gremlin.structure.Vertex;
38 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper;
39 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter;
40 import org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry;
42 public class GraphSON implements FormatMapper {
44 private final GraphSONMapper mapper =
45 GraphSONMapper.build().addRegistry(JanusGraphIoRegistry.getInstance()).create();
46 private final GraphSONWriter writer = GraphSONWriter.build().mapper(mapper).create();
47 protected JsonParser parser = new JsonParser();
50 public Optional<JsonObject> formatObject(Object v) {
51 OutputStream os = new ByteArrayOutputStream();
54 writer.writeVertex(os, (Vertex) v, Direction.BOTH);
56 result = os.toString();
57 } catch (IOException e) {
58 // TODO Auto-generated catch block
62 JsonObject jsonObject = parser.parse(result).getAsJsonObject();
64 if (jsonObject != null) {
66 if (jsonObject.has("outE")) {
67 JsonObject outEdges = jsonObject.get("outE").getAsJsonObject();
68 removePrivateEdges(jsonObject, outEdges, "outE");
71 if (jsonObject.has("inE")) {
72 JsonObject inEdges = jsonObject.get("inE").getAsJsonObject();
73 removePrivateEdges(jsonObject, inEdges, "inE");
78 return Optional.of(jsonObject);
83 * Removes the private edges from the json object
85 * Please note that the reason to choose to remove the private
86 * edges from the json object instead of removing it from the vertex
87 * itself is the fact that even though the transaction will be rolled back
88 * is because of the possible incosistent behavior where the actual edge
89 * might actually be removed in a long running transaction and is not worth the risk
91 * @param jsonObject - JSON Object from which we are removing the private edges for
92 * @param edges - JSONObject HashMap representing all of the edges
93 * @param edgeDirection - a string indicating the direction of the edge
95 private void removePrivateEdges(JsonObject jsonObject, JsonObject edges, String edgeDirection) {
97 Iterator it = edges.entrySet().iterator();
98 while (it.hasNext()) {
99 Map.Entry<String, JsonElement> outEntry = (Map.Entry<String, JsonElement>) it.next();
100 JsonArray edgePropertiesArray = outEntry.getValue().getAsJsonArray();
101 for (int index = 0; index < edgePropertiesArray.size(); ++index) {
102 JsonElement jsonElement = edgePropertiesArray.get(index);
103 JsonObject obj = jsonElement.getAsJsonObject();
104 if (obj.has("properties")) {
105 JsonObject objProperties = obj.get("properties").getAsJsonObject();
106 if (objProperties.has("private")) {
107 boolean isPrivate = objProperties.get("private").getAsBoolean();
109 if (edges.size() == 1) {
110 if (edgePropertiesArray.size() == 1) {
111 jsonObject.remove(edgeDirection);
113 edgePropertiesArray.remove(jsonElement);
116 edgePropertiesArray.remove(jsonElement);
122 if (edgePropertiesArray.size() == 0) {
129 public int parallelThreshold() {