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;
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.).
27 public class ContextData {
29 private final String requestId;
30 private final String serviceName;
31 private final String partnerName;
33 private ContextData(final ContextDataBuilder builder) {
34 this.requestId = builder.requestId;
35 this.serviceName = builder.serviceName;
36 this.partnerName = builder.partnerName;
40 * Uniques request ID received from a calling peer, or created.
42 * @return unique identifier of a request
44 public String getRequestId() {
49 * Service, in the context of which logs will be written.
51 * @return a string that identifies an exposed service
53 public String getServiceName() {
58 * Identifies a peer (if any).
60 * @return identification of a calling partner
62 public String getPartnerName() {
67 public String toString() {
68 return "ContextData{responseCode=" + requestId + ", responseDescription=" + serviceName
69 + ", clientIpAddress=" + partnerName + '}';
72 public static ContextDataBuilder builder() {
73 return new ContextDataBuilder();
77 * Fluent API for building context data.
79 public static class ContextDataBuilder {
81 private String requestId;
82 private String serviceName;
83 private String partnerName;
85 ContextDataBuilder() { /* package-private default constructor to hide the public one */ }
88 * Unique request ID, most likely propagated via an HTTP header.
90 * @param requestId generated or propagated request ID.
91 * @return this builder for fluent API
93 public ContextDataBuilder requestId(final String requestId) {
94 this.requestId = requestId;
99 * Name of a invoked API, by which it can be identified in the application.
101 * @param serviceName human-friendly service identifier
102 * @return this builder for fluent API
104 public ContextDataBuilder serviceName(final String serviceName) {
105 this.serviceName = serviceName;
110 * Identifier of a peer calling a service {@link #serviceName(String)}).
112 * @param partnerName an string that is received from a calling peer and can identify it
113 * @return this builder for fluent API
115 public ContextDataBuilder partnerName(final String partnerName) {
116 this.partnerName = partnerName;
121 * Create an instance of {@link ContextData}.
123 * @return a populated instance of audit data
125 public ContextData build() {
126 return new ContextData(this);