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;
19 import org.onap.logging.ref.slf4j.ONAPLogConstants.ResponseStatus;
23 * Builder to populate <i>audit</i> data. This includes only data known to an application, and not otherwise available
24 * to the logging framework. As opposed, for example, to local runtime, host address, etc.
26 * @author KATYR, evitaliy
27 * @since February 15, 2018
30 public class AuditData {
32 // don't inherit from MetricsData because it has a very different meaning
34 private final long startTime;
35 private final long endTime;
36 private final ResponseStatus 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 public String toString() {
52 return "AuditData{startTime=" + startTime + ", endTime=" + endTime + ", statusCode=" + statusCode
53 + ", responseCode=" + responseCode + ", responseDescription=" + responseDescription
54 + ", clientIpAddress=" + clientIpAddress + '}';
57 public static AuditDataBuilder builder() {
58 return new AuditDataBuilder();
62 * Fluent API for building audit data.
64 public static class AuditDataBuilder {
66 private long startTime;
68 private ResponseStatus statusCode;
69 private String responseCode;
70 private String responseDescription;
71 private String clientIpAddress;
73 AuditDataBuilder() { /* package-private default constructor to hide the public one */ }
76 * Begin timestamp of an activity being audited.
78 * @param startTime local timestamp, usually received from {@link System#currentTimeMillis()}
79 * @return this builder for fluent API
81 public AuditDataBuilder startTime(final long startTime) {
82 this.startTime = startTime;
87 * End timestamp of an activity being audited.
89 * @param endTime local timestamp, usually received from {@link System#currentTimeMillis()}
90 * @return this builder for fluent API
92 public AuditDataBuilder endTime(final long endTime) {
93 this.endTime = endTime;
98 * Indicate whether an invocation was successful. It is up the the application to decide if a particular result
99 * must be treated as a success or a failure.
101 * @param statusCode invocation status success/failure
102 * @return this builder for fluent API
104 public AuditDataBuilder statusCode(final ResponseStatus statusCode) {
105 this.statusCode = statusCode;
110 * Application/protocol specific response code. For a Web API, it is likely a standard HTTP response code.
112 * @param responseCode response code that depends on application and invocation protocol
113 * @return this builder for fluent API
115 public AuditDataBuilder responseCode(final String responseCode) {
116 this.responseCode = responseCode;
121 * Response description that explains {@link #responseCode(String)} in a human-friendly way. For a Web API, it
122 * is likely to be a standard HTTP response phrase.
124 * @param responseDescription human-friendly response description
125 * @return this builder for fluent API
127 public AuditDataBuilder responseDescription(final String responseDescription) {
128 this.responseDescription = responseDescription;
133 * IP address of an invoking client.
135 * @param clientIpAddress IP address
136 * @return this builder for fluent API
138 public AuditDataBuilder clientIpAddress(final String clientIpAddress) {
139 this.clientIpAddress = clientIpAddress;
144 * Create an instance of {@link AuditData}.
146 * @return a populated instance of audit data
148 public AuditData build() {
149 return new AuditData(this);