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;
29 import org.openecomp.aai.db.props.AAIProperties;
30 import org.openecomp.aai.exceptions.AAIException;
31 import org.openecomp.aai.introspection.Introspector;
32 import org.openecomp.aai.introspection.Loader;
33 import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
34 import org.openecomp.aai.serialization.db.DBSerializer;
35 import org.openecomp.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
36 import org.openecomp.aai.serialization.queryformats.utils.UrlBuilder;
37 import com.google.gson.JsonObject;
38 import com.google.gson.JsonParser;
40 public class Resource implements FormatMapper {
42 private final Loader loader;
43 private final DBSerializer serializer;
44 private final JsonParser parser;
45 private final UrlBuilder urlBuilder;
46 private final boolean includeUrl;
48 private Resource (Builder builder) {
49 this.parser = new JsonParser();
50 this.loader = builder.getLoader();
51 this.serializer = builder.getSerializer();
52 this.urlBuilder = builder.getUrlBuilder();
53 this.includeUrl = builder.isIncludeUrl();
57 public JsonObject formatObject(Object input) throws AAIFormatVertexException {
58 Vertex v = (Vertex)input;
59 JsonObject json = new JsonObject();
61 if (this.includeUrl) {
62 json.addProperty("url", this.urlBuilder.pathed(v));
64 json.add(v.<String>property(AAIProperties.NODE_TYPE)
65 .orElse(null), this.vertexToJsonObject(v));
70 protected JsonObject vertexToJsonObject(Vertex v) throws AAIFormatVertexException {
72 final Introspector obj = getLoader().introspectorFromName(
73 v.<String>property(AAIProperties.NODE_TYPE)
77 final List<Vertex> wrapper = new ArrayList<>();
82 getSerializer().dbToObject(wrapper, obj, 1, false, "false");
83 } catch (AAIException | UnsupportedEncodingException e) {
84 throw new AAIFormatVertexException("Failed to format vertex - error while serializing: " + e.getMessage(), e);
87 final String json = obj.marshal(false);
89 return getParser().parse(json).getAsJsonObject();
90 } catch (AAIUnknownObjectException e) {
91 throw new AAIFormatVertexException("Failed to format vertex - unknown object", e);
96 public int parallelThreshold() {
100 private Loader getLoader() { return loader; }
101 private DBSerializer getSerializer() { return serializer; }
102 private JsonParser getParser() { return parser; }
104 public static class Builder {
106 private final Loader loader;
107 private final DBSerializer serializer;
108 private final UrlBuilder urlBuilder;
109 private boolean includeUrl = false;
111 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
112 this.loader = loader;
113 this.serializer = serializer;
114 this.urlBuilder = urlBuilder;
117 protected Loader getLoader() {
121 protected DBSerializer getSerializer() {
122 return this.serializer;
125 protected UrlBuilder getUrlBuilder() {
126 return this.urlBuilder;
129 public Builder includeUrl() {
130 this.includeUrl = true;
134 protected boolean isIncludeUrl() {
135 return this.includeUrl;
138 public Resource build() {
139 return new Resource(this);