8636ebfa0ec03eea71a47743eb585d28b8811a5e
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / RawFormat.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.Gson;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
27
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Optional;
31
32 import org.apache.tinkerpop.gremlin.structure.Direction;
33 import org.apache.tinkerpop.gremlin.structure.Edge;
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
35 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
36 import org.onap.aai.db.props.AAIProperties;
37 import org.onap.aai.introspection.Loader;
38 import org.onap.aai.serialization.db.DBSerializer;
39 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
40 import org.onap.aai.serialization.queryformats.params.Depth;
41 import org.onap.aai.serialization.queryformats.params.NodesOnly;
42 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
43
44 public class RawFormat extends MultiFormatMapper {
45     protected JsonParser parser = new JsonParser();
46     protected final DBSerializer serializer;
47     protected final Loader loader;
48     protected final UrlBuilder urlBuilder;
49     protected final int depth;
50     protected final boolean nodesOnly;
51
52     protected RawFormat(Builder builder) {
53         this.urlBuilder = builder.getUrlBuilder();
54         this.loader = builder.getLoader();
55         this.serializer = builder.getSerializer();
56         this.depth = builder.getDepth();
57         this.nodesOnly = builder.isNodesOnly();
58     }
59
60     @Override
61     public int parallelThreshold() {
62         return 100;
63     }
64
65     public Optional<JsonObject> createPropertiesObject(Vertex v) throws AAIFormatVertexException {
66         JsonObject json = new JsonObject();
67         Iterator<VertexProperty<Object>> iter = v.properties();
68
69         while (iter.hasNext()) {
70             VertexProperty<Object> prop = iter.next();
71             if (prop.value() instanceof String) {
72                 json.addProperty(prop.key(), (String) prop.value());
73             } else if (prop.value() instanceof Boolean) {
74                 json.addProperty(prop.key(), (Boolean) prop.value());
75             } else if (prop.value() instanceof Number) {
76                 json.addProperty(prop.key(), (Number) prop.value());
77             } else if (prop.value() instanceof List) {
78                 Gson gson = new Gson();
79                 String list = gson.toJson(prop.value());
80
81                 json.addProperty(prop.key(), list);
82             } else {
83                 // throw exception?
84                 return null;
85             }
86         }
87
88         return Optional.of(json);
89     }
90
91     protected JsonArray createRelationshipObject(Vertex v) throws AAIFormatVertexException {
92         JsonArray jarray = new JsonArray();
93         Iterator<Edge> inIter = v.edges(Direction.IN);
94         Iterator<Edge> outIter = v.edges(Direction.OUT);
95
96         while (inIter.hasNext()) {
97             Edge e = inIter.next();
98             Vertex outVertex = e.outVertex();
99             this.addEdge(e, outVertex, jarray);
100         }
101
102         while (outIter.hasNext()) {
103             Edge e = outIter.next();
104             Vertex inVertex = e.inVertex();
105             this.addEdge(e, inVertex, jarray);
106         }
107
108         return jarray;
109     }
110
111     protected void addEdge(Edge e, Vertex vertex, JsonArray array) throws AAIFormatVertexException {
112         array.add(this.getRelatedObject(e.label(), vertex));
113     }
114
115     protected JsonObject getRelatedObject(String label, Vertex related) throws AAIFormatVertexException {
116         JsonObject json = new JsonObject();
117         json.addProperty("id", related.id().toString());
118         json.addProperty("relationship-label", label);
119         json.addProperty("node-type", related.<String>value(AAIProperties.NODE_TYPE));
120         json.addProperty("url", this.urlBuilder.pathed(related));
121
122         return json;
123     }
124
125     public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
126
127         protected final Loader loader;
128         protected final DBSerializer serializer;
129         protected final UrlBuilder urlBuilder;
130         protected boolean includeUrl = false;
131         protected boolean nodesOnly = false;
132         protected int depth = 1;
133         protected boolean modelDriven = false;
134
135         public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
136             this.loader = loader;
137             this.serializer = serializer;
138             this.urlBuilder = urlBuilder;
139         }
140
141         protected Loader getLoader() {
142             return this.loader;
143         }
144
145         protected DBSerializer getSerializer() {
146             return this.serializer;
147         }
148
149         protected UrlBuilder getUrlBuilder() {
150             return this.urlBuilder;
151         }
152
153         public Builder includeUrl() {
154             this.includeUrl = true;
155             return this;
156         }
157
158         public Builder nodesOnly(Boolean nodesOnly) {
159             this.nodesOnly = nodesOnly;
160             return this;
161         }
162
163         public boolean isNodesOnly() {
164             return this.nodesOnly;
165         }
166
167         public Builder depth(Integer depth) {
168             this.depth = depth;
169             return this;
170         }
171
172         public int getDepth() {
173             return this.depth;
174         }
175
176         public boolean isIncludeUrl() {
177             return this.includeUrl;
178         }
179
180         public Builder modelDriven() {
181             this.modelDriven = true;
182             return this;
183         }
184
185         public boolean getModelDriven() {
186             return this.modelDriven;
187         }
188
189         public RawFormat build() {
190             if (modelDriven) {
191                 return new SimpleFormat(this);
192             } else {
193                 return new RawFormat(this);
194             }
195         }
196     }
197
198     @Override
199     protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
200
201         JsonObject json = new JsonObject();
202         json.addProperty("id", v.id().toString());
203         json.addProperty("node-type", v.<String>value(AAIProperties.NODE_TYPE));
204         json.addProperty("url", this.urlBuilder.pathed(v));
205         Optional<JsonObject> properties = this.createPropertiesObject(v);
206         if (properties.isPresent()) {
207             json.add("properties", properties.get());
208         } else {
209             return Optional.empty();
210         }
211         if (!nodesOnly) {
212             json.add("related-to", this.createRelationshipObject(v));
213         }
214         return Optional.of(json);
215     }
216 }