* ============LICENSE_START=======================================================
* org.onap.aai
* ================================================================================
- * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
- *
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
*/
package org.onap.aai.logging;
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";
+
+ public static final Map<String, String> responseMap = new HashMap();
+
+ static {
+ responseMap.put(SUCCESS, "Success");
+ responseMap.put(UNKNOWN_ERROR, "Unknown error");
+ }
//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());
}
} 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();
+ // set response code to 0 since we don't know what the outcome of this request is yet
+ String responseCode = LoggingContext.DATA_ERROR;
+ LoggingContext.responseCode(responseCode);
+ LoggingContext.responseDescription("Unable to use UUID " + requestId + " (Not formatted properly) ");
+ LoggingContext.statusCode(StatusCode.ERROR);
+
+ LOGGER.warn("Using generated UUID=" + generatedRequestUuid);
+ LoggingContext.restore();
+
}
}
MDC.put(LoggingField.SEVERITY.toString(), String.valueOf(severity));
}
+ public static void successStatusFields() {
+ responseCode(SUCCESS);
+ statusCode(LoggingContext.StatusCode.COMPLETE);
+ responseDescription("Success");
+ }
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.