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.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 com.google.gson.Gson;
39 import com.google.gson.JsonArray;
40 import com.google.gson.JsonObject;
41 import com.google.gson.JsonParser;
44 public class RawFormat implements FormatMapper {
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;
51 protected RawFormat(Builder builder) {
52 this.urlBuilder = builder.getUrlBuilder();
53 this.loader = builder.getLoader();
54 this.serializer = builder.getSerializer();
55 this.depth = builder.getDepth();
56 this.nodesOnly = builder.isNodesOnly();
60 public JsonObject formatObject(Object input) throws AAIFormatVertexException {
61 Vertex v = (Vertex)input;
62 JsonObject json = new JsonObject();
63 json.addProperty("id", v.id().toString());
64 json.addProperty("node-type", v.<String>value(AAIProperties.NODE_TYPE));
65 json.addProperty("url", this.urlBuilder.pathed(v));
66 json.add("properties", this.createPropertiesObject(v));
68 json.add("related-to", this.createRelationshipObject(v));
74 public int parallelThreshold() {
79 public JsonObject createPropertiesObject(Vertex v) throws AAIFormatVertexException {
80 JsonObject json = new JsonObject();
81 Iterator<VertexProperty<Object>> iter = v.properties();
83 while (iter.hasNext()) {
84 VertexProperty<Object> prop = iter.next();
85 if (prop.value() instanceof String) {
86 json.addProperty(prop.key(), (String)prop.value());
87 } else if (prop.value() instanceof Boolean) {
88 json.addProperty(prop.key(), (Boolean)prop.value());
89 } else if (prop.value() instanceof Number) {
90 json.addProperty(prop.key(), (Number)prop.value());
91 } else if (prop.value() instanceof List) {
92 Gson gson = new Gson();
93 String list = gson.toJson(prop.value());
95 json.addProperty(prop.key(), list);
104 protected JsonArray createRelationshipObject(Vertex v) throws AAIFormatVertexException {
105 JsonArray jarray = new JsonArray();
106 Iterator<Vertex> iter = v.vertices(Direction.BOTH);
108 while (iter.hasNext()) {
109 Vertex related = iter.next();
111 JsonObject json = new JsonObject();
112 json.addProperty("id", related.id().toString());
113 json.addProperty("node-type", related.<String>value(AAIProperties.NODE_TYPE));
114 json.addProperty("url", this.urlBuilder.pathed(related));
121 public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
123 protected final Loader loader;
124 protected final DBSerializer serializer;
125 protected final UrlBuilder urlBuilder;
126 protected boolean includeUrl = false;
127 protected boolean nodesOnly = false;
128 protected int depth = 1;
129 protected boolean modelDriven = false;
130 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
131 this.loader = loader;
132 this.serializer = serializer;
133 this.urlBuilder = urlBuilder;
136 protected Loader getLoader() {
140 protected DBSerializer getSerializer() {
141 return this.serializer;
144 protected UrlBuilder getUrlBuilder() {
145 return this.urlBuilder;
148 public Builder includeUrl() {
149 this.includeUrl = true;
153 public Builder nodesOnly(Boolean nodesOnly) {
154 this.nodesOnly = nodesOnly;
157 public boolean isNodesOnly() {
158 return this.nodesOnly;
161 public Builder depth(Integer depth) {
166 public int getDepth() {
170 public boolean isIncludeUrl() {
171 return this.includeUrl;
174 public Builder modelDriven() {
175 this.modelDriven = true;
179 public boolean getModelDriven() {
180 return this.modelDriven;
182 public RawFormat build() {
184 return new SimpleFormat(this);
186 return new RawFormat(this);