10bba7f69240abf42526ca6850b0759f6010e442
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / IdURL.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 class IdURL extends MultiFormatMapper {
46
47     private final UrlBuilder urlBuilder;
48     private final JsonParser parser;
49     private final Loader loader;
50
51     public IdURL(Loader loader, UrlBuilder urlBuilder) throws AAIException {
52         this.urlBuilder = urlBuilder;
53         this.parser = new JsonParser();
54         this.loader = loader;
55     }
56
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();
62     }
63
64     @Override
65     public int parallelThreshold() {
66         return 2500;
67     }
68
69     @Override
70     protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
71
72         try {
73             final Introspector searchResult = this.loader.introspectorFromName("result-data");
74
75             searchResult.setValue("resource-type", v.value(AAIProperties.NODE_TYPE));
76             searchResult.setValue("resource-link", this.urlBuilder.id(v));
77
78             final String json = searchResult.marshal(false);
79
80             return Optional.of(parser.parse(json).getAsJsonObject());
81
82         } catch (AAIUnknownObjectException e) {
83             throw new RuntimeException("Fatal error - result-data object does not exist!");
84         }
85
86     }
87
88     @Override
89     protected Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties)
90             throws AAIFormatVertexException {
91         return Optional.empty();
92     }
93
94     public static class Builder implements NodesOnly<Builder>, Depth<Builder>, AsTree<Builder> {
95
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;
104
105         public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
106             this.loader = loader;
107             this.serializer = serializer;
108             this.urlBuilder = urlBuilder;
109         }
110
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;
117         }
118
119         protected Loader getLoader() {
120             return this.loader;
121         }
122
123         protected DBSerializer getSerializer() {
124             return this.serializer;
125         }
126
127         protected UrlBuilder getUrlBuilder() {
128             return this.urlBuilder;
129         }
130
131         protected MultivaluedMap<String, String> getParams() {
132             return this.params;
133         }
134
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"));
141                 } else {
142                     // if skip-related-to param is missing, then default it to false;
143                     isSkipRelatedTo = false;
144                 }
145                 return isSkipRelatedTo;
146             }
147             return true;
148         }
149
150         protected boolean isTree() {
151             return this.tree;
152         }
153
154         public Builder isTree(Boolean tree) {
155             this.tree = tree;
156             return this;
157         }
158
159         public Builder includeUrl() {
160             this.includeUrl = true;
161             return this;
162         }
163
164         public Builder nodesOnly(Boolean nodesOnly) {
165             this.nodesOnly = nodesOnly;
166             return this;
167         }
168
169         public boolean isNodesOnly() {
170             return this.nodesOnly;
171         }
172
173         public Builder depth(Integer depth) {
174             this.depth = depth;
175             return this;
176         }
177
178         public int getDepth() {
179             return this.depth;
180         }
181
182         public boolean isIncludeUrl() {
183             return this.includeUrl;
184         }
185
186         public IdURL build() throws AAIException {
187             return new IdURL(this);
188         }
189     }
190 }