2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.serialization.queryformats;
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;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.Optional;
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();
59 public int parallelThreshold() {
64 public Optional<JsonObject> createPropertiesObject(Vertex v) throws AAIFormatVertexException {
65 JsonObject json = new JsonObject();
66 Iterator<VertexProperty<Object>> iter = v.properties();
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());
80 json.addProperty(prop.key(), list);
87 return Optional.of(json);
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);
95 while (inIter.hasNext()) {
96 Edge e = inIter.next();
97 Vertex outVertex = e.outVertex();
98 this.addEdge(e, outVertex, jarray);
101 while (outIter.hasNext()) {
102 Edge e = outIter.next();
103 Vertex inVertex = e.inVertex();
104 this.addEdge(e, inVertex, jarray);
110 protected void addEdge(Edge e, Vertex vertex, JsonArray array) throws AAIFormatVertexException {
111 array.add(this.getRelatedObject(e.label(), vertex));
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));
124 public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
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;
139 protected Loader getLoader() {
143 protected DBSerializer getSerializer() {
144 return this.serializer;
147 protected UrlBuilder getUrlBuilder() {
148 return this.urlBuilder;
151 public Builder includeUrl() {
152 this.includeUrl = true;
156 public Builder nodesOnly(Boolean nodesOnly) {
157 this.nodesOnly = nodesOnly;
160 public boolean isNodesOnly() {
161 return this.nodesOnly;
164 public Builder depth(Integer depth) {
169 public int getDepth() {
173 public boolean isIncludeUrl() {
174 return this.includeUrl;
177 public Builder modelDriven() {
178 this.modelDriven = true;
182 public boolean getModelDriven() {
183 return this.modelDriven;
185 public RawFormat build() {
187 return new SimpleFormat(this);
189 return new RawFormat(this);
195 protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
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());
205 return Optional.empty();
208 json.add("related-to", this.createRelationshipObject(v));
210 return Optional.of(json);