Test: Get the cord-tester ready for real CORD deployment.
Fabric test, manifest, option to specify container image prefix, gradle build changes to publish and a host of other changes for cord.

Change-Id: I4bd1c8d9ff4c0a6d117219ca847ae03c61784096
diff --git a/src/test/utils/CordContainer.py b/src/test/utils/CordContainer.py
index e5a3a1d..11f257b 100644
--- a/src/test/utils/CordContainer.py
+++ b/src/test/utils/CordContainer.py
@@ -48,8 +48,14 @@
 
 class Container(object):
     dckr = Client()
-    def __init__(self, name, image, tag = 'latest', command = 'bash', quagga_config = None):
+    IMAGE_PREFIX = '' ##for saving global prefix for all test classes
+
+    def __init__(self, name, image, prefix='', tag = 'candidate', command = 'bash', quagga_config = None):
         self.name = name
+        self.prefix = prefix
+        if prefix:
+            self.prefix += '/'
+            image = '{}{}'.format(self.prefix, image)
         self.image = image
         self.tag = tag
         if tag:
@@ -108,7 +114,7 @@
         return '/{0}'.format(self.name) in list(flatten(n['Names'] for n in self.dckr.containers()))
 
     def img_exists(self):
-        return self.image_name in [ctn['RepoTags'][0] for ctn in self.dckr.images()]
+        return self.image_name[len(self.prefix):] in [ctn['RepoTags'][0] for ctn in self.dckr.images()]
 
     def ip(self):
         cnt_list = filter(lambda c: c['Image'] == self.image_name, self.dckr.containers())
@@ -286,6 +292,9 @@
     NAME = 'cord-onos'
     ##the ip of ONOS in default cluster.json in setup/onos-config
     CLUSTER_CFG_IP = '172.17.0.2'
+    IMAGE = 'onosproject/onos'
+    TAG = 'latest'
+    PREFIX = ''
 
     @classmethod
     def onos_generate_cluster_cfg(cls, ip):
@@ -294,7 +303,7 @@
             os.system(cmd)
         except: pass
 
