2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.serialization.queryformats;
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
28 import java.io.UnsupportedEncodingException;
29 import java.util.ArrayList;
30 import java.util.List;
32 import java.util.Optional;
35 import javax.ws.rs.core.MultivaluedMap;
37 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
38 import org.apache.tinkerpop.gremlin.structure.Vertex;
39 import org.onap.aai.db.props.AAIProperties;
40 import org.onap.aai.exceptions.AAIException;
41 import org.onap.aai.introspection.Introspector;
42 import org.onap.aai.introspection.Loader;
43 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
44 import org.onap.aai.serialization.db.DBSerializer;
45 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
46 import org.onap.aai.serialization.queryformats.params.AsTree;
47 import org.onap.aai.serialization.queryformats.params.Depth;
48 import org.onap.aai.serialization.queryformats.params.NodesOnly;
49 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 public class Resource extends MultiFormatMapper {
55 private static final Logger logger = LoggerFactory.getLogger(Resource.class);
57 private final Loader loader;
58 private final DBSerializer serializer;
59 private final JsonParser parser;
60 private final UrlBuilder urlBuilder;
61 private final boolean includeUrl;
62 private final boolean nodesOnly;
63 private final int depth;
64 private final boolean isSkipRelatedTo;
66 public Resource(Builder builder) {
67 this.parser = new JsonParser();
68 this.loader = builder.getLoader();
69 this.serializer = builder.getSerializer();
70 this.urlBuilder = builder.getUrlBuilder();
71 this.includeUrl = builder.isIncludeUrl();
72 this.nodesOnly = builder.isNodesOnly();
73 this.depth = builder.getDepth();
74 this.isSkipRelatedTo = builder.isSkipRelatedTo();
75 this.isTree = builder.isTree();
79 protected Optional<JsonObject> getRelatedNodesFromTree(Tree<?> tree, Map<String, List<String>> properties)
80 throws AAIFormatVertexException {
82 return Optional.of(new JsonObject());
85 Map<String, Set<String>> filterPropertiesMap = createFilteredPropertyMap(properties);
87 JsonObject t = new JsonObject();
88 JsonArray ja = this.getRelatedNodesArray(tree, filterPropertiesMap, "related-nodes");
91 return Optional.of(t);
94 return Optional.empty();
97 protected JsonArray getRelatedNodesArray(Tree<?> tree, Map<String, Set<String>> filterPropertiesMap,
98 String nodeIdentifier) throws AAIFormatVertexException {
99 JsonArray nodes = new JsonArray();
100 if (tree.isEmpty()) {
103 for (Map.Entry<?, ? extends Tree<?>> entry : tree.entrySet()) {
104 JsonObject me = new JsonObject();
105 if (entry.getKey() instanceof Vertex) {
106 Optional<JsonObject> obj = this.getJsonFromVertex((Vertex) entry.getKey());
107 if (obj.isPresent()) {
108 me = getPropertyFilteredObject(obj, filterPropertiesMap);
113 JsonArray ja = this.getRelatedNodesArray(entry.getValue(), filterPropertiesMap, nodeIdentifier);
116 for (Map.Entry<String, JsonElement> mapEntry : me.entrySet()) {
117 JsonElement value = mapEntry.getValue();
118 if (value != null && value.isJsonObject()) {
119 value.getAsJsonObject().add(nodeIdentifier, ja);
122 } catch (Exception e) {
123 logger.debug("Failed to add related-nodes array: {}", e.getMessage());
124 throw new AAIFormatVertexException("Failed to add related-nodes array: " + e.getMessage(), e);
133 protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
135 JsonObject json = new JsonObject();
137 if (this.includeUrl) {
138 json.addProperty("url", this.urlBuilder.pathed(v));
140 Optional<JsonObject> jsonObject = this.vertexToJsonObject(v);
141 if (jsonObject.isPresent()) {
142 json.add(v.<String>property(AAIProperties.NODE_TYPE).orElse(null), jsonObject.get());
144 return Optional.empty();
146 return Optional.of(json);
150 protected Optional<JsonObject> getJsonFromVertex(Vertex v, Map<String, List<String>> properties)
151 throws AAIFormatVertexException {
152 JsonObject json = new JsonObject();
154 if (this.includeUrl) {
155 json.addProperty("url", this.urlBuilder.pathed(v));
157 Optional<JsonObject> jsonObject = this.vertexToJsonObject(v);
158 if (jsonObject.isPresent()) {
159 String nodeType = v.<String>value(AAIProperties.NODE_TYPE);
160 Map<String, Set<String>> filterPropertiesMap = createFilteredPropertyMap(properties); // this change is for
163 // and no as-tree req
164 JsonObject jo = filterProperties(jsonObject, nodeType, filterPropertiesMap);
165 json.add(v.<String>property(AAIProperties.NODE_TYPE).orElse(null), jo);
167 return Optional.empty();
169 return Optional.of(json);
172 protected Optional<JsonObject> vertexToJsonObject(Vertex v) throws AAIFormatVertexException {
174 return Optional.empty();
177 final Introspector obj =
178 getLoader().introspectorFromName(v.<String>property(AAIProperties.NODE_TYPE).orElse(null));
180 final List<Vertex> wrapper = new ArrayList<>();
185 getSerializer().dbToObject(wrapper, obj, this.depth, this.nodesOnly, "false", isSkipRelatedTo);
186 } catch (AAIException | UnsupportedEncodingException e) {
187 throw new AAIFormatVertexException(
188 "Failed to format vertex - error while serializing: " + e.getMessage(), e);
191 final String json = obj.marshal(false);
193 return Optional.of(getParser().parse(json).getAsJsonObject());
194 } catch (AAIUnknownObjectException e) {
195 return Optional.empty();
200 public int parallelThreshold() {
204 private Loader getLoader() {
208 private DBSerializer getSerializer() {
212 private JsonParser getParser() {
216 public static class Builder implements NodesOnly<Builder>, Depth<Builder>, AsTree<Builder> {
218 private final Loader loader;
219 private final DBSerializer serializer;
220 private final UrlBuilder urlBuilder;
221 private boolean includeUrl = false;
222 private boolean nodesOnly = false;
223 private int depth = 1;
224 private MultivaluedMap<String, String> params;
225 private boolean tree = false;
227 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
228 this.loader = loader;
229 this.serializer = serializer;
230 this.urlBuilder = urlBuilder;
233 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder,
234 MultivaluedMap<String, String> params) {
235 this.loader = loader;
236 this.serializer = serializer;
237 this.urlBuilder = urlBuilder;
238 this.params = params;
241 protected Loader getLoader() {
245 protected DBSerializer getSerializer() {
246 return this.serializer;
249 protected UrlBuilder getUrlBuilder() {
250 return this.urlBuilder;
253 protected MultivaluedMap<String, String> getParams() {
257 public boolean isSkipRelatedTo() {
258 if (params != null) {
259 boolean isSkipRelatedTo = true;
260 if (params.containsKey("skip-related-to")) {
261 String skipRelatedTo = params.getFirst("skip-related-to");
262 isSkipRelatedTo = !(skipRelatedTo != null && skipRelatedTo.equals("false"));
264 // if skip-related-to param is missing, then default it to false;
265 isSkipRelatedTo = false;
267 return isSkipRelatedTo;
272 protected boolean isTree() {
276 public Builder isTree(Boolean tree) {
281 public Builder includeUrl() {
282 this.includeUrl = true;
286 public Builder nodesOnly(Boolean nodesOnly) {
287 this.nodesOnly = nodesOnly;
291 public boolean isNodesOnly() {
292 return this.nodesOnly;
295 public Builder depth(Integer depth) {
300 public int getDepth() {
304 public boolean isIncludeUrl() {
305 return this.includeUrl;
308 public Resource build() {
309 return new Resource(this);