Fix serialization of the "key" property
[aai/champ.git] / champ-lib / champ-core / src / main / java / org / onap / aai / champcore / event / envelope / ChampEventHeader.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.champcore.event.envelope;
23
24 import java.time.Instant;
25 import java.time.ZoneOffset;
26 import java.time.format.DateTimeFormatter;
27 import java.util.Objects;
28 import java.util.UUID;
29 import org.apache.commons.lang3.builder.EqualsBuilder;
30 import com.fasterxml.jackson.annotation.JsonInclude.Include;
31 import com.fasterxml.jackson.annotation.JsonProperty;
32 import com.fasterxml.jackson.core.JsonProcessingException;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34
35 public class ChampEventHeader {
36
37     private static final String SOURCE_NAME = "CHAMP";
38
39     public enum EventType {
40         UPDATE_RESULT("update-result"),
41         UPDATE_NOTIFICATION("update-notification-raw");
42
43         private final String name;
44
45         EventType(String name) {
46             this.name = name;
47         }
48
49         public String getName() {
50             return name;
51         }
52     }
53
54     @JsonProperty("request-id")
55     private String requestId;
56
57     private String timestamp;
58
59     @JsonProperty("source-name")
60     private String sourceName;
61
62     @JsonProperty("event-type")
63     private String eventType;
64
65     @JsonProperty("validation-entity-type")
66     private String validationEntityType;
67
68     @JsonProperty("validation-top-entity-type")
69     private String validationTopEntityType;
70
71     @JsonProperty("entity-link")
72     private String entityLink;
73
74     public static class Builder {
75
76         private final EventType eventType;
77
78         private String requestId;
79         private String validationEntityType;
80         private String validationTopEntityType;
81         private String entityLink;
82
83         public Builder(EventType eventType) {
84             this.eventType = eventType;
85         }
86
87         public Builder requestId(String val) {
88             requestId = val;
89             return this;
90         }
91
92         public Builder validationEntityType(String val) {
93             validationEntityType = val;
94             return this;
95         }
96
97         public Builder validationTopEntityType(String val) {
98             validationTopEntityType = val;
99             return this;
100         }
101
102         public Builder entityLink(String val) {
103             entityLink = val;
104             return this;
105         }
106
107         public ChampEventHeader build() {
108             return new ChampEventHeader(this);
109         }
110     }
111
112     private ChampEventHeader(Builder builder) {
113         requestId = builder.requestId != null ? builder.requestId : UUID.randomUUID().toString();
114         timestamp = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmssX").withZone(ZoneOffset.UTC).format(Instant.now());
115         sourceName = SOURCE_NAME;
116         eventType = builder.eventType.getName();
117
118         validationEntityType = builder.validationEntityType;
119         validationTopEntityType = builder.validationTopEntityType;
120         entityLink = builder.entityLink;
121     }
122
123     /**
124      * Serializes this object into a JSON string representation.
125      *
126      * @return a JSON format string representation of this object.
127      */
128     public String toJson() {
129         ObjectMapper mapper = new ObjectMapper();
130         mapper.setSerializationInclusion(Include.NON_NULL);
131
132         try {
133             return mapper.writeValueAsString(this);
134         } catch (JsonProcessingException e) {
135             return "Unmarshallable: " + e.getMessage();
136         }
137     }
138
139     ///////////////////////////////////////////////////////////////////////////
140     // GETTERS AND SETTERS
141     ///////////////////////////////////////////////////////////////////////////
142
143     public String getRequestId() {
144         return requestId;
145     }
146
147     public void setRequestId(String requestId) {
148         this.requestId = requestId;
149     }
150
151     public String getTimestamp() {
152         return timestamp;
153     }
154
155     public void setTimestamp(String timestamp) {
156         this.timestamp = timestamp;
157     }
158
159     public String getSourceName() {
160         return sourceName;
161     }
162
163     public void setSourceName(String sourceName) {
164         this.sourceName = sourceName;
165     }
166
167     public String getEventType() {
168         return eventType;
169     }
170
171     public void setEventType(String eventType) {
172         this.eventType = eventType;
173     }
174
175     public String getValidationEntityType() {
176         return validationEntityType;
177     }
178
179     public void setValidationEntityType(String validationEntityType) {
180         this.validationEntityType = validationEntityType;
181     }
182
183     public String getValidationTopEntityType() {
184         return validationTopEntityType;
185     }
186
187     public void setValidationTopEntityType(String validationTopEntityType) {
188         this.validationTopEntityType = validationTopEntityType;
189     }
190
191     public String getEntityLink() {
192         return entityLink;
193     }
194
195     public void setEntityLink(String entityLink) {
196         this.entityLink = entityLink;
197     }
198
199     ///////////////////////////////////////////////////////////////////////////
200     // OVERRIDES
201     ///////////////////////////////////////////////////////////////////////////
202
203     /*
204      * (non-Javadoc)
205      *
206      * @see java.lang.Object#hashCode()
207      */
208     @Override
209     public int hashCode() {
210         return Objects.hash(this.requestId, this.timestamp, this.sourceName, this.eventType, this.validationEntityType,
211                 this.validationTopEntityType, this.entityLink);
212     }
213
214     /*
215      * (non-Javadoc)
216      *
217      * @see java.lang.Object#equals(java.lang.Object)
218      */
219     @Override
220     public boolean equals(Object obj) {
221         if (!(obj instanceof ChampEventHeader)) {
222             return false;
223         } else if (obj == this) {
224             return true;
225         }
226         ChampEventHeader rhs = (ChampEventHeader) obj;
227      // @formatter:off
228      return new EqualsBuilder()
229                   .append(requestId, rhs.requestId)
230                   .append(timestamp, rhs.timestamp)
231                   .append(sourceName, rhs.sourceName)
232                   .append(eventType, rhs.eventType)
233                   .append(validationEntityType, rhs.validationEntityType)
234                   .append(validationTopEntityType, rhs.validationTopEntityType)
235                   .append(entityLink, rhs.entityLink)
236                   .isEquals();
237      // @formatter:on
238     }
239
240     /*
241      * (non-Javadoc)
242      *
243      * @see java.lang.Object#toString()
244      */
245     @Override
246     public String toString() {
247         return this.toJson();
248     }
249 }