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 javax.ws.rs.core.MultivaluedMap;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.onap.aai.db.props.AAIProperties;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.introspection.Introspector;
36 import org.onap.aai.introspection.Loader;
37 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
38 import org.onap.aai.serialization.db.DBSerializer;
39 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
40 import org.onap.aai.serialization.queryformats.params.AsTree;
41 import org.onap.aai.serialization.queryformats.params.Depth;
42 import org.onap.aai.serialization.queryformats.params.NodesOnly;
43 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
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)
90 throws AAIFormatVertexException {
91 return Optional.empty();
94 public static class Builder implements NodesOnly<Builder>, Depth<Builder>, AsTree<Builder> {
96 private final Loader loader;
97 private final DBSerializer serializer;
98 private final UrlBuilder urlBuilder;
99 private boolean includeUrl = false;
100 private boolean nodesOnly = false;
101 private int depth = 1;
102 private MultivaluedMap<String, String> params;
103 private boolean tree = false;
105 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
106 this.loader = loader;
107 this.serializer = serializer;
108 this.urlBuilder = urlBuilder;
111 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder,
112 MultivaluedMap<String, String> params) {
113 this.loader = loader;
114 this.serializer = serializer;
115 this.urlBuilder = urlBuilder;
116 this.params = params;
119 protected Loader getLoader() {
123 protected DBSerializer getSerializer() {
124 return this.serializer;
127 protected UrlBuilder getUrlBuilder() {
128 return this.urlBuilder;
131 protected MultivaluedMap<String, String> getParams() {
135 public boolean isSkipRelatedTo() {
136 if (params != null) {
137 boolean isSkipRelatedTo = true;
138 if (params.containsKey("skip-related-to")) {
139 String skipRelatedTo = params.getFirst("skip-related-to");
140 isSkipRelatedTo = !(skipRelatedTo != null && skipRelatedTo.equals("false"));
142 // if skip-related-to param is missing, then default it to false;
143 isSkipRelatedTo = false;
145 return isSkipRelatedTo;
150 protected boolean isTree() {
154 public Builder isTree(Boolean tree) {
159 public Builder includeUrl() {
160 this.includeUrl = true;
164 public Builder nodesOnly(Boolean nodesOnly) {
165 this.nodesOnly = nodesOnly;
169 public boolean isNodesOnly() {
170 return this.nodesOnly;
173 public Builder depth(Integer depth) {
178 public int getDepth() {
182 public boolean isIncludeUrl() {
183 return this.includeUrl;
186 public IdURL build() throws AAIException {
187 return new IdURL(this);