ecf795b2d5d286fd5aea92902e2136f913769c9f
[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 add new fields without
31     // affecting client code. Also, the builder can be implemented differently if needed, without affecting client code.
32     // For instance, it may delegate the population and instantiation to a service provider.
33
34     private final long startTime;
35     private final long endTime;
36     private final StatusCode statusCode;
37     private final String responseCode;
38     private final String responseDescription;
39     private final String clientIpAddress;
40
41     private AuditData(final AuditDataBuilder builder) {
42         this.startTime = builder.startTime;
43         this.endTime = builder.endTime;
44         this.statusCode = builder.statusCode;
45         this.responseCode = builder.responseCode;
46         this.responseDescription = builder.responseDescription;
47         this.clientIpAddress = builder.clientIpAddress;
48     }
49
50     /**
51      * Begin timestamp of an API invocation.
52      *
53      * @return timestamp
54      */
55     public long getStartTime() {
56         return startTime;
57     }
58
59     /**
60      * End timestamp of an API invocation.
61      *
62      * @return timestamp
63      */
64     public long getEndTime() {
65         return endTime;
66     }
67
68     /**
69      * Result status of an API invocation.
70      *
71      * @return protocol and application agnostic status code
72      */
73     public StatusCode getStatusCode() {
74         return statusCode;
75     }
76
77     /**
78      * Application/protocol specific response status of an API invocation.
79      *
80      * @return response code
81      */
82     public String getResponseCode() {
83         return responseCode;
84     }
85
86     /**
87      * Application/protocol specific response in a human-friendly way.
88      *
89      * @return human-friendly response description
90      */
91     public String getResponseDescription() {
92         return responseDescription;
93     }
94
95     /**
96      * IP address of the invoking client when available.
97      *
98      * @return IP address
99      */
100     public String getClientIpAddress() {
101         return clientIpAddress;
102     }
103
104     @Override
105     public String toString() {
106         return "AuditData{startTime=" + startTime + ", endTime=" + endTime + ", statusCode=" + statusCode
107                 + ", responseCode=" + responseCode + ", responseDescription=" + responseDescription
108                 + ", clientIpAddress=" + clientIpAddress + '}';
109     }
110
111     public static AuditDataBuilder builder() {
112         return new AuditDataBuilder();
113     }
114
115     /**
116      * Fluent API for building audit data.
117      */
118     public static class AuditDataBuilder {
119
120         private long startTime;
121         private long endTime;
122         private StatusCode statusCode;
123         private String responseCode;
124         private String responseDescription;
125         private String clientIpAddress;
126
127         AuditDataBuilder() { /* package-private default constructor to hide the public one */ }
128
129         /**
130          * Begin timestamp of an activity being audited.
131          *
132          * @param startTime local timestamp, usually received from {@link System#currentTimeMillis()}
133          * @return this builder for fluent API
134          */
135         public AuditDataBuilder startTime(final long startTime) {
136             this.startTime = startTime;
137             return this;
138         }
139
140         /**
141          * End timestamp of an activity being audited.
142          *
143          * @param endTime local timestamp, usually received from {@link System#currentTimeMillis()}
144          * @return this builder for fluent API
145          */
146         public AuditDataBuilder endTime(final long endTime) {
147             this.endTime = endTime;
148             return this;
149         }
150
151         /**
152          * Indicate whether an invocation was successful. It is up the the application to decide if a particular result
153          * must be treated as a success or a failure.
154          *
155          * @param statusCode invocation status success/failure
156          * @return this builder for fluent API
157          */
158         public AuditDataBuilder statusCode(final StatusCode statusCode) {
159             this.statusCode = statusCode;
160             return this;
161         }
162
163         /**
164          * Application/protocol specific response code. For a Web API, it is likely a standard HTTP response code.
165          *
166          * @param responseCode response code that depends on application and invocation protocol
167          * @return this builder for fluent API
168          */
169         public AuditDataBuilder responseCode(final String responseCode) {
170             this.responseCode = responseCode;
171             return this;
172         }
173
174         /**
175          * Response description that explains {@link #responseCode(String)} in a human-friendly way. For a Web API, it
176          * is likely to be a standard HTTP response phrase.
177          *
178          * @param responseDescription human-friendly response description
179          * @return this builder for fluent API
180          */
181         public AuditDataBuilder responseDescription(final String responseDescription) {
182             this.responseDescription = responseDescription;
183             return this;
184         }
185
186         /**
187          * IP address of an invoking client.
188          *
189          * @param clientIpAddress IP address
190          * @return this builder for fluent API
191          */
192         public AuditDataBuilder clientIpAddress(final String clientIpAddress) {
193             this.clientIpAddress = clientIpAddress;
194             return this;
195         }
196
197         /**
198          * Create an instance of {@link AuditData}.
199          *
200          * @return a populated instance of audit data
201          */
202         public AuditData build() {
203             return new AuditData(this);
204         }
205     }
206 }