2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.crud.event;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.annotations.SerializedName;
30 import org.onap.crud.exception.CrudException;
32 import javax.ws.rs.core.Response.Status;
34 public class GraphEvent {
36 public enum GraphEventOperation {
37 CREATE, UPDATE, DELETE
40 public enum GraphEventResult {
44 private GraphEventOperation operation;
46 @SerializedName("transaction-id")
47 private String transactionId;
49 @SerializedName("database-transaction-id")
50 private String dbTransactionId;
52 private long timestamp;
54 private GraphEventVertex vertex;
56 private GraphEventEdge edge;
58 private GraphEventResult result;
60 @SerializedName("error-message")
61 private String errorMessage;
63 private Status httpErrorStatus;
66 * Marshaller/unmarshaller for converting to/from JSON.
68 private static final Gson gson = new GsonBuilder().disableHtmlEscaping()
69 .setPrettyPrinting().create();
71 public static Builder builder(GraphEventOperation operation) {
72 return new Builder(operation);
75 public GraphEventOperation getOperation() {
79 public String getTransactionId() {
83 public String getDbTransactionId() {
84 return dbTransactionId;
87 public void setDbTransactionId(String id) {
91 public long getTimestamp() {
95 public GraphEventVertex getVertex() {
99 public GraphEventEdge getEdge() {
103 public GraphEventResult getResult() {
107 public String getErrorMessage() {
111 public void setResult(GraphEventResult result) {
112 this.result = result;
116 public Status getHttpErrorStatus() {
117 return httpErrorStatus;
120 public void setHttpErrorStatus(Status httpErrorStatus) {
121 this.httpErrorStatus = httpErrorStatus;
124 public void setTimestamp(long timestamp) {
125 this.timestamp = timestamp;
128 public void setErrorMessage(String errorMessage) {
129 this.errorMessage = errorMessage;
132 public void setVertex(GraphEventVertex vertex) {
133 this.vertex = vertex;
136 public void setEdge(GraphEventEdge edge) {
141 * Unmarshalls this Vertex object into a JSON string.
143 * @return - A JSON format string representation of this Vertex.
145 public String toJson() {
146 return gson.toJson(this);
150 * Marshalls the provided JSON string into a Vertex object.
152 * @param json - The JSON string to produce the Vertex from.
153 * @return - A Vertex object.
154 * @throws SpikeException
156 public static GraphEvent fromJson(String json) throws CrudException {
160 // Make sure that we were actually provided a non-empty string
163 if (json == null || json.isEmpty()) {
164 throw new CrudException("Empty or null JSON string.", Status.BAD_REQUEST);
167 // Marshall the string into a Vertex object.
168 return gson.fromJson(json, GraphEvent.class);
170 } catch (Exception ex) {
171 throw new CrudException("Unable to parse JSON string: "+json, Status.BAD_REQUEST);
176 public String toString() {
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();
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();
201 public static class Builder {
203 GraphEvent event = null;
205 public Builder(GraphEventOperation operation) {
206 event = new GraphEvent();
207 event.operation = operation;
210 public Builder vertex(GraphEventVertex vertex) {
211 event.vertex = vertex;
215 public Builder edge(GraphEventEdge edge) {
220 public Builder result(GraphEventResult result) {
221 event.result = result;
225 public Builder errorMessage(String errorMessage) {
226 event.errorMessage = errorMessage;
230 public Builder httpErrorStatus(Status httpErrorStatus) {
231 event.httpErrorStatus = httpErrorStatus;
235 public GraphEvent build() {
237 event.timestamp = System.currentTimeMillis();
238 event.transactionId = java.util.UUID.randomUUID().toString();