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 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 long getTimestamp() {
84 public GraphEventVertex getVertex() {
88 public GraphEventEdge getEdge() {
92 public GraphEventResult getResult() {
96 public String getErrorMessage() {
100 public void setResult(GraphEventResult result) {
101 this.result = result;
105 public Status getHttpErrorStatus() {
106 return httpErrorStatus;
109 public void setHttpErrorStatus(Status httpErrorStatus) {
110 this.httpErrorStatus = httpErrorStatus;
113 public void setTimestamp(long timestamp) {
114 this.timestamp = timestamp;
117 public void setErrorMessage(String errorMessage) {
118 this.errorMessage = errorMessage;
121 public void setVertex(GraphEventVertex vertex) {
122 this.vertex = vertex;
125 public void setEdge(GraphEventEdge edge) {
130 * Unmarshalls this Vertex object into a JSON string.
132 * @return - A JSON format string representation of this Vertex.
134 public String toJson() {
135 return gson.toJson(this);
139 * Marshalls the provided JSON string into a Vertex object.
141 * @param json - The JSON string to produce the Vertex from.
142 * @return - A Vertex object.
143 * @throws SpikeException
145 public static GraphEvent fromJson(String json) throws CrudException {
149 // Make sure that we were actually provided a non-empty string
152 if (json == null || json.isEmpty()) {
153 throw new CrudException("Empty or null JSON string.", Status.BAD_REQUEST);
156 // Marshall the string into a Vertex object.
157 return gson.fromJson(json, GraphEvent.class);
159 } catch (Exception ex) {
160 throw new CrudException("Unable to parse JSON string: "+json, Status.BAD_REQUEST);
165 public String toString() {
170 public String getObjectKey() {
171 if (this.getVertex() != null) {
172 return this.getVertex().getId();
173 } else if (this.getEdge() != null) {
174 return this.getEdge().getId();
181 public String getObjectType() {
182 if (this.getVertex() != null) {
183 return "vertex->" + this.getVertex().getType();
184 } else if (this.getEdge() != null) {
185 return "edge->" + this.getEdge().getType();
192 public static class Builder {
194 GraphEvent event = null;
196 public Builder(GraphEventOperation operation) {
197 event = new GraphEvent();
198 event.operation = operation;
201 public Builder vertex(GraphEventVertex vertex) {
202 event.vertex = vertex;
206 public Builder edge(GraphEventEdge edge) {
211 public Builder result(GraphEventResult result) {
212 event.result = result;
216 public Builder errorMessage(String errorMessage) {
217 event.errorMessage = errorMessage;
221 public Builder httpErrorStatus(Status httpErrorStatus) {
222 event.httpErrorStatus = httpErrorStatus;
226 public GraphEvent build() {
228 event.timestamp = System.currentTimeMillis();
229 event.transactionId = java.util.UUID.randomUUID().toString();