Port champ-microservice project restructure
[aai/champ.git] / champ-service / src / main / java / org / onap / champ / 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 and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.onap.champ.event;
26
27 import javax.ws.rs.core.Response.Status;
28
29 import org.onap.champ.exception.ChampServiceException;
30
31 import com.google.gson.Gson;
32 import com.google.gson.GsonBuilder;
33 import com.google.gson.annotations.SerializedName;
34
35 public class GraphEvent {
36
37   public enum GraphEventOperation {
38     CREATE, UPDATE, DELETE
39   }
40
41   public enum GraphEventResult {
42     SUCCESS, FAILURE
43   }
44
45   private GraphEventOperation operation;
46
47   @SerializedName("transaction-id")
48   private String transactionId;
49
50   @SerializedName("database-transaction-id")
51   private String dbTransactionId;
52   
53   private long timestamp;
54
55   private GraphEventVertex vertex;
56
57   private GraphEventEdge edge;
58
59   private GraphEventResult result;
60
61   @SerializedName("error-message")
62   private String errorMessage;
63
64   private Status httpErrorStatus;
65
66   /**
67    * Marshaller/unmarshaller for converting to/from JSON.
68    */
69   private static final Gson gson = new GsonBuilder().disableHtmlEscaping()
70       .setPrettyPrinting().create();
71
72   public static Builder builder(GraphEventOperation operation) {
73     return new Builder(operation);
74   }
75
76   public GraphEventOperation getOperation() {
77     return operation;
78   }
79
80   public String getTransactionId() {
81     return transactionId;
82   }
83   
84   public String getDbTransactionId() {
85     return dbTransactionId;
86   }
87   
88   public void setDbTransactionId(String id) {
89     dbTransactionId = id;
90   }
91   
92   public long getTimestamp() {
93     return timestamp;
94   }
95
96   public GraphEventVertex getVertex() {
97     return vertex;
98   }
99
100   public GraphEventEdge getEdge() {
101     return edge;
102   }
103
104   public GraphEventResult getResult() {
105     return result;
106   }
107
108   public String getErrorMessage() {
109     return errorMessage;
110   }
111
112   public void setResult(GraphEventResult result) {
113     this.result = result;
114   }
115
116
117   public Status getHttpErrorStatus() {
118     return httpErrorStatus;
119   }
120
121   public void setHttpErrorStatus(Status httpErrorStatus) {
122     this.httpErrorStatus = httpErrorStatus;
123   }
124
125   public void setTimestamp(long timestamp) {
126     this.timestamp = timestamp;
127   }
128
129   public void setErrorMessage(String errorMessage) {
130     this.errorMessage = errorMessage;
131   }
132
133   public void setVertex(GraphEventVertex vertex) {
134     this.vertex = vertex;
135   }
136
137   public void setEdge(GraphEventEdge edge) {
138     this.edge = edge;
139   }
140
141   /**
142    * Unmarshalls this Vertex object into a JSON string.
143    *
144    * @return - A JSON format string representation of this Vertex.
145    */
146   public String toJson() {
147     return gson.toJson(this);
148   }
149
150   /**
151    * Marshalls the provided JSON string into a Vertex object.
152    *
153    * @param json - The JSON string to produce the Vertex from.
154    * @return - A Vertex object.
155    * @throws SpikeException
156    */
157   public static GraphEvent fromJson(String json) throws ChampServiceException {
158
159     try {
160
161       // Make sure that we were actually provided a non-empty string
162       // before we
163       // go any further.
164       if (json == null || json.isEmpty()) {
165         throw new ChampServiceException("Empty or null JSON string.", Status.BAD_REQUEST);
166       }
167
168       // Marshall the string into a Vertex object.
169       return gson.fromJson(json, GraphEvent.class);
170
171     } catch (Exception ex) {
172       throw new ChampServiceException("Unable to parse JSON string: ", Status.BAD_REQUEST);
173     }
174   }
175
176   @Override
177   public String toString() {
178
179     return toJson();
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 }