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 java.io.UnsupportedEncodingException;
24 import java.util.ArrayList;
25 import java.util.List;
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28 import org.openecomp.aai.db.props.AAIProperties;
29 import org.openecomp.aai.exceptions.AAIException;
30 import org.openecomp.aai.introspection.Introspector;
31 import org.openecomp.aai.introspection.Loader;
32 import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
33 import org.openecomp.aai.serialization.db.DBSerializer;
34 import org.openecomp.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
35 import org.openecomp.aai.serialization.queryformats.params.Depth;
36 import org.openecomp.aai.serialization.queryformats.params.NodesOnly;
37 import org.openecomp.aai.serialization.queryformats.utils.UrlBuilder;
39 import com.google.gson.JsonObject;
40 import com.google.gson.JsonParser;
42 public class Resource implements FormatMapper {
44 private final Loader loader;
45 private final DBSerializer serializer;
46 private final JsonParser parser;
47 private final UrlBuilder urlBuilder;
48 private final boolean includeUrl;
49 private final boolean nodesOnly;
50 private final int depth;
51 private Resource (Builder builder) {
52 this.parser = new JsonParser();
53 this.loader = builder.getLoader();
54 this.serializer = builder.getSerializer();
55 this.urlBuilder = builder.getUrlBuilder();
56 this.includeUrl = builder.isIncludeUrl();
57 this.nodesOnly = builder.isNodesOnly();
58 this.depth = builder.getDepth();
62 public JsonObject formatObject(Object input) throws AAIFormatVertexException {
63 Vertex v = (Vertex)input;
64 JsonObject json = new JsonObject();
66 if (this.includeUrl) {
67 json.addProperty("url", this.urlBuilder.pathed(v));
69 json.add(v.<String>property(AAIProperties.NODE_TYPE)
70 .orElse(null), this.vertexToJsonObject(v));
75 protected JsonObject vertexToJsonObject(Vertex v) throws AAIFormatVertexException {
77 final Introspector obj = getLoader().introspectorFromName(
78 v.<String>property(AAIProperties.NODE_TYPE)
82 final List<Vertex> wrapper = new ArrayList<>();
87 getSerializer().dbToObject(wrapper, obj, this.depth, this.nodesOnly, "false");
88 } catch (AAIException | UnsupportedEncodingException e) {
89 throw new AAIFormatVertexException("Failed to format vertex - error while serializing: " + e.getMessage(), e);
92 final String json = obj.marshal(false);
94 return getParser().parse(json).getAsJsonObject();
95 } catch (AAIUnknownObjectException e) {
96 throw new AAIFormatVertexException("Failed to format vertex - unknown object", e);
101 public int parallelThreshold() {
105 private Loader getLoader() { return loader; }
106 private DBSerializer getSerializer() { return serializer; }
107 private JsonParser getParser() { return parser; }
109 public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
111 private final Loader loader;
112 private final DBSerializer serializer;
113 private final UrlBuilder urlBuilder;
114 private boolean includeUrl = false;
115 private boolean nodesOnly = false;
116 private int depth = 1;
117 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
118 this.loader = loader;
119 this.serializer = serializer;
120 this.urlBuilder = urlBuilder;
123 protected Loader getLoader() {
127 protected DBSerializer getSerializer() {
128 return this.serializer;
131 protected UrlBuilder getUrlBuilder() {
132 return this.urlBuilder;
135 public Builder includeUrl() {
136 this.includeUrl = true;
140 public Builder nodesOnly(Boolean nodesOnly) {
141 this.nodesOnly = nodesOnly;
144 public boolean isNodesOnly() {
145 return this.nodesOnly;
147 public Builder depth(Integer depth) {
151 public int getDepth() {
154 public boolean isIncludeUrl() {
155 return this.includeUrl;
158 public Resource build() {
159 return new Resource(this);