2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.logging.api;
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.
25 * @author KATYR, evitaliy
26 * @since February 15, 2018
28 public class AuditData {
30 // A concrete implementation interface was chosen over an interface to enable to gracefully
31 // add new fields without affecting client code
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;
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;
50 * Begin timestamp of an API invocation
54 public long getStartTime() {
59 * End timestamp of an API invocation
63 public long getEndTime() {
68 * Result status of an API invocation
70 * @return protocol and application agnostic status code
72 public StatusCode getStatusCode() {
77 * Application/protocol specific response status of an API invocation
79 * @return response code
81 public String getResponseCode() {
86 * Application/protocol specific response in a human-friendly way
88 * @return human-friendly response description
90 public String getResponseDescription() {
91 return responseDescription;
95 * IP address of the invoking client when available
99 public String getClientIpAddress() {
100 return clientIpAddress;
104 public String toString() {
105 return "AuditData{startTime=" + startTime + ", endTime=" + endTime + ", statusCode=" + statusCode +
106 ", responseCode='" + responseCode + ", responseDescription=" + responseDescription + ", clientIpAddress="
107 + clientIpAddress + '}';
110 public static AuditDataBuilder builder() {
111 return new AuditDataBuilder();
114 public static class AuditDataBuilder {
116 private long startTime;
117 private long endTime;
118 private StatusCode statusCode;
119 private String responseCode;
120 private String responseDescription;
121 private String clientIpAddress;
123 AuditDataBuilder() { /* package-private default constructor to hide the public one */ }
126 * Begin timestamp of an activity being audited
128 * @param startTime local timestamp, usually received from {@link System#currentTimeMillis()}
129 * @return this builder for fluent API
131 public AuditDataBuilder startTime(final long startTime) {
132 this.startTime = startTime;
137 * End timestamp of an activity being audited
139 * @param endTime local timestamp, usually received from {@link System#currentTimeMillis()}
140 * @return this builder for fluent API
142 public AuditDataBuilder endTime(final long endTime) {
143 this.endTime = endTime;
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.
151 * @param statusCode invocation status success/failure
152 * @return this builder for fluent API
154 public AuditDataBuilder statusCode(final StatusCode statusCode) {
155 this.statusCode = statusCode;
160 * Application/protocol specific response code. For a Web API, it is likely a standard HTTP response code.
162 * @param responseCode response code that depends on application and invocation protocol
163 * @return this builder for fluent API
165 public AuditDataBuilder responseCode(final String responseCode) {
166 this.responseCode = responseCode;
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.
174 * @param responseDescription human-friendly response description
175 * @return this builder for fluent API
177 public AuditDataBuilder responseDescription(final String responseDescription) {
178 this.responseDescription = responseDescription;
183 * IP address of an invoking client.
185 * @param clientIpAddress IP address
186 * @return this builder for fluent API
188 public AuditDataBuilder clientIpAddress(final String clientIpAddress) {
189 this.clientIpAddress = clientIpAddress;
194 * Create an instance of {@link AuditData}
196 * @return a populated instance of audit data
198 public AuditData build() {
199 return new AuditData(this);