7859719eb90a6feeddb7dc6cce5df1935ac6d319
[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
29 import org.openecomp.aai.db.props.AAIProperties;
30 import org.openecomp.aai.exceptions.AAIException;
31 import org.openecomp.aai.introspection.Introspector;
32 import org.openecomp.aai.introspection.Loader;
33 import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
34 import org.openecomp.aai.serialization.db.DBSerializer;
35 import org.openecomp.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
36 import org.openecomp.aai.serialization.queryformats.utils.UrlBuilder;
37 import com.google.gson.JsonObject;
38 import com.google.gson.JsonParser;
39
40 public class Resource implements FormatMapper {
41
42         private final Loader loader;
43         private final DBSerializer serializer;
44         private final JsonParser parser;
45         private final UrlBuilder urlBuilder;
46         private final boolean includeUrl;
47         
48         private Resource (Builder builder) {
49                 this.parser = new JsonParser();
50                 this.loader = builder.getLoader();
51                 this.serializer = builder.getSerializer();
52                 this.urlBuilder = builder.getUrlBuilder();
53                 this.includeUrl = builder.isIncludeUrl();
54         }
55
56         @Override
57         public JsonObject formatObject(Object input) throws AAIFormatVertexException {
58                 Vertex v = (Vertex)input;
59                 JsonObject json = new JsonObject();
60                 
61                 if (this.includeUrl) {
62                         json.addProperty("url", this.urlBuilder.pathed(v));
63                 }
64                 json.add(v.<String>property(AAIProperties.NODE_TYPE)
65                                                                                         .orElse(null), this.vertexToJsonObject(v));
66                 
67                 return json;
68         }
69
70         protected JsonObject vertexToJsonObject(Vertex v) throws AAIFormatVertexException {
71                 try {
72                         final Introspector obj = getLoader().introspectorFromName(
73                                                                                 v.<String>property(AAIProperties.NODE_TYPE)
74                                                                                         .orElse(null)
75                                                                          );
76
77                         final List<Vertex> wrapper = new ArrayList<>();
78
79                         wrapper.add(v);
80
81                         try {
82                                 getSerializer().dbToObject(wrapper, obj, 1, false, "false");
83                         } catch (AAIException | UnsupportedEncodingException  e) {
84                                 throw new AAIFormatVertexException("Failed to format vertex - error while serializing: " + e.getMessage(), e);
85                         }
86
87                         final String json = obj.marshal(false);
88
89                         return getParser().parse(json).getAsJsonObject();
90                 } catch (AAIUnknownObjectException e) {
91                         throw new AAIFormatVertexException("Failed to format vertex - unknown object", e);
92                 }
93         }
94
95         @Override
96         public int parallelThreshold() {
97                 return 20;
98         }
99
100         private Loader getLoader() { return loader; }
101         private DBSerializer getSerializer() { return serializer; }
102         private JsonParser getParser() { return parser; }
103         
104         public static class Builder {
105                 
106                 private final Loader loader;
107                 private final DBSerializer serializer;
108                 private final UrlBuilder urlBuilder;
109                 private boolean includeUrl = false;
110                 
111                 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
112                         this.loader = loader;
113                         this.serializer = serializer;
114                         this.urlBuilder = urlBuilder;
115                 }
116                 
117                 protected Loader getLoader() {
118                         return this.loader;
119                 }
120
121                 protected DBSerializer getSerializer() {
122                         return this.serializer;
123                 }
124
125                 protected UrlBuilder getUrlBuilder() {
126                         return this.urlBuilder;
127                 }
128                 
129                 public Builder includeUrl() {
130                         this.includeUrl = true;
131                         return this;
132                 }
133
134                 protected boolean isIncludeUrl() {
135                         return this.includeUrl;
136                 }
137                 
138                 public Resource build() {
139                         return new Resource(this);
140                 }
141         }
142 }