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