6b9ef75b6126c31f8d4432fde856c5cddfc178c3
[sdc.git] / common-app-logging / src / main / java / org / openecomp / sdc / common / log / elements / LoggerSupportability.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  * Modifications copyright (c) 2020 Nordix Foundation
20  * ================================================================================
21  */
22 package org.openecomp.sdc.common.log.elements;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Map;
29 import org.openecomp.sdc.common.log.api.ILogConfiguration;
30 import org.openecomp.sdc.common.log.api.ILogFieldsHandler;
31 import org.openecomp.sdc.common.log.enums.LogLevel;
32 import org.openecomp.sdc.common.log.enums.LogMarkers;
33 import org.openecomp.sdc.common.log.enums.LoggerSupportabilityActions;
34 import org.openecomp.sdc.common.log.enums.StatusCode;
35 import org.slf4j.Logger;
36 import org.slf4j.MarkerFactory;
37
38 public class LoggerSupportability extends LoggerBase {
39
40     private static ArrayList<String> mandatoryFields = new ArrayList<>(Arrays.asList(
41         ILogConfiguration.MDC_SUPPORTABLITY_ACTION,
42         ILogConfiguration.MDC_SUPPORTABLITY_CSAR_UUID,
43         ILogConfiguration.MDC_SUPPORTABLITY_CSAR_VERSION,
44         ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_NAME,
45         ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_UUID,
46         ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_VERSION,
47         ILogConfiguration.MDC_SUPPORTABLITY_STATUS_CODE));
48
49     public LoggerSupportability(ILogFieldsHandler ecompLogFieldsHandler, Logger logger) {
50         super(ecompLogFieldsHandler, MarkerFactory.getMarker(LogMarkers.SUPPORTABILITY_MARKER.getText()),
51             logger);
52     }
53
54     public static LoggerSupportability getLogger(String className) {
55         return LoggerFactory.getMdcLogger(LoggerSupportability.class,
56             org.slf4j.LoggerFactory.getLogger(className));
57     }
58
59     public void log(LoggerSupportabilityActions action, Map<String, String> componentMetaData, StatusCode statusCode,
60                     String message, Object... params) {
61         fillFieldsBeforeLogging(action, componentMetaData, statusCode);
62         super.log(LogLevel.INFO, message, params);
63     }
64
65     public void log(LoggerSupportabilityActions action, StatusCode statusCode, String message, Object... params) {
66         log(action, null, statusCode, message, params);
67     }
68
69     private void fillFieldsBeforeLogging(LoggerSupportabilityActions action, Map<String, String> componentMetaData,
70                                          StatusCode statusCode) {
71         clear();
72         if (componentMetaData != null) {
73             ecompLogFieldsHandler
74                 .setSupportablityCsarUUID(componentMetaData.get(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_UUID));
75             ecompLogFieldsHandler
76                 .setSupportablityCsarVersion(componentMetaData.get(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_VERSION));
77             ecompLogFieldsHandler.setSupportablityComponentName(
78                 componentMetaData.get(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_NAME));
79             ecompLogFieldsHandler.setSupportablityComponentUUID(
80                 componentMetaData.get(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_UUID));
81             ecompLogFieldsHandler.setSupportablityComponentVersion(
82                 componentMetaData.get(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_VERSION));
83         }
84         ecompLogFieldsHandler.setSupportablityAction(action.getName());
85         ecompLogFieldsHandler.setSupportablityStatusCode(statusCode.getStatusCode());
86     }
87
88     @Override
89     public LoggerSupportability clear() {
90         LogFieldsMdcHandler.getInstance().removeSupportablityAction();
91         LogFieldsMdcHandler.getInstance().removeSupportablityCsarUUID();
92         LogFieldsMdcHandler.getInstance().removeSupportablityCsarVersion();
93         LogFieldsMdcHandler.getInstance().removeSupportablityComponentName();
94         LogFieldsMdcHandler.getInstance().removeSupportablityComponentUUID();
95         LogFieldsMdcHandler.getInstance().removeSupportablityComponentVersion();
96         LogFieldsMdcHandler.getInstance().removeSupportablityStatusCode();
97         return this;
98     }
99
100
101     @Override
102     public List<String> getMandatoryFields() {
103         return Collections.unmodifiableList(mandatoryFields);
104     }
105
106     @Override
107     public LoggerSupportability startTimer() {
108         return this;
109     }
110
111 }