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