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
|
/**
* ============LICENSE_START=======================================================
* org.onap.aai
* ================================================================================
* Copyright © 2017-2018 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.onap.aai.serialization.queryformats;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper;
import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter;
import org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry;
import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class GraphSON implements FormatMapper {
private static final Logger logger = LoggerFactory.getLogger(GraphSON.class);
private final GraphSONMapper mapper =
GraphSONMapper.build().addRegistry(JanusGraphIoRegistry.getInstance()).create();
private final GraphSONWriter writer = GraphSONWriter.build().mapper(mapper).create();
@Override
public Optional<JsonObject> formatObject(Object v) {
OutputStream os = new ByteArrayOutputStream();
String result = "";
try {
writer.writeVertex(os, (Vertex) v, Direction.BOTH);
result = os.toString();
} catch (IOException e) {
logger.debug("GraphSON writeVertex error : {}", e.getMessage());
}
JsonObject jsonObject = JsonParser.parseString(result).getAsJsonObject();
if (jsonObject != null) {
if (jsonObject.has("outE")) {
JsonObject outEdges = jsonObject.get("outE").getAsJsonObject();
removePrivateEdges(jsonObject, outEdges, "outE");
}
if (jsonObject.has("inE")) {
JsonObject inEdges = jsonObject.get("inE").getAsJsonObject();
removePrivateEdges(jsonObject, inEdges, "inE");
}
}
return Optional.of(jsonObject);
}
@Override
public Optional<JsonObject> formatObject(Object obj, Map<String, List<String>> properties)
throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
return Optional.empty();
}
/**
* Removes the private edges from the json object
*
* Please note that the reason to choose to remove the private
* edges from the json object instead of removing it from the vertex
* itself is the fact that even though the transaction will be rolled back
* is because of the possible incosistent behavior where the actual edge
* might actually be removed in a long running transaction and is not worth the risk
*
* @param jsonObject - JSON Object from which we are removing the private edges for
* @param edges - JSONObject HashMap representing all of the edges
* @param edgeDirection - a string indicating the direction of the edge
*/
private void removePrivateEdges(JsonObject jsonObject, JsonObject edges, String edgeDirection) {
Iterator it = edges.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, JsonElement> outEntry = (Map.Entry<String, JsonElement>) it.next();
JsonArray edgePropertiesArray = outEntry.getValue().getAsJsonArray();
for (int index = 0; index < edgePropertiesArray.size(); ++index) {
JsonElement jsonElement = edgePropertiesArray.get(index);
JsonObject obj = jsonElement.getAsJsonObject();
if (obj.has("properties")) {
JsonObject objProperties = obj.get("properties").getAsJsonObject();
if (objProperties.has("private")) {
boolean isPrivate = objProperties.get("private").getAsBoolean();
if (isPrivate) {
if (edges.size() == 1) {
if (edgePropertiesArray.size() == 1) {
jsonObject.remove(edgeDirection);
} else {
edgePropertiesArray.remove(jsonElement);
}
} else {
edgePropertiesArray.remove(jsonElement);
}
}
}
}
}
if (edgePropertiesArray.size() == 0) {
it.remove();
}
}
}
@Override
public int parallelThreshold() {
return 50;
}
}
|