Test: Adding a new module of stats monitoring
using collectd tool which can be applied for
any test cases to check behaviour.

Description:
   collectd is a daemon which collects system and application
   stats.

   Following are the changes:
   1. Added changes in test container to initiate stat collection
      using collectd.
   2. Provided an option in cli to enable collectd for stats.
   3. Also provided an option for seperate collectd container as a external
      monitoring service for stats collection.
   4. Multiple test cases added w.r.t collectd itself also.

Change-Id: I5017e521b18f1e20ca8cd61b07be796d8e187a3d
diff --git a/src/test/utils/CordLogger.py b/src/test/utils/CordLogger.py
index bb9e571..cc1845b 100644
--- a/src/test/utils/CordLogger.py
+++ b/src/test/utils/CordLogger.py
@@ -1,6 +1,11 @@
 from OnosLog import OnosLog
 from scapy.all import log
 from onosclidriver import OnosCliDriver
+from OnosCtrl import OnosCtrl, get_mac
+from docker import Client
+from CordContainer import *
+import json
+import requests
 import unittest
 import os
 import time
@@ -22,7 +27,7 @@
     onosLogLevel = 'INFO'
     curLogLevel = onosLogLevel
     testLogLevel = os.getenv('LOG_LEVEL', onosLogLevel)
-    setup_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), '../setup')
+    setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../setup')
     archive_dir = os.path.join(setup_dir, 'test_logs')
     onos_data_dir = os.path.join(setup_dir, 'cord-onos-data')
 
@@ -155,3 +160,66 @@
         except:
             pass
         cls.cliSessionExit()
+
+    @classmethod
+    def stat_option(cls, stat = None, serverDetails = None):
+        # each stat option we can do some specific functions
+        if stat is None:
+           stat = cls.statOptionsList
+        if serverDetails is None:
+           serverDetails = cls.serverOptionsList
+        stat_choice = 'COLLECTD'
+        test_name = cls.testHostName
+        test_image = 'cord-test/nose'
+        if stat_choice in stat:
+           onos_ctrl = OnosCtrl('org.onosproject.cpman')
+           status, _ = onos_ctrl.activate()
+           if serverDetails is '':
+              ## default Test Container is used to install CollectD
+              pass
+           elif serverDetails in 'NEW':
+                test_image = 'cord-test/exserver'
+                test_name ='cord-collectd'
+           else:
+               pass
+               # cls.connect_server(serverDetails)
+               ## TO-DO for already up and running server, install collectd agent etc...
+           cls.start_collectd_agent_in_server(name = test_name, image = test_image)
+           for controller in cls.controllers:
+               if not controller:
+                  continue
+               url_mem_stats =  'http://%s:8181/onos/cpman/controlmetrics/memory_metrics'%(controller)
+               url_cpu_stats =  'http://%s:8181/onos/cpman/controlmetrics/cpu_metrics'%(controller)
+               auth = ('karaf', 'karaf')
+               cls.collectd_agent_metrics(controller, auth, url = url_cpu_stats)
+               cls.collectd_agent_metrics(controller, auth, url = url_mem_stats)
+        return
+
+
+    @classmethod
+    def collectd_agent_metrics(cls,controller=None, auth =None, url = None):
+        '''This function is getting rules from ONOS with json format'''
+        if url:
+           resp = requests.get(url, auth = auth)
+           log.info('Collectd agent has provided metrics via ONOS controller, url = %s \nand status = %s' %(url,resp.json()))
+        return resp
+
+
+    @classmethod
+    def start_collectd_agent_in_server(cls, name = None, image = None):
+        container_cmd_exec = Container(name = name, image = image)
+        tty = False
+        dckr = Client()
+        cmd =  'sudo /etc/init.d/collectd start'
+        i = container_cmd_exec.execute(cmd = cmd, tty= tty, stream = True)
+        return
+
+    @classmethod
+    def disable_onos_apps(cls, stat = None, app = None):
+        stat_choice = 'COLLECTD'
+        if stat is None:
+           stat = cls.statOptionsList
+        if stat_choice in stat:
+            onos_ctrl = OnosCtrl('org.onosproject.cpman')
+            status, _ = onos_ctrl.deactivate()
+