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 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;
30 public class GraphEvent {
32 public enum GraphEventOperation {
33 CREATE, UPDATE, DELETE
36 public enum GraphEventResult {
40 private GraphEventOperation operation;
42 @SerializedName("transaction-id")
43 private String transactionId;
45 @SerializedName("database-transaction-id")
46 private String dbTransactionId;
48 private long timestamp;
50 private GraphEventVertex vertex;
52 private GraphEventEdge edge;
54 private GraphEventResult result;
56 @SerializedName("error-message")
57 private String errorMessage;
59 private Status httpErrorStatus;
62 * Marshaller/unmarshaller for converting to/from JSON.
64 private static final Gson gson = new GsonBuilder().disableHtmlEscaping()
65 .setPrettyPrinting().create();
67 public static Builder builder(GraphEventOperation operation) {
68 return new Builder(operation);
71 public GraphEventOperation getOperation() {
75 public String getTransactionId() {
79 public String getDbTransactionId() {
80 return dbTransactionId;
83 public void setDbTransactionId(String id) {
87 public long getTimestamp() {
91 public GraphEventVertex getVertex() {
95 public GraphEventEdge getEdge() {
99 public GraphEventResult getResult() {
103 public String getErrorMessage() {
107 public void setResult(GraphEventResult result) {
108 this.result = result;
112 public Status getHttpErrorStatus() {
113 return httpErrorStatus;
116 public void setHttpErrorStatus(Status httpErrorStatus) {
117 this.httpErrorStatus = httpErrorStatus;
120 public void setTimestamp(long timestamp) {
121 this.timestamp = timestamp;
124 public void setErrorMessage(String errorMessage) {
125 this.errorMessage = errorMessage;
128 public void setVertex(GraphEventVertex vertex) {
129 this.vertex = vertex;
132 public void setEdge(GraphEventEdge edge) {
137 * Unmarshalls this Vertex object into a JSON string.
139 * @return - A JSON format string representation of this Vertex.
141 public String toJson() {
142 return gson.toJson(this);
146 * Marshalls the provided JSON string into a Vertex object.
148 * @param json - The JSON string to produce the Vertex from.
149 * @return - A Vertex object.
150 * @throws SpikeException
152 public static GraphEvent fromJson(String json) throws CrudException {
156 // Make sure that we were actually provided a non-empty string
159 if (json == null || json.isEmpty()) {
160 throw new CrudException("Empty or null JSON string.", Status.BAD_REQUEST);
163 // Marshall the string into a Vertex object.
164 return gson.fromJson(json, GraphEvent.class);
166 } catch (Exception ex) {
167 throw new CrudException("Unable to parse JSON string: "+json, Status.BAD_REQUEST);
172 public String toString() {
177 public boolean equals(Object o){
182 if( !(o instanceof GraphEvent)){
186 GraphEvent graphEvent = (GraphEvent) o;
187 return this.dbTransactionId.equals(
188 graphEvent.getDbTransactionId())
189 && this.timestamp == graphEvent.getTimestamp()
190 && this.operation.equals(graphEvent.getOperation()
195 public int hashCode() {
196 return Objects.hash(this.dbTransactionId, this.timestamp, this.operation);
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();
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();
219 public static class Builder {
221 GraphEvent event = null;
223 public Builder(GraphEventOperation operation) {
224 event = new GraphEvent();
225 event.operation = operation;
228 public Builder vertex(GraphEventVertex vertex) {
229 event.vertex = vertex;
233 public Builder edge(GraphEventEdge edge) {
238 public Builder result(GraphEventResult result) {
239 event.result = result;
243 public Builder errorMessage(String errorMessage) {
244 event.errorMessage = errorMessage;
248 public Builder httpErrorStatus(Status httpErrorStatus) {
249 event.httpErrorStatus = httpErrorStatus;
253 public GraphEvent build() {
255 event.timestamp = System.currentTimeMillis();
256 event.transactionId = java.util.UUID.randomUUID().toString();