b7c4e0d78f0f46e36181bc5a3785a273c0d48d7a
[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 <i>audit</i> data. This includes only data known to an application, and not otherwise available
21  * to the logging framework. As opposed, for example, to local runtime, host address, etc.
22  *
23  * @author KATYR, evitaliy
24  * @since February 15, 2018
25  */
26 public class AuditData {
27
28     // don't inherit from MetricsData because it has a very different meaning
29
30     private final long startTime;
31     private final long endTime;
32     private final StatusCode statusCode;
33     private final String responseCode;
34     private final String responseDescription;
35     private final String clientIpAddress;
36
37     private AuditData(final AuditDataBuilder builder) {
38         this.startTime = builder.startTime;
39         this.endTime = builder.endTime;
40         this.statusCode = builder.statusCode;
41         this.responseCode = builder.responseCode;
42         this.responseDescription = builder.responseDescription;
43         this.clientIpAddress = builder.clientIpAddress;
44     }
45
46     /**
47      * Begin timestamp of an API invocation.
48      *
49      * @return timestamp
50      */
51     public long getStartTime() {
52         return startTime;
53     }
54
55     /**
56      * End timestamp of an API invocation.
57      *
58      * @return timestamp
59      */
60     public long getEndTime() {
61         return endTime;
62     }
63
64     /**
65      * Result status of an API invocation.
66      *
67      * @return protocol and application agnostic status code
68      */
69     public StatusCode getStatusCode() {
70         return statusCode;
71     }
72
73     /**
74      * Application/protocol specific response status of an API invocation.
75      *
76      * @return response code
77      */
78     public String getResponseCode() {
79         return responseCode;
80     }
81
82     /**
83      * Application/protocol specific response in a human-friendly way.
84      *
85      * @return human-friendly response description
86      */
87     public String getResponseDescription() {
88         return responseDescription;
89     }
90
91     /**
92      * IP address of the invoking client when available.
93      *
94      * @return IP address
95      */
96     public String getClientIpAddress() {
97         return clientIpAddress;
98     }
99
100     @Override
101     public String toString() {
102         return "AuditData{startTime=" + startTime + ", endTime=" + endTime + ", statusCode=" + statusCode
103                 + ", responseCode=" + responseCode + ", responseDescription=" + responseDescription
104                 + ", clientIpAddress=" + clientIpAddress + '}';
105     }
106
107     public static AuditDataBuilder builder() {
108         return new AuditDataBuilder();
109     }
110
111     /**
112      * Fluent API for building audit data.
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 }