2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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=========================================================
21 package org.openecomp.aai.serialization.queryformats;
23 import java.util.Iterator;
24 import java.util.List;
26 import org.apache.tinkerpop.gremlin.structure.Direction;
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
29 import org.openecomp.aai.db.props.AAIProperties;
30 import org.openecomp.aai.introspection.Loader;
31 import org.openecomp.aai.serialization.db.DBSerializer;
32 import org.openecomp.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
33 import org.openecomp.aai.serialization.queryformats.params.Depth;
34 import org.openecomp.aai.serialization.queryformats.params.NodesOnly;
35 import org.openecomp.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 implements FormatMapper {
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 JsonObject formatObject(Object input) throws AAIFormatVertexException {
60 Vertex v = (Vertex)input;
61 JsonObject json = new JsonObject();
62 json.addProperty("id", v.id().toString());
63 json.addProperty("node-type", v.<String>value(AAIProperties.NODE_TYPE));
64 json.addProperty("url", this.urlBuilder.pathed(v));
65 json.add("properties", this.createPropertiesObject(v));
67 json.add("related-to", this.createRelationshipObject(v));
73 public int parallelThreshold() {
78 public JsonObject createPropertiesObject(Vertex v) throws AAIFormatVertexException {
79 JsonObject json = new JsonObject();
80 Iterator<VertexProperty<Object>> iter = v.properties();
82 while (iter.hasNext()) {
83 VertexProperty<Object> prop = iter.next();
84 if (prop.value() instanceof String) {
85 json.addProperty(prop.key(), (String)prop.value());
86 } else if (prop.value() instanceof Boolean) {
87 json.addProperty(prop.key(), (Boolean)prop.value());
88 } else if (prop.value() instanceof Number) {
89 json.addProperty(prop.key(), (Number)prop.value());
90 } else if (prop.value() instanceof List) {
91 Gson gson = new Gson();
92 String list = gson.toJson(prop.value());
94 json.addProperty(prop.key(), list);
103 protected JsonArray createRelationshipObject(Vertex v) throws AAIFormatVertexException {
104 JsonArray jarray = new JsonArray();
105 Iterator<Vertex> iter = v.vertices(Direction.BOTH);
107 while (iter.hasNext()) {
108 Vertex related = iter.next();
110 JsonObject json = new JsonObject();
111 json.addProperty("id", related.id().toString());
112 json.addProperty("node-type", related.<String>value(AAIProperties.NODE_TYPE));
113 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);