c44ab42efaabaa72abbd6cbf9dba2d74fd1a4899
[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 /**
20  * Builder to populate <i>metrics</i> data. This includes only data known to an application, and not otherwise available
21  * to the logging framework.
22  *
23  * @author evitaliy
24  * @since 26 Mar 2018
25  */
26 public class MetricsData {
27
28     // don't inherit from AuditData because it has a very different meaning
29
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;
36     private final String targetVirtualEntity;
37     private final String targetEntity;
38
39     private MetricsData(final MetricsDataBuilder builder) {
40         this.startTime = builder.startTime;
41         this.endTime = builder.endTime;
42         this.statusCode = builder.statusCode;
43         this.responseCode = builder.responseCode;
44         this.responseDescription = builder.responseDescription;
45         this.clientIpAddress = builder.clientIpAddress;
46         this.targetEntity = builder.targetEntity;
47         this.targetVirtualEntity = builder.targetVirtualEntity;
48     }
49
50     /**
51      * Begin timestamp of an API invocation.
52      *
53      * @return timestamp
54      */
55     public long getStartTime() {
56         return startTime;
57     }
58
59     /**
60      * End timestamp of an API invocation.
61      *
62      * @return timestamp
63      */
64     public long getEndTime() {
65         return endTime;
66     }
67
68     /**
69      * Result status of an API invocation.
70      *
71      * @return protocol and application agnostic status code
72      */
73     public StatusCode getStatusCode() {
74         return statusCode;
75     }
76
77     /**
78      * Application/protocol specific response status of an API invocation.
79      *
80      * @return response code
81      */
82     public String getResponseCode() {
83         return responseCode;
84     }
85
86     /**
87      * Application/protocol specific response in a human-friendly way.
88      *
89      * @return human-friendly response description
90      */
91     public String getResponseDescription() {
92         return responseDescription;
93     }
94
95     /**
96      * IP address of the invoking client when available.
97      *
98      * @return IP address
99      */
100     public String getClientIpAddress() {
101         return clientIpAddress;
102     }
103
104     /**
105      * External entity invoked by the local system.
106      *
107      * @return identifier of an external entity (system, component, sub-component)
108      */
109     public String getTargetEntity() {
110         return targetEntity;
111     }
112
113     /**
114      * External API invoked by the local system.
115      *
116      * @return name of an external API
117      */
118     public String getTargetVirtualEntity() {
119         return targetVirtualEntity;
120     }
121
122     @Override
123     public String toString() {
124         return "AuditData{startTime=" + startTime + ", endTime=" + endTime + ", statusCode=" + statusCode
125                 + ", responseCode=" + responseCode + ", responseDescription=" + responseDescription
126                 + ", clientIpAddress=" + clientIpAddress + '}';
127     }
128
129     public static MetricsDataBuilder builder() {
130         return new MetricsDataBuilder();
131     }
132
133     /**
134      * Fluent API for building metrics data.
135      */
136     public static class MetricsDataBuilder {
137
138         private long startTime;
139         private long endTime;
140         private StatusCode statusCode;
141         private String responseCode;
142         private String responseDescription;
143         private String clientIpAddress;
144         private String targetEntity;
145         private String targetVirtualEntity;
146
147         MetricsDataBuilder() { /* package-private default constructor to hide the public one */ }
148
149         /**
150          * Begin timestamp of an activity being audited.
151          *
152          * @param startTime local timestamp, usually received from {@link System#currentTimeMillis()}
153          * @return this builder for fluent API
154          */
155         public MetricsDataBuilder startTime(final long startTime) {
156             this.startTime = startTime;
157             return this;
158         }
159
160         /**
161          * End timestamp of an activity being audited.
162          *
163          * @param endTime local timestamp, usually received from {@link System#currentTimeMillis()}
164          * @return this builder for fluent API
165          */
166         public MetricsDataBuilder endTime(final long endTime) {
167             this.endTime = endTime;
168             return this;
169         }
170
171         /**
172          * Indicate whether an invocation was successful. It is up the the application to decide if a particular result
173          * must be treated as a success or a failure.
174          *
175          * @param statusCode invocation status success/failure
176          * @return this builder for fluent API
177          */
178         public MetricsDataBuilder statusCode(final StatusCode statusCode) {
179             this.statusCode = statusCode;
180             return this;
181         }
182
183         /**
184          * Application/protocol specific response code. For a Web API, it is likely a standard HTTP response code.
185          *
186          * @param responseCode response code that depends on application and invocation protocol
187          * @return this builder for fluent API
188          */
189         public MetricsDataBuilder responseCode(final String responseCode) {
190             this.responseCode = responseCode;
191             return this;
192         }
193
194         /**
195          * Response description that explains {@link #responseCode(String)} in a human-friendly way. For a Web API, it
196          * is likely to be a standard HTTP response phrase.
197          *
198          * @param responseDescription human-friendly response description
199          * @return this builder for fluent API
200          */
201         public MetricsDataBuilder responseDescription(final String responseDescription) {
202             this.responseDescription = responseDescription;
203             return this;
204         }
205
206         /**
207          * IP address of an invoking client.
208          *
209          * @param clientIpAddress IP address
210          * @return this builder for fluent API
211          */
212         public MetricsDataBuilder clientIpAddress(final String clientIpAddress) {
213             this.clientIpAddress = clientIpAddress;
214             return this;
215         }
216
217         /**
218          * External entity at which the operation is invoked.
219          *
220          * @param targetEntity external entity identifier
221          * @return this builder for fluent API
222          */
223         public MetricsDataBuilder targetEntity(String targetEntity) {
224             this.targetEntity = targetEntity;
225             return this;
226         }
227
228         /**
229          * Name of the API or operation activities invoked at the external entity.
230          *
231          * @param targetVirtualEntity invoked external API
232          * @return this builder for fluent API
233          */
234         public MetricsDataBuilder targetVirtualEntity(String targetVirtualEntity) {
235             this.targetVirtualEntity = targetVirtualEntity;
236             return this;
237         }
238
239         /**
240          * Create an instance of {@link MetricsData}.
241          *
242          * @return a populated instance of audit data
243          */
244         public MetricsData build() {
245             return new MetricsData(this);
246         }
247     }
248 }