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;
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;
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;
40 public class ChampEvent {
42 public enum ChampOperation {
48 private static ObjectMapper mapper = new ObjectMapper();
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;
61 public static Builder builder() {
65 public ChampOperation getOperation() {
69 public void setOperation(ChampOperation operation) {
70 this.operation = operation;
73 public long getTimestamp() {
77 public void setTimestamp(long timestamp) {
78 this.timestamp = timestamp;
81 @JsonProperty("transaction-id")
82 public String getTransactionId() {
86 public void setTransactionId(String transactionId) {
87 this.transactionId = transactionId;
90 public ChampObject getVertex() {
94 public void setVertex(ChampObject vertex) {
98 public ChampRelationship getRelationship() {
102 public void setRelationship(ChampRelationship relationship) {
103 this.relationship = relationship;
106 public ChampPartition getPartition() {
110 public void setPartition(ChampPartition partition) {
111 this.partition = partition;
114 public ChampObjectIndex getObjectIndex() {
118 public void setObjectIndex(ChampObjectIndex index) {
119 this.objectIndex = index;
122 public ChampRelationshipIndex getRelationshipIndex() {
123 return relationshipIndex;
126 public void setRelationshipIndex(ChampRelationshipIndex relationshipIndex) {
127 this.relationshipIndex = relationshipIndex;
130 @JsonProperty("database-transaction-id")
131 public String getDbTransactionId () { return dbTransactionId; }
134 public void setDbTransactionId ( String id ) { this.dbTransactionId = id; }
138 public String toJson() {
140 ObjectMapper mapper = new ObjectMapper();
141 mapper.setSerializationInclusion(Include.NON_NULL);
144 return mapper.writeValueAsString(this);
145 } catch (JsonProcessingException e) {
146 return "Unmarshallable: " + e.getMessage();
150 public static ChampEvent fromJson(String json) throws JsonParseException, JsonMappingException, IOException {
152 mapper.setSerializationInclusion(Include.NON_NULL);
153 return mapper.readValue(json, ChampEvent.class);
156 public String toString() {
161 public static class Builder {
163 ChampEvent event = null;
167 event = new ChampEvent();
170 public Builder operation(ChampOperation operation) {
171 event.setOperation(operation);
175 public Builder entity(ChampObject entity) {
176 event.setVertex(entity);
180 public Builder entity(ChampRelationship relationship) {
181 event.relationship = relationship;
185 public Builder entity(ChampPartition partition) {
186 event.partition = partition;
190 public Builder entity(ChampObjectIndex index) {
191 event.objectIndex = index;
195 public Builder entity(ChampRelationshipIndex relationshipIndex) {
196 event.relationshipIndex = relationshipIndex;
201 public ChampEvent build() {
203 event.setTimestamp(System.currentTimeMillis());
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());