Update payload format for update notification
[aai/champ.git] / champ-lib / champ-core / src / main / java / org / onap / aai / champcore / event / ChampEvent.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.champcore.event;
22
23
24 import java.io.IOException;
25 import org.onap.aai.champcore.model.ChampObject;
26 import org.onap.aai.champcore.model.ChampObjectIndex;
27 import org.onap.aai.champcore.model.ChampPartition;
28 import org.onap.aai.champcore.model.ChampRelationship;
29 import org.onap.aai.champcore.model.ChampRelationshipIndex;
30 import com.fasterxml.jackson.annotation.JsonInclude.Include;
31 import com.fasterxml.jackson.annotation.JsonProperty;
32 import com.fasterxml.jackson.core.JsonParseException;
33 import com.fasterxml.jackson.core.JsonProcessingException;
34 import com.fasterxml.jackson.databind.JsonMappingException;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36
37
38 public class ChampEvent {
39
40     public enum ChampOperation {
41         STORE, REPLACE, DELETE
42     }
43
44     private static ObjectMapper mapper = new ObjectMapper();
45
46     private ChampOperation operation;
47     private long timestamp;
48     private String transactionId = null;
49     private ChampObject vertex = null;
50     private ChampRelationship relationship = null;
51     private ChampPartition partition = null;
52     private ChampObjectIndex objectIndex = null;
53     private ChampRelationshipIndex relationshipIndex = null;
54     private String dbTransactionId = null;
55
56
57     public static Builder builder() {
58         return new Builder();
59     }
60
61     public ChampOperation getOperation() {
62         return operation;
63     }
64
65     public void setOperation(ChampOperation operation) {
66         this.operation = operation;
67     }
68
69     public long getTimestamp() {
70         return timestamp;
71     }
72
73     public void setTimestamp(long timestamp) {
74         this.timestamp = timestamp;
75     }
76
77     @JsonProperty("transaction-id")
78     public String getTransactionId() {
79         return transactionId;
80     }
81
82     public void setTransactionId(String transactionId) {
83         this.transactionId = transactionId;
84     }
85
86     public ChampObject getVertex() {
87         return vertex;
88     }
89
90     public void setVertex(ChampObject vertex) {
91         this.vertex = vertex;
92     }
93
94     public ChampRelationship getRelationship() {
95         return relationship;
96     }
97
98     public void setRelationship(ChampRelationship relationship) {
99         this.relationship = relationship;
100     }
101
102     public ChampPartition getPartition() {
103         return partition;
104     }
105
106     public void setPartition(ChampPartition partition) {
107         this.partition = partition;
108     }
109
110     public ChampObjectIndex getObjectIndex() {
111         return objectIndex;
112     }
113
114     public void setObjectIndex(ChampObjectIndex index) {
115         this.objectIndex = index;
116     }
117
118     public ChampRelationshipIndex getRelationshipIndex() {
119         return relationshipIndex;
120     }
121
122     public void setRelationshipIndex(ChampRelationshipIndex relationshipIndex) {
123         this.relationshipIndex = relationshipIndex;
124     }
125
126     @JsonProperty("database-transaction-id")
127     public String getDbTransactionId() {
128         return dbTransactionId;
129     }
130
131
132     public void setDbTransactionId(String id) {
133         this.dbTransactionId = id;
134     }
135
136
137
138     public String toJson() {
139
140         ObjectMapper mapper = new ObjectMapper();
141         mapper.setSerializationInclusion(Include.NON_NULL);
142
143         try {
144             return mapper.writeValueAsString(this);
145         } catch (JsonProcessingException e) {
146             return "Unmarshallable: " + e.getMessage();
147         }
148     }
149
150     public static ChampEvent fromJson(String json) throws JsonParseException, JsonMappingException, IOException {
151
152         mapper.setSerializationInclusion(Include.NON_NULL);
153         return mapper.readValue(json, ChampEvent.class);
154     }
155
156     @Override
157     public String toString() {
158
159         return toJson();
160     }
161
162     public static class Builder {
163
164         ChampEvent event = null;
165
166
167         public Builder() {
168             event = new ChampEvent();
169         }
170
171         public Builder operation(ChampOperation operation) {
172             event.setOperation(operation);
173             return this;
174         }
175
176         public Builder entity(ChampObject entity) {
177             event.setVertex(entity);
178             return this;
179         }
180
181         public Builder entity(ChampRelationship relationship) {
182             event.relationship = relationship;
183             return this;
184         }
185
186         public Builder entity(ChampPartition partition) {
187             event.partition = partition;
188             return this;
189         }
190
191         public Builder entity(ChampObjectIndex index) {
192             event.objectIndex = index;
193             return this;
194         }
195
196         public Builder entity(ChampRelationshipIndex relationshipIndex) {
197             event.relationshipIndex = relationshipIndex;
198             return this;
199         }
200
201
202         public ChampEvent build() {
203
204             event.setTimestamp(System.currentTimeMillis());
205
206             // Set a unique transaction id on this event that can be used by downstream entities
207             // for log correlation.
208             event.setTransactionId(java.util.UUID.randomUUID().toString());
209
210             return event;
211         }
212     }
213 }