ONOS audit REST API support

Change-Id: Ic2910785f1f16fe5e36b33c1a73f44539cd8fbea
diff --git a/core/net/BUILD b/core/net/BUILD
index fc6c46d..50e0a3f 100644
--- a/core/net/BUILD
+++ b/core/net/BUILD
@@ -3,6 +3,7 @@
     "//utils/rest:onlab-rest",
     "//core/store/serializers:onos-core-serializers",
     "//core/store/primitives:onos-core-primitives",
+    "//cli:onos-cli",
     "@org_osgi_service_cm//jar",
 ]
 
diff --git a/core/net/src/main/java/org/onosproject/net/OsgiPropertyConstants.java b/core/net/src/main/java/org/onosproject/net/OsgiPropertyConstants.java
index dd96525..db5c5e3 100644
--- a/core/net/src/main/java/org/onosproject/net/OsgiPropertyConstants.java
+++ b/core/net/src/main/java/org/onosproject/net/OsgiPropertyConstants.java
@@ -124,4 +124,11 @@
 
     public static final String DTP_MAX_BATCH_MS = "maxBatchMs";
     public static final int DTP_MAX_BATCH_MS_DEFAULT = 50;
+
+    public static final String AUDIT_STATUS_DESC = "auditEnabled";
+    public static final boolean AUDIT_STATUS_DEFAULT = false;
+
+    public static final String AUDIT_FILE_TYPE_DESC = "auditFile";
+    public static final String AUDIT_FILE_TYPE_DEFAULT = "all";
+
 }
diff --git a/core/net/src/main/java/org/onosproject/net/audit/impl/AuditManager.java b/core/net/src/main/java/org/onosproject/net/audit/impl/AuditManager.java
new file mode 100644
index 0000000..31b6a18
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/audit/impl/AuditManager.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.audit.impl;
+
+import org.onlab.rest.AuditFilter;
+
+import org.onosproject.cfg.ComponentConfigService;
+import org.onosproject.cli.AbstractShellCommand;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Modified;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+
+import java.util.Dictionary;
+
+import static org.onlab.util.Tools.get;
+import static org.onosproject.net.OsgiPropertyConstants.AUDIT_FILE_TYPE_DESC;
+import static org.onosproject.net.OsgiPropertyConstants.AUDIT_FILE_TYPE_DEFAULT;
+import static org.onosproject.net.OsgiPropertyConstants.AUDIT_STATUS_DESC;
+import static org.onosproject.net.OsgiPropertyConstants.AUDIT_STATUS_DEFAULT;
+
+
+/**
+ * Component to manage REST API Audit.
+ */
+@Component(
+        immediate = true,
+        property = {
+                AUDIT_FILE_TYPE_DESC + "=" + AUDIT_FILE_TYPE_DEFAULT,
+                AUDIT_STATUS_DESC + ":Boolean=" + AUDIT_STATUS_DEFAULT
+        })
+public class AuditManager {
+
+    public String auditFile = AUDIT_FILE_TYPE_DEFAULT;
+    public boolean auditEnabled = AUDIT_STATUS_DEFAULT;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected ComponentConfigService cfgService;
+
+    @Activate
+    public void activate(ComponentContext context) {
+        cfgService.registerProperties(getClass());
+        setAuditStatus(auditFile, auditEnabled);
+    }
+
+    @Modified
+    protected void modifyFileType(ComponentContext context) {
+        Dictionary<?, ?> properties = context.getProperties();
+        if (properties == null) {
+            return;
+        }
+        auditFile = get(properties, AUDIT_FILE_TYPE_DESC);
+        String enableAuditStr = get(properties, AUDIT_STATUS_DESC);
+
+        auditEnabled = Boolean.parseBoolean(enableAuditStr);
+        setAuditStatus(auditFile, auditEnabled);
+    }
+
+    /**
+     * To enable Audit and set file type for REST API and  CLI as  per the changes in configuration properties.
+     *
+     * @param auditFile    file which audit logs are saved.
+     * @param auditEnabled status of REST API Audit and CLI Audit.
+     */
+    public void setAuditStatus(String auditFile, boolean auditEnabled) {
+        if (auditEnabled) {
+            AuditFilter.enableAudit();
+            AbstractShellCommand.enableAudit();
+        } else {
+            AuditFilter.disableAudit();
+            AbstractShellCommand.disableAudit();
+        }
+        AuditFilter.setAuditFile(auditFile);
+        AbstractShellCommand.setAuditFile(auditFile);
+    }
+}
diff --git a/core/net/src/main/java/org/onosproject/net/audit/impl/package-info.java b/core/net/src/main/java/org/onosproject/net/audit/impl/package-info.java
new file mode 100644
index 0000000..2845808
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/audit/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Implementation of Audit Configuration.
+ */
+package org.onosproject.net.audit.impl;
\ No newline at end of file