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;
36 import com.google.gson.annotations.SerializedName;
39 public class ChampEvent {
41 public enum ChampOperation {
42 STORE, REPLACE, DELETE
45 private static ObjectMapper mapper = new ObjectMapper();
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;
60 public static Builder builder() {
64 public ChampOperation getOperation() {
68 public void setOperation(ChampOperation operation) {
69 this.operation = operation;
72 public long getTimestamp() {
76 public void setTimestamp(long timestamp) {
77 this.timestamp = timestamp;
80 @JsonProperty("transaction-id")
81 public String getTransactionId() {
85 public void setTransactionId(String transactionId) {
86 this.transactionId = transactionId;
89 public ChampObject getVertex() {
93 public void setVertex(ChampObject vertex) {
97 public ChampRelationship getRelationship() {
101 public void setRelationship(ChampRelationship relationship) {
102 this.relationship = relationship;
105 public ChampPartition getPartition() {
109 public void setPartition(ChampPartition partition) {
110 this.partition = partition;
113 public ChampObjectIndex getObjectIndex() {
117 public void setObjectIndex(ChampObjectIndex index) {
118 this.objectIndex = index;
121 public ChampRelationshipIndex getRelationshipIndex() {
122 return relationshipIndex;
125 public void setRelationshipIndex(ChampRelationshipIndex relationshipIndex) {
126 this.relationshipIndex = relationshipIndex;
129 @JsonProperty("database-transaction-id")
130 public String getDbTransactionId() {
131 return dbTransactionId;
135 public void setDbTransactionId(String id) {
136 this.dbTransactionId = id;
141 public String toJson() {
143 ObjectMapper mapper = new ObjectMapper();
144 mapper.setSerializationInclusion(Include.NON_NULL);
147 return mapper.writeValueAsString(this);
148 } catch (JsonProcessingException e) {
149 return "Unmarshallable: " + e.getMessage();
153 public static ChampEvent fromJson(String json) throws JsonParseException, JsonMappingException, IOException {
155 mapper.setSerializationInclusion(Include.NON_NULL);
156 return mapper.readValue(json, ChampEvent.class);
160 public String toString() {
165 public static class Builder {
167 ChampEvent event = null;
171 event = new ChampEvent();
174 public Builder operation(ChampOperation operation) {
175 event.setOperation(operation);
179 public Builder entity(ChampObject entity) {
180 event.setVertex(entity);
184 public Builder entity(ChampRelationship relationship) {
185 event.relationship = relationship;
189 public Builder entity(ChampPartition partition) {
190 event.partition = partition;
194 public Builder entity(ChampObjectIndex index) {
195 event.objectIndex = index;
199 public Builder entity(ChampRelationshipIndex relationshipIndex) {
200 event.relationshipIndex = relationshipIndex;
205 public ChampEvent build() {
207 event.setTimestamp(System.currentTimeMillis());
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());