Update the license for 2017-2018 license
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / logging / LoggingContext.java
index 1e61c65..8b8cb17 100644 (file)
@@ -2,7 +2,7 @@
  * ============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.
@@ -16,8 +16,6 @@
  * 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;
 
@@ -48,7 +46,22 @@ public class LoggingContext {
        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"),
@@ -75,7 +88,7 @@ public class LoggingContext {
                
                //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");
@@ -99,7 +112,7 @@ public class LoggingContext {
                LoggingContext.serverIpAddress();
        }
 
-       private static void startTime() {
+       public static void startTime() {
                MDC.put(LoggingField.START_TIME.toString(), LogFormatTools.getCurrentDateTime());
        }
 
@@ -115,12 +128,8 @@ public class LoggingContext {
                MDC.put(LoggingField.REQUEST_ID.toString(), requestId.toString());
        }
 
-       public static void requestId(String requestId) throws AAIException {
-
+       public static void requestId(String requestId) {
                try {
-                       if(requestId == null){
-                               throw new IllegalArgumentException();
-                       }
                        if (requestId.contains(":")) {
                                String[] uuidParts = requestId.split(":");
                                requestId = uuidParts[0];
@@ -129,7 +138,16 @@ public class LoggingContext {
                } 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();
+
                }
        }
 
@@ -177,6 +195,11 @@ public class LoggingContext {
                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());
@@ -247,6 +270,17 @@ public class LoggingContext {
                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()));
        }
@@ -328,10 +362,12 @@ public class LoggingContext {
 
                        @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) {
@@ -339,12 +375,22 @@ public class LoggingContext {
                                        //                      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.