Fix sonar violations
[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 boolean equals(Object o){
178     if(o == this){
179       return true;
180     }
181
182     if( !(o instanceof GraphEvent)){
183       return false;
184     }
185
186     GraphEvent graphEvent = (GraphEvent) o;
187     return this.dbTransactionId.equals(
188         graphEvent.getDbTransactionId())
189         && this.timestamp == graphEvent.getTimestamp()
190         && this.operation.equals(graphEvent.getOperation()
191     );
192   }
193
194   @Override
195   public int hashCode() {
196     return Objects.hash(this.dbTransactionId, this.timestamp, this.operation);
197   }
198
199   public String getObjectKey() {
200     if (this.getVertex() != null) {
201       return this.getVertex().getId();
202     } else if (this.getEdge() != null) {
203       return this.getEdge().getId();
204     }
205     
206     return null;
207   }
208
209   public String getObjectType() {
210     if (this.getVertex() != null) {
211       return "vertex->" + this.getVertex().getType();
212     } else if (this.getEdge() != null) {
213       return "edge->" + this.getEdge().getType();
214     }
215
216     return null;
217   }
218
219   public static class Builder {
220
221     GraphEvent event = null;
222
223     public Builder(GraphEventOperation operation) {
224       event = new GraphEvent();
225       event.operation = operation;
226     }
227
228     public Builder vertex(GraphEventVertex vertex) {
229       event.vertex = vertex;
230       return this;
231     }
232
233     public Builder edge(GraphEventEdge edge) {
234       event.edge = edge;
235       return this;
236     }
237
238     public Builder result(GraphEventResult result) {
239       event.result = result;
240       return this;
241     }
242
243     public Builder errorMessage(String errorMessage) {
244       event.errorMessage = errorMessage;
245       return this;
246     }
247
248     public Builder httpErrorStatus(Status httpErrorStatus) {
249       event.httpErrorStatus = httpErrorStatus;
250       return this;
251     }
252
253     public GraphEvent build() {
254
255       event.timestamp = System.currentTimeMillis();
256       event.transactionId = java.util.UUID.randomUUID().toString();
257
258       return event;
259     }
260   }
261
262 }