-    def __init__(self, name = NAME, image = 'onosproject/onos', tag = 'latest',
+    def __init__(self, name = NAME, image = 'onosproject/onos', prefix = '', tag = 'latest',
                  boot_delay = 60, restart = False, network_cfg = None):
         if restart is True:
             ##Find the right image to restart
@@ -306,7 +315,7 @@
                     tag = image_name.split(':')[1]
                 except: pass
 
-        super(Onos, self).__init__(name, image, tag = tag, quagga_config = self.quagga_config)
+        super(Onos, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = self.quagga_config)
         if restart is True and self.exists():
             self.kill()
         if not self.exists():
@@ -369,11 +378,11 @@
     IMAGE = 'cord-test/radius'
     NAME = 'cord-radius'
 
-    def __init__(self, name = NAME, image = IMAGE, tag = 'latest',
+    def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate',
                  boot_delay = 10, restart = False, update = False):
-        super(Radius, self).__init__(name, image, tag = tag, command = self.start_command)
+        super(Radius, self).__init__(name, image, prefix = prefix, tag = tag, command = self.start_command)
         if update is True or not self.img_exists():
-            self.build_image(image)
+            self.build_image(self.image_name)
         if restart is True and self.exists():
             self.kill()
         if not self.exists():
@@ -416,11 +425,11 @@
     IMAGE = 'cord-test/quagga'
     NAME = 'cord-quagga'
 
-    def __init__(self, name = NAME, image = IMAGE, tag = 'latest',
+    def __init__(self, name = NAME, image = IMAGE, prefix = '', tag = 'candidate',
                  boot_delay = 15, restart = False, config_file = quagga_config_file, update = False):
-        super(Quagga, self).__init__(name, image, tag = tag, quagga_config = self.quagga_config)
+        super(Quagga, self).__init__(name, image, prefix = prefix, tag = tag, quagga_config = self.quagga_config)
         if update is True or not self.img_exists():
-            self.build_image(image)
+            self.build_image(self.image_name)
         if restart is True and self.exists():
             self.kill()
         if not self.exists():
diff --git a/src/test/utils/CordTestServer.py b/src/test/utils/CordTestServer.py
index 3b97814..47b84e3 100644
--- a/src/test/utils/CordTestServer.py
+++ b/src/test/utils/CordTestServer.py
@@ -30,8 +30,8 @@
 CORD_TEST_PORT = 25000
 
 class QuaggaStopWrapper(Container):
-    def __init__(self, name = Quagga.NAME, image = Quagga.IMAGE, tag = 'latest'):
-        super(QuaggaStopWrapper, self).__init__(name, image, tag = tag)
+    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()
 
@@ -53,7 +53,7 @@
         if self.onos_cord:
             self.onos_cord.start(restart = True, network_cfg = config)
         else:
-            Onos(restart = True, network_cfg = config)
+            Onos(restart = True, network_cfg = config, image = Onos.IMAGE, tag = Onos.TAG)
         return 'DONE'
 
     def restart_onos(self, kwargs):
@@ -67,7 +67,7 @@
             with open(quagga_config, 'w+') as fd:
                 fd.write(str(config))
         print('Restarting QUAGGA with config file %s, delay %d' %(config_file, boot_delay))
-        Quagga(restart = True, config_file = config_file, boot_delay = boot_delay)
+        Quagga(prefix = Container.IMAGE_PREFIX, restart = True, config_file = config_file, boot_delay = boot_delay)
         return 'DONE'
 
     def restart_quagga(self, kwargs):
@@ -103,7 +103,7 @@
 
     def restart_radius(self):
         print('Restarting RADIUS Server')
-        Radius(restart = True)
+        Radius(prefix = Container.IMAGE_PREFIX, restart = True)
         return 'DONE'
 
 @nottest
diff --git a/src/test/utils/Fabric.py b/src/test/utils/Fabric.py
new file mode 100644
index 0000000..c7a3e21
--- /dev/null
+++ b/src/test/utils/Fabric.py
@@ -0,0 +1,133 @@
+# 
+# Copyright 2016-present Ciena Corporation
+#
+# 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.
+#
+import os, sys
+import json
+import platform
+import subprocess
+from apiclient.maas_client import MAASOAuth, MAASDispatcher, MAASClient
+from paramiko import SSHClient, WarningPolicy, AutoAddPolicy
+from CordTestServer import CORD_TEST_HOST
+
+class FabricMAAS(object):
+    head_node = os.getenv('HEAD_NODE', CORD_TEST_HOST)
+    maas_url = 'http://{}/MAAS/api/1.0/'.format(head_node)
+
+    def __init__(self, api_key = None, url = maas_url):
+        if api_key == None:
+            self.api_key = self.get_api_key()
+        else:
+            self.api_key = api_key
+        self.auth = MAASOAuth(*self.api_key.split(':'))
+        self.url = url
+        self.client = MAASClient(self.auth, MAASDispatcher(), self.url)
+
+    @classmethod
+    def get_api_key(cls):
+        api_key = os.getenv('MAAS_API_KEY', None)
+        if api_key:
+            return api_key
+        cmd = ['maas-region-admin', 'apikey', '--username=cord']
+        try:
+            p = subprocess.Popen(cmd, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
+        except:
+            return 'UNKNOWN'
+        out, err = p.communicate()
+        if err:
+            raise Exception('Cannot get api key for MAAS')
+        return out.strip()
+
+    def get_node_list(self):
+        nodes = self.client.get(u'nodes/', 'list').read()
+        node_list = json.loads(nodes)
+        hosts = [ self.head_node ] +  map(lambda n: n['hostname'], node_list)
+        return hosts
+
+class Fabric(object):
+    entropy = 1
+    simulation = False
+    def __init__(self, node_list, user = 'ubuntu', passwd = 'ubuntu', key_file = None, verbose = False):
+        self.cur_node = None
+        if Fabric.simulation:
+            self.cur_node = FabricMAAS.head_node
+        self.node_list = node_list
+        self.user = user
+        self.passwd = passwd
+        self.key_file = key_file
+        self.verbose = verbose
+        self.client = SSHClient()
+        self.client.load_system_host_keys()
+        self.client.set_missing_host_key_policy(AutoAddPolicy())
+
+    def run_cmd(self, node, neighbor, cmd, simulation = False):
+        if simulation is True:
+            Fabric.entropy = Fabric.entropy ^ 1
+            return bool(Fabric.entropy)
+        if node == self.cur_node:
+            res = os.system(cmd)
+            return res == 0
+        try:
+            self.client.connect(node, username = self.user, key_filename = self.key_file, timeout = 5)
+        except:
+            print('Unable to ssh to node %s for neighbor %s' %(node, neighbor))
+            return False
+        channel = self.client.get_transport().open_session()
+        channel.exec_command(cmd)
+        status = channel.recv_exit_status()
+        channel.close()
+        if self.verbose:
+            print('Cmd %s returned with status %d on node %s for neighbor %s' %(cmd, status, node, neighbor))
+        return status == 0
+
+    def ping_neighbor(self, node, neighbor):
+        cmd = 'ping -c 1 -w 2 {}'.format(neighbor)
+        return self.run_cmd(node, neighbor, cmd, Fabric.simulation)
+
+    def ping_neighbors(self):
+        result_map = []
+        for n in self.node_list:
+            for adj in self.node_list:
+                if adj == n:
+                    continue
+                res = self.ping_neighbor(n, adj)
+                result_map.append((n,adj,res))
+
+        ##report
+        if self.verbose:
+            for node, neighbor, res in result_map:
+                print('Ping from node %s to neighbor %s returned %s\n' %(node, neighbor, res))
+
+        failed_nodes = filter(lambda f: f[2] == False, result_map)
+        return failed_nodes
+
+if __name__ == '__main__':
+    if len(sys.argv) > 1:
+        nodes_file = sys.argv[1]
+        with open(nodes_file, 'r') as fd:
+            nodes = json.load(fd)
+        node_list = nodes['node_list']
+    else:
+        m = FabricMAAS()
+        node_list = m.get_node_list()
+        print('Node list: %s' %node_list)
+    Fabric.simulation = True
+    fab = Fabric(node_list, verbose = False)
+    failed_nodes = fab.ping_neighbors()
+    if failed_nodes:
+        print('Failed nodes: %s' %failed_nodes)
+        for node, neighbor, _ in failed_nodes:
+            print('Ping from node %s to neighbor %s Failed' %(node, neighbor))
+    else:
+        print('Fabric test between nodes %s is successful' %node_list)
diff --git a/src/test/utils/TestManifest.py b/src/test/utils/TestManifest.py
new file mode 100644
index 0000000..2156972
--- /dev/null
+++ b/src/test/utils/TestManifest.py
@@ -0,0 +1,28 @@
+# 
+# Copyright 2016-present Ciena Corporation
+#
+# 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.
+#
+import json
+import os
+import shutil
+
+class TestManifest(object):
+
+    def __init__(self, manifest):
+        self.manifest = manifest
+        with open(self.manifest, 'r') as fd:
+            data = json.load(fd)
+        self.onos_ip = data.get('onos', None)
+        self.radius_ip = data.get('radius', None)
+        self.head_node = data.get('head_node', None)