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=========================================================
20 package org.onap.aai.serialization.queryformats;
22 import com.google.gson.JsonArray;
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonParser;
26 import org.apache.tinkerpop.gremlin.structure.Direction;
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper;
29 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter;
30 import org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry;
32 import java.io.ByteArrayOutputStream;
33 import java.io.IOException;
34 import java.io.OutputStream;
35 import java.util.HashMap;
36 import java.util.Iterator;
38 import java.util.Optional;
40 public class GraphSON implements FormatMapper {
42 private final GraphSONMapper mapper = GraphSONMapper.build().addRegistry(JanusGraphIoRegistry.getInstance()).create();
43 private final GraphSONWriter writer = GraphSONWriter.build().mapper(mapper).create();
44 protected JsonParser parser = new JsonParser();
47 public Optional<JsonObject> formatObject(Object v) {
48 OutputStream os = new ByteArrayOutputStream();
51 writer.writeVertex(os, (Vertex)v, Direction.BOTH);
53 result = os.toString();
54 } catch (IOException e) {
55 // TODO Auto-generated catch block
59 JsonObject jsonObject = parser.parse(result).getAsJsonObject();
61 if(jsonObject != null){
63 if(jsonObject.has("outE")){
64 JsonObject outEdges = jsonObject.get("outE").getAsJsonObject();
65 removePrivateEdges(jsonObject, outEdges, "outE");
68 if(jsonObject.has("inE")){
69 JsonObject inEdges = jsonObject.get("inE").getAsJsonObject();
70 removePrivateEdges(jsonObject, inEdges, "inE");
75 return Optional.of(jsonObject);
80 * Removes the private edges from the json object
82 * Please note that the reason to choose to remove the private
83 * edges from the json object instead of removing it from the vertex
84 * itself is the fact that even though the transaction will be rolled back
85 * is because of the possible incosistent behavior where the actual edge
86 * might actually be removed in a long running transaction and is not worth the risk
88 * @param jsonObject - JSON Object from which we are removing the private edges for
89 * @param edges - JSONObject HashMap representing all of the edges
90 * @param edgeDirection - a string indicating the direction of the edge
92 private void removePrivateEdges(JsonObject jsonObject, JsonObject edges, String edgeDirection) {
94 Iterator it = edges.entrySet().iterator();
96 Map.Entry<String, JsonElement> outEntry = (Map.Entry<String, JsonElement>) it.next();
97 JsonArray edgePropertiesArray = outEntry.getValue().getAsJsonArray();
98 for(int index = 0; index < edgePropertiesArray.size(); ++index){
99 JsonElement jsonElement = edgePropertiesArray.get(index);
100 JsonObject obj = jsonElement.getAsJsonObject();
101 if (obj.has("properties")) {
102 JsonObject objProperties = obj.get("properties").getAsJsonObject();
103 if (objProperties.has("private")) {
104 boolean isPrivate = objProperties.get("private").getAsBoolean();
106 if (edges.size() == 1) {
107 if (edgePropertiesArray.size() == 1) {
108 jsonObject.remove(edgeDirection);
110 edgePropertiesArray.remove(jsonElement);
113 edgePropertiesArray.remove(jsonElement);
119 if(edgePropertiesArray.size() == 0){
126 public int parallelThreshold() {