2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.serialization.queryformats;
24 import java.util.Iterator;
25 import java.util.List;
27 import org.apache.tinkerpop.gremlin.structure.Direction;
28 import org.apache.tinkerpop.gremlin.structure.Edge;
29 import org.apache.tinkerpop.gremlin.structure.Vertex;
30 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
31 import org.onap.aai.db.props.AAIProperties;
32 import org.onap.aai.introspection.Loader;
33 import org.onap.aai.serialization.db.DBSerializer;
34 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
35 import org.onap.aai.serialization.queryformats.params.Depth;
36 import org.onap.aai.serialization.queryformats.params.NodesOnly;
37 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
39 import com.google.gson.Gson;
40 import com.google.gson.JsonArray;
41 import com.google.gson.JsonObject;
42 import com.google.gson.JsonParser;
45 public class RawFormat implements FormatMapper {
46 protected JsonParser parser = new JsonParser();
47 protected final DBSerializer serializer;
48 protected final Loader loader;
49 protected final UrlBuilder urlBuilder;
50 protected final int depth;
51 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 JsonObject formatObject(Object input) throws AAIFormatVertexException {
62 Vertex v = (Vertex)input;
63 JsonObject json = new JsonObject();
64 json.addProperty("id", v.id().toString());
65 json.addProperty("node-type", v.<String>value(AAIProperties.NODE_TYPE));
66 json.addProperty("url", this.urlBuilder.pathed(v));
67 json.add("properties", this.createPropertiesObject(v));
69 json.add("related-to", this.createRelationshipObject(v));
75 public int parallelThreshold() {
80 public JsonObject createPropertiesObject(Vertex v) throws AAIFormatVertexException {
81 JsonObject json = new JsonObject();
82 Iterator<VertexProperty<Object>> iter = v.properties();
84 while (iter.hasNext()) {
85 VertexProperty<Object> prop = iter.next();
86 if (prop.value() instanceof String) {
87 json.addProperty(prop.key(), (String)prop.value());
88 } else if (prop.value() instanceof Boolean) {
89 json.addProperty(prop.key(), (Boolean)prop.value());
90 } else if (prop.value() instanceof Number) {
91 json.addProperty(prop.key(), (Number)prop.value());
92 } else if (prop.value() instanceof List) {
93 Gson gson = new Gson();
94 String list = gson.toJson(prop.value());
96 json.addProperty(prop.key(), list);
106 protected JsonArray createRelationshipObject(Vertex v) throws AAIFormatVertexException {
107 JsonArray jarray = new JsonArray();
108 Iterator<Edge> inIter = v.edges(Direction.IN);
109 Iterator<Edge> outIter = v.edges(Direction.OUT);
111 while (inIter.hasNext()) {
112 Edge e = inIter.next();
113 jarray.add(this.getRelatedObject(e.label(), e.outVertex()));
116 while (outIter.hasNext()) {
117 Edge e = outIter.next();
118 jarray.add(this.getRelatedObject(e.label(), e.inVertex()));
124 private JsonObject getRelatedObject(String label, Vertex related) throws AAIFormatVertexException {
125 JsonObject json = new JsonObject();
126 json.addProperty("id", related.id().toString());
127 json.addProperty("relationship-label", label);
128 json.addProperty("node-type", related.<String>value(AAIProperties.NODE_TYPE));
129 json.addProperty("url", this.urlBuilder.pathed(related));
134 public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
136 protected final Loader loader;
137 protected final DBSerializer serializer;
138 protected final UrlBuilder urlBuilder;
139 protected boolean includeUrl = false;
140 protected boolean nodesOnly = false;
141 protected int depth = 1;
142 protected boolean modelDriven = false;
143 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
144 this.loader = loader;
145 this.serializer = serializer;
146 this.urlBuilder = urlBuilder;
149 protected Loader getLoader() {
153 protected DBSerializer getSerializer() {
154 return this.serializer;
157 protected UrlBuilder getUrlBuilder() {
158 return this.urlBuilder;
161 public Builder includeUrl() {
162 this.includeUrl = true;
166 public Builder nodesOnly(Boolean nodesOnly) {
167 this.nodesOnly = nodesOnly;
170 public boolean isNodesOnly() {
171 return this.nodesOnly;
174 public Builder depth(Integer depth) {
179 public int getDepth() {
183 public boolean isIncludeUrl() {
184 return this.includeUrl;
187 public Builder modelDriven() {
188 this.modelDriven = true;
192 public boolean getModelDriven() {
193 return this.modelDriven;
195 public RawFormat build() {
197 return new SimpleFormat(this);
199 return new RawFormat(this);