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