[AAI] Fix doc config files
[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 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Optional;
34 import org.apache.tinkerpop.gremlin.structure.Direction;
35 import org.apache.tinkerpop.gremlin.structure.Vertex;
36 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper;
37 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter;
38 import org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry;
39 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
40 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 public class GraphSON implements FormatMapper {
45
46     private static final Logger logger = LoggerFactory.getLogger(GraphSON.class);
47
48     private final GraphSONMapper mapper =
49             GraphSONMapper.build().addRegistry(JanusGraphIoRegistry.getInstance()).create();
50     private final GraphSONWriter writer = GraphSONWriter.build().mapper(mapper).create();
51     protected JsonParser parser = new JsonParser();
52
53     @Override
54     public Optional<JsonObject> formatObject(Object v) {
55         OutputStream os = new ByteArrayOutputStream();
56         String result = "";
57         try {
58             writer.writeVertex(os, (Vertex) v, Direction.BOTH);
59
60             result = os.toString();
61         } catch (IOException e) {
62             logger.debug("GraphSON writeVertex error : {}", e.getMessage());
63         }
64
65         JsonObject jsonObject = parser.parse(result).getAsJsonObject();
66
67         if (jsonObject != null) {
68
69             if (jsonObject.has("outE")) {
70                 JsonObject outEdges = jsonObject.get("outE").getAsJsonObject();
71                 removePrivateEdges(jsonObject, outEdges, "outE");
72             }
73
74             if (jsonObject.has("inE")) {
75                 JsonObject inEdges = jsonObject.get("inE").getAsJsonObject();
76                 removePrivateEdges(jsonObject, inEdges, "inE");
77             }
78
79         }
80
81         return Optional.of(jsonObject);
82
83     }
84
85     @Override
86     public Optional<JsonObject> formatObject(Object obj, Map<String, List<String>> properties)
87         throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
88         return Optional.empty();
89     }
90
91     /**
92      * Removes the private edges from the json object
93      *
94      * Please note that the reason to choose to remove the private
95      * edges from the json object instead of removing it from the vertex
96      * itself is the fact that even though the transaction will be rolled back
97      * is because of the possible incosistent behavior where the actual edge
98      * might actually be removed in a long running transaction and is not worth the risk
99      *
100      * @param jsonObject - JSON Object from which we are removing the private edges for
101      * @param edges - JSONObject HashMap representing all of the edges
102      * @param edgeDirection - a string indicating the direction of the edge
103      */
104     private void removePrivateEdges(JsonObject jsonObject, JsonObject edges, String edgeDirection) {
105
106         Iterator it = edges.entrySet().iterator();
107         while (it.hasNext()) {
108             Map.Entry<String, JsonElement> outEntry = (Map.Entry<String, JsonElement>) it.next();
109             JsonArray edgePropertiesArray = outEntry.getValue().getAsJsonArray();
110             for (int index = 0; index < edgePropertiesArray.size(); ++index) {
111                 JsonElement jsonElement = edgePropertiesArray.get(index);
112                 JsonObject obj = jsonElement.getAsJsonObject();
113                 if (obj.has("properties")) {
114                     JsonObject objProperties = obj.get("properties").getAsJsonObject();
115                     if (objProperties.has("private")) {
116                         boolean isPrivate = objProperties.get("private").getAsBoolean();
117                         if (isPrivate) {
118                             if (edges.size() == 1) {
119                                 if (edgePropertiesArray.size() == 1) {
120                                     jsonObject.remove(edgeDirection);
121                                 } else {
122                                     edgePropertiesArray.remove(jsonElement);
123                                 }
124                             } else {
125                                 edgePropertiesArray.remove(jsonElement);
126                             }
127                         }
128                     }
129                 }
130             }
131             if (edgePropertiesArray.size() == 0) {
132                 it.remove();
133             }
134         }
135     }
136
137     @Override
138     public int parallelThreshold() {
139         return 50;
140     }
141 }