Merge "[AAI] Fix doc config files"
[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 JsonParser parser;
49     private final Loader loader;
50     private boolean includeUrl = false;
51
52     public PathedURL(Loader loader, UrlBuilder urlBuilder) throws AAIException {
53         this.urlBuilder = urlBuilder;
54         this.parser = new JsonParser();
55         this.loader = loader;
56     }
57
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();
64     }
65
66     @Override
67     public int parallelThreshold() {
68         return 20;
69     }
70
71     @Override
72     protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
73
74         try {
75             final Introspector searchResult = this.loader.introspectorFromName("result-data");
76
77             searchResult.setValue("resource-type", v.value(AAIProperties.NODE_TYPE));
78
79             searchResult.setValue("resource-link", this.urlBuilder.pathed(v));
80
81             if (includeUrl)
82                 searchResult.setValue("resource-version", v.value(AAIProperties.RESOURCE_VERSION));
83
84             final String json = searchResult.marshal(false);
85             return Optional.of(this.parser.parse(json).getAsJsonObject());
86
87         } catch (AAIUnknownObjectException e) {
88             throw new RuntimeException("Fatal error - result-data does not exist!", e);
89         }
90
91     }
92
93     @Override
94     protected Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties)
95             throws AAIFormatVertexException {
96         return Optional.empty();
97     }
98
99     public static class Builder implements NodesOnly<Builder>, Depth<Builder>, AsTree<Builder> {
100
101         private final Loader loader;
102         private final DBSerializer serializer;
103         private final UrlBuilder urlBuilder;
104         private boolean includeUrl = false;
105         private boolean nodesOnly = false;
106         private int depth = 1;
107         private MultivaluedMap<String, String> params;
108         private boolean tree = false;
109
110         public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
111             this.loader = loader;
112             this.serializer = serializer;
113             this.urlBuilder = urlBuilder;
114         }
115
116         public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder,
117                 MultivaluedMap<String, String> params) {
118             this.loader = loader;
119             this.serializer = serializer;
120             this.urlBuilder = urlBuilder;
121             this.params = params;
122         }
123
124         protected Loader getLoader() {
125             return this.loader;
126         }
127
128         protected DBSerializer getSerializer() {
129             return this.serializer;
130         }
131
132         protected UrlBuilder getUrlBuilder() {
133             return this.urlBuilder;
134         }
135
136         protected MultivaluedMap<String, String> getParams() {
137             return this.params;
138         }
139
140         public boolean isSkipRelatedTo() {
141             if (params != null) {
142                 boolean isSkipRelatedTo = true;
143                 if (params.containsKey("skip-related-to")) {
144                     String skipRelatedTo = params.getFirst("skip-related-to");
145                     isSkipRelatedTo = !(skipRelatedTo != null && skipRelatedTo.equals("false"));
146                 } else {
147                     // if skip-related-to param is missing, then default it to false;
148                     isSkipRelatedTo = false;
149                 }
150                 return isSkipRelatedTo;
151             }
152             return true;
153         }
154
155         protected boolean isTree() {
156             return this.tree;
157         }
158
159         public Builder isTree(Boolean tree) {
160             this.tree = tree;
161             return this;
162         }
163
164         public Builder includeUrl() {
165             this.includeUrl = true;
166             return this;
167         }
168
169         public Builder nodesOnly(Boolean nodesOnly) {
170             this.nodesOnly = nodesOnly;
171             return this;
172         }
173
174         public boolean isNodesOnly() {
175             return this.nodesOnly;
176         }
177
178         public Builder depth(Integer depth) {
179             this.depth = depth;
180             return this;
181         }
182
183         public int getDepth() {
184             return this.depth;
185         }
186
187         public boolean isIncludeUrl() {
188             return this.includeUrl;
189         }
190
191         public PathedURL build() throws AAIException {
192             return new PathedURL(this);
193         }
194     }
195
196 }