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 com.google.gson.Gson;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
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.openecomp.aai.db.props.AAIProperties;
31 import org.openecomp.aai.introspection.Loader;
32 import org.openecomp.aai.serialization.db.DBSerializer;
33 import org.openecomp.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
34 import org.openecomp.aai.serialization.queryformats.params.Depth;
35 import org.openecomp.aai.serialization.queryformats.params.NodesOnly;
36 import org.openecomp.aai.serialization.queryformats.utils.UrlBuilder;
38 import java.util.Iterator;
39 import java.util.List;
42 public class RawFormat implements FormatMapper {
43 protected JsonParser parser = new JsonParser();
44 protected final DBSerializer serializer;
45 protected final Loader loader;
46 protected final UrlBuilder urlBuilder;
47 protected final int depth;
48 protected final boolean nodesOnly;
49 protected RawFormat(Builder builder) {
50 this.urlBuilder = builder.getUrlBuilder();
51 this.loader = builder.getLoader();
52 this.serializer = builder.getSerializer();
53 this.depth = builder.getDepth();
54 this.nodesOnly = builder.isNodesOnly();
58 public JsonObject formatObject(Object input) throws AAIFormatVertexException {
59 Vertex v = (Vertex)input;
60 JsonObject json = new JsonObject();
61 json.addProperty("id", v.id().toString());
62 json.addProperty("node-type", v.<String>value(AAIProperties.NODE_TYPE));
63 json.addProperty("url", this.urlBuilder.pathed(v));
64 json.add("properties", this.createPropertiesObject(v));
66 json.add("related-to", this.createRelationshipObject(v));
72 public int parallelThreshold() {
77 public JsonObject createPropertiesObject(Vertex v) throws AAIFormatVertexException {
78 JsonObject json = new JsonObject();
79 Iterator<VertexProperty<Object>> iter = v.properties();
81 while (iter.hasNext()) {
82 VertexProperty<Object> prop = iter.next();
83 if (prop.value() instanceof String) {
84 json.addProperty(prop.key(), (String)prop.value());
85 } else if (prop.value() instanceof Boolean) {
86 json.addProperty(prop.key(), (Boolean)prop.value());
87 } else if (prop.value() instanceof Number) {
88 json.addProperty(prop.key(), (Number)prop.value());
89 } else if (prop.value() instanceof List) {
90 Gson gson = new Gson();
91 String list = gson.toJson(prop.value());
93 json.addProperty(prop.key(), list);
102 protected JsonArray createRelationshipObject(Vertex v) throws AAIFormatVertexException {
103 JsonArray jarray = new JsonArray();
104 Iterator<Vertex> iter = v.vertices(Direction.BOTH);
106 while (iter.hasNext()) {
107 Vertex related = iter.next();
109 JsonObject json = new JsonObject();
110 json.addProperty("id", related.id().toString());
111 json.addProperty("node-type", related.<String>value(AAIProperties.NODE_TYPE));
112 json.addProperty("url", this.urlBuilder.pathed(related));
119 public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
121 protected final Loader loader;
122 protected final DBSerializer serializer;
123 protected final UrlBuilder urlBuilder;
124 protected boolean includeUrl = false;
125 protected boolean nodesOnly = false;
126 protected int depth = 1;
127 protected boolean modelDriven = false;
128 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
129 this.loader = loader;
130 this.serializer = serializer;
131 this.urlBuilder = urlBuilder;
134 protected Loader getLoader() {
138 protected DBSerializer getSerializer() {
139 return this.serializer;
142 protected UrlBuilder getUrlBuilder() {
143 return this.urlBuilder;
146 public Builder includeUrl() {
147 this.includeUrl = true;
151 public Builder nodesOnly(Boolean nodesOnly) {
152 this.nodesOnly = nodesOnly;
155 public boolean isNodesOnly() {
156 return this.nodesOnly;
159 public Builder depth(Integer depth) {
164 public int getDepth() {
168 public boolean isIncludeUrl() {
169 return this.includeUrl;
172 public Builder modelDriven() {
173 this.modelDriven = true;
177 public boolean getModelDriven() {
178 return this.modelDriven;
180 public RawFormat build() {
182 return new SimpleFormat(this);
184 return new RawFormat(this);