2fef96d2bb0b9c1216027022dedffde80f357077
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.logging.api;
18
19 import org.onap.logging.ref.slf4j.ONAPLogConstants.ResponseStatus;
20 import lombok.Getter;
21
22 /**
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.
25  *
26  * @author evitaliy
27  * @since 26 Mar 2018
28  */
29 @Getter
30 public class MetricsData {
31
32     // don't inherit from AuditData because it has a very different meaning
33
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;
42
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;
52     }
53
54     @Override
55     public String toString() {
56         return "AuditData{startTime=" + startTime + ", endTime=" + endTime + ", statusCode=" + statusCode
57                 + ", responseCode=" + responseCode + ", responseDescription=" + responseDescription
58                 + ", clientIpAddress=" + clientIpAddress + '}';
59     }
60
61     public static MetricsDataBuilder builder() {
62         return new MetricsDataBuilder();
63     }
64
65     /**
66      * Fluent API for building metrics data.
67      */
68     public static class MetricsDataBuilder {
69
70         private long startTime;
71         private long endTime;
72         private ResponseStatus statusCode;
73         private String responseCode;
74         private String responseDescription;
75         private String clientIpAddress;
76         private String targetEntity;
77         private String targetVirtualEntity;
78
79         MetricsDataBuilder() { /* package-private default constructor to hide the public one */ }
80
81         /**
82          * Begin timestamp of an activity being audited.
83          *
84          * @param startTime local timestamp, usually received from {@link System#currentTimeMillis()}
85          * @return this builder for fluent API
86          */
87         public MetricsDataBuilder startTime(final long startTime) {
88             this.startTime = startTime;
89             return this;
90         }
91
92         /**
93          * End timestamp of an activity being audited.
94          *
95          * @param endTime local timestamp, usually received from {@link System#currentTimeMillis()}
96          * @return this builder for fluent API
97          */
98         public MetricsDataBuilder endTime(final long endTime) {
99             this.endTime = endTime;
100             return this;
101         }
102
103         /**
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.
106          *
107          * @param statusCode invocation status success/failure
108          * @return this builder for fluent API
109          */
110         public MetricsDataBuilder statusCode(final ResponseStatus statusCode) {
111             this.statusCode = statusCode;
112             return this;
113         }
114
115         /**
116          * Application/protocol specific response code. For a Web API, it is likely a standard HTTP response code.
117          *
118          * @param responseCode response code that depends on application and invocation protocol
119          * @return this builder for fluent API
120          */
121         public MetricsDataBuilder responseCode(final String responseCode) {
122             this.responseCode = responseCode;
123             return this;
124         }
125
126         /**
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.
129          *
130          * @param responseDescription human-friendly response description
131          * @return this builder for fluent API
132          */
133         public MetricsDataBuilder responseDescription(final String responseDescription) {
134             this.responseDescription = responseDescription;
135             return this;
136         }
137
138         /**
139          * IP address of an invoking client.
140          *
141          * @param clientIpAddress IP address
142          * @return this builder for fluent API
143          */
144         public MetricsDataBuilder clientIpAddress(final String clientIpAddress) {
145             this.clientIpAddress = clientIpAddress;
146             return this;
147         }
148
149         /**
150          * External entity at which the operation is invoked.
151          *
152          * @param targetEntity external entity identifier
153          * @return this builder for fluent API
154          */
155         public MetricsDataBuilder targetEntity(String targetEntity) {
156             this.targetEntity = targetEntity;
157             return this;
158         }
159
160         /**
161          * Name of the API or operation activities invoked at the external entity.
162          *
163          * @param targetVirtualEntity invoked external API
164          * @return this builder for fluent API
165          */
166         public MetricsDataBuilder targetVirtualEntity(String targetVirtualEntity) {
167             this.targetVirtualEntity = targetVirtualEntity;
168             return this;
169         }
170
171         /**
172          * Create an instance of {@link MetricsData}.
173          *
174          * @return a populated instance of audit data
175          */
176         public MetricsData build() {
177             return new MetricsData(this);
178         }
179     }
180 }