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.util.List;
28 import java.util.Optional;
30 import org.apache.tinkerpop.gremlin.structure.Vertex;
31 import org.onap.aai.db.props.AAIProperties;
32 import org.onap.aai.exceptions.AAIException;
33 import org.onap.aai.introspection.Introspector;
34 import org.onap.aai.introspection.Loader;
35 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
36 import org.onap.aai.serialization.db.DBSerializer;
37 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
38 import org.onap.aai.serialization.queryformats.params.AsTree;
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 import javax.ws.rs.core.MultivaluedMap;
45 public class IdURL extends MultiFormatMapper {
47 private final UrlBuilder urlBuilder;
48 private final JsonParser parser;
49 private final Loader loader;
51 public IdURL(Loader loader, UrlBuilder urlBuilder) throws AAIException {
52 this.urlBuilder = urlBuilder;
53 this.parser = new JsonParser();
57 public IdURL(Builder builder) throws AAIException {
58 this.urlBuilder = builder.getUrlBuilder();
59 this.parser = new JsonParser();
60 this.loader = builder.getLoader();
61 this.isTree = builder.isTree();
65 public int parallelThreshold() {
70 protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
73 final Introspector searchResult = this.loader.introspectorFromName("result-data");
75 searchResult.setValue("resource-type", v.value(AAIProperties.NODE_TYPE));
76 searchResult.setValue("resource-link", this.urlBuilder.id(v));
78 final String json = searchResult.marshal(false);
80 return Optional.of(parser.parse(json).getAsJsonObject());
82 } catch (AAIUnknownObjectException e) {
83 throw new RuntimeException("Fatal error - result-data object does not exist!");
89 protected Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties) throws AAIFormatVertexException {
90 return Optional.empty();
93 public static class Builder implements NodesOnly<Builder>, Depth<Builder>, AsTree<Builder> {
95 private final Loader loader;
96 private final DBSerializer serializer;
97 private final UrlBuilder urlBuilder;
98 private boolean includeUrl = false;
99 private boolean nodesOnly = false;
100 private int depth = 1;
101 private MultivaluedMap<String, String> params;
102 private boolean tree = false;
104 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
105 this.loader = loader;
106 this.serializer = serializer;
107 this.urlBuilder = urlBuilder;
110 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder, MultivaluedMap<String, String> params) {
111 this.loader = loader;
112 this.serializer = serializer;
113 this.urlBuilder = urlBuilder;
114 this.params = params;
117 protected Loader getLoader() {
121 protected DBSerializer getSerializer() {
122 return this.serializer;
125 protected UrlBuilder getUrlBuilder() {
126 return this.urlBuilder;
129 protected MultivaluedMap<String, String> getParams() { return this.params; }
131 public boolean isSkipRelatedTo() {
132 if (params != null) {
133 boolean isSkipRelatedTo = true;
134 if (params.containsKey("skip-related-to")) {
135 String skipRelatedTo = params.getFirst("skip-related-to");
136 isSkipRelatedTo = !(skipRelatedTo != null && skipRelatedTo.equals("false"));
138 // if skip-related-to param is missing, then default it to false;
139 isSkipRelatedTo = false;
141 return isSkipRelatedTo;
146 protected boolean isTree() { return this.tree; }
148 public Builder isTree(Boolean tree) {
153 public Builder includeUrl() {
154 this.includeUrl = true;
158 public Builder nodesOnly(Boolean nodesOnly) {
159 this.nodesOnly = nodesOnly;
163 public boolean isNodesOnly() {
164 return this.nodesOnly;
167 public Builder depth(Integer depth) {
172 public int getDepth() {
176 public boolean isIncludeUrl() {
177 return this.includeUrl;
180 public IdURL build() throws AAIException {
181 return new IdURL(this);