[AAI-2175] Change aai champ container processes to run as non-root on the host
[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 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
37
38 public class ChampEvent {
39
40     public enum ChampOperation {
41         STORE, REPLACE, DELETE
42     }
43
44     private static ObjectMapper mapper = new ObjectMapper();
45
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;
55
56
57     public static Builder builder() {
58         return new Builder();
59     }
60
61     public ChampOperation getOperation() {
62         return operation;
63     }
64
65     public void setOperation(ChampOperation operation) {
66         this.operation = operation;
67     }
68
69     public long getTimestamp() {
70         return timestamp;
71     }
72
73     public void setTimestamp(long timestamp) {
74         this.timestamp = timestamp;
75     }
76
77     @JsonProperty("transaction-id")
78     public String getTransactionId() {
79         return transactionId;
80     }
81
82     public void setTransactionId(String transactionId) {
83         this.transactionId = transactionId;
84     }
85
86     public ChampObject getVertex() {
87         return vertex;
88     }
89
90     public void setVertex(ChampObject vertex) {
91         this.vertex = vertex;
92     }
93
94     public ChampRelationship getRelationship() {
95         return relationship;
96     }
97
98     public void setRelationship(ChampRelationship relationship) {
99         this.relationship = relationship;
100     }
101
102     public ChampPartition getPartition() {
103         return partition;
104     }
105
106     public void setPartition(ChampPartition partition) {
107         this.partition = partition;
108     }
109
110     public ChampObjectIndex getObjectIndex() {
111         return objectIndex;
112     }
113
114     public void setObjectIndex(ChampObjectIndex index) {
115         this.objectIndex = index;
116     }
117
118     public ChampRelationshipIndex getRelationshipIndex() {
119         return relationshipIndex;
120     }
121
122     public void setRelationshipIndex(ChampRelationshipIndex relationshipIndex) {
123         this.relationshipIndex = relationshipIndex;
124     }
125
126     @JsonProperty("database-transaction-id")
127     public String getDbTransactionId() {
128         return dbTransactionId;
129     }
130
131     public void setDbTransactionId(String id) {
132         this.dbTransactionId = id;
133     }
134
135     public String toJson() {
136         ObjectMapper mapper = new ObjectMapper();
137         mapper.setSerializationInclusion(Include.NON_NULL);
138
139         try {
140             return mapper.writeValueAsString(this);
141         } catch (JsonProcessingException e) {
142             return "Unmarshallable: " + e.getMessage();
143         }
144     }
145
146     public static ChampEvent fromJson(String json) throws JsonParseException, JsonMappingException, IOException {
147         mapper.setSerializationInclusion(Include.NON_NULL);
148         return mapper.readValue(json, ChampEvent.class);
149     }
150
151     @Override
152     public String toString() {
153         return toJson();
154     }
155
156     public static class Builder {
157
158         ChampEvent event = null;
159
160         public Builder() {
161             event = new ChampEvent();
162         }
163
164         public Builder operation(ChampOperation operation) {
165             event.setOperation(operation);
166             return this;
167         }
168
169         public Builder entity(ChampObject entity) {
170             event.setVertex(entity);
171             return this;
172         }
173
174         public Builder entity(ChampRelationship relationship) {
175             event.relationship = relationship;
176             return this;
177         }
178
179         public Builder entity(ChampPartition partition) {
180             event.partition = partition;
181             return this;
182         }
183
184         public Builder entity(ChampObjectIndex index) {
185             event.objectIndex = index;
186             return this;
187         }
188
189         public Builder entity(ChampRelationshipIndex relationshipIndex) {
190             event.relationshipIndex = relationshipIndex;
191             return this;
192         }
193
194         public ChampEvent build() {
195             event.setTimestamp(System.currentTimeMillis());
196
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());
200
201             return event;
202         }
203     }
204 }