7e0f3f6cc1632cd6905ce040fb265036ce64b03e
[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 package org.onap.aai.serialization.queryformats;
21
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;
31
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;
37 import java.util.Map;
38 import java.util.Optional;
39
40 public class GraphSON implements FormatMapper {
41
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();
45         
46         @Override
47         public Optional<JsonObject> formatObject(Object v) {
48                 OutputStream os = new ByteArrayOutputStream();
49                 String result = "";
50                 try {
51                         writer.writeVertex(os, (Vertex)v, Direction.BOTH);
52                         
53                         result = os.toString();
54                 } catch (IOException e) {
55                         // TODO Auto-generated catch block
56                         e.printStackTrace();
57                 }
58
59                 JsonObject jsonObject = parser.parse(result).getAsJsonObject();
60
61                 if(jsonObject != null){
62
63                         if(jsonObject.has("outE")){
64                                 JsonObject outEdges = jsonObject.get("outE").getAsJsonObject();
65                                 removePrivateEdges(jsonObject, outEdges, "outE");
66                         }
67
68                         if(jsonObject.has("inE")){
69                                 JsonObject inEdges = jsonObject.get("inE").getAsJsonObject();
70                                 removePrivateEdges(jsonObject, inEdges, "inE");
71                         }
72
73                 }
74
75                 return Optional.of(jsonObject);
76                 
77         }
78
79         /**
80          * Removes the private edges from the json object
81          *
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
87          *
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
91          */
92         private void removePrivateEdges(JsonObject jsonObject, JsonObject edges, String edgeDirection) {
93
94             Iterator it = edges.entrySet().iterator();
95             while(it.hasNext()){
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();
105                                                 if (isPrivate) {
106                                                         if (edges.size() == 1) {
107                                                                 if (edgePropertiesArray.size() == 1) {
108                                                                         jsonObject.remove(edgeDirection);
109                                                                 } else {
110                                                                         edgePropertiesArray.remove(jsonElement);
111                                                                 }
112                                                         } else {
113                                                                 edgePropertiesArray.remove(jsonElement);
114                                                         }
115                                                 }
116                                         }
117                                 }
118                         }
119                         if(edgePropertiesArray.size() == 0){
120                             it.remove();
121                         }
122                 }
123         }
124
125         @Override
126         public int parallelThreshold() {
127                 return 50;
128         }
129 }