090f680aa7dafe2ceeb560578b3f1468771798d9
[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>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.
25  *
26  * @author KATYR, evitaliy
27  * @since February 15, 2018
28  */
29 @Getter
30 public class AuditData {
31
32     // don't inherit from MetricsData 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
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;
48     }
49
50     @Override
51     public String toString() {
52         return "AuditData{startTime=" + startTime + ", endTime=" + endTime + ", statusCode=" + statusCode
53                 + ", responseCode=" + responseCode + ", responseDescription=" + responseDescription
54                 + ", clientIpAddress=" + clientIpAddress + '}';
55     }
56
57     public static AuditDataBuilder builder() {
58         return new AuditDataBuilder();
59     }
60
61     /**
62      * Fluent API for building audit data.
63      */
64     public static class AuditDataBuilder {
65
66         private long startTime;
67         private long endTime;
68         private ResponseStatus statusCode;
69         private String responseCode;
70         private String responseDescription;
71         private String clientIpAddress;
72
73         AuditDataBuilder() { /* package-private default constructor to hide the public one */ }
74
75         /**
76          * Begin timestamp of an activity being audited.
77          *
78          * @param startTime local timestamp, usually received from {@link System#currentTimeMillis()}
79          * @return this builder for fluent API
80          */
81         public AuditDataBuilder startTime(final long startTime) {
82             this.startTime = startTime;
83             return this;
84         }
85
86         /**
87          * End timestamp of an activity being audited.
88          *
89          * @param endTime local timestamp, usually received from {@link System#currentTimeMillis()}
90          * @return this builder for fluent API
91          */
92         public AuditDataBuilder endTime(final long endTime) {
93             this.endTime = endTime;
94             return this;
95         }
96
97         /**
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.
100          *
101          * @param statusCode invocation status success/failure
102          * @return this builder for fluent API
103          */
104         public AuditDataBuilder statusCode(final ResponseStatus statusCode) {
105             this.statusCode = statusCode;
106             return this;
107         }
108
109         /**
110          * Application/protocol specific response code. For a Web API, it is likely a standard HTTP response code.
111          *
112          * @param responseCode response code that depends on application and invocation protocol
113          * @return this builder for fluent API
114          */
115         public AuditDataBuilder responseCode(final String responseCode) {
116             this.responseCode = responseCode;
117             return this;
118         }
119
120         /**
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.
123          *
124          * @param responseDescription human-friendly response description
125          * @return this builder for fluent API
126          */
127         public AuditDataBuilder responseDescription(final String responseDescription) {
128             this.responseDescription = responseDescription;
129             return this;
130         }
131
132         /**
133          * IP address of an invoking client.
134          *
135          * @param clientIpAddress IP address
136          * @return this builder for fluent API
137          */
138         public AuditDataBuilder clientIpAddress(final String clientIpAddress) {
139             this.clientIpAddress = clientIpAddress;
140             return this;
141         }
142
143         /**
144          * Create an instance of {@link AuditData}.
145          *
146          * @return a populated instance of audit data
147          */
148         public AuditData build() {
149             return new AuditData(this);
150         }
151     }
152 }