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=========================================================
21 package org.onap.aai.serialization.queryformats;
23 import com.google.gson.Gson;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Optional;
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;
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;
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();
61 public int parallelThreshold() {
65 public Optional<JsonObject> createPropertiesObject(Vertex v) throws AAIFormatVertexException {
66 JsonObject json = new JsonObject();
67 Iterator<VertexProperty<Object>> iter = v.properties();
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());
81 json.addProperty(prop.key(), list);
88 return Optional.of(json);
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);
96 while (inIter.hasNext()) {
97 Edge e = inIter.next();
98 Vertex outVertex = e.outVertex();
99 this.addEdge(e, outVertex, jarray);
102 while (outIter.hasNext()) {
103 Edge e = outIter.next();
104 Vertex inVertex = e.inVertex();
105 this.addEdge(e, inVertex, jarray);
111 protected void addEdge(Edge e, Vertex vertex, JsonArray array) throws AAIFormatVertexException {
112 array.add(this.getRelatedObject(e.label(), vertex));
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));
125 public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
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;
135 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
136 this.loader = loader;
137 this.serializer = serializer;
138 this.urlBuilder = urlBuilder;
141 protected Loader getLoader() {
145 protected DBSerializer getSerializer() {
146 return this.serializer;
149 protected UrlBuilder getUrlBuilder() {
150 return this.urlBuilder;
153 public Builder includeUrl() {
154 this.includeUrl = true;
158 public Builder nodesOnly(Boolean nodesOnly) {
159 this.nodesOnly = nodesOnly;
163 public boolean isNodesOnly() {
164 return this.nodesOnly;
167 public Builder depth(Integer depth) {
172 public int getDepth() {
176 public boolean isIncludeUrl() {
177 return this.includeUrl;
180 public Builder modelDriven() {
181 this.modelDriven = true;
185 public boolean getModelDriven() {
186 return this.modelDriven;
189 public RawFormat build() {
191 return new SimpleFormat(this);
193 return new RawFormat(this);
199 protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
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());
209 return Optional.empty();
212 json.add("related-to", this.createRelationshipObject(v));
214 return Optional.of(json);