Migrate to Spring Boot
[aai/gizmo.git] / src / main / java / org / onap / crud / event / envelope / GraphEventHeader.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.crud.event.envelope;
22
23 import java.time.Instant;
24 import java.time.ZoneOffset;
25 import java.time.format.DateTimeFormatter;
26 import java.util.Objects;
27 import java.util.UUID;
28 import org.apache.commons.lang3.builder.EqualsBuilder;
29 import com.google.gson.Gson;
30 import com.google.gson.GsonBuilder;
31 import com.google.gson.annotations.SerializedName;
32
33 public class GraphEventHeader {
34
35     @SerializedName("request-id")
36     private String requestId;
37
38     private String timestamp;
39
40     @SerializedName("source-name")
41     private String sourceName;
42
43     @SerializedName("event-type")
44     private String eventType;
45
46     @SerializedName("validation-entity-type")
47     private String validationEntityType;
48
49     @SerializedName("validation-top-entity-type")
50     private String validationTopEntityType;
51
52     @SerializedName("entity-link")
53     private String entityLink;
54
55     private static final String MICROSERVICE_NAME = "GIZMO";
56     private static final String EVENT_TYPE_PENDING_UPDATE = "pending-update";
57     private static final Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
58
59     public static class Builder {
60
61         private String requestId;
62         private String validationEntityType;
63         private String validationTopEntityType;
64         private String entityLink;
65
66         public Builder requestId(String val) {
67             requestId = val;
68             return this;
69         }
70
71         public Builder validationEntityType(String val) {
72             validationEntityType = val;
73             return this;
74         }
75
76         public Builder validationTopEntityType(String val) {
77             validationTopEntityType = val;
78             return this;
79         }
80
81         public Builder entityLink(String val) {
82             entityLink = val;
83             return this;
84         }
85
86         public GraphEventHeader build() {
87             return new GraphEventHeader(this);
88         }
89     }
90
91     private GraphEventHeader(Builder builder) {
92         requestId = builder.requestId != null ? builder.requestId : UUID.randomUUID().toString();
93         timestamp = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmssX").withZone(ZoneOffset.UTC).format(Instant.now());
94         sourceName = MICROSERVICE_NAME;
95         eventType = EVENT_TYPE_PENDING_UPDATE;
96
97         validationEntityType = builder.validationEntityType;
98         validationTopEntityType = builder.validationTopEntityType;
99         entityLink = builder.entityLink;
100     }
101
102     /**
103      * Serializes this object into a JSON string representation.
104      *
105      * @return a JSON format string representation of this object.
106      */
107     public String toJson() {
108         return gson.toJson(this);
109     }
110
111     ///////////////////////////////////////////////////////////////////////////
112     // GETTERS
113     ///////////////////////////////////////////////////////////////////////////
114
115     public String getRequestId() {
116         return requestId;
117     }
118
119     public String getTimestamp() {
120         return timestamp;
121     }
122
123     public String getSourceName() {
124         return sourceName;
125     }
126
127     public String getEventType() {
128         return eventType;
129     }
130
131     public String getValidationEntityType() {
132         return validationEntityType;
133     }
134
135     public String getValidationTopEntityType() {
136         return validationTopEntityType;
137     }
138
139     public String getEntityLink() {
140         return entityLink;
141     }
142
143     ///////////////////////////////////////////////////////////////////////////
144     // OVERRIDES
145     ///////////////////////////////////////////////////////////////////////////
146
147     /*
148      * (non-Javadoc)
149      *
150      * @see java.lang.Object#hashCode()
151      */
152     @Override
153     public int hashCode() {
154         return Objects.hash(this.requestId, this.timestamp, this.sourceName, this.eventType, this.validationEntityType,
155                 this.validationTopEntityType, this.entityLink);
156     }
157
158     /*
159      * (non-Javadoc)
160      *
161      * @see java.lang.Object#equals(java.lang.Object)
162      */
163     @Override
164     public boolean equals(Object obj) {
165         if (!(obj instanceof GraphEventHeader)) {
166             return false;
167         } else if (obj == this) {
168             return true;
169         }
170         GraphEventHeader rhs = (GraphEventHeader) obj;
171      // @formatter:off
172      return new EqualsBuilder()
173                   .append(requestId, rhs.requestId)
174                   .append(timestamp, rhs.timestamp)
175                   .append(sourceName, rhs.sourceName)
176                   .append(eventType, rhs.sourceName)
177                   .append(validationEntityType, rhs.validationEntityType)
178                   .append(validationTopEntityType, rhs.validationTopEntityType)
179                   .append(entityLink, rhs.entityLink)
180                   .isEquals();
181      // @formatter:on
182     }
183
184     /*
185      * (non-Javadoc)
186      *
187      * @see java.lang.Object#toString()
188      */
189     @Override
190     public String toString() {
191         return this.toJson();
192     }
193
194 }