[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-logging-lib / openecomp-sdc-logging-core / src / main / java / org / openecomp / sdc / logging / context / impl / MdcDataDebugMessage.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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  */
20
21 package org.openecomp.sdc.logging.context.impl;
22
23 import org.openecomp.sdc.logging.api.Logger;
24 import org.openecomp.sdc.logging.api.LoggerFactory;
25 import org.openecomp.sdc.logging.context.MdcData;
26 import org.openecomp.sdc.logging.messages.DebugMessages;
27 import org.openecomp.sdc.logging.types.DebugConstants;
28 import org.openecomp.sdc.logging.types.LoggerErrorCategory;
29 import org.openecomp.sdc.logging.util.LoggingUtils;
30
31 import java.util.HashMap;
32 import java.util.Map;
33
34 public class MdcDataDebugMessage extends MdcData {
35
36   private static Logger logger;
37   private static Map<String, String> mapExitOrEntryToMessage;
38
39   static {
40     mapExitOrEntryToMessage = new HashMap<>();
41     mapExitOrEntryToMessage.put(DebugConstants.ENTER, DebugMessages.ENTER_METHOD);
42     mapExitOrEntryToMessage.put(DebugConstants.ENTER_DEFAULT, DebugMessages.DEFAULT_ENTER_METHOD);
43     mapExitOrEntryToMessage.put(DebugConstants.EXIT, DebugMessages.EXIT_METHOD);
44     mapExitOrEntryToMessage.put(DebugConstants.EXIT_DEFAULT, DebugMessages.DEFAULT_EXIT_METHOD);
45   }
46
47   public MdcDataDebugMessage() {
48     super(LoggerErrorCategory.DEBUG.name(), null);
49   }
50
51   //todo add more explanations as to the first parameter structure in case of multiples and in
52   // case of no params in method
53   /**
54    * Debug entry message.
55    *
56    * @param entityParameter the entity parameter
57    * @param ids             the ids
58    */
59   public void debugEntryMessage(String entityParameter, String... ids) {
60     logDebugMessage(entityParameter, DebugConstants.ENTER, ids);
61   }
62
63   /**
64    * Debug exit message.
65    *
66    * @param entityParameter the entity parameter
67    * @param ids             the ids
68    */
69   public void debugExitMessage(String entityParameter, String... ids) {
70     logDebugMessage(entityParameter, DebugConstants.EXIT, ids);
71   }
72
73   public void logDebugMessage(String entityParameter, String enterOrExit, String... ids) {
74     String methodName = LoggingUtils.getCallingMethodNameForDebugging();
75     String declaringClass = LoggingUtils.getDeclaringClass();
76     logger = (Logger) LoggerFactory.getLogger(declaringClass);
77     String messageToWrite;
78
79     if (entityParameter == null || ids == null) {
80       messageToWrite = mapExitOrEntryToMessage.get(enterOrExit + "_" + DebugConstants.DEFAULT);
81       logger.debug(String.format(messageToWrite, methodName));
82     } else {
83       messageToWrite = mapExitOrEntryToMessage.get(enterOrExit);
84       logger.debug(String
85           .format(messageToWrite, methodName, entityParameter, String.join(",", ids)));
86     }
87   }
88 }