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