Migrate Spike code to ONAP
[aai/spike.git] / src / main / java / org / onap / aai / spike / event / outgoing / SpikeGraphEvent.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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  */
21 package org.onap.aai.spike.event.outgoing;
22
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;
27
28 public class SpikeGraphEvent {
29
30     public enum SpikeOperation {
31         CREATE, UPDATE, DELETE
32     }
33
34     private SpikeOperation operation;
35
36     @SerializedName("transaction-id")
37     private String transactionId;
38
39     @SerializedName("database-transaction-id")
40     private String dbTransactionId;
41
42     @SerializedName("timestamp")
43     private long operationTimestamp;
44
45     private SpikeVertex vertex;
46
47     private SpikeEdge relationship;
48
49     // Time this event was received in spike, used to determine when to send the event
50     @GsonExclude
51     private long spikeTimestamp = System.currentTimeMillis();
52
53     /** Serializer/deserializer for converting to/from JSON. */
54     private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
55
56     public SpikeOperation getOperation() {
57         return operation;
58     }
59
60     public void setOperation(SpikeOperation operation) {
61         this.operation = operation;
62     }
63
64     public long getOperationTimestamp() {
65         return operationTimestamp;
66     }
67
68     public void setOperationTimestamp(long operationTimestamp) {
69         this.operationTimestamp = operationTimestamp;
70     }
71
72
73
74     public SpikeVertex getVertex() {
75         return vertex;
76     }
77
78     public void setVertex(SpikeVertex vertex) {
79         this.vertex = vertex;
80     }
81
82     public SpikeEdge getRelationship() {
83         return relationship;
84     }
85
86     public void setRelationship(SpikeEdge relationship) {
87         this.relationship = relationship;
88     }
89
90     public String getTransactionId() {
91         return transactionId;
92     }
93
94     public void setTransactionId(String transactionId) {
95         this.transactionId = transactionId;
96     }
97
98     public void setDbTransactionId(String dbTransactionId) {
99         this.dbTransactionId = dbTransactionId;
100     }
101
102     public long getSpikeTimestamp() {
103         return spikeTimestamp;
104     }
105
106     /**
107      * Unmarshalls this Vertex object into a JSON string.
108      *
109      * @return - A JSON format string representation of this Vertex.
110      */
111     public String toJson() {
112         return gson.toJson(this);
113     }
114
115
116     /**
117      * Marshalls the provided JSON string into a Vertex object.
118      *
119      * @param json - The JSON string to produce the Vertex from.
120      *
121      * @return - A Vertex object.
122      *
123      * @throws SpikeException
124      */
125     public static SpikeGraphEvent fromJson(String json) throws SpikeException {
126
127         try {
128             // Make sure that we were actually provided a non-empty string
129             // before we
130             // go any further.
131             if (json == null || json.isEmpty()) {
132                 throw new SpikeException("Empty or null JSON string.");
133             }
134
135             // Marshall the string into a Vertex object.
136             return gson.fromJson(json, SpikeGraphEvent.class);
137
138         } catch (Exception ex) {
139             throw new SpikeException("Unable to parse JSON string: " + ex.getMessage());
140         }
141     }
142
143     @Override
144     public String toString() {
145         return toJson();
146     }
147
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();
153         }
154
155         return null;
156     }
157
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();
163         }
164
165         return null;
166     }
167 }
168