Update Janusgraph to 0.4.0 in aai-common
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / PathedURL.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.serialization.queryformats;
22
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonParser;
25
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Optional;
29
30 import javax.ws.rs.core.MultivaluedMap;
31
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;
44
45 public final class PathedURL extends MultiFormatMapper {
46
47     private final UrlBuilder urlBuilder;
48     private final Loader loader;
49     private boolean includeUrl = false;
50
51     public PathedURL(Loader loader, UrlBuilder urlBuilder) throws AAIException {
52         this.urlBuilder = urlBuilder;
53         this.loader = loader;
54     }
55
56     public PathedURL(Builder builder) {
57         this.urlBuilder = builder.getUrlBuilder();
58         this.loader = builder.getLoader();
59         this.isTree = builder.isTree();
60         this.includeUrl = builder.isIncludeUrl();
61     }
62
63     @Override
64     public int parallelThreshold() {
65         return 20;
66     }
67
68     @Override
69     protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
70
71         try {
72             final Introspector searchResult = this.loader.introspectorFromName("result-data");
73
74             searchResult.setValue("resource-type", v.value(AAIProperties.NODE_TYPE));
75
76             searchResult.setValue("resource-link", this.urlBuilder.pathed(v));
77
78             if (includeUrl)
79                 searchResult.setValue("resource-version", v.value(AAIProperties.RESOURCE_VERSION));
80
81             final String json = searchResult.marshal(false);
82             return Optional.of(JsonParser.parseString(json).getAsJsonObject());
83
84         } catch (AAIUnknownObjectException e) {
85             throw new RuntimeException("Fatal error - result-data does not exist!", e);
86         }
87
88     }
89
90     @Override
91     protected Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties)
92             throws AAIFormatVertexException {
93         return Optional.empty();
94     }
95
96     public static class Builder implements NodesOnly<Builder>, Depth<Builder>, AsTree<Builder> {
97
98         private final Loader loader;
99         private final DBSerializer serializer;
100         private final UrlBuilder urlBuilder;
101         private boolean includeUrl = false;
102         private boolean nodesOnly = false;
103         private int depth = 1;
104         private MultivaluedMap<String, String> params;
105         private boolean tree = false;
106
107         public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
108             this.loader = loader;
109             this.serializer = serializer;
110             this.urlBuilder = urlBuilder;
111         }
112
113         public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder,
114                 MultivaluedMap<String, String> params) {
115             this.loader = loader;
116             this.serializer = serializer;
117             this.urlBuilder = urlBuilder;
118             this.params = params;
119         }
120
121         protected Loader getLoader() {
122             return this.loader;
123         }
124
125         protected DBSerializer getSerializer() {
126             return this.serializer;
127         }
128
129         protected UrlBuilder getUrlBuilder() {
130             return this.urlBuilder;
131         }
132
133         protected MultivaluedMap<String, String> getParams() {
134             return this.params;
135         }
136
137         public boolean isSkipRelatedTo() {
138             if (params != null) {
139                 boolean isSkipRelatedTo = true;
140                 if (params.containsKey("skip-related-to")) {
141                     String skipRelatedTo = params.getFirst("skip-related-to");
142                     isSkipRelatedTo = !(skipRelatedTo != null && skipRelatedTo.equals("false"));
143                 } else {
144                     // if skip-related-to param is missing, then default it to false;
145                     isSkipRelatedTo = false;
146                 }
147                 return isSkipRelatedTo;
148             }
149             return true;
150         }
151
152         protected boolean isTree() {
153             return this.tree;
154         }
155
156         public Builder isTree(Boolean tree) {
157             this.tree = tree;
158             return this;
159         }
160
161         public Builder includeUrl() {
162             this.includeUrl = true;
163             return this;
164         }
165
166         public Builder nodesOnly(Boolean nodesOnly) {
167             this.nodesOnly = nodesOnly;
168             return this;
169         }
170
171         public boolean isNodesOnly() {
172             return this.nodesOnly;
173         }
174
175         public Builder depth(Integer depth) {
176             this.depth = depth;
177             return this;
178         }
179
180         public int getDepth() {
181             return this.depth;
182         }
183
184         public boolean isIncludeUrl() {
185             return this.includeUrl;
186         }
187
188         public PathedURL build() throws AAIException {
189             return new PathedURL(this);
190         }
191     }
192
193 }