[CORD-3091] Pulling NNI and PON ports

Change-Id: I001782ffd8a092371f4939869c9c6dda2360bf98
diff --git a/xos/synchronizer/steps/sync_olt_device.py b/xos/synchronizer/steps/sync_olt_device.py
index 5997de5..8ebef89 100644
--- a/xos/synchronizer/steps/sync_olt_device.py
+++ b/xos/synchronizer/steps/sync_olt_device.py
@@ -17,7 +17,7 @@
 import requests
 from multistructlog import create_logger
 from requests.auth import HTTPBasicAuth
-from synchronizers.new_base.SyncInstanceUsingAnsible import SyncStep
+from synchronizers.new_base.syncstep import SyncStep, DeferredException
 from synchronizers.new_base.modelaccessor import OLTDevice, model_accessor
 from xosconfig import Config
 
@@ -115,11 +115,17 @@
 
     def configure_onos(self, model):
 
+        log.info("Adding OLT device in onos-voltha", object=str(model), **model.tologdict())
+
         onos_voltha = Helpers.get_onos_voltha_info(model.volt_service)
         onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass'])
 
-        # For now, we assume that each OLT has only one port
-        vlan = model.ports.all()[0].s_tag
+        try:
+            # NOTE For now, we assume that each OLT has only one pon port
+            vlan = model.pon_ports.all()[0].s_tag
+        except Exception as e:
+            raise DeferredException("Waiting for pon_ports to come up")
+
 
         # Add device info to onos-voltha
         data = {
@@ -162,34 +168,34 @@
 
         self.configure_onos(model)
 
-    def delete_record(self, o):
-        log.info("Deleting OLT device", object=str(o), **o.tologdict())
+    def delete_record(self, model):
+        log.info("Deleting OLT device", object=str(model), **model.tologdict())
 
-        voltha = Helpers.get_voltha_info(o.volt_service)
-        onos_voltha = Helpers.get_onos_voltha_info(o.volt_service)
+        voltha = Helpers.get_voltha_info(model.volt_service)
+        onos_voltha = Helpers.get_onos_voltha_info(model.volt_service)
         onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass'])
 
-        if not o.device_id:
-            log.error("OLTDevice %s has no device_id" % o.name)
+        if not model.device_id:
+            log.error("OLTDevice %s has no device_id" % model.name)
         else:
             # Disable the OLT device
-            request = requests.post("%s:%d/api/v1/devices/%s/disable" % (voltha['url'], voltha['port'], o.device_id))
+            request = requests.post("%s:%d/api/v1/devices/%s/disable" % (voltha['url'], voltha['port'], model.device_id))
 
             if request.status_code != 200:
-                log.error("Failed to disable OLT device in VOLTHA: %s - %s" % (o.name, o.device_id), rest_response=request.text, rest_status_code=request.status_code)
+                log.error("Failed to disable OLT device in VOLTHA: %s - %s" % (model.name, model.device_id), rest_response=request.text, rest_status_code=request.status_code)
                 raise Exception("Failed to disable OLT device in VOLTHA")
 
             # Delete the OLT device
-            request = requests.delete("%s:%d/api/v1/devices/%s/delete" % (voltha['url'], voltha['port'], o.device_id))
+            request = requests.delete("%s:%d/api/v1/devices/%s/delete" % (voltha['url'], voltha['port'], model.device_id))
 
             if request.status_code != 200:
-                log.error("Failed to delete OLT device from VOLTHA: %s - %s" % (o.name, o.device_id), rest_response=request.text, rest_status_code=request.status_code)
+                log.error("Failed to delete OLT device from VOLTHA: %s - %s" % (model.name, model.device_id), rest_response=request.text, rest_status_code=request.status_code)
                 raise Exception("Failed to delete OLT device from VOLTHA")
 
             # Remove the device from ONOS
             request = requests.delete("%s:%d/onos/v1/network/configuration/devices/%s" % (
-            onos_voltha['url'], onos_voltha['port'], o.of_id), auth=onos_voltha_basic_auth)
+                onos_voltha['url'], onos_voltha['port'], model.of_id), auth=onos_voltha_basic_auth)
 
             if request.status_code != 204:
-                log.error("Failed to remove OLT device from ONOS: %s - %s" % (o.name, o.of_id), rest_response=request.text, rest_status_code=request.status_code)
+                log.error("Failed to remove OLT device from ONOS: %s - %s" % (model.name, model.of_id), rest_response=request.text, rest_status_code=request.status_code)
                 raise Exception("Failed to remove OLT device from ONOS")
diff --git a/xos/synchronizer/steps/test_sync_olt_device.py b/xos/synchronizer/steps/test_sync_olt_device.py
index 55d477e..4d785b5 100644
--- a/xos/synchronizer/steps/test_sync_olt_device.py
+++ b/xos/synchronizer/steps/test_sync_olt_device.py
@@ -53,6 +53,7 @@
 
 class TestSyncOLTDevice(unittest.TestCase):
     def setUp(self):
+        global DeferredException
         self.sys_path_save = sys.path
         sys.path.append(xos_dir)
         sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
@@ -74,7 +75,7 @@
                                                          get_models_fn("../profiles/rcord", "rcord.xproto")])
 
         import synchronizers.new_base.modelaccessor
-        from sync_olt_device import SyncOLTDevice
+        from sync_olt_device import SyncOLTDevice, DeferredException
         self.sync_step = SyncOLTDevice
 
         pon_port = Mock()
@@ -108,7 +109,7 @@
 
         o.save.return_value = "Saved"
 
-        o.ports.all.return_value = [pon_port]
+        o.pon_ports.all.return_value = [pon_port]
 
         self.o = o
 
@@ -221,5 +222,16 @@
 
         # We don't need to assert here if there are no exceptions happening
 
+    def test_deferred_for_port(self):
+        self.o.pon_ports.all.side_effect = Exception
+        with self.assertRaises(DeferredException) as e:
+            self.sync_step().configure_onos(self.o)
+        self.assertEqual(e.exception.message, "Waiting for pon_ports to come up")
+
+        self.o.pon_ports.all.return_value = []
+        with self.assertRaises(DeferredException) as e:
+            self.sync_step().configure_onos(self.o)
+        self.assertEqual(e.exception.message, "Waiting for pon_ports to come up")
+
 if __name__ == "__main__":
     unittest.main()