43f0143774bba955974843e3513b01b6ea8755ee
[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 logging <i>context</i> data, i.e. data that should be available to any log writing event
21  * throughout an application. This includes only data known at some point to the application (e.g. at an API call),
22  * and not otherwise available to the logging framework (e.g. information about local runtime, machine, etc.).
23  *
24  * @author evitaliy
25  * @since Mar 22, 2018
26  */
27 public class ContextData {
28
29     private final String requestId;
30     private final String serviceName;
31     private final String partnerName;
32
33     private ContextData(final ContextDataBuilder builder) {
34         this.requestId = builder.requestId;
35         this.serviceName = builder.serviceName;
36         this.partnerName = builder.partnerName;
37     }
38
39     /**
40      * Uniques request ID received from a calling peer, or created.
41      *
42      * @return unique identifier of a request
43      */
44     public String getRequestId() {
45         return requestId;
46     }
47
48     /**
49      * Service, in the context of which logs will be written.
50      *
51      * @return a string that identifies an exposed service
52      */
53     public String getServiceName() {
54         return serviceName;
55     }
56
57     /**
58      * Identifies a peer (if any).
59      *
60      * @return identification of a calling partner
61      */
62     public String getPartnerName() {
63         return partnerName;
64     }
65
66     @Override
67     public String toString() {
68         return "ContextData{responseCode=" + requestId + ", responseDescription=" + serviceName
69                 + ", clientIpAddress=" + partnerName + '}';
70     }
71
72     public static ContextDataBuilder builder() {
73         return new ContextDataBuilder();
74     }
75
76     /**
77      * Fluent API for building context data.
78      */
79     public static class ContextDataBuilder {
80
81         private String requestId;
82         private String serviceName;
83         private String partnerName;
84
85         ContextDataBuilder() { /* package-private default constructor to hide the public one */ }
86
87         /**
88          * Unique request ID, most likely propagated via an HTTP header.
89          *
90          * @param requestId generated or propagated request ID.
91          * @return this builder for fluent API
92          */
93         public ContextDataBuilder requestId(final String requestId) {
94             this.requestId = requestId;
95             return this;
96         }
97
98         /**
99          * Name of a invoked API, by which it can be identified in the application.
100          *
101          * @param serviceName human-friendly service identifier
102          * @return this builder for fluent API
103          */
104         public ContextDataBuilder serviceName(final String serviceName) {
105             this.serviceName = serviceName;
106             return this;
107         }
108
109         /**
110          * Identifier of a peer calling a service {@link #serviceName(String)}).
111          *
112          * @param partnerName an string that is received from a calling peer and can identify it
113          * @return this builder for fluent API
114          */
115         public ContextDataBuilder partnerName(final String partnerName) {
116             this.partnerName = partnerName;
117             return this;
118         }
119
120         /**
121          * Create an instance of {@link ContextData}.
122          *
123          * @return a populated instance of audit data
124          */
125         public ContextData build() {
126             return new ContextData(this);
127         }
128     }
129 }