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