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