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