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