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.aai.spike.event.outgoing;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.annotations.SerializedName;
26 import org.onap.aai.spike.exception.SpikeException;
28 public class SpikeGraphEvent {
30 public enum SpikeOperation {
31 CREATE, UPDATE, DELETE
34 private SpikeOperation operation;
36 @SerializedName("transaction-id")
37 private String transactionId;
39 @SerializedName("database-transaction-id")
40 private String dbTransactionId;
42 @SerializedName("timestamp")
43 private long operationTimestamp;
45 private SpikeVertex vertex;
47 private SpikeEdge relationship;
49 // Time this event was received in spike, used to determine when to send the event
51 private long spikeTimestamp = System.currentTimeMillis();
53 /** Serializer/deserializer for converting to/from JSON. */
54 private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
56 public SpikeOperation getOperation() {
60 public void setOperation(SpikeOperation operation) {
61 this.operation = operation;
64 public long getOperationTimestamp() {
65 return operationTimestamp;
68 public void setOperationTimestamp(long operationTimestamp) {
69 this.operationTimestamp = operationTimestamp;
74 public SpikeVertex getVertex() {
78 public void setVertex(SpikeVertex vertex) {
82 public SpikeEdge getRelationship() {
86 public void setRelationship(SpikeEdge relationship) {
87 this.relationship = relationship;
90 public String getTransactionId() {
94 public void setTransactionId(String transactionId) {
95 this.transactionId = transactionId;
98 public void setDbTransactionId(String dbTransactionId) {
99 this.dbTransactionId = dbTransactionId;
102 public long getSpikeTimestamp() {
103 return spikeTimestamp;
107 * Unmarshalls this Vertex object into a JSON string.
109 * @return - A JSON format string representation of this Vertex.
111 public String toJson() {
112 return gson.toJson(this);
117 * Marshalls the provided JSON string into a Vertex object.
119 * @param json - The JSON string to produce the Vertex from.
121 * @return - A Vertex object.
123 * @throws SpikeException
125 public static SpikeGraphEvent fromJson(String json) throws SpikeException {
128 // Make sure that we were actually provided a non-empty string
131 if (json == null || json.isEmpty()) {
132 throw new SpikeException("Empty or null JSON string.");
135 // Marshall the string into a Vertex object.
136 return gson.fromJson(json, SpikeGraphEvent.class);
138 } catch (Exception ex) {
139 throw new SpikeException("Unable to parse JSON string: " + ex.getMessage());
144 public String toString() {
148 public String getObjectKey() {
149 if (this.getVertex() != null) {
150 return this.getVertex().getId();
151 } else if (this.getRelationship() != null) {
152 return this.getRelationship().getId();
158 public String getObjectType() {
159 if (this.getVertex() != null) {
160 return "Vertex->" + this.getVertex().getType();
161 } else if (this.getRelationship() != null) {
162 return "Relationship->" + this.getRelationship().getType();