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