Implement log archiver for test cases after test cases are run.
The test case logs are archived in test_logs directory.

Change-Id: Ica1cb96ebeb784101ef171c8deb095ffeca513a7
diff --git a/src/test/cluster/clusterTest.py b/src/test/cluster/clusterTest.py
index a694bad..64ae73f 100644
--- a/src/test/cluster/clusterTest.py
+++ b/src/test/cluster/clusterTest.py
@@ -388,6 +388,11 @@
             except:
                 time.sleep(5)
                 continue
+
+            #first archive the test case logs for this run
+            CordLogger.archive_results('test_cluster_controller_restarts',
+                                       controllers = controllers,
+                                       iteration = 'iteration_{}'.format(num+1))
             next_controller = check_exception(controller = controller)
 
     def test_cluster_single_controller_restarts(self):
@@ -457,6 +462,10 @@
             log.info('Restarting back the controller %s' %controller_name)
             cord_test_onos_restart(node = controller_name)
             time.sleep(60)
+            #archive the logs for this run
+            CordLogger.archive_results('test_cluster_single_controller_restarts',
+                                       controllers = controllers,
+                                       iteration = 'iteration_{}'.format(num+1))
             check_exception(controller, inclusive = True)
 
     def test_cluster_restarts(self):
@@ -510,6 +519,11 @@
             except:
                 time.sleep(10)
                 continue
+
+            #archive the logs for this run before verification
+            CordLogger.archive_results('test_cluster_restarts',
+                                       controllers = controllers,
+                                       iteration = 'iteration_{}'.format(num+1))
             #check for exceptions on the adjacent nodes
             check_exception()
 
diff --git a/src/test/utils/CordLogger.py b/src/test/utils/CordLogger.py
index 081e33c..51c7dc8 100644
--- a/src/test/utils/CordLogger.py
+++ b/src/test/utils/CordLogger.py
@@ -12,6 +12,8 @@
     onosLogLevel = 'INFO'
     curLogLevel = onosLogLevel
     testLogLevel = os.getenv('LOG_LEVEL', onosLogLevel)
+    setup_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), '../setup')
+    archive_dir = os.path.join(setup_dir, 'test_logs')
 
     @classmethod
     def cliSessionEnter(cls):
@@ -61,6 +63,42 @@
             else:
                 log.info('\nTest %s has no errors and warnings in the logs' %self._testMethodName)
         except: pass
+        try:
+            self.archive_results(self._testMethodName)
+        except: pass
+
+    @classmethod
+    def archive_results(cls, testName, controllers = None, iteration = None, cache_result = False):
+        log_map = {}
+        if controllers is None:
+            controllers = cls.controllers
+        else:
+            if type(controllers) in [ str, unicode ]:
+                controllers = [ controllers ]
+        try:
+            for controller in controllers:
+                onosLog = OnosLog(host = controller)
+                st, output = onosLog.get_log(cache_result = cache_result)
+                log_map[controller] = (st, output)
+        except:
+            return
+
+        if not os.path.exists(cls.archive_dir):
+            os.mkdir(cls.archive_dir)
+        for controller, results in log_map.items():
+            st, output = results
+            if st and output:
+                iteration_str = '' if iteration is None else '_{}'.format(iteration)
+                archive_file = os.path.join(cls.archive_dir,
+                                            'logs_{}_{}{}'.format(controller, testName, iteration_str))
+                archive_cmd = 'gzip -9 -f {}'.format(archive_file)
+                if os.access(archive_file, os.F_OK):
+                    os.unlink(archive_file)
+                with open(archive_file, 'w') as fd:
+                    fd.write(output)
+                try:
+                    os.system(archive_cmd)
+                except: pass
 
     @classmethod
     def logSet(cls, level = None, app = 'org.onosproject', controllers = None, forced = False):
diff --git a/src/test/utils/OnosLog.py b/src/test/utils/OnosLog.py
index f5dcb77..a5e0fa7 100644
--- a/src/test/utils/OnosLog.py
+++ b/src/test/utils/OnosLog.py
@@ -25,7 +25,7 @@
     def update_last_snapshot(cls, host, res):
         cls.last_snapshot_map[host] = res
 
-    def get_log(self, search_terms = None, exception = True):
+    def get_log(self, search_terms = None, exception = True, cache_result = True):
         """Run the command on the test host"""
         cmd = 'cat /root/onos/apache-karaf-3.0.5/data/log/karaf.log'
         st, output = self.ssh_agent.run_cmd(cmd)
@@ -56,7 +56,8 @@
             output += '\n'.join(exception_map['Exception'])
 
         #update the last snapshot
-        self.update_last_snapshot(self.ssh_agent.host, lines)
+        if cache_result is True:
+            self.update_last_snapshot(self.ssh_agent.host, lines)
         return st, output
 
     def search_log_pattern(self, pattern):