Enhancements for the aai-common library
[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 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;
42
43 import javax.ws.rs.core.MultivaluedMap;
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     }
64
65     @Override
66     public int parallelThreshold() {
67         return 20;
68     }
69
70     public PathedURL includeUrl() {
71         this.includeUrl = true;
72         return this;
73     }
74
75     @Override
76     protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
77
78         try {
79             final Introspector searchResult = this.loader.introspectorFromName("result-data");
80
81             searchResult.setValue("resource-type", v.value(AAIProperties.NODE_TYPE));
82
83             searchResult.setValue("resource-link", this.urlBuilder.pathed(v));
84
85             if (includeUrl)
86                 searchResult.setValue("resource-version", v.value(AAIProperties.RESOURCE_VERSION));
87
88             final String json = searchResult.marshal(false);
89             return Optional.of(this.parser.parse(json).getAsJsonObject());
90
91         } catch (AAIUnknownObjectException e) {
92             throw new RuntimeException("Fatal error - result-data does not exist!", e);
93         }
94
95     }
96
97     @Override
98     protected Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties) throws AAIFormatVertexException {
99         return Optional.empty();
100     }
101
102     public static class Builder implements NodesOnly<Builder>, Depth<Builder>, AsTree<Builder> {
103
104         private final Loader loader;
105         private final DBSerializer serializer;
106         private final UrlBuilder urlBuilder;
107         private boolean includeUrl = false;
108         private boolean nodesOnly = false;
109         private int depth = 1;
110         private MultivaluedMap<String, String> params;
111         private boolean tree = false;
112
113         public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
114             this.loader = loader;
115             this.serializer = serializer;
116             this.urlBuilder = urlBuilder;
117         }
118
119         public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder, MultivaluedMap<String, String> params) {
120             this.loader = loader;
121             this.serializer = serializer;
122             this.urlBuilder = urlBuilder;
123             this.params = params;
124         }
125
126         protected Loader getLoader() {
127             return this.loader;
128         }
129
130         protected DBSerializer getSerializer() {
131             return this.serializer;
132         }
133
134         protected UrlBuilder getUrlBuilder() {
135             return this.urlBuilder;
136         }
137
138         protected MultivaluedMap<String, String> getParams() { return this.params; }
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() { return this.tree; }
156
157         public Builder isTree(Boolean tree) {
158             this.tree = tree;
159             return this;
160         }
161
162         public Builder includeUrl() {
163             this.includeUrl = true;
164             return this;
165         }
166
167         public Builder nodesOnly(Boolean nodesOnly) {
168             this.nodesOnly = nodesOnly;
169             return this;
170         }
171
172         public boolean isNodesOnly() {
173             return this.nodesOnly;
174         }
175
176         public Builder depth(Integer depth) {
177             this.depth = depth;
178             return this;
179         }
180
181         public int getDepth() {
182             return this.depth;
183         }
184
185         public boolean isIncludeUrl() {
186             return this.includeUrl;
187         }
188
189         public PathedURL build() throws AAIException {
190             return new PathedURL(this);
191         }
192     }
193
194 }