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 extends MultiFormatMapper {
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 int parallelThreshold() {
66 public JsonObject createPropertiesObject(Vertex v) throws AAIFormatVertexException {
67 JsonObject json = new JsonObject();
68 Iterator<VertexProperty<Object>> iter = v.properties();
70 while (iter.hasNext()) {
71 VertexProperty<Object> prop = iter.next();
72 if (prop.value() instanceof String) {
73 json.addProperty(prop.key(), (String)prop.value());
74 } else if (prop.value() instanceof Boolean) {
75 json.addProperty(prop.key(), (Boolean)prop.value());
76 } else if (prop.value() instanceof Number) {
77 json.addProperty(prop.key(), (Number)prop.value());
78 } else if (prop.value() instanceof List) {
79 Gson gson = new Gson();
80 String list = gson.toJson(prop.value());
82 json.addProperty(prop.key(), list);
92 protected JsonArray createRelationshipObject(Vertex v) throws AAIFormatVertexException {
93 JsonArray jarray = new JsonArray();
94 Iterator<Edge> inIter = v.edges(Direction.IN);
95 Iterator<Edge> outIter = v.edges(Direction.OUT);
97 while (inIter.hasNext()) {
98 Edge e = inIter.next();
99 jarray.add(this.getRelatedObject(e.label(), e.outVertex()));
102 while (outIter.hasNext()) {
103 Edge e = outIter.next();
104 jarray.add(this.getRelatedObject(e.label(), e.inVertex()));
110 private JsonObject getRelatedObject(String label, Vertex related) throws AAIFormatVertexException {
111 JsonObject json = new JsonObject();
112 json.addProperty("id", related.id().toString());
113 json.addProperty("relationship-label", label);
114 json.addProperty("node-type", related.<String>value(AAIProperties.NODE_TYPE));
115 json.addProperty("url", this.urlBuilder.pathed(related));
120 public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
122 protected final Loader loader;
123 protected final DBSerializer serializer;
124 protected final UrlBuilder urlBuilder;
125 protected boolean includeUrl = false;
126 protected boolean nodesOnly = false;
127 protected int depth = 1;
128 protected boolean modelDriven = false;
129 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
130 this.loader = loader;
131 this.serializer = serializer;
132 this.urlBuilder = urlBuilder;
135 protected Loader getLoader() {
139 protected DBSerializer getSerializer() {
140 return this.serializer;
143 protected UrlBuilder getUrlBuilder() {
144 return this.urlBuilder;
147 public Builder includeUrl() {
148 this.includeUrl = true;
152 public Builder nodesOnly(Boolean nodesOnly) {
153 this.nodesOnly = nodesOnly;
156 public boolean isNodesOnly() {
157 return this.nodesOnly;
160 public Builder depth(Integer depth) {
165 public int getDepth() {
169 public boolean isIncludeUrl() {
170 return this.includeUrl;
173 public Builder modelDriven() {
174 this.modelDriven = true;
178 public boolean getModelDriven() {
179 return this.modelDriven;
181 public RawFormat build() {
183 return new SimpleFormat(this);
185 return new RawFormat(this);
191 protected JsonObject getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
193 JsonObject json = new JsonObject();
194 json.addProperty("id", v.id().toString());
195 json.addProperty("node-type", v.<String>value(AAIProperties.NODE_TYPE));
196 json.addProperty("url", this.urlBuilder.pathed(v));
197 json.add("properties", this.createPropertiesObject(v));
199 json.add("related-to", this.createRelationshipObject(v));