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 and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.champ.event;
27 import javax.ws.rs.core.Response.Status;
29 import org.onap.champ.exception.ChampServiceException;
31 import com.google.gson.Gson;
32 import com.google.gson.GsonBuilder;
33 import com.google.gson.annotations.SerializedName;
35 public class GraphEvent {
37 public enum GraphEventOperation {
38 CREATE, UPDATE, DELETE
41 public enum GraphEventResult {
45 private GraphEventOperation operation;
47 @SerializedName("transaction-id")
48 private String transactionId;
50 @SerializedName("database-transaction-id")
51 private String dbTransactionId;
53 private long timestamp;
55 private GraphEventVertex vertex;
57 private GraphEventEdge edge;
59 private GraphEventResult result;
61 @SerializedName("error-message")
62 private String errorMessage;
64 private Status httpErrorStatus;
67 * Marshaller/unmarshaller for converting to/from JSON.
69 private static final Gson gson = new GsonBuilder().disableHtmlEscaping()
70 .setPrettyPrinting().create();
72 public static Builder builder(GraphEventOperation operation) {
73 return new Builder(operation);
76 public GraphEventOperation getOperation() {
80 public String getTransactionId() {
84 public String getDbTransactionId() {
85 return dbTransactionId;
88 public void setDbTransactionId(String id) {
92 public long getTimestamp() {
96 public GraphEventVertex getVertex() {
100 public GraphEventEdge getEdge() {
104 public GraphEventResult getResult() {
108 public String getErrorMessage() {
112 public void setResult(GraphEventResult result) {
113 this.result = result;
117 public Status getHttpErrorStatus() {
118 return httpErrorStatus;
121 public void setHttpErrorStatus(Status httpErrorStatus) {
122 this.httpErrorStatus = httpErrorStatus;
125 public void setTimestamp(long timestamp) {
126 this.timestamp = timestamp;
129 public void setErrorMessage(String errorMessage) {
130 this.errorMessage = errorMessage;
133 public void setVertex(GraphEventVertex vertex) {
134 this.vertex = vertex;
137 public void setEdge(GraphEventEdge edge) {
142 * Unmarshalls this Vertex object into a JSON string.
144 * @return - A JSON format string representation of this Vertex.
146 public String toJson() {
147 return gson.toJson(this);
151 * Marshalls the provided JSON string into a Vertex object.
153 * @param json - The JSON string to produce the Vertex from.
154 * @return - A Vertex object.
155 * @throws SpikeException
157 public static GraphEvent fromJson(String json) throws ChampServiceException {
161 // Make sure that we were actually provided a non-empty string
164 if (json == null || json.isEmpty()) {
165 throw new ChampServiceException("Empty or null JSON string.", Status.BAD_REQUEST);
168 // Marshall the string into a Vertex object.
169 return gson.fromJson(json, GraphEvent.class);
171 } catch (Exception ex) {
172 throw new ChampServiceException("Unable to parse JSON string: ", Status.BAD_REQUEST);
177 public String toString() {
182 public String getObjectKey() {
183 if (this.getVertex() != null) {
184 return this.getVertex().getId();
185 } else if (this.getEdge() != null) {
186 return this.getEdge().getId();
192 public String getObjectType() {
193 if (this.getVertex() != null) {
194 return "vertex->" + this.getVertex().getType();
195 } else if (this.getEdge() != null) {
196 return "edge->" + this.getEdge().getType();
202 public static class Builder {
204 GraphEvent event = null;
206 public Builder(GraphEventOperation operation) {
207 event = new GraphEvent();
208 event.operation = operation;
211 public Builder vertex(GraphEventVertex vertex) {
212 event.vertex = vertex;
216 public Builder edge(GraphEventEdge edge) {
221 public Builder result(GraphEventResult result) {
222 event.result = result;
226 public Builder errorMessage(String errorMessage) {
227 event.errorMessage = errorMessage;
231 public Builder httpErrorStatus(Status httpErrorStatus) {
232 event.httpErrorStatus = httpErrorStatus;
236 public GraphEvent build() {
238 event.timestamp = System.currentTimeMillis();
239 event.transactionId = java.util.UUID.randomUUID().toString();