2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.crud.event;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.annotations.SerializedName;
27 import org.onap.crud.exception.CrudException;
29 import javax.ws.rs.core.Response.Status;
31 public class GraphEvent {
33 public enum GraphEventOperation {
34 CREATE, UPDATE, DELETE
37 public enum GraphEventResult {
41 private GraphEventOperation operation;
43 @SerializedName("transaction-id")
44 private String transactionId;
46 @SerializedName("database-transaction-id")
47 private String dbTransactionId;
49 private long timestamp;
51 private GraphEventVertex vertex;
53 private GraphEventEdge edge;
55 private GraphEventResult result;
57 @SerializedName("error-message")
58 private String errorMessage;
60 private Status httpErrorStatus;
63 * Marshaller/unmarshaller for converting to/from JSON.
65 private static final Gson gson = new GsonBuilder().disableHtmlEscaping()
66 .setPrettyPrinting().create();
68 public static Builder builder(GraphEventOperation operation) {
69 return new Builder(operation);
72 public GraphEventOperation getOperation() {
76 public String getTransactionId() {
80 public String getDbTransactionId() {
81 return dbTransactionId;
84 public void setDbTransactionId(String id) {
88 public long getTimestamp() {
92 public GraphEventVertex getVertex() {
96 public GraphEventEdge getEdge() {
100 public GraphEventResult getResult() {
104 public String getErrorMessage() {
108 public void setResult(GraphEventResult result) {
109 this.result = result;
113 public Status getHttpErrorStatus() {
114 return httpErrorStatus;
117 public void setHttpErrorStatus(Status httpErrorStatus) {
118 this.httpErrorStatus = httpErrorStatus;
121 public void setTimestamp(long timestamp) {
122 this.timestamp = timestamp;
125 public void setErrorMessage(String errorMessage) {
126 this.errorMessage = errorMessage;
129 public void setVertex(GraphEventVertex vertex) {
130 this.vertex = vertex;
133 public void setEdge(GraphEventEdge edge) {
138 * Unmarshalls this Vertex object into a JSON string.
140 * @return - A JSON format string representation of this Vertex.
142 public String toJson() {
143 return gson.toJson(this);
147 * Marshalls the provided JSON string into a Vertex object.
149 * @param json - The JSON string to produce the Vertex from.
150 * @return - A Vertex object.
151 * @throws SpikeException
153 public static GraphEvent fromJson(String json) throws CrudException {
157 // Make sure that we were actually provided a non-empty string
160 if (json == null || json.isEmpty()) {
161 throw new CrudException("Empty or null JSON string.", Status.BAD_REQUEST);
164 // Marshall the string into a Vertex object.
165 return gson.fromJson(json, GraphEvent.class);
167 } catch (Exception ex) {
168 throw new CrudException("Unable to parse JSON string: "+json, Status.BAD_REQUEST);
173 public String toString() {
178 public String getObjectKey() {
179 if (this.getVertex() != null) {
180 return this.getVertex().getId();
181 } else if (this.getEdge() != null) {
182 return this.getEdge().getId();
188 public String getObjectType() {
189 if (this.getVertex() != null) {
190 return "vertex->" + this.getVertex().getType();
191 } else if (this.getEdge() != null) {
192 return "edge->" + this.getEdge().getType();
198 public static class Builder {
200 GraphEvent event = null;
202 public Builder(GraphEventOperation operation) {
203 event = new GraphEvent();
204 event.operation = operation;
207 public Builder vertex(GraphEventVertex vertex) {
208 event.vertex = vertex;
212 public Builder edge(GraphEventEdge edge) {
217 public Builder result(GraphEventResult result) {
218 event.result = result;
222 public Builder errorMessage(String errorMessage) {
223 event.errorMessage = errorMessage;
227 public Builder httpErrorStatus(Status httpErrorStatus) {
228 event.httpErrorStatus = httpErrorStatus;
232 public GraphEvent build() {
234 event.timestamp = System.currentTimeMillis();
235 event.transactionId = java.util.UUID.randomUUID().toString();