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 <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.
23 * @author KATYR, evitaliy
24 * @since February 15, 2018
26 public class AuditData {
28 // don't inherit from MetricsData because it has a very different meaning
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;
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;
47 * Begin timestamp of an API invocation.
51 public long getStartTime() {
56 * End timestamp of an API invocation.
60 public long getEndTime() {
65 * Result status of an API invocation.
67 * @return protocol and application agnostic status code
69 public StatusCode getStatusCode() {
74 * Application/protocol specific response status of an API invocation.
76 * @return response code
78 public String getResponseCode() {
83 * Application/protocol specific response in a human-friendly way.
85 * @return human-friendly response description
87 public String getResponseDescription() {
88 return responseDescription;
92 * IP address of the invoking client when available.
96 public String getClientIpAddress() {
97 return clientIpAddress;
101 public String toString() {
102 return "AuditData{startTime=" + startTime + ", endTime=" + endTime + ", statusCode=" + statusCode
103 + ", responseCode=" + responseCode + ", responseDescription=" + responseDescription
104 + ", clientIpAddress=" + clientIpAddress + '}';
107 public static AuditDataBuilder builder() {
108 return new AuditDataBuilder();
112 * Fluent API for building audit data.
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);