2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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 package org.onap.aai.serialization.queryformats;
22 import java.io.UnsupportedEncodingException;
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.onap.aai.db.props.AAIProperties;
28 import org.onap.aai.exceptions.AAIException;
29 import org.onap.aai.introspection.Introspector;
30 import org.onap.aai.introspection.Loader;
31 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
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.JsonObject;
39 import com.google.gson.JsonParser;
41 public class Resource extends MultiFormatMapper {
43 private final Loader loader;
44 private final DBSerializer serializer;
45 private final JsonParser parser;
46 private final UrlBuilder urlBuilder;
47 private final boolean includeUrl;
48 private final boolean nodesOnly;
49 private final int depth;
50 private Resource (Builder builder) {
51 this.parser = new JsonParser();
52 this.loader = builder.getLoader();
53 this.serializer = builder.getSerializer();
54 this.urlBuilder = builder.getUrlBuilder();
55 this.includeUrl = builder.isIncludeUrl();
56 this.nodesOnly = builder.isNodesOnly();
57 this.depth = builder.getDepth();
61 protected JsonObject getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
63 JsonObject json = new JsonObject();
65 if (this.includeUrl) {
66 json.addProperty("url", this.urlBuilder.pathed(v));
68 json.add(v.<String>property(AAIProperties.NODE_TYPE)
69 .orElse(null), this.vertexToJsonObject(v));
74 protected JsonObject vertexToJsonObject(Vertex v) throws AAIFormatVertexException {
76 final Introspector obj = getLoader().introspectorFromName(
77 v.<String>property(AAIProperties.NODE_TYPE)
81 final List<Vertex> wrapper = new ArrayList<>();
86 getSerializer().dbToObject(wrapper, obj, this.depth, this.nodesOnly, "false");
87 } catch (AAIException | UnsupportedEncodingException e) {
88 throw new AAIFormatVertexException("Failed to format vertex - error while serializing: " + e.getMessage(), e);
91 final String json = obj.marshal(false);
93 return getParser().parse(json).getAsJsonObject();
94 } catch (AAIUnknownObjectException e) {
95 throw new AAIFormatVertexException("Failed to format vertex - unknown object", e);
100 public int parallelThreshold() {
104 private Loader getLoader() { return loader; }
105 private DBSerializer getSerializer() { return serializer; }
106 private JsonParser getParser() { return parser; }
108 public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
110 private final Loader loader;
111 private final DBSerializer serializer;
112 private final UrlBuilder urlBuilder;
113 private boolean includeUrl = false;
114 private boolean nodesOnly = false;
115 private int depth = 1;
116 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
117 this.loader = loader;
118 this.serializer = serializer;
119 this.urlBuilder = urlBuilder;
122 protected Loader getLoader() {
126 protected DBSerializer getSerializer() {
127 return this.serializer;
130 protected UrlBuilder getUrlBuilder() {
131 return this.urlBuilder;
134 public Builder includeUrl() {
135 this.includeUrl = true;
139 public Builder nodesOnly(Boolean nodesOnly) {
140 this.nodesOnly = nodesOnly;
143 public boolean isNodesOnly() {
144 return this.nodesOnly;
146 public Builder depth(Integer depth) {
150 public int getDepth() {
153 public boolean isIncludeUrl() {
154 return this.includeUrl;
157 public Resource build() {
158 return new Resource(this);