2 * ============LICENSE_START==========================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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============================================
21 package org.onap.aai.champcore.event;
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;
38 public class ChampEvent {
40 public enum ChampOperation {
41 STORE, REPLACE, DELETE
44 private static ObjectMapper mapper = new ObjectMapper();
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;
57 public static Builder builder() {
61 public ChampOperation getOperation() {
65 public void setOperation(ChampOperation operation) {
66 this.operation = operation;
69 public long getTimestamp() {
73 public void setTimestamp(long timestamp) {
74 this.timestamp = timestamp;
77 @JsonProperty("transaction-id")
78 public String getTransactionId() {
82 public void setTransactionId(String transactionId) {
83 this.transactionId = transactionId;
86 public ChampObject getVertex() {
90 public void setVertex(ChampObject vertex) {
94 public ChampRelationship getRelationship() {
98 public void setRelationship(ChampRelationship relationship) {
99 this.relationship = relationship;
102 public ChampPartition getPartition() {
106 public void setPartition(ChampPartition partition) {
107 this.partition = partition;
110 public ChampObjectIndex getObjectIndex() {
114 public void setObjectIndex(ChampObjectIndex index) {
115 this.objectIndex = index;
118 public ChampRelationshipIndex getRelationshipIndex() {
119 return relationshipIndex;
122 public void setRelationshipIndex(ChampRelationshipIndex relationshipIndex) {
123 this.relationshipIndex = relationshipIndex;
126 @JsonProperty("database-transaction-id")
127 public String getDbTransactionId() {
128 return dbTransactionId;
131 public void setDbTransactionId(String id) {
132 this.dbTransactionId = id;
135 public String toJson() {
136 ObjectMapper mapper = new ObjectMapper();
137 mapper.setSerializationInclusion(Include.NON_NULL);
140 return mapper.writeValueAsString(this);
141 } catch (JsonProcessingException e) {
142 return "Unmarshallable: " + e.getMessage();
146 public static ChampEvent fromJson(String json) throws JsonParseException, JsonMappingException, IOException {
147 mapper.setSerializationInclusion(Include.NON_NULL);
148 return mapper.readValue(json, ChampEvent.class);
152 public String toString() {
156 public static class Builder {
158 ChampEvent event = null;
161 event = new ChampEvent();
164 public Builder operation(ChampOperation operation) {
165 event.setOperation(operation);
169 public Builder entity(ChampObject entity) {
170 event.setVertex(entity);
174 public Builder entity(ChampRelationship relationship) {
175 event.relationship = relationship;
179 public Builder entity(ChampPartition partition) {
180 event.partition = partition;
184 public Builder entity(ChampObjectIndex index) {
185 event.objectIndex = index;
189 public Builder entity(ChampRelationshipIndex relationshipIndex) {
190 event.relationshipIndex = relationshipIndex;
194 public ChampEvent build() {
195 event.setTimestamp(System.currentTimeMillis());
197 // Set a unique transaction id on this event that can be used by downstream entities
198 // for log correlation.
199 event.setTransactionId(java.util.UUID.randomUUID().toString());