Implement ONOS shutdown.
If a node is specified, then ONOS running in cluster is shutdown.
Also move quaggashutdown wrappers to CordContainer.

Change-Id: I766a01bccd97f0ce033b533a6478556c1134942f
diff --git a/src/test/utils/CordContainer.py b/src/test/utils/CordContainer.py
index 88823ff..2ab077b 100644
--- a/src/test/utils/CordContainer.py
+++ b/src/test/utils/CordContainer.py
@@ -279,6 +279,21 @@
         build_cmd = 'cd {} && docker-compose build'.format(self.onos_cord_dir)
         os.system(build_cmd)
 
+class OnosCordStopWrapper(Container):
+    onos_cord_dir = os.path.join(os.getenv('HOME'), 'cord-tester-cord')
+    docker_yaml = os.path.join(onos_cord_dir, 'docker-compose.yml')
+
+    def __init__(self):
+        if os.access(self.docker_yaml, os.F_OK):
+            with open(self.docker_yaml, 'r') as f:
+                yaml_config = yaml.load(f)
+                image = yaml_config['services'].keys()[0]
+                name = 'cordtestercord_{}_1'.format(image)
+            super(OnosCordStopWrapper, self).__init__(name, image, tag = '')
+            if self.exists():
+                print('Killing container %s' %self.name)
+                self.kill()
+
 class Onos(Container):
 
     quagga_config = ( { 'bridge' : 'quagga-br', 'ip': '10.10.0.4', 'mask' : 16 }, )
@@ -337,7 +352,7 @@
                     os.unlink(f)
                 except: pass
 
-    def __init__(self, name = NAME, image = 'onosproject/onos', prefix = '', tag = 'latest',
+    def __init__(self, name = NAME, image = IMAGE, prefix = PREFIX, tag = TAG,
                  boot_delay = 60, restart = False, network_cfg = None, cluster = False):
         if restart is True:
             ##Find the right image to restart
@@ -525,6 +540,19 @@
             print('ONOS app %s, version %s %s' %(app, version, 'installed' if ok else 'failed to install'))
             time.sleep(2)
 
+class OnosStopWrapper(Container):
+    def __init__(self, name):
+        super(OnosStopWrapper, self).__init__(name, Onos.IMAGE, tag = Onos.TAG, prefix = Container.IMAGE_PREFIX)
+        if self.exists():
+            self.kill()
+        else:
+            if Onos.cluster_mode is True:
+                valid_node = filter(lambda onos: name in [ onos.ipaddr, onos.name ], Onos.cluster_instances)
+                if valid_node:
+                    onos = valid_node.pop()
+                    if onos.exists():
+                        onos.kill()
+
 class Radius(Container):
     ports = [ 1812, 1813 ]
     env = {'TIMEZONE':'America/Los_Angeles',
@@ -631,6 +659,13 @@
         super(Quagga, cls).build_image(dockerfile, image)
         print('Done building image %s' %image)
 
+class QuaggaStopWrapper(Container):
+    def __init__(self, name = Quagga.NAME, image = Quagga.IMAGE, tag = 'candidate'):
+        super(QuaggaStopWrapper, self).__init__(name, image, prefix = Container.IMAGE_PREFIX, tag = tag)
+        if self.exists():
+            self.kill()
+
+
 def reinitContainerClients():
     docker_netns.dckr = Client()
     Container.dckr = Client()
@@ -784,7 +819,6 @@
     def build_image(cls, image = IMAGE):
         Xos.build_image(image, cls.dockerfile_path)
 
-
 class XosSyncVtn(Xos):
     ports = [8080,]
     env = None
@@ -870,4 +904,3 @@
     @classmethod
     def build_image(cls, image = IMAGE):
         Xos.build_image(image, cls.dockerfile_path)
-
diff --git a/src/test/utils/CordTestServer.py b/src/test/utils/CordTestServer.py
index e236008..cf280f6 100644
--- a/src/test/utils/CordTestServer.py
+++ b/src/test/utils/CordTestServer.py
@@ -13,7 +13,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-from CordContainer import Container, Onos, OnosCord, Quagga, Radius, reinitContainerClients
+from CordContainer import Container, Onos, OnosStopWrapper, OnosCord, OnosCordStopWrapper, Quagga, QuaggaStopWrapper, Radius, reinitContainerClients
 from nose.tools import nottest
 from SimpleXMLRPCServer import SimpleXMLRPCServer
 import daemon
@@ -30,12 +30,6 @@
 CORD_TEST_HOST = '172.17.0.1'
 CORD_TEST_PORT = 25000
 
-class QuaggaStopWrapper(Container):
-    def __init__(self, name = Quagga.NAME, image = Quagga.IMAGE, tag = 'candidate'):
-        super(QuaggaStopWrapper, self).__init__(name, image, prefix = Container.IMAGE_PREFIX, tag = tag)
-        if self.exists():
-            self.kill()
-
 class CordTestServer(object):
 
     onos_cord = None
@@ -60,6 +54,15 @@
     def restart_onos(self, kwargs):
         return self.__restart_onos(**kwargs)
 
+    def __shutdown_onos(self, node = None):
+        if node is None:
+            node = Onos.NAME
+        OnosStopWrapper(node)
+        return 'DONE'
+
+    def shutdown_onos(self, kwargs):
+        return self.__shutdown_onos(**kwargs)
+
     def __restart_quagga(self, config = None, boot_delay = 30 ):
         config_file = Quagga.quagga_config_file
         if config is not None:
@@ -161,6 +164,17 @@
     return False
 
 @nottest
+def __cord_test_onos_shutdown(**kwargs):
+    return rpc_server_instance().shutdown_onos(kwargs)
+
+@nottest
+def cord_test_onos_shutdown(node = None):
+    data = __cord_test_onos_shutdown(node = node)
+    if data == 'DONE':
+        return True
+    return False
+
+@nottest
 def __cord_test_quagga_restart(**kwargs):
     return rpc_server_instance().restart_quagga(kwargs)