fdef45a57f7ca286ce3ba88ecd661978e2191906
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.logging.api;
18
19 /**
20  * Builder to populate audit data for logging according to
21  * <a href="https://wiki.onap.org/download/attachments/1015849/ONAP%20application%20logging%20guidelines.pdf?api=v2>
22  * ONAP application logging guidelines</a>. This includes only data known to an application, and not otherwise available
23  * to the logging framework.
24  *
25  * @author KATYR, evitaliy
26  * @since February 15, 2018
27  */
28 public class AuditData {
29
30     // A concrete implementation interface was chosen over an interface to enable to gracefully
31     // add new fields without affecting client code
32
33     private final long startTime;
34     private final long endTime;
35     private final StatusCode statusCode;
36     private final String responseCode;
37     private final String responseDescription;
38     private final String clientIpAddress;
39
40     AuditData(final AuditDataBuilder builder) {
41         this.startTime = builder.startTime;
42         this.endTime = builder.endTime;
43         this.statusCode = builder.statusCode;
44         this.responseCode = builder.responseCode;
45         this.responseDescription = builder.responseDescription;
46         this.clientIpAddress = builder.clientIpAddress;
47     }
48
49     /**
50      * Begin timestamp of an API invocation
51      *
52      * @return timestamp
53      */
54     public long getStartTime() {
55         return startTime;
56     }
57
58     /**
59      * End timestamp of an API invocation
60      *
61      * @return timestamp
62      */
63     public long getEndTime() {
64         return endTime;
65     }
66
67     /**
68      * Result status of an API invocation
69      *
70      * @return protocol and application agnostic status code
71      */
72     public StatusCode getStatusCode() {
73         return statusCode;
74     }
75
76     /**
77      * Application/protocol specific response status of an API invocation
78      *
79      * @return response code
80      */
81     public String getResponseCode() {
82         return responseCode;
83     }
84
85     /**
86      * Application/protocol specific response in a human-friendly way
87      *
88      * @return human-friendly response description
89      */
90     public String getResponseDescription() {
91         return responseDescription;
92     }
93
94     /**
95      * IP address of the invoking client when available
96      *
97      * @return IP address
98      */
99     public String getClientIpAddress() {
100         return clientIpAddress;
101     }
102
103     @Override
104     public String toString() {
105         return "AuditData{startTime=" + startTime + ", endTime=" + endTime + ", statusCode=" + statusCode +
106             ", responseCode='" + responseCode + ", responseDescription=" + responseDescription + ", clientIpAddress="
107             + clientIpAddress + '}';
108     }
109
110     public static AuditDataBuilder builder() {
111         return new AuditDataBuilder();
112     }
113
114     public static class AuditDataBuilder {
115
116         private long startTime;
117         private long endTime;
118         private StatusCode statusCode;
119         private String responseCode;
120         private String responseDescription;
121         private String clientIpAddress;
122
123         AuditDataBuilder() { /* package-private default constructor to hide the public one */ }
124
125         /**
126          * Begin timestamp of an activity being audited
127          *
128          * @param startTime local timestamp, usually received from {@link System#currentTimeMillis()}
129          * @return this builder for fluent API
130          */
131         public AuditDataBuilder startTime(final long startTime) {
132             this.startTime = startTime;
133             return this;
134         }
135
136         /**
137          * End timestamp of an activity being audited
138          *
139          * @param endTime local timestamp, usually received from {@link System#currentTimeMillis()}
140          * @return this builder for fluent API
141          */
142         public AuditDataBuilder endTime(final long endTime) {
143             this.endTime = endTime;
144             return this;
145         }
146
147         /**
148          * Indicate whether an invocation was successful. It is up the the application to decide if a particular result
149          * must be treated as a success or a failure.
150          *
151          * @param statusCode invocation status success/failure
152          * @return this builder for fluent API
153          */
154         public AuditDataBuilder statusCode(final StatusCode statusCode) {
155             this.statusCode = statusCode;
156             return this;
157         }
158
159         /**
160          * Application/protocol specific response code. For a Web API, it is likely a standard HTTP response code.
161          *
162          * @param responseCode response code that depends on application and invocation protocol
163          * @return this builder for fluent API
164          */
165         public AuditDataBuilder responseCode(final String responseCode) {
166             this.responseCode = responseCode;
167             return this;
168         }
169
170         /**
171          * Response description that explains {@link #responseCode(String)} in a human-friendly way. For a Web API, it
172          * is likely to be a standard HTTP response phrase.
173          *
174          * @param responseDescription human-friendly response description
175          * @return this builder for fluent API
176          */
177         public AuditDataBuilder responseDescription(final String responseDescription) {
178             this.responseDescription = responseDescription;
179             return this;
180         }
181
182         /**
183          * IP address of an invoking client.
184          *
185          * @param clientIpAddress IP address
186          * @return this builder for fluent API
187          */
188         public AuditDataBuilder clientIpAddress(final String clientIpAddress) {
189             this.clientIpAddress = clientIpAddress;
190             return this;
191         }
192
193         /**
194          * Create an instance of {@link AuditData}
195          *
196          * @return a populated instance of audit data
197          */
198         public AuditData build() {
199             return new AuditData(this);
200         }
201     }
202 }