2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.logging.api;
19 import java.util.Objects;
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.).
29 public class ContextData {
31 private final String requestId;
32 private final String serviceName;
33 private final String partnerName;
35 private ContextData(final ContextDataBuilder builder) {
36 this.requestId = builder.requestId;
37 this.serviceName = builder.serviceName;
38 this.partnerName = builder.partnerName;
42 * Uniques request ID received from a calling peer, or created.
44 * @return unique identifier of a request
46 public String getRequestId() {
51 * Service, in the context of which logs will be written.
53 * @return a string that identifies an exposed service
55 public String getServiceName() {
60 * Identifies a peer (if any).
62 * @return identification of a calling partner
64 public String getPartnerName() {
69 public boolean equals(Object o) {
75 if (o == null || getClass() != o.getClass()) {
79 ContextData that = (ContextData) o;
80 return Objects.equals(requestId, that.requestId) && Objects.equals(serviceName, that.serviceName)
81 && Objects.equals(partnerName, that.partnerName);
85 public int hashCode() {
86 return Objects.hash(requestId, serviceName, partnerName);
90 public String toString() {
91 return "ContextData{responseCode=" + requestId + ", responseDescription=" + serviceName
92 + ", clientIpAddress=" + partnerName + '}';
95 public static ContextDataBuilder builder() {
96 return new ContextDataBuilder();
100 * Fluent API for building context data.
102 public static class ContextDataBuilder {
104 private String requestId;
105 private String serviceName;
106 private String partnerName;
108 ContextDataBuilder() { /* package-private default constructor to hide the public one */ }
111 * Unique request ID, most likely propagated via an HTTP header.
113 * @param requestId generated or propagated request ID.
114 * @return this builder for fluent API
116 public ContextDataBuilder requestId(final String requestId) {
117 this.requestId = requestId;
122 * Name of a invoked API, by which it can be identified in the application.
124 * @param serviceName human-friendly service identifier
125 * @return this builder for fluent API
127 public ContextDataBuilder serviceName(final String serviceName) {
128 this.serviceName = serviceName;
133 * Identifier of a peer calling a service {@link #serviceName(String)}).
135 * @param partnerName an string that is received from a calling peer and can identify it
136 * @return this builder for fluent API
138 public ContextDataBuilder partnerName(final String partnerName) {
139 this.partnerName = partnerName;
144 * Create an instance of {@link ContextData}.
146 * @return a populated instance of audit data
148 public ContextData build() {
149 return new ContextData(this);