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