3a4fdf890e9b06eef10d86fa2471a7f16cbac623
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / Resource.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 package org.onap.aai.serialization.queryformats;
21
22 import com.google.gson.JsonObject;
23 import com.google.gson.JsonParser;
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.onap.aai.db.props.AAIProperties;
26 import org.onap.aai.exceptions.AAIException;
27 import org.onap.aai.introspection.Introspector;
28 import org.onap.aai.introspection.Loader;
29 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
30 import org.onap.aai.serialization.db.DBSerializer;
31 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
32 import org.onap.aai.serialization.queryformats.params.Depth;
33 import org.onap.aai.serialization.queryformats.params.NodesOnly;
34 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
35
36 import java.io.UnsupportedEncodingException;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Optional;
40
41 public class Resource extends MultiFormatMapper {
42
43         private final Loader loader;
44         private final DBSerializer serializer;
45         private final JsonParser parser;
46         private final UrlBuilder urlBuilder;
47         private final boolean includeUrl;
48         private final boolean nodesOnly;
49         private final int depth;
50         private Resource (Builder builder) {
51                 this.parser = new JsonParser();
52                 this.loader = builder.getLoader();
53                 this.serializer = builder.getSerializer();
54                 this.urlBuilder = builder.getUrlBuilder();
55                 this.includeUrl = builder.isIncludeUrl();
56                 this.nodesOnly = builder.isNodesOnly();
57                 this.depth = builder.getDepth();
58         }
59
60         @Override
61         protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
62
63                 JsonObject json = new JsonObject();
64
65                 if (this.includeUrl) {
66                         json.addProperty("url", this.urlBuilder.pathed(v));
67                 }
68                 Optional<JsonObject> jsonObject = this.vertexToJsonObject(v);
69                 if (jsonObject.isPresent()) {
70                         json.add(v.<String>property(AAIProperties.NODE_TYPE).orElse(null), jsonObject.get());
71                 } else {
72                         return Optional.empty();
73                 }
74                 return Optional.of(json);
75         }
76
77         protected Optional<JsonObject> vertexToJsonObject(Vertex v) throws AAIFormatVertexException {
78                 try {
79                         final Introspector obj = getLoader().introspectorFromName(
80                                                                                 v.<String>property(AAIProperties.NODE_TYPE)
81                                                                                         .orElse(null)
82                                                                          );
83
84                         final List<Vertex> wrapper = new ArrayList<>();
85
86                         wrapper.add(v);
87
88                         try {
89                                 getSerializer().dbToObject(wrapper, obj, this.depth, this.nodesOnly, "false");
90                         } catch (AAIException | UnsupportedEncodingException  e) {
91                                 throw new AAIFormatVertexException("Failed to format vertex - error while serializing: " + e.getMessage(), e);
92                         }
93
94                         final String json = obj.marshal(false);
95
96                         return Optional.of(getParser().parse(json).getAsJsonObject());
97                 } catch (AAIUnknownObjectException e) {
98                         return Optional.empty();
99                 }
100         }
101
102         @Override
103         public int parallelThreshold() {
104                 return 20;
105         }
106
107         private Loader getLoader() { return loader; }
108         private DBSerializer getSerializer() { return serializer; }
109         private JsonParser getParser() { return parser; }
110         
111         public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
112                 
113                 private final Loader loader;
114                 private final DBSerializer serializer;
115                 private final UrlBuilder urlBuilder;
116                 private boolean includeUrl = false;
117                 private boolean nodesOnly = false;
118                 private int depth = 1;
119                 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
120                         this.loader = loader;
121                         this.serializer = serializer;
122                         this.urlBuilder = urlBuilder;
123                 }
124                 
125                 protected Loader getLoader() {
126                         return this.loader;
127                 }
128
129                 protected DBSerializer getSerializer() {
130                         return this.serializer;
131                 }
132
133                 protected UrlBuilder getUrlBuilder() {
134                         return this.urlBuilder;
135                 }
136                 
137                 public Builder includeUrl() {
138                         this.includeUrl = true;
139                         return this;
140                 }
141                 
142                 public Builder nodesOnly(Boolean nodesOnly) {
143                         this.nodesOnly = nodesOnly;
144                         return this;
145                 }
146                 public boolean isNodesOnly() {
147                         return this.nodesOnly;
148                 }
149                 public Builder depth(Integer depth) {
150                         this.depth = depth;
151                         return this;
152                 }
153                 public int getDepth() {
154                         return this.depth;
155                 }
156                 public boolean isIncludeUrl() {
157                         return this.includeUrl;
158                 }
159                 
160                 public Resource build() {
161                         return new Resource(this);
162                 }
163         }
164 }