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>metrics</i> data. This includes only data known to an application, and not otherwise available
24 * to the logging framework.
30 public class MetricsData {
32 // don't inherit from AuditData 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;
40 private final String targetVirtualEntity;
41 private final String targetEntity;
43 private MetricsData(final MetricsDataBuilder builder) {
44 this.startTime = builder.startTime;
45 this.endTime = builder.endTime;
46 this.statusCode = builder.statusCode;
47 this.responseCode = builder.responseCode;
48 this.responseDescription = builder.responseDescription;
49 this.clientIpAddress = builder.clientIpAddress;
50 this.targetEntity = builder.targetEntity;
51 this.targetVirtualEntity = builder.targetVirtualEntity;
55 public String toString() {
56 return "AuditData{startTime=" + startTime + ", endTime=" + endTime + ", statusCode=" + statusCode
57 + ", responseCode=" + responseCode + ", responseDescription=" + responseDescription
58 + ", clientIpAddress=" + clientIpAddress + '}';
61 public static MetricsDataBuilder builder() {
62 return new MetricsDataBuilder();
66 * Fluent API for building metrics data.
68 public static class MetricsDataBuilder {
70 private long startTime;
72 private ResponseStatus statusCode;
73 private String responseCode;
74 private String responseDescription;
75 private String clientIpAddress;
76 private String targetEntity;
77 private String targetVirtualEntity;
79 MetricsDataBuilder() { /* package-private default constructor to hide the public one */ }
82 * Begin timestamp of an activity being audited.
84 * @param startTime local timestamp, usually received from {@link System#currentTimeMillis()}
85 * @return this builder for fluent API
87 public MetricsDataBuilder startTime(final long startTime) {
88 this.startTime = startTime;
93 * End timestamp of an activity being audited.
95 * @param endTime local timestamp, usually received from {@link System#currentTimeMillis()}
96 * @return this builder for fluent API
98 public MetricsDataBuilder endTime(final long endTime) {
99 this.endTime = endTime;
104 * Indicate whether an invocation was successful. It is up the the application to decide if a particular result
105 * must be treated as a success or a failure.
107 * @param statusCode invocation status success/failure
108 * @return this builder for fluent API
110 public MetricsDataBuilder statusCode(final ResponseStatus statusCode) {
111 this.statusCode = statusCode;
116 * Application/protocol specific response code. For a Web API, it is likely a standard HTTP response code.
118 * @param responseCode response code that depends on application and invocation protocol
119 * @return this builder for fluent API
121 public MetricsDataBuilder responseCode(final String responseCode) {
122 this.responseCode = responseCode;
127 * Response description that explains {@link #responseCode(String)} in a human-friendly way. For a Web API, it
128 * is likely to be a standard HTTP response phrase.
130 * @param responseDescription human-friendly response description
131 * @return this builder for fluent API
133 public MetricsDataBuilder responseDescription(final String responseDescription) {
134 this.responseDescription = responseDescription;
139 * IP address of an invoking client.
141 * @param clientIpAddress IP address
142 * @return this builder for fluent API
144 public MetricsDataBuilder clientIpAddress(final String clientIpAddress) {
145 this.clientIpAddress = clientIpAddress;
150 * External entity at which the operation is invoked.
152 * @param targetEntity external entity identifier
153 * @return this builder for fluent API
155 public MetricsDataBuilder targetEntity(String targetEntity) {
156 this.targetEntity = targetEntity;
161 * Name of the API or operation activities invoked at the external entity.
163 * @param targetVirtualEntity invoked external API
164 * @return this builder for fluent API
166 public MetricsDataBuilder targetVirtualEntity(String targetVirtualEntity) {
167 this.targetVirtualEntity = targetVirtualEntity;
172 * Create an instance of {@link MetricsData}.
174 * @return a populated instance of audit data
176 public MetricsData build() {
177 return new MetricsData(this);