2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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============================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.champ.event;
24 import javax.ws.rs.core.Response.Status;
26 import org.onap.champ.exception.ChampServiceException;
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30 import com.google.gson.annotations.SerializedName;
32 public class GraphEvent {
34 public enum GraphEventOperation {
35 CREATE, UPDATE, DELETE
38 public enum GraphEventResult {
42 private GraphEventOperation operation;
44 @SerializedName("transaction-id")
45 private String transactionId;
47 @SerializedName("database-transaction-id")
48 private String dbTransactionId;
50 private long timestamp;
52 private GraphEventVertex vertex;
54 private GraphEventEdge edge;
56 private GraphEventResult result;
58 @SerializedName("error-message")
59 private String errorMessage;
61 private Status httpErrorStatus;
64 * Marshaller/unmarshaller for converting to/from JSON.
66 private static final Gson gson = new GsonBuilder().disableHtmlEscaping()
67 .setPrettyPrinting().create();
69 public static Builder builder(GraphEventOperation operation) {
70 return new Builder(operation);
73 public GraphEventOperation getOperation() {
77 public String getTransactionId() {
81 public String getDbTransactionId() {
82 return dbTransactionId;
85 public void setDbTransactionId(String id) {
89 public long getTimestamp() {
93 public GraphEventVertex getVertex() {
97 public GraphEventEdge getEdge() {
101 public GraphEventResult getResult() {
105 public String getErrorMessage() {
109 public void setResult(GraphEventResult result) {
110 this.result = result;
114 public Status getHttpErrorStatus() {
115 return httpErrorStatus;
118 public void setHttpErrorStatus(Status httpErrorStatus) {
119 this.httpErrorStatus = httpErrorStatus;
122 public void setTimestamp(long timestamp) {
123 this.timestamp = timestamp;
126 public void setErrorMessage(String errorMessage) {
127 this.errorMessage = errorMessage;
130 public void setVertex(GraphEventVertex vertex) {
131 this.vertex = vertex;
134 public void setEdge(GraphEventEdge edge) {
139 * Unmarshalls this Vertex object into a JSON string.
141 * @return - A JSON format string representation of this Vertex.
143 public String toJson() {
144 return gson.toJson(this);
148 * Marshalls the provided JSON string into a Vertex object.
150 * @param json - The JSON string to produce the Vertex from.
151 * @return - A Vertex object.
152 * @throws SpikeException
154 public static GraphEvent fromJson(String json) throws ChampServiceException {
158 // Make sure that we were actually provided a non-empty string
161 if (json == null || json.isEmpty()) {
162 throw new ChampServiceException("Empty or null JSON string.", Status.BAD_REQUEST);
165 // Marshall the string into a Vertex object.
166 return gson.fromJson(json, GraphEvent.class);
168 } catch (Exception ex) {
169 throw new ChampServiceException("Unable to parse JSON string: ", Status.BAD_REQUEST);
174 public String toString() {
179 public String getObjectKey() {
180 if (this.getVertex() != null) {
181 return this.getVertex().getId();
182 } else if (this.getEdge() != null) {
183 return this.getEdge().getId();
189 public String getObjectType() {
190 if (this.getVertex() != null) {
191 return "vertex->" + this.getVertex().getType();
192 } else if (this.getEdge() != null) {
193 return "edge->" + this.getEdge().getType();
199 public static class Builder {
201 GraphEvent event = null;
203 public Builder(GraphEventOperation operation) {
204 event = new GraphEvent();
205 event.operation = operation;
208 public Builder vertex(GraphEventVertex vertex) {
209 event.vertex = vertex;
213 public Builder edge(GraphEventEdge edge) {
218 public Builder result(GraphEventResult result) {
219 event.result = result;
223 public Builder errorMessage(String errorMessage) {
224 event.errorMessage = errorMessage;
228 public Builder httpErrorStatus(Status httpErrorStatus) {
229 event.httpErrorStatus = httpErrorStatus;
233 public GraphEvent build() {
235 event.timestamp = System.currentTimeMillis();
236 event.transactionId = java.util.UUID.randomUUID().toString();