Update payload format for update notification
[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.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
32 import com.google.gson.annotations.SerializedName;
33
34 public class ChampEventHeader {
35
36     private static final String SOURCE_NAME = "CHAMP";
37
38     public enum EventType {
39         UPDATE_RESULT("update-result"),
40         UPDATE_NOTIFICATION("update-notification-raw");
41
42         private final String name;
43
44         EventType(String name) {
45             this.name = name;
46         }
47
48         public String getName() {
49             return name;
50         }
51     }
52
53     @SerializedName("request-id")
54     private String requestId;
55
56     private String timestamp;
57
58     @SerializedName("source-name")
59     private String sourceName;
60
61     @SerializedName("event-type")
62     private String eventType;
63
64     @SerializedName("validation-entity-type")
65     private String validationEntityType;
66
67     @SerializedName("validation-top-entity-type")
68     private String validationTopEntityType;
69
70     @SerializedName("entity-link")
71     private String entityLink;
72
73     private static final Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
74
75     public static class Builder {
76
77         private final EventType eventType;
78
79         private String requestId;
80         private String validationEntityType;
81         private String validationTopEntityType;
82         private String entityLink;
83
84         public Builder(EventType eventType) {
85             this.eventType = eventType;
86         }
87
88         public Builder requestId(String val) {
89             requestId = val;
90             return this;
91         }
92
93         public Builder validationEntityType(String val) {
94             validationEntityType = val;
95             return this;
96         }
97
98         public Builder validationTopEntityType(String val) {
99             validationTopEntityType = val;
100             return this;
101         }
102
103         public Builder entityLink(String val) {
104             entityLink = val;
105             return this;
106         }
107
108         public ChampEventHeader build() {
109             return new ChampEventHeader(this);
110         }
111     }
112
113     private ChampEventHeader(Builder builder) {
114         requestId = builder.requestId != null ? builder.requestId : UUID.randomUUID().toString();
115         timestamp = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmssX").withZone(ZoneOffset.UTC).format(Instant.now());
116         sourceName = SOURCE_NAME;
117         eventType = builder.eventType.getName();
118
119         validationEntityType = builder.validationEntityType;
120         validationTopEntityType = builder.validationTopEntityType;
121         entityLink = builder.entityLink;
122     }
123
124     /**
125      * Serializes this object into a JSON string representation.
126      *
127      * @return a JSON format string representation of this object.
128      */
129     public String toJson() {
130         return gson.toJson(this);
131     }
132
133     ///////////////////////////////////////////////////////////////////////////
134     // GETTERS AND SETTERS
135     ///////////////////////////////////////////////////////////////////////////
136
137     public String getRequestId() {
138         return requestId;
139     }
140
141     public void setRequestId(String requestId) {
142         this.requestId = requestId;
143     }
144
145     public String getTimestamp() {
146         return timestamp;
147     }
148
149     public void setTimestamp(String timestamp) {
150         this.timestamp = timestamp;
151     }
152
153     public String getSourceName() {
154         return sourceName;
155     }
156
157     public void setSourceName(String sourceName) {
158         this.sourceName = sourceName;
159     }
160
161     public String getEventType() {
162         return eventType;
163     }
164
165     public void setEventType(String eventType) {
166         this.eventType = eventType;
167     }
168
169     public String getValidationEntityType() {
170         return validationEntityType;
171     }
172
173     public void setValidationEntityType(String validationEntityType) {
174         this.validationEntityType = validationEntityType;
175     }
176
177     public String getValidationTopEntityType() {
178         return validationTopEntityType;
179     }
180
181     public void setValidationTopEntityType(String validationTopEntityType) {
182         this.validationTopEntityType = validationTopEntityType;
183     }
184
185     public String getEntityLink() {
186         return entityLink;
187     }
188
189     public void setEntityLink(String entityLink) {
190         this.entityLink = entityLink;
191     }
192
193     ///////////////////////////////////////////////////////////////////////////
194     // OVERRIDES
195     ///////////////////////////////////////////////////////////////////////////
196
197     /*
198      * (non-Javadoc)
199      *
200      * @see java.lang.Object#hashCode()
201      */
202     @Override
203     public int hashCode() {
204         return Objects.hash(this.requestId, this.timestamp, this.sourceName, this.eventType, this.validationEntityType,
205                 this.validationTopEntityType, this.entityLink);
206     }
207
208     /*
209      * (non-Javadoc)
210      *
211      * @see java.lang.Object#equals(java.lang.Object)
212      */
213     @Override
214     public boolean equals(Object obj) {
215         if (!(obj instanceof ChampEventHeader)) {
216             return false;
217         } else if (obj == this) {
218             return true;
219         }
220         ChampEventHeader rhs = (ChampEventHeader) obj;
221      // @formatter:off
222      return new EqualsBuilder()
223                   .append(requestId, rhs.requestId)
224                   .append(timestamp, rhs.timestamp)
225                   .append(sourceName, rhs.sourceName)
226                   .append(eventType, rhs.eventType)
227                   .append(validationEntityType, rhs.validationEntityType)
228                   .append(validationTopEntityType, rhs.validationTopEntityType)
229                   .append(entityLink, rhs.entityLink)
230                   .isEquals();
231      // @formatter:on
232     }
233
234     /*
235      * (non-Javadoc)
236      *
237      * @see java.lang.Object#toString()
238      */
239     @Override
240     public String toString() {
241         return this.toJson();
242     }
243 }