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;
27 import java.io.UnsupportedEncodingException;
28 import java.util.ArrayList;
29 import java.util.List;
31 import java.util.Optional;
33 import javax.ws.rs.core.MultivaluedMap;
34 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
35 import org.apache.tinkerpop.gremlin.structure.Vertex;
36 import org.onap.aai.db.props.AAIProperties;
37 import org.onap.aai.exceptions.AAIException;
38 import org.onap.aai.introspection.Introspector;
39 import org.onap.aai.introspection.Loader;
40 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
41 import org.onap.aai.serialization.db.DBSerializer;
42 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
43 import org.onap.aai.serialization.queryformats.params.AsTree;
44 import org.onap.aai.serialization.queryformats.params.Depth;
45 import org.onap.aai.serialization.queryformats.params.NodesOnly;
46 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class Resource extends MultiFormatMapper {
52 private static final Logger logger = LoggerFactory.getLogger(Resource.class);
54 private final Loader loader;
55 private final DBSerializer serializer;
56 private final JsonParser parser;
57 private final UrlBuilder urlBuilder;
58 private final boolean includeUrl;
59 private final boolean nodesOnly;
60 private final int depth;
61 private final boolean isSkipRelatedTo;
63 public Resource(Builder builder) {
64 this.parser = new JsonParser();
65 this.loader = builder.getLoader();
66 this.serializer = builder.getSerializer();
67 this.urlBuilder = builder.getUrlBuilder();
68 this.includeUrl = builder.isIncludeUrl();
69 this.nodesOnly = builder.isNodesOnly();
70 this.depth = builder.getDepth();
71 this.isSkipRelatedTo = builder.isSkipRelatedTo();
72 this.isTree = builder.isTree();
76 protected Optional<JsonObject> getRelatedNodesFromTree(Tree<?> tree, Map<String, List<String>> properties) throws AAIFormatVertexException {
78 return Optional.of(new JsonObject());
81 Map<String, Set<String>> filterPropertiesMap = createFilteredPropertyMap(properties);
83 JsonObject t = new JsonObject();
84 JsonArray ja = this.getRelatedNodesArray(tree, filterPropertiesMap,"related-nodes");
87 return Optional.of(t);
90 return Optional.empty();
93 protected JsonArray getRelatedNodesArray(Tree<?> tree, Map<String, Set<String>> filterPropertiesMap, String nodeIdentifier) throws AAIFormatVertexException {
94 JsonArray nodes = new JsonArray();
98 for (Map.Entry<?, ? extends Tree<?>> entry : tree.entrySet()) {
99 JsonObject me = new JsonObject();
100 if (entry.getKey() instanceof Vertex) {
101 Optional<JsonObject> obj = this.getJsonFromVertex((Vertex) entry.getKey());
102 if (obj.isPresent()) {
103 me = getPropertyFilteredObject(obj, filterPropertiesMap);
108 JsonArray ja = this.getRelatedNodesArray(entry.getValue(), filterPropertiesMap, nodeIdentifier);
111 for (Map.Entry<String, JsonElement> mapEntry : me.entrySet()) {
112 JsonElement value = mapEntry.getValue();
113 if (value != null && value.isJsonObject()) {
114 value.getAsJsonObject().add(nodeIdentifier, ja);
117 } catch(Exception e) {
118 logger.debug("Failed to add related-nodes array: {}", e.getMessage());
119 throw new AAIFormatVertexException("Failed to add related-nodes array: " + e.getMessage(), e);
128 protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
130 JsonObject json = new JsonObject();
132 if (this.includeUrl) {
133 json.addProperty("url", this.urlBuilder.pathed(v));
135 Optional<JsonObject> jsonObject = this.vertexToJsonObject(v);
136 if (jsonObject.isPresent()) {
137 json.add(v.<String>property(AAIProperties.NODE_TYPE).orElse(null), jsonObject.get());
139 return Optional.empty();
141 return Optional.of(json);
145 protected Optional<JsonObject> getJsonFromVertex(Vertex v, Map<String, List<String>> properties) throws AAIFormatVertexException {
146 JsonObject json = new JsonObject();
148 if (this.includeUrl) {
149 json.addProperty("url", this.urlBuilder.pathed(v));
151 Optional<JsonObject> jsonObject = this.vertexToJsonObject(v);
152 if (jsonObject.isPresent()) {
153 String nodeType = v.<String>value(AAIProperties.NODE_TYPE);
154 Map<String, Set<String>> filterPropertiesMap = createFilteredPropertyMap(properties); // this change is for resource_and_url with/out as-tree. and no as-tree req
155 JsonObject jo = filterProperties(jsonObject, nodeType, filterPropertiesMap);
156 json.add(v.<String>property(AAIProperties.NODE_TYPE).orElse(null), jo);
158 return Optional.empty();
160 return Optional.of(json);
163 protected Optional<JsonObject> vertexToJsonObject(Vertex v) throws AAIFormatVertexException {
165 return Optional.empty();
168 final Introspector obj =
169 getLoader().introspectorFromName(v.<String>property(AAIProperties.NODE_TYPE).orElse(null));
171 final List<Vertex> wrapper = new ArrayList<>();
176 getSerializer().dbToObject(wrapper, obj, this.depth, this.nodesOnly, "false", isSkipRelatedTo);
177 } catch (AAIException | UnsupportedEncodingException e) {
178 throw new AAIFormatVertexException(
179 "Failed to format vertex - error while serializing: " + e.getMessage(), e);
182 final String json = obj.marshal(false);
184 return Optional.of(getParser().parse(json).getAsJsonObject());
185 } catch (AAIUnknownObjectException e) {
186 return Optional.empty();
191 public int parallelThreshold() {
195 private Loader getLoader() {
199 private DBSerializer getSerializer() {
203 private JsonParser getParser() {
207 public static class Builder implements NodesOnly<Builder>, Depth<Builder>, AsTree<Builder> {
209 private final Loader loader;
210 private final DBSerializer serializer;
211 private final UrlBuilder urlBuilder;
212 private boolean includeUrl = false;
213 private boolean nodesOnly = false;
214 private int depth = 1;
215 private MultivaluedMap<String, String> params;
216 private boolean tree = false;
218 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
219 this.loader = loader;
220 this.serializer = serializer;
221 this.urlBuilder = urlBuilder;
224 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder, MultivaluedMap<String, String> params) {
225 this.loader = loader;
226 this.serializer = serializer;
227 this.urlBuilder = urlBuilder;
228 this.params = params;
231 protected Loader getLoader() {
235 protected DBSerializer getSerializer() {
236 return this.serializer;
239 protected UrlBuilder getUrlBuilder() {
240 return this.urlBuilder;
243 protected MultivaluedMap<String, String> getParams() { return this.params; }
245 public boolean isSkipRelatedTo() {
246 if (params != null) {
247 boolean isSkipRelatedTo = true;
248 if (params.containsKey("skip-related-to")) {
249 String skipRelatedTo = params.getFirst("skip-related-to");
250 isSkipRelatedTo = !(skipRelatedTo != null && skipRelatedTo.equals("false"));
252 // if skip-related-to param is missing, then default it to false;
253 isSkipRelatedTo = false;
255 return isSkipRelatedTo;
260 protected boolean isTree() { return this.tree; }
262 public Builder isTree(Boolean tree) {
268 public Builder includeUrl() {
269 this.includeUrl = true;
273 public Builder nodesOnly(Boolean nodesOnly) {
274 this.nodesOnly = nodesOnly;
278 public boolean isNodesOnly() {
279 return this.nodesOnly;
282 public Builder depth(Integer depth) {
287 public int getDepth() {
291 public boolean isIncludeUrl() {
292 return this.includeUrl;
295 public Resource build() {
296 return new Resource(this);