Update published event to include header and body
[aai/gizmo.git] / src / main / java / org / onap / crud / event / envelope / GraphEventHeader.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Gizmo
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  *
22  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23  */
24 package org.onap.crud.event.envelope;
25
26 import java.time.Instant;
27 import java.time.ZoneOffset;
28 import java.time.format.DateTimeFormatter;
29 import java.util.Objects;
30 import java.util.UUID;
31 import org.apache.commons.lang3.builder.EqualsBuilder;
32 import com.google.gson.Gson;
33 import com.google.gson.GsonBuilder;
34 import com.google.gson.annotations.SerializedName;
35
36 public class GraphEventHeader {
37
38     @SerializedName("request-id")
39     private String requestId;
40
41     private String timestamp;
42
43     @SerializedName("source-name")
44     private String sourceName;
45
46     @SerializedName("event-type")
47     private String eventType;
48
49     @SerializedName("validation-entity-type")
50     private String validationEntityType;
51
52     @SerializedName("validation-top-entity-type")
53     private String validationTopEntityType;
54
55     @SerializedName("entity-link")
56     private String entityLink;
57
58     private static final String MICROSERVICE_NAME = "GIZMO";
59     private static final String EVENT_TYPE_PENDING_UPDATE = "pending-update";
60     private static final Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
61
62     public static class Builder {
63
64         private String requestId;
65         private String validationEntityType;
66         private String validationTopEntityType;
67         private String entityLink;
68
69         public Builder requestId(String val) {
70             requestId = val;
71             return this;
72         }
73
74         public Builder validationEntityType(String val) {
75             validationEntityType = val;
76             return this;
77         }
78
79         public Builder validationTopEntityType(String val) {
80             validationTopEntityType = val;
81             return this;
82         }
83
84         public Builder entityLink(String val) {
85             entityLink = val;
86             return this;
87         }
88
89         public GraphEventHeader build() {
90             return new GraphEventHeader(this);
91         }
92     }
93
94     private GraphEventHeader(Builder builder) {
95         requestId = builder.requestId != null ? builder.requestId : UUID.randomUUID().toString();
96         timestamp = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmssX").withZone(ZoneOffset.UTC).format(Instant.now());
97         sourceName = MICROSERVICE_NAME;
98         eventType = EVENT_TYPE_PENDING_UPDATE;
99
100         validationEntityType = builder.validationEntityType;
101         validationTopEntityType = builder.validationTopEntityType;
102         entityLink = builder.entityLink;
103     }
104
105     /**
106      * Serializes this object into a JSON string representation.
107      *
108      * @return a JSON format string representation of this object.
109      */
110     public String toJson() {
111         return gson.toJson(this);
112     }
113
114     ///////////////////////////////////////////////////////////////////////////
115     // GETTERS
116     ///////////////////////////////////////////////////////////////////////////
117
118     public String getRequestId() {
119         return requestId;
120     }
121
122     public String getTimestamp() {
123         return timestamp;
124     }
125
126     public String getSourceName() {
127         return sourceName;
128     }
129
130     public String getEventType() {
131         return eventType;
132     }
133
134     public String getValidationEntityType() {
135         return validationEntityType;
136     }
137
138     public String getValidationTopEntityType() {
139         return validationTopEntityType;
140     }
141
142     public String getEntityLink() {
143         return entityLink;
144     }
145
146     ///////////////////////////////////////////////////////////////////////////
147     // OVERRIDES
148     ///////////////////////////////////////////////////////////////////////////
149
150     /*
151      * (non-Javadoc)
152      *
153      * @see java.lang.Object#hashCode()
154      */
155     @Override
156     public int hashCode() {
157         return Objects.hash(this.requestId, this.timestamp, this.sourceName, this.eventType, this.validationEntityType,
158                 this.validationTopEntityType, this.entityLink);
159     }
160
161     /*
162      * (non-Javadoc)
163      *
164      * @see java.lang.Object#equals(java.lang.Object)
165      */
166     @Override
167     public boolean equals(Object obj) {
168         if (!(obj instanceof GraphEventHeader)) {
169             return false;
170         } else if (obj == this) {
171             return true;
172         }
173         GraphEventHeader rhs = (GraphEventHeader) obj;
174      // @formatter:off
175      return new EqualsBuilder()
176                   .append(requestId, rhs.requestId)
177                   .append(timestamp, rhs.timestamp)
178                   .append(sourceName, rhs.sourceName)
179                   .append(eventType, rhs.sourceName)
180                   .append(validationEntityType, rhs.validationEntityType)
181                   .append(validationTopEntityType, rhs.validationTopEntityType)
182                   .append(entityLink, rhs.entityLink)
183                   .isEquals();
184      // @formatter:on
185     }
186
187     /*
188      * (non-Javadoc)
189      *
190      * @see java.lang.Object#toString()
191      */
192     @Override
193     public String toString() {
194         return this.toJson();
195     }
196
197 }