46f1b61d4214abf87a6910e92f0812054bd21e25
[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 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.champcore.event;
23
24
25 import java.io.IOException;
26
27 import org.onap.aai.champcore.model.ChampObject;
28 import org.onap.aai.champcore.model.ChampObjectIndex;
29 import org.onap.aai.champcore.model.ChampPartition;
30 import org.onap.aai.champcore.model.ChampRelationship;
31 import org.onap.aai.champcore.model.ChampRelationshipIndex;
32
33 import com.fasterxml.jackson.annotation.JsonInclude.Include;
34 import com.fasterxml.jackson.annotation.JsonProperty;
35 import com.fasterxml.jackson.core.JsonParseException;
36 import com.fasterxml.jackson.core.JsonProcessingException;
37 import com.fasterxml.jackson.databind.JsonMappingException;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39
40
41 public class ChampEvent {
42
43   public enum ChampOperation {
44     STORE,
45     REPLACE,
46     DELETE
47   }
48   
49   private static ObjectMapper mapper = new ObjectMapper();
50   
51   private ChampOperation         operation;
52   private long                   timestamp;
53   private String                 transactionId     = null;
54   private ChampObject            vertex            = null;
55   private ChampRelationship      relationship      = null;
56   private ChampPartition         partition         = null;
57   private ChampObjectIndex       objectIndex       = null;
58   private ChampRelationshipIndex relationshipIndex = null;
59   private String                 dbTransactionId   = null;
60   
61   
62   public static Builder builder() {
63     return new Builder();
64   }
65   
66   public ChampOperation getOperation() {
67     return operation;
68   }
69   
70   public void setOperation(ChampOperation operation) {
71     this.operation = operation;
72   }
73     
74   public long getTimestamp() {
75     return timestamp;
76   }
77   
78   public void setTimestamp(long timestamp) {
79     this.timestamp = timestamp;
80   }
81   
82   @JsonProperty("transaction-id")
83   public String getTransactionId() {
84     return transactionId;
85   }
86   
87   public void setTransactionId(String transactionId) {
88     this.transactionId = transactionId;
89   }
90   
91   public ChampObject getVertex() {
92     return vertex;
93   }
94   
95   public void setVertex(ChampObject vertex) {
96     this.vertex = vertex;
97   }
98   
99   public ChampRelationship getRelationship() {
100     return relationship;
101   }
102   
103   public void setRelationship(ChampRelationship relationship) {
104     this.relationship = relationship;
105   }
106   
107   public ChampPartition getPartition() {
108     return partition;
109   }
110   
111   public void setPartition(ChampPartition partition) {
112     this.partition = partition;
113   }
114   
115   public ChampObjectIndex getObjectIndex() {
116     return objectIndex;
117   }
118   
119   public void setObjectIndex(ChampObjectIndex index) {
120     this.objectIndex = index;
121   }
122   
123   public ChampRelationshipIndex getRelationshipIndex() {
124     return relationshipIndex;
125   }
126   
127   public void setRelationshipIndex(ChampRelationshipIndex relationshipIndex) {
128     this.relationshipIndex = relationshipIndex;
129   }
130
131   @JsonProperty("database-transaction-id")
132   public String getDbTransactionId () { return dbTransactionId; }
133
134
135   public void setDbTransactionId ( String id ) { this.dbTransactionId = id; }
136
137
138
139   public String toJson() {
140     
141     ObjectMapper mapper = new ObjectMapper();
142     mapper.setSerializationInclusion(Include.NON_NULL);
143     
144     try {
145       return mapper.writeValueAsString(this);
146     } catch (JsonProcessingException e) {
147       return "Unmarshallable: " + e.getMessage();
148     }
149   }
150   
151   public static ChampEvent fromJson(String json) throws JsonParseException, JsonMappingException, IOException {
152
153     mapper.setSerializationInclusion(Include.NON_NULL);
154     return mapper.readValue(json, ChampEvent.class);
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 }