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 com.google.gson.JsonObject;
23 import com.google.gson.JsonParser;
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.onap.aai.db.props.AAIProperties;
26 import org.onap.aai.exceptions.AAIException;
27 import org.onap.aai.introspection.Introspector;
28 import org.onap.aai.introspection.Loader;
29 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
30 import org.onap.aai.serialization.db.DBSerializer;
31 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
32 import org.onap.aai.serialization.queryformats.params.Depth;
33 import org.onap.aai.serialization.queryformats.params.NodesOnly;
34 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
36 import java.io.UnsupportedEncodingException;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Optional;
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 Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
63 JsonObject json = new JsonObject();
65 if (this.includeUrl) {
66 json.addProperty("url", this.urlBuilder.pathed(v));
68 Optional<JsonObject> jsonObject = this.vertexToJsonObject(v);
69 if (jsonObject.isPresent()) {
70 json.add(v.<String>property(AAIProperties.NODE_TYPE).orElse(null), jsonObject.get());
72 return Optional.empty();
74 return Optional.of(json);
77 protected Optional<JsonObject> vertexToJsonObject(Vertex v) throws AAIFormatVertexException {
79 final Introspector obj = getLoader().introspectorFromName(
80 v.<String>property(AAIProperties.NODE_TYPE)
84 final List<Vertex> wrapper = new ArrayList<>();
89 getSerializer().dbToObject(wrapper, obj, this.depth, this.nodesOnly, "false");
90 } catch (AAIException | UnsupportedEncodingException e) {
91 throw new AAIFormatVertexException("Failed to format vertex - error while serializing: " + e.getMessage(), e);
94 final String json = obj.marshal(false);
96 return Optional.of(getParser().parse(json).getAsJsonObject());
97 } catch (AAIUnknownObjectException e) {
98 return Optional.empty();
103 public int parallelThreshold() {
107 private Loader getLoader() { return loader; }
108 private DBSerializer getSerializer() { return serializer; }
109 private JsonParser getParser() { return parser; }
111 public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
113 private final Loader loader;
114 private final DBSerializer serializer;
115 private final UrlBuilder urlBuilder;
116 private boolean includeUrl = false;
117 private boolean nodesOnly = false;
118 private int depth = 1;
119 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
120 this.loader = loader;
121 this.serializer = serializer;
122 this.urlBuilder = urlBuilder;
125 protected Loader getLoader() {
129 protected DBSerializer getSerializer() {
130 return this.serializer;
133 protected UrlBuilder getUrlBuilder() {
134 return this.urlBuilder;
137 public Builder includeUrl() {
138 this.includeUrl = true;
142 public Builder nodesOnly(Boolean nodesOnly) {
143 this.nodesOnly = nodesOnly;
146 public boolean isNodesOnly() {
147 return this.nodesOnly;
149 public Builder depth(Integer depth) {
153 public int getDepth() {
156 public boolean isIncludeUrl() {
157 return this.includeUrl;
160 public Resource build() {
161 return new Resource(this);