Enhancements for the aai-common library
[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 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 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) throws AAIFormatVertexException {
90         return Optional.empty();
91     }
92
93     public static class Builder implements NodesOnly<Builder>, Depth<Builder>, AsTree<Builder> {
94
95         private final Loader loader;
96         private final DBSerializer serializer;
97         private final UrlBuilder urlBuilder;
98         private boolean includeUrl = false;
99         private boolean nodesOnly = false;
100         private int depth = 1;
101         private MultivaluedMap<String, String> params;
102         private boolean tree = false;
103
104         public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
105             this.loader = loader;
106             this.serializer = serializer;
107             this.urlBuilder = urlBuilder;
108         }
109
110         public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder, MultivaluedMap<String, String> params) {
111             this.loader = loader;
112             this.serializer = serializer;
113             this.urlBuilder = urlBuilder;
114             this.params = params;
115         }
116
117         protected Loader getLoader() {
118             return this.loader;
119         }
120
121         protected DBSerializer getSerializer() {
122             return this.serializer;
123         }
124
125         protected UrlBuilder getUrlBuilder() {
126             return this.urlBuilder;
127         }
128
129         protected MultivaluedMap<String, String> getParams() { return this.params; }
130
131         public boolean isSkipRelatedTo() {
132             if (params != null) {
133                 boolean isSkipRelatedTo = true;
134                 if (params.containsKey("skip-related-to")) {
135                     String skipRelatedTo = params.getFirst("skip-related-to");
136                     isSkipRelatedTo = !(skipRelatedTo != null && skipRelatedTo.equals("false"));
137                 } else {
138                     // if skip-related-to param is missing, then default it to false;
139                     isSkipRelatedTo = false;
140                 }
141                 return isSkipRelatedTo;
142             }
143             return true;
144         }
145
146         protected boolean isTree() { return this.tree; }
147
148         public Builder isTree(Boolean tree) {
149             this.tree = tree;
150             return this;
151         }
152
153         public Builder includeUrl() {
154             this.includeUrl = true;
155             return this;
156         }
157
158         public Builder nodesOnly(Boolean nodesOnly) {
159             this.nodesOnly = nodesOnly;
160             return this;
161         }
162
163         public boolean isNodesOnly() {
164             return this.nodesOnly;
165         }
166
167         public Builder depth(Integer depth) {
168             this.depth = depth;
169             return this;
170         }
171
172         public int getDepth() {
173             return this.depth;
174         }
175
176         public boolean isIncludeUrl() {
177             return this.includeUrl;
178         }
179
180         public IdURL build() throws AAIException {
181             return new IdURL(this);
182         }
183     }
184 }