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;
33 import org.apache.tinkerpop.gremlin.structure.Direction;
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
35 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper;
36 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter;
37 import org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry;
38 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
39 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
41 public class GraphSON implements FormatMapper {
43 private final GraphSONMapper mapper =
44 GraphSONMapper.build().addRegistry(JanusGraphIoRegistry.getInstance()).create();
45 private final GraphSONWriter writer = GraphSONWriter.build().mapper(mapper).create();
46 protected JsonParser parser = new JsonParser();
49 public Optional<JsonObject> formatObject(Object v) {
50 OutputStream os = new ByteArrayOutputStream();
53 writer.writeVertex(os, (Vertex) v, Direction.BOTH);
55 result = os.toString();
56 } catch (IOException e) {
57 // TODO Auto-generated catch block
61 JsonObject jsonObject = parser.parse(result).getAsJsonObject();
63 if (jsonObject != null) {
65 if (jsonObject.has("outE")) {
66 JsonObject outEdges = jsonObject.get("outE").getAsJsonObject();
67 removePrivateEdges(jsonObject, outEdges, "outE");
70 if (jsonObject.has("inE")) {
71 JsonObject inEdges = jsonObject.get("inE").getAsJsonObject();
72 removePrivateEdges(jsonObject, inEdges, "inE");
77 return Optional.of(jsonObject);
82 public Optional<JsonObject> formatObject(Object o, Map<String, List<String>> properties) throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
83 return Optional.empty();
87 * Removes the private edges from the json object
89 * Please note that the reason to choose to remove the private
90 * edges from the json object instead of removing it from the vertex
91 * itself is the fact that even though the transaction will be rolled back
92 * is because of the possible incosistent behavior where the actual edge
93 * might actually be removed in a long running transaction and is not worth the risk
95 * @param jsonObject - JSON Object from which we are removing the private edges for
96 * @param edges - JSONObject HashMap representing all of the edges
97 * @param edgeDirection - a string indicating the direction of the edge
99 private void removePrivateEdges(JsonObject jsonObject, JsonObject edges, String edgeDirection) {
101 Iterator it = edges.entrySet().iterator();
102 while (it.hasNext()) {
103 Map.Entry<String, JsonElement> outEntry = (Map.Entry<String, JsonElement>) it.next();
104 JsonArray edgePropertiesArray = outEntry.getValue().getAsJsonArray();
105 for (int index = 0; index < edgePropertiesArray.size(); ++index) {
106 JsonElement jsonElement = edgePropertiesArray.get(index);
107 JsonObject obj = jsonElement.getAsJsonObject();
108 if (obj.has("properties")) {
109 JsonObject objProperties = obj.get("properties").getAsJsonObject();
110 if (objProperties.has("private")) {
111 boolean isPrivate = objProperties.get("private").getAsBoolean();
113 if (edges.size() == 1) {
114 if (edgePropertiesArray.size() == 1) {
115 jsonObject.remove(edgeDirection);
117 edgePropertiesArray.remove(jsonElement);
120 edgePropertiesArray.remove(jsonElement);
126 if (edgePropertiesArray.size() == 0) {
133 public int parallelThreshold() {