Refactoring audit subsystem to clean-up and eliminate back-dependency from core to CLI; still needs additional work.

Change-Id: I93c04c94f27b7b89c582b359eebe125458a573a7
diff --git a/cli/src/main/java/org/onosproject/cli/AbstractShellCommand.java b/cli/src/main/java/org/onosproject/cli/AbstractShellCommand.java
index 494d7d8..cbcd8d4 100644
--- a/cli/src/main/java/org/onosproject/cli/AbstractShellCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/AbstractShellCommand.java
@@ -29,6 +29,7 @@
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.security.AuditService;
 import org.slf4j.Logger;
 
 import static org.slf4j.LoggerFactory.getLogger;
@@ -43,63 +44,12 @@
 
     protected static final Logger log = getLogger(AbstractShellCommand.class);
 
+    private final ObjectMapper mapper = new ObjectMapper();
+
     @Option(name = "-j", aliases = "--json", description = "Output JSON",
             required = false, multiValued = false)
     private boolean json = false;
 
-    private static String auditFile = "all";
-    private static boolean auditEnabled = false;
-
-    /**
-     * To check if CLI Audit is enabled.
-     *
-     * @return true if the CLI Audit is enabled.
-     */
-    private static boolean isEnabled() {
-        return auditEnabled;
-    }
-
-    /**
-     * To enable CLI Audit.
-     */
-    public static void enableAudit() {
-        auditEnabled = true;
-    }
-
-    /**
-     * To disable CLI Audit.
-     */
-    public static void disableAudit() {
-        auditEnabled = false;
-    }
-
-    /**
-     * To set audit file type which CLI Audit logs must be saved.
-     *
-     * @param auditFile file that CLI Audit logs must be saved.
-     */
-    public static void setAuditFile(String auditFile) {
-        AbstractShellCommand.auditFile = auditFile;
-    }
-
-    /**
-     * To save audit logs into the log file.
-     *
-     * @param msg audit message.
-     */
-    private static void saveAuditLog(String msg) {
-        if (isEnabled()) {
-            if (auditFile.equals("all")) {
-                log.info(msg);
-                log.info("AuditLog : " + msg);
-            } else if (auditFile.equals("karaf")) {
-                log.info(msg);
-            } else if (auditFile.equals("audit")) {
-                log.info("AuditLog : " + msg);
-            }
-        }
-    }
-
     /**
      * Returns the reference to the implementation of the specified service.
      *
@@ -109,7 +59,6 @@
      * @throws org.onlab.osgi.ServiceNotFoundException if service is unavailable
      */
     public static <T> T get(Class<T> serviceClass) {
-        saveAuditLog("Audit ");
         return DefaultServiceDirectory.getService(serviceClass);
     }
 
@@ -204,8 +153,9 @@
     }
 
     @Override
-    public Object execute() throws Exception {
+    public final Object execute() throws Exception {
         try {
+            auditCommand();
             doExecute();
         } catch (ServiceNotFoundException e) {
             error(e.getMessage());
@@ -213,15 +163,23 @@
         return null;
     }
 
-    protected void doExecute() throws Exception {
-        try {
-            execute();
-        } catch (ServiceNotFoundException e) {
-            error(e.getMessage());
+    // Handles auditing
+    private void auditCommand() {
+        AuditService auditService = get(AuditService.class);
+        if (auditService != null && auditService.isAuditing()) {
+            // FIXME: Compose and log audit message here; this is a hack
+            String user = "foo"; // FIXME
+            String action = Thread.currentThread().getName().substring(5); // FIXME
+            auditService.logUserAction(user, action);
         }
     }
 
-    private final ObjectMapper mapper = new ObjectMapper();
+    /**
+     * Body of the shell command.
+     *
+     * @throws Exception thrown when problem is encountered
+     */
+    protected abstract void doExecute() throws Exception;
 
     @Override
     public ObjectMapper mapper() {