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 final class PathedURL extends MultiFormatMapper {
47 private final UrlBuilder urlBuilder;
48 private final JsonParser parser;
49 private final Loader loader;
50 private boolean includeUrl = false;
52 public PathedURL(Loader loader, UrlBuilder urlBuilder) throws AAIException {
53 this.urlBuilder = urlBuilder;
54 this.parser = new JsonParser();
58 public PathedURL(Builder builder) {
59 this.urlBuilder = builder.getUrlBuilder();
60 this.parser = new JsonParser();
61 this.loader = builder.getLoader();
62 this.isTree = builder.isTree();
63 this.includeUrl = builder.isIncludeUrl();
67 public int parallelThreshold() {
72 protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
75 final Introspector searchResult = this.loader.introspectorFromName("result-data");
77 searchResult.setValue("resource-type", v.value(AAIProperties.NODE_TYPE));
79 searchResult.setValue("resource-link", this.urlBuilder.pathed(v));
82 searchResult.setValue("resource-version", v.value(AAIProperties.RESOURCE_VERSION));
84 final String json = searchResult.marshal(false);
85 return Optional.of(this.parser.parse(json).getAsJsonObject());
87 } catch (AAIUnknownObjectException e) {
88 throw new RuntimeException("Fatal error - result-data does not exist!", e);
94 protected Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties) throws AAIFormatVertexException {
95 return Optional.empty();
98 public static class Builder implements NodesOnly<Builder>, Depth<Builder>, AsTree<Builder> {
100 private final Loader loader;
101 private final DBSerializer serializer;
102 private final UrlBuilder urlBuilder;
103 private boolean includeUrl = false;
104 private boolean nodesOnly = false;
105 private int depth = 1;
106 private MultivaluedMap<String, String> params;
107 private boolean tree = false;
109 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
110 this.loader = loader;
111 this.serializer = serializer;
112 this.urlBuilder = urlBuilder;
115 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder, MultivaluedMap<String, String> params) {
116 this.loader = loader;
117 this.serializer = serializer;
118 this.urlBuilder = urlBuilder;
119 this.params = params;
122 protected Loader getLoader() {
126 protected DBSerializer getSerializer() {
127 return this.serializer;
130 protected UrlBuilder getUrlBuilder() {
131 return this.urlBuilder;
134 protected MultivaluedMap<String, String> getParams() { return this.params; }
136 public boolean isSkipRelatedTo() {
137 if (params != null) {
138 boolean isSkipRelatedTo = true;
139 if (params.containsKey("skip-related-to")) {
140 String skipRelatedTo = params.getFirst("skip-related-to");
141 isSkipRelatedTo = !(skipRelatedTo != null && skipRelatedTo.equals("false"));
143 // if skip-related-to param is missing, then default it to false;
144 isSkipRelatedTo = false;
146 return isSkipRelatedTo;
151 protected boolean isTree() { return this.tree; }
153 public Builder isTree(Boolean tree) {
158 public Builder includeUrl() {
159 this.includeUrl = true;
163 public Builder nodesOnly(Boolean nodesOnly) {
164 this.nodesOnly = nodesOnly;
168 public boolean isNodesOnly() {
169 return this.nodesOnly;
172 public Builder depth(Integer depth) {
177 public int getDepth() {
181 public boolean isIncludeUrl() {
182 return this.includeUrl;
185 public PathedURL build() throws AAIException {
186 return new PathedURL(this);