Implement Attributes/Outputs BE (part 1)
[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(Class<?> clazz) {
55         return LoggerFactory.getMdcLogger(LoggerSupportability.class, org.slf4j.LoggerFactory.getLogger(clazz));
56     }
57
58     public static LoggerSupportability getLogger(String className) {
59         return LoggerFactory.getMdcLogger(LoggerSupportability.class,
60             org.slf4j.LoggerFactory.getLogger(className));
61     }
62
63     public void log(LoggerSupportabilityActions action, Map<String, String> componentMetaData, StatusCode statusCode,
64                     String message, Object... params) {
65         fillFieldsBeforeLogging(action, componentMetaData, statusCode);
66         super.log(LogLevel.INFO, message, params);
67     }
68
69     public void log(LoggerSupportabilityActions action, StatusCode statusCode, String message, Object... params) {
70         log(action, null, statusCode, message, params);
71     }
72
73     private void fillFieldsBeforeLogging(LoggerSupportabilityActions action, Map<String, String> componentMetaData,
74                                          StatusCode statusCode) {
75         clear();
76         if (componentMetaData != null) {
77             ecompLogFieldsHandler
78                 .setSupportablityCsarUUID(componentMetaData.get(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_UUID));
79             ecompLogFieldsHandler
80                 .setSupportablityCsarVersion(componentMetaData.get(ILogConfiguration.MDC_SUPPORTABLITY_CSAR_VERSION));
81             ecompLogFieldsHandler.setSupportablityComponentName(
82                 componentMetaData.get(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_NAME));
83             ecompLogFieldsHandler.setSupportablityComponentUUID(
84                 componentMetaData.get(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_UUID));
85             ecompLogFieldsHandler.setSupportablityComponentVersion(
86                 componentMetaData.get(ILogConfiguration.MDC_SUPPORTABLITY_COMPONENT_VERSION));
87         }
88         ecompLogFieldsHandler.setSupportablityAction(action.getName());
89         ecompLogFieldsHandler.setSupportablityStatusCode(statusCode.getStatusCode());
90     }
91
92     @Override
93     public LoggerSupportability clear() {
94         LogFieldsMdcHandler.getInstance().removeSupportablityAction();
95         LogFieldsMdcHandler.getInstance().removeSupportablityCsarUUID();
96         LogFieldsMdcHandler.getInstance().removeSupportablityCsarVersion();
97         LogFieldsMdcHandler.getInstance().removeSupportablityComponentName();
98         LogFieldsMdcHandler.getInstance().removeSupportablityComponentUUID();
99         LogFieldsMdcHandler.getInstance().removeSupportablityComponentVersion();
100         LogFieldsMdcHandler.getInstance().removeSupportablityStatusCode();
101         return this;
102     }
103
104
105     @Override
106     public List<String> getMandatoryFields() {
107         return Collections.unmodifiableList(mandatoryFields);
108     }
109
110     @Override
111     public LoggerSupportability startTimer() {
112         return this;
113     }
114
115 }