70e3dd6479ee36a14fe056fa05b44cec8776120d
[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.slf4j;
18
19 import java.util.EnumMap;
20 import java.util.Map;
21 import org.openecomp.sdc.logging.api.ContextData;
22
23 /**
24  * Maps request data sent to the context service to corresponding MDC fields.
25  *
26  * @author evitaliy
27  * @since 23 Mar 2018
28  */
29 class RequestContextProvider implements ContextProvider {
30
31     private final ContextData data;
32
33     private RequestContextProvider(ContextData contextData) {
34         this.data = contextData;
35     }
36
37     static RequestContextProvider from(ContextData contextData) {
38         return new RequestContextProvider(contextData);
39     }
40
41     static ContextData to(Map<ContextField, String> values) {
42         return ContextData.builder()
43                 .requestId(values.get(ContextField.REQUEST_ID))
44                 .serviceName(values.get(ContextField.SERVICE_NAME))
45                 .partnerName(values.get(ContextField.PARTNER_NAME)).build();
46     }
47
48     @Override
49     public Map<ContextField, String> values() {
50
51         Map<ContextField, String> values = new EnumMap<>(ContextField.class);
52
53         putIfNotNull(values, ContextField.REQUEST_ID, data.getRequestId());
54         putIfNotNull(values, ContextField.SERVICE_NAME, data.getServiceName());
55         putIfNotNull(values, ContextField.PARTNER_NAME, data.getPartnerName());
56
57         return values;
58     }
59
60     private void putIfNotNull(Map<ContextField, String> values, ContextField field, String value) {
61         if (value != null) {
62             values.put(field, value);
63         }
64     }
65 }