eeac2195b8b123947b964073b871d2f68afae719
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / GraphSON.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.serialization.queryformats;
22
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
27
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;
33 import java.util.Map;
34 import java.util.Optional;
35
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;
41
42 public class GraphSON implements FormatMapper {
43
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();
48
49     @Override
50     public Optional<JsonObject> formatObject(Object v) {
51         OutputStream os = new ByteArrayOutputStream();
52         String result = "";
53         try {
54             writer.writeVertex(os, (Vertex) v, Direction.BOTH);
55
56             result = os.toString();
57         } catch (IOException e) {
58             // TODO Auto-generated catch block
59             e.printStackTrace();
60         }
61
62         JsonObject jsonObject = parser.parse(result).getAsJsonObject();
63
64         if (jsonObject != null) {
65
66             if (jsonObject.has("outE")) {
67                 JsonObject outEdges = jsonObject.get("outE").getAsJsonObject();
68                 removePrivateEdges(jsonObject, outEdges, "outE");
69             }
70
71             if (jsonObject.has("inE")) {
72                 JsonObject inEdges = jsonObject.get("inE").getAsJsonObject();
73                 removePrivateEdges(jsonObject, inEdges, "inE");
74             }
75
76         }
77
78         return Optional.of(jsonObject);
79
80     }
81
82     /**
83      * Removes the private edges from the json object
84      *
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
90      *
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
94      */
95     private void removePrivateEdges(JsonObject jsonObject, JsonObject edges, String edgeDirection) {
96
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();
108                         if (isPrivate) {
109                             if (edges.size() == 1) {
110                                 if (edgePropertiesArray.size() == 1) {
111                                     jsonObject.remove(edgeDirection);
112                                 } else {
113                                     edgePropertiesArray.remove(jsonElement);
114                                 }
115                             } else {
116                                 edgePropertiesArray.remove(jsonElement);
117                             }
118                         }
119                     }
120                 }
121             }
122             if (edgePropertiesArray.size() == 0) {
123                 it.remove();
124             }
125         }
126     }
127
128     @Override
129     public int parallelThreshold() {
130         return 50;
131     }
132 }