import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
+import org.onap.aai.exceptions.AAIException;
import org.slf4j.MDC;
import com.att.eelf.configuration.EELFLogger;
private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(LoggingContext.class);
private static final String PREVIOUS_CONTEXTS_KEY = "_PREVIOUS_CONTEXTS";
-
+
+ //Response codes from ECOMP Logging Guidelines
+ public static final String SUCCESS = "0";
+ public static final String PERMISSION_ERROR = "100";
+ public static final String AVAILABILITY_TIMEOUT_ERROR = "200";
+ public static final String DATA_ERROR = "200";
+ public static final String SCHEMA_ERROR = "400";
+ public static final String BUSINESS_PROCESS_ERROR = "500";
+ public static final String UNKNOWN_ERROR = "900";
+
//ECOMP Specific Log Event Fields
public static enum LoggingField {
START_TIME("startTime"),
//ECOMP Specific Metric Log Event Fields
TARGET_ENTITY("targetEntity"),
-
+ TARGET_SERVICE_NAME("targetServiceName"),
//A&AI Specific Log Event Fields
COMPONENT("component"),
STOP_WATCH_START("stopWatchStart");
LoggingContext.serverIpAddress();
}
- private static void startTime() {
+ public static void startTime() {
MDC.put(LoggingField.START_TIME.toString(), LogFormatTools.getCurrentDateTime());
}
public static void requestId(String requestId) {
try {
+ if(requestId == null){
+ throw new IllegalArgumentException();
+ }
if (requestId.contains(":")) {
String[] uuidParts = requestId.split(":");
requestId = uuidParts[0];
} catch (IllegalArgumentException e) {
final UUID generatedRequestUuid = UUID.randomUUID();
MDC.put(LoggingField.REQUEST_ID.toString(), generatedRequestUuid.toString());
- LOGGER.warn("Unable to use UUID " + requestId + " (Not formatted properly). Using generated UUID=" + generatedRequestUuid);
+ LoggingContext.save();
+ AAIException ex = new AAIException("AAI_7405", e);
+ String responseCode = Integer.toString(ex.getErrorObject().getHTTPResponseCode().getStatusCode());
+ LoggingContext.responseCode(responseCode);
+
+ LOGGER.warn("Unable to use UUID " + requestId + " (Not formatted properly). Using generated UUID="
+ + generatedRequestUuid);
+ LoggingContext.restore();
+
}
}
MDC.put(LoggingField.SEVERITY.toString(), String.valueOf(severity));
}
+ public static void successStatusFields() {
+ responseCode(SUCCESS);
+ statusCode(LoggingContext.StatusCode.COMPLETE);
+ }
private static void serverIpAddress() {
try {
MDC.put(LoggingField.SERVER_IP_ADDRESS.toString(), InetAddress.getLocalHost().getHostAddress());
MDC.put(LoggingField.TARGET_ENTITY.toString(), targetEntity);
}
+ public static void targetServiceName(String targetServiceName) {
+ MDC.put(LoggingField.TARGET_SERVICE_NAME.toString(), targetServiceName);
+ }
+
+ public static boolean isStopWatchStarted() {
+ final String rawStopWatchStart = MDC.get(LoggingField.STOP_WATCH_START.toString());
+ if (rawStopWatchStart == null) {
+ return false;
+ }
+ return true;
+ }
public static void stopWatchStart() {
MDC.put(LoggingField.STOP_WATCH_START.toString(), String.valueOf(System.nanoTime()));
}
@SuppressWarnings("unchecked")
final Iterator<String> keys = previousContext.keys();
-
+ boolean foundElapsedTime = false;
while (keys.hasNext()) {
final String key = keys.next();
-
+ if (LoggingField.ELAPSED_TIME.toString().equals(key)) {
+ foundElapsedTime = true;
+ }
try {
MDC.put(key, previousContext.getString(key));
} catch (JSONException e) {
// or the value is invalid (they are all strings)
}
}
-
+ if ( !foundElapsedTime ) {
+ MDC.remove(LoggingField.ELAPSED_TIME.toString());
+ }
MDC.put(PREVIOUS_CONTEXTS_KEY, removeLast(previousContexts).toString());
} catch (JSONException e) {
//Ignore, the previousContext is serialized from a JSONObject
}
}
+ public static void restoreIfPossible() {
+ try {
+ restore();
+ }
+ catch (LoggingContextNotExistsException e) {
+ //Ignore
+ }
+ }
/**
* AJSC declares an ancient version of org.json:json in one of the parent POMs of this project.