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 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.
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;
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;
51 * Begin timestamp of an API invocation.
55 public long getStartTime() {
60 * End timestamp of an API invocation.
64 public long getEndTime() {
69 * Result status of an API invocation.
71 * @return protocol and application agnostic status code
73 public StatusCode getStatusCode() {
78 * Application/protocol specific response status of an API invocation.
80 * @return response code
82 public String getResponseCode() {
87 * Application/protocol specific response in a human-friendly way.
89 * @return human-friendly response description
91 public String getResponseDescription() {
92 return responseDescription;
96 * IP address of the invoking client when available.
100 public String getClientIpAddress() {
101 return clientIpAddress;
105 public String toString() {
106 return "AuditData{startTime=" + startTime + ", endTime=" + endTime + ", statusCode=" + statusCode
107 + ", responseCode=" + responseCode + ", responseDescription=" + responseDescription
108 + ", clientIpAddress=" + clientIpAddress + '}';
111 public static AuditDataBuilder builder() {
112 return new AuditDataBuilder();
116 * Fluent API for building audit data.
118 public static class AuditDataBuilder {
120 private long startTime;
121 private long endTime;
122 private StatusCode statusCode;
123 private String responseCode;
124 private String responseDescription;
125 private String clientIpAddress;
127 AuditDataBuilder() { /* package-private default constructor to hide the public one */ }
130 * Begin timestamp of an activity being audited.
132 * @param startTime local timestamp, usually received from {@link System#currentTimeMillis()}
133 * @return this builder for fluent API
135 public AuditDataBuilder startTime(final long startTime) {
136 this.startTime = startTime;
141 * End timestamp of an activity being audited.
143 * @param endTime local timestamp, usually received from {@link System#currentTimeMillis()}
144 * @return this builder for fluent API
146 public AuditDataBuilder endTime(final long endTime) {
147 this.endTime = endTime;
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.
155 * @param statusCode invocation status success/failure
156 * @return this builder for fluent API
158 public AuditDataBuilder statusCode(final StatusCode statusCode) {
159 this.statusCode = statusCode;
164 * Application/protocol specific response code. For a Web API, it is likely a standard HTTP response code.
166 * @param responseCode response code that depends on application and invocation protocol
167 * @return this builder for fluent API
169 public AuditDataBuilder responseCode(final String responseCode) {
170 this.responseCode = responseCode;
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.
178 * @param responseDescription human-friendly response description
179 * @return this builder for fluent API
181 public AuditDataBuilder responseDescription(final String responseDescription) {
182 this.responseDescription = responseDescription;
187 * IP address of an invoking client.
189 * @param clientIpAddress IP address
190 * @return this builder for fluent API
192 public AuditDataBuilder clientIpAddress(final String clientIpAddress) {
193 this.clientIpAddress = clientIpAddress;
198 * Create an instance of {@link AuditData}.
200 * @return a populated instance of audit data
202 public AuditData build() {
203 return new AuditData(this);