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 java.util.Iterator;
23 import java.util.List;
25 import org.apache.tinkerpop.gremlin.structure.Direction;
26 import org.apache.tinkerpop.gremlin.structure.Edge;
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
29 import org.onap.aai.db.props.AAIProperties;
30 import org.onap.aai.introspection.Loader;
31 import org.onap.aai.serialization.db.DBSerializer;
32 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
33 import org.onap.aai.serialization.queryformats.params.Depth;
34 import org.onap.aai.serialization.queryformats.params.NodesOnly;
35 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
37 import com.google.gson.Gson;
38 import com.google.gson.JsonArray;
39 import com.google.gson.JsonObject;
40 import com.google.gson.JsonParser;
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 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);
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 jarray.add(this.getRelatedObject(e.label(), e.outVertex()));
100 while (outIter.hasNext()) {
101 Edge e = outIter.next();
102 jarray.add(this.getRelatedObject(e.label(), e.inVertex()));
108 private JsonObject getRelatedObject(String label, Vertex related) throws AAIFormatVertexException {
109 JsonObject json = new JsonObject();
110 json.addProperty("id", related.id().toString());
111 json.addProperty("relationship-label", label);
112 json.addProperty("node-type", related.<String>value(AAIProperties.NODE_TYPE));
113 json.addProperty("url", this.urlBuilder.pathed(related));
118 public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
120 protected final Loader loader;
121 protected final DBSerializer serializer;
122 protected final UrlBuilder urlBuilder;
123 protected boolean includeUrl = false;
124 protected boolean nodesOnly = false;
125 protected int depth = 1;
126 protected boolean modelDriven = false;
127 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
128 this.loader = loader;
129 this.serializer = serializer;
130 this.urlBuilder = urlBuilder;
133 protected Loader getLoader() {
137 protected DBSerializer getSerializer() {
138 return this.serializer;
141 protected UrlBuilder getUrlBuilder() {
142 return this.urlBuilder;
145 public Builder includeUrl() {
146 this.includeUrl = true;
150 public Builder nodesOnly(Boolean nodesOnly) {
151 this.nodesOnly = nodesOnly;
154 public boolean isNodesOnly() {
155 return this.nodesOnly;
158 public Builder depth(Integer depth) {
163 public int getDepth() {
167 public boolean isIncludeUrl() {
168 return this.includeUrl;
171 public Builder modelDriven() {
172 this.modelDriven = true;
176 public boolean getModelDriven() {
177 return this.modelDriven;
179 public RawFormat build() {
181 return new SimpleFormat(this);
183 return new RawFormat(this);
189 protected JsonObject getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
191 JsonObject json = new JsonObject();
192 json.addProperty("id", v.id().toString());
193 json.addProperty("node-type", v.<String>value(AAIProperties.NODE_TYPE));
194 json.addProperty("url", this.urlBuilder.pathed(v));
195 json.add("properties", this.createPropertiesObject(v));
197 json.add("related-to", this.createRelationshipObject(v));