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