Migrate Spike code to ONAP
[aai/spike.git] / src / main / java / org / onap / aai / spike / event / incoming / GizmoGraphEvent.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.incoming;
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.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;
33
34 public class GizmoGraphEvent {
35     private String operation;
36
37     @SerializedName("transaction-id")
38     private String transactionId;
39
40     @SerializedName("database-transaction-id")
41     private String dbTransactionId;
42
43     private long timestamp;
44
45     private GizmoVertex vertex;
46
47     private GizmoEdge relationship;
48
49     /** Marshaller/unmarshaller for converting to/from JSON. */
50     private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
51
52     public String getOperation() {
53         return operation;
54     }
55
56     public void setOperation(String operation) {
57         this.operation = operation;
58     }
59
60     public long getTimestamp() {
61         return timestamp;
62     }
63
64     public void setTimestamp(long timestamp) {
65         this.timestamp = timestamp;
66     }
67
68     public GizmoVertex getVertex() {
69         return vertex;
70     }
71
72     public void setVertex(GizmoVertex vertex) {
73         this.vertex = vertex;
74     }
75
76     public GizmoEdge getRelationship() {
77         return relationship;
78     }
79
80     public void setRelationship(GizmoEdge relationship) {
81         this.relationship = relationship;
82     }
83
84     public String getTransactionId() {
85         return transactionId;
86     }
87
88     public void setTransactionId(String transactionId) {
89         this.transactionId = transactionId;
90     }
91
92     public String getDbTransactionId() {
93         return dbTransactionId;
94     }
95
96     /**
97      * Unmarshalls this Vertex object into a JSON string.
98      * 
99      * @return - A JSON format string representation of this Vertex.
100      */
101     public String toJson() {
102         return gson.toJson(this);
103     }
104
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);
116         } else {
117             throw new SpikeException("Invalid operation in GizmoGraphEvent: " + this.getOperation());
118         }
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);
126
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());
132
133             SpikeVertex spikeSourceVertex = new SpikeVertex();
134             spikeSourceVertex.setId(this.getRelationship().getSource().getId());
135             spikeSourceVertex.setType(this.getRelationship().getSource().getType());
136             spikeEdge.setSource(spikeSourceVertex);
137
138             SpikeVertex spikeTargetVertex = new SpikeVertex();
139             spikeTargetVertex.setId(this.getRelationship().getTarget().getId());
140             spikeTargetVertex.setType(this.getRelationship().getTarget().getType());
141             spikeEdge.setTarget(spikeTargetVertex);
142
143             spikeEdge.setProperties(this.getRelationship().getProperties());
144             spikeGraphEvent.setRelationship(spikeEdge);
145         }
146
147         return spikeGraphEvent;
148
149     }
150
151     /**
152      * Marshalls the provided JSON string into a Vertex object.
153      * 
154      * @param json - The JSON string to produce the Vertex from.
155      * 
156      * @return - A Vertex object.
157      * 
158      * @throws SpikeException
159      */
160     public static GizmoGraphEvent fromJson(String json) throws SpikeException {
161
162         try {
163
164             // Make sure that we were actually provided a non-empty string
165             // before we
166             // go any further.
167             if (json == null || json.isEmpty()) {
168                 throw new SpikeException("Empty or null JSON string.");
169             }
170
171             // Marshall the string into a Vertex object.
172             return gson.fromJson(json, GizmoGraphEvent.class);
173
174         } catch (Exception ex) {
175             throw new SpikeException("Unable to parse JSON string: " + ex.getMessage());
176         }
177     }
178
179     @Override
180     public String toString() {
181
182         return toJson();
183     }
184
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();
190         }
191
192         return null;
193
194     }
195
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();
201         }
202
203         return null;
204
205     }
206 }