958c227c052e09bef444b209c756130a9db0cd59
[aai/gizmo.git] / src / main / java / org / onap / crud / event / GraphEvent.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.crud.event;
22
23 import java.util.Objects;
24 import javax.ws.rs.core.Response.Status;
25 import org.onap.crud.exception.CrudException;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.annotations.SerializedName;
29
30 public class GraphEvent {
31
32   public enum GraphEventOperation {
33     CREATE, UPDATE, DELETE
34   }
35
36   public enum GraphEventResult {
37     SUCCESS, FAILURE
38   }
39
40   private GraphEventOperation operation;
41
42   @SerializedName("transaction-id")
43   private String transactionId;
44   
45   @SerializedName("database-transaction-id")
46   private String dbTransactionId;
47
48   private long timestamp;
49
50   private GraphEventVertex vertex;
51
52   private GraphEventEdge edge;
53
54   private GraphEventResult result;
55
56   @SerializedName("error-message")
57   private String errorMessage;
58
59   private Status httpErrorStatus;
60
61   /**
62    * Marshaller/unmarshaller for converting to/from JSON.
63    */
64   private static final Gson gson = new GsonBuilder().disableHtmlEscaping()
65       .setPrettyPrinting().create();
66
67   public static Builder builder(GraphEventOperation operation) {
68     return new Builder(operation);
69   }
70
71   public GraphEventOperation getOperation() {
72     return operation;
73   }
74
75   public String getTransactionId() {
76     return transactionId;
77   }
78   
79   public String getDbTransactionId() {
80     return dbTransactionId;
81   }
82   
83   public void setDbTransactionId(String id) {
84     dbTransactionId = id;
85   }
86
87   public long getTimestamp() {
88     return timestamp;
89   }
90
91   public GraphEventVertex getVertex() {
92     return vertex;
93   }
94
95   public GraphEventEdge getEdge() {
96     return edge;
97   }
98
99   public GraphEventResult getResult() {
100     return result;
101   }
102
103   public String getErrorMessage() {
104     return errorMessage;
105   }
106
107   public void setResult(GraphEventResult result) {
108     this.result = result;
109   }
110
111
112   public Status getHttpErrorStatus() {
113     return httpErrorStatus;
114   }
115
116   public void setHttpErrorStatus(Status httpErrorStatus) {
117     this.httpErrorStatus = httpErrorStatus;
118   }
119
120   public void setTimestamp(long timestamp) {
121     this.timestamp = timestamp;
122   }
123
124   public void setErrorMessage(String errorMessage) {
125     this.errorMessage = errorMessage;
126   }
127
128   public void setVertex(GraphEventVertex vertex) {
129     this.vertex = vertex;
130   }
131
132   public void setEdge(GraphEventEdge edge) {
133     this.edge = edge;
134   }
135
136   /**
137    * Unmarshalls this Vertex object into a JSON string.
138    *
139    * @return - A JSON format string representation of this Vertex.
140    */
141   public String toJson() {
142     return gson.toJson(this);
143   }
144
145   /**
146    * Marshalls the provided JSON string into a Vertex object.
147    *
148    * @param json - The JSON string to produce the Vertex from.
149    * @return - A Vertex object.
150    * @throws SpikeException
151    */
152   public static GraphEvent fromJson(String json) throws CrudException {
153
154     try {
155
156       // Make sure that we were actually provided a non-empty string
157       // before we
158       // go any further.
159       if (json == null || json.isEmpty()) {
160         throw new CrudException("Empty or null JSON string.", Status.BAD_REQUEST);
161       }
162
163       // Marshall the string into a Vertex object.
164       return gson.fromJson(json, GraphEvent.class);
165
166     } catch (Exception ex) {
167       throw new CrudException("Unable to parse JSON string: "+json, Status.BAD_REQUEST);
168     }
169   }
170
171   @Override
172   public String toString() {
173     return toJson();
174   }
175   
176   @Override
177   public int hashCode() {
178     return Objects.hash(this.dbTransactionId, this.timestamp, this.edge, this.vertex, this.operation,
179               this.result);
180   }
181
182   public String getObjectKey() {
183     if (this.getVertex() != null) {
184       return this.getVertex().getId();
185     } else if (this.getEdge() != null) {
186       return this.getEdge().getId();
187     }
188     
189     return null;
190   }
191
192   public String getObjectType() {
193     if (this.getVertex() != null) {
194       return "vertex->" + this.getVertex().getType();
195     } else if (this.getEdge() != null) {
196       return "edge->" + this.getEdge().getType();
197     }
198
199     return null;
200   }
201
202   public static class Builder {
203
204     GraphEvent event = null;
205
206     public Builder(GraphEventOperation operation) {
207       event = new GraphEvent();
208       event.operation = operation;
209     }
210
211     public Builder vertex(GraphEventVertex vertex) {
212       event.vertex = vertex;
213       return this;
214     }
215
216     public Builder edge(GraphEventEdge edge) {
217       event.edge = edge;
218       return this;
219     }
220
221     public Builder result(GraphEventResult result) {
222       event.result = result;
223       return this;
224     }
225
226     public Builder errorMessage(String errorMessage) {
227       event.errorMessage = errorMessage;
228       return this;
229     }
230
231     public Builder httpErrorStatus(Status httpErrorStatus) {
232       event.httpErrorStatus = httpErrorStatus;
233       return this;
234     }
235
236     public GraphEvent build() {
237
238       event.timestamp = System.currentTimeMillis();
239       event.transactionId = java.util.UUID.randomUUID().toString();
240
241       return event;
242     }
243   }
244
245 }