Enhancements for the aai-common library
[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.*;
32
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;
40
41 public class GraphSON implements FormatMapper {
42
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();
47
48     @Override
49     public Optional<JsonObject> formatObject(Object v) {
50         OutputStream os = new ByteArrayOutputStream();
51         String result = "";
52         try {
53             writer.writeVertex(os, (Vertex) v, Direction.BOTH);
54
55             result = os.toString();
56         } catch (IOException e) {
57             // TODO Auto-generated catch block
58             e.printStackTrace();
59         }
60
61         JsonObject jsonObject = parser.parse(result).getAsJsonObject();
62
63         if (jsonObject != null) {
64
65             if (jsonObject.has("outE")) {
66                 JsonObject outEdges = jsonObject.get("outE").getAsJsonObject();
67                 removePrivateEdges(jsonObject, outEdges, "outE");
68             }
69
70             if (jsonObject.has("inE")) {
71                 JsonObject inEdges = jsonObject.get("inE").getAsJsonObject();
72                 removePrivateEdges(jsonObject, inEdges, "inE");
73             }
74
75         }
76
77         return Optional.of(jsonObject);
78
79     }
80
81     @Override
82     public Optional<JsonObject> formatObject(Object o, Map<String, List<String>> properties) throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
83         return Optional.empty();
84     }
85
86     /**
87      * Removes the private edges from the json object
88      *
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
94      *
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
98      */
99     private void removePrivateEdges(JsonObject jsonObject, JsonObject edges, String edgeDirection) {
100
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();
112                         if (isPrivate) {
113                             if (edges.size() == 1) {
114                                 if (edgePropertiesArray.size() == 1) {
115                                     jsonObject.remove(edgeDirection);
116                                 } else {
117                                     edgePropertiesArray.remove(jsonElement);
118                                 }
119                             } else {
120                                 edgePropertiesArray.remove(jsonElement);
121                             }
122                         }
123                     }
124                 }
125             }
126             if (edgePropertiesArray.size() == 0) {
127                 it.remove();
128             }
129         }
130     }
131
132     @Override
133     public int parallelThreshold() {
134         return 50;
135     }
136 }