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