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=========================================================
21 package org.onap.aai.serialization.queryformats;
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonParser;
26 import java.io.UnsupportedEncodingException;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Optional;
31 import org.apache.tinkerpop.gremlin.structure.Vertex;
32 import org.onap.aai.db.props.AAIProperties;
33 import org.onap.aai.exceptions.AAIException;
34 import org.onap.aai.introspection.Introspector;
35 import org.onap.aai.introspection.Loader;
36 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
37 import org.onap.aai.serialization.db.DBSerializer;
38 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
39 import org.onap.aai.serialization.queryformats.params.Depth;
40 import org.onap.aai.serialization.queryformats.params.NodesOnly;
41 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
43 public class Resource extends MultiFormatMapper {
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;
53 private Resource(Builder builder) {
54 this.parser = new JsonParser();
55 this.loader = builder.getLoader();
56 this.serializer = builder.getSerializer();
57 this.urlBuilder = builder.getUrlBuilder();
58 this.includeUrl = builder.isIncludeUrl();
59 this.nodesOnly = builder.isNodesOnly();
60 this.depth = builder.getDepth();
64 protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
66 JsonObject json = new JsonObject();
68 if (this.includeUrl) {
69 json.addProperty("url", this.urlBuilder.pathed(v));
71 Optional<JsonObject> jsonObject = this.vertexToJsonObject(v);
72 if (jsonObject.isPresent()) {
73 json.add(v.<String>property(AAIProperties.NODE_TYPE).orElse(null), jsonObject.get());
75 return Optional.empty();
77 return Optional.of(json);
80 protected Optional<JsonObject> vertexToJsonObject(Vertex v) throws AAIFormatVertexException {
82 final Introspector obj =
83 getLoader().introspectorFromName(v.<String>property(AAIProperties.NODE_TYPE).orElse(null));
85 final List<Vertex> wrapper = new ArrayList<>();
90 getSerializer().dbToObject(wrapper, obj, this.depth, this.nodesOnly, "false");
91 } catch (AAIException | UnsupportedEncodingException e) {
92 throw new AAIFormatVertexException(
93 "Failed to format vertex - error while serializing: " + e.getMessage(), e);
96 final String json = obj.marshal(false);
98 return Optional.of(getParser().parse(json).getAsJsonObject());
99 } catch (AAIUnknownObjectException e) {
100 return Optional.empty();
105 public int parallelThreshold() {
109 private Loader getLoader() {
113 private DBSerializer getSerializer() {
117 private JsonParser getParser() {
121 public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
123 private final Loader loader;
124 private final DBSerializer serializer;
125 private final UrlBuilder urlBuilder;
126 private boolean includeUrl = false;
127 private boolean nodesOnly = false;
128 private int depth = 1;
130 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
131 this.loader = loader;
132 this.serializer = serializer;
133 this.urlBuilder = urlBuilder;
136 protected Loader getLoader() {
140 protected DBSerializer getSerializer() {
141 return this.serializer;
144 protected UrlBuilder getUrlBuilder() {
145 return this.urlBuilder;
148 public Builder includeUrl() {
149 this.includeUrl = true;
153 public Builder nodesOnly(Boolean nodesOnly) {
154 this.nodesOnly = nodesOnly;
158 public boolean isNodesOnly() {
159 return this.nodesOnly;
162 public Builder depth(Integer depth) {
167 public int getDepth() {
171 public boolean isIncludeUrl() {
172 return this.includeUrl;
175 public Resource build() {
176 return new Resource(this);