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.incoming;
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.event.outgoing.SpikeEdge;
27 import org.onap.aai.spike.event.outgoing.SpikeGraphEvent;
28 import org.onap.aai.spike.event.outgoing.SpikeGraphEvent.SpikeOperation;
29 import org.onap.aai.spike.event.outgoing.SpikeVertex;
30 import org.onap.aai.spike.exception.SpikeException;
31 import org.onap.aai.spike.schema.EdgeRulesLoader;
32 import org.onap.aai.spike.schema.OXMModelLoader;
34 public class GizmoGraphEvent {
35 private String operation;
37 @SerializedName("transaction-id")
38 private String transactionId;
40 @SerializedName("database-transaction-id")
41 private String dbTransactionId;
43 private long timestamp;
45 private GizmoVertex vertex;
47 private GizmoEdge relationship;
49 /** Marshaller/unmarshaller for converting to/from JSON. */
50 private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
52 public String getOperation() {
56 public void setOperation(String operation) {
57 this.operation = operation;
60 public long getTimestamp() {
64 public void setTimestamp(long timestamp) {
65 this.timestamp = timestamp;
68 public GizmoVertex getVertex() {
72 public void setVertex(GizmoVertex vertex) {
76 public GizmoEdge getRelationship() {
80 public void setRelationship(GizmoEdge relationship) {
81 this.relationship = relationship;
84 public String getTransactionId() {
88 public void setTransactionId(String transactionId) {
89 this.transactionId = transactionId;
92 public String getDbTransactionId() {
93 return dbTransactionId;
97 * Unmarshalls this Vertex object into a JSON string.
99 * @return - A JSON format string representation of this Vertex.
101 public String toJson() {
102 return gson.toJson(this);
105 public SpikeGraphEvent toSpikeGraphEvent() throws SpikeException {
106 SpikeGraphEvent spikeGraphEvent = new SpikeGraphEvent();
107 spikeGraphEvent.setTransactionId(this.getTransactionId());
108 spikeGraphEvent.setDbTransactionId(this.getDbTransactionId());
109 spikeGraphEvent.setOperationTimestamp(this.getTimestamp());
110 if (this.getOperation().equalsIgnoreCase("STORE")) {
111 spikeGraphEvent.setOperation(SpikeOperation.CREATE);
112 } else if (this.getOperation().equalsIgnoreCase("REPLACE")) {
113 spikeGraphEvent.setOperation(SpikeOperation.UPDATE);
114 } else if (this.getOperation().equalsIgnoreCase("DELETE")) {
115 spikeGraphEvent.setOperation(SpikeOperation.DELETE);
117 throw new SpikeException("Invalid operation in GizmoGraphEvent: " + this.getOperation());
119 if (this.getVertex() != null) {
120 SpikeVertex spikeVertex = new SpikeVertex();
121 spikeVertex.setId(this.getVertex().getId());
122 spikeVertex.setType(this.getVertex().getType());
123 spikeVertex.setModelVersion(OXMModelLoader.getLatestVersion());
124 spikeVertex.setProperties(this.getVertex().getProperties());
125 spikeGraphEvent.setVertex(spikeVertex);
127 } else if (this.getRelationship() != null) {
128 SpikeEdge spikeEdge = new SpikeEdge();
129 spikeEdge.setId(this.getRelationship().getId());
130 spikeEdge.setModelVersion(EdgeRulesLoader.getLatestSchemaVersion());
131 spikeEdge.setType(this.getRelationship().getType());
133 SpikeVertex spikeSourceVertex = new SpikeVertex();
134 spikeSourceVertex.setId(this.getRelationship().getSource().getId());
135 spikeSourceVertex.setType(this.getRelationship().getSource().getType());
136 spikeEdge.setSource(spikeSourceVertex);
138 SpikeVertex spikeTargetVertex = new SpikeVertex();
139 spikeTargetVertex.setId(this.getRelationship().getTarget().getId());
140 spikeTargetVertex.setType(this.getRelationship().getTarget().getType());
141 spikeEdge.setTarget(spikeTargetVertex);
143 spikeEdge.setProperties(this.getRelationship().getProperties());
144 spikeGraphEvent.setRelationship(spikeEdge);
147 return spikeGraphEvent;
152 * Marshalls the provided JSON string into a Vertex object.
154 * @param json - The JSON string to produce the Vertex from.
156 * @return - A Vertex object.
158 * @throws SpikeException
160 public static GizmoGraphEvent fromJson(String json) throws SpikeException {
164 // Make sure that we were actually provided a non-empty string
167 if (json == null || json.isEmpty()) {
168 throw new SpikeException("Empty or null JSON string.");
171 // Marshall the string into a Vertex object.
172 return gson.fromJson(json, GizmoGraphEvent.class);
174 } catch (Exception ex) {
175 throw new SpikeException("Unable to parse JSON string: " + ex.getMessage());
180 public String toString() {
185 public String getObjectKey() {
186 if (this.getVertex() != null) {
187 return this.getVertex().getId();
188 } else if (this.getRelationship() != null) {
189 return this.getRelationship().getId();
196 public String getObjectType() {
197 if (this.getVertex() != null) {
198 return "Vertex->" + this.getVertex().getType();
199 } else if (this.getRelationship() != null) {
200 return "Relationship->" + this.getRelationship().getType();