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.io.UnsupportedEncodingException;
25 import java.util.ArrayList;
26 import java.util.List;
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
29 import org.onap.aai.db.props.AAIProperties;
30 import org.onap.aai.exceptions.AAIException;
31 import org.onap.aai.introspection.Introspector;
32 import org.onap.aai.introspection.Loader;
33 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
34 import org.onap.aai.serialization.db.DBSerializer;
35 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
36 import org.onap.aai.serialization.queryformats.params.Depth;
37 import org.onap.aai.serialization.queryformats.params.NodesOnly;
38 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
40 import com.google.gson.JsonObject;
41 import com.google.gson.JsonParser;
43 public class Resource implements FormatMapper {
45 private final Loader loader;
46 private final DBSerializer serializer;
47 private final JsonParser parser;
48 private final UrlBuilder urlBuilder;
49 private final boolean includeUrl;
50 private final boolean nodesOnly;
51 private final int depth;
52 private Resource (Builder builder) {
53 this.parser = new JsonParser();
54 this.loader = builder.getLoader();
55 this.serializer = builder.getSerializer();
56 this.urlBuilder = builder.getUrlBuilder();
57 this.includeUrl = builder.isIncludeUrl();
58 this.nodesOnly = builder.isNodesOnly();
59 this.depth = builder.getDepth();
63 public JsonObject formatObject(Object input) throws AAIFormatVertexException {
64 Vertex v = (Vertex)input;
65 JsonObject json = new JsonObject();
67 if (this.includeUrl) {
68 json.addProperty("url", this.urlBuilder.pathed(v));
70 json.add(v.<String>property(AAIProperties.NODE_TYPE)
71 .orElse(null), this.vertexToJsonObject(v));
76 protected JsonObject vertexToJsonObject(Vertex v) throws AAIFormatVertexException {
78 final Introspector obj = getLoader().introspectorFromName(
79 v.<String>property(AAIProperties.NODE_TYPE)
83 final List<Vertex> wrapper = new ArrayList<>();
88 getSerializer().dbToObject(wrapper, obj, this.depth, this.nodesOnly, "false");
89 } catch (AAIException | UnsupportedEncodingException e) {
90 throw new AAIFormatVertexException("Failed to format vertex - error while serializing: " + e.getMessage(), e);
93 final String json = obj.marshal(false);
95 return getParser().parse(json).getAsJsonObject();
96 } catch (AAIUnknownObjectException e) {
97 throw new AAIFormatVertexException("Failed to format vertex - unknown object", e);
102 public int parallelThreshold() {
106 private Loader getLoader() { return loader; }
107 private DBSerializer getSerializer() { return serializer; }
108 private JsonParser getParser() { return parser; }
110 public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
112 private final Loader loader;
113 private final DBSerializer serializer;
114 private final UrlBuilder urlBuilder;
115 private boolean includeUrl = false;
116 private boolean nodesOnly = false;
117 private int depth = 1;
118 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
119 this.loader = loader;
120 this.serializer = serializer;
121 this.urlBuilder = urlBuilder;
124 protected Loader getLoader() {
128 protected DBSerializer getSerializer() {
129 return this.serializer;
132 protected UrlBuilder getUrlBuilder() {
133 return this.urlBuilder;
136 public Builder includeUrl() {
137 this.includeUrl = true;
141 public Builder nodesOnly(Boolean nodesOnly) {
142 this.nodesOnly = nodesOnly;
145 public boolean isNodesOnly() {
146 return this.nodesOnly;
148 public Builder depth(Integer depth) {
152 public int getDepth() {
155 public boolean isIncludeUrl() {
156 return this.includeUrl;
159 public Resource build() {
160 return new Resource(this);