SEBA-117 Allow FabricCrossconnectServiceInstance to specify westbound fields directly

Change-Id: Ie96fe912ecf68f5c8274c9fcbb2ee339bbe7f19d
diff --git a/xos/synchronizer/steps/sync_fabric_crossconnect_service_instance.py b/xos/synchronizer/steps/sync_fabric_crossconnect_service_instance.py
old mode 100755
new mode 100644
index ee067de..165eda9
--- a/xos/synchronizer/steps/sync_fabric_crossconnect_service_instance.py
+++ b/xos/synchronizer/steps/sync_fabric_crossconnect_service_instance.py
@@ -55,16 +55,16 @@
             'pass': fabric_onos.rest_password
         }
 
-    def make_handle(self, s_tag, west_dpid):
+    def make_handle(self, s_tag, switch_datapath_id):
         # Generate a backend_handle that uniquely identifies the cross connect. ONOS doesn't provide us a handle, so
         # we make up our own. This helps us to detect other FabricCrossconnectServiceInstance using the same
         # entry, as well as to be able to extract the necessary information to delete the entry later.
-        return "%d/%s" % (s_tag, west_dpid)
+        return "%d/%s" % (s_tag, switch_datapath_id)
 
     def extract_handle(self, backend_handle):
-        (s_tag, dpid) = backend_handle.split("/",1)
+        (s_tag, switch_datapath_id) = backend_handle.split("/",1)
         s_tag = int(s_tag)
-        return (s_tag, dpid)
+        return (s_tag, switch_datapath_id)
 
     def range_matches(self, value, pattern):
         value=int(value)
@@ -102,22 +102,30 @@
     def sync_record(self, o):
         self.log.info("Sync'ing Fabric Crossconnect Service Instance", service_instance=o)
 
+        if (o.policed is None) or (o.policed < o.updated):
+            raise DeferredException("Waiting for model_policy to run on fcsi %s" % o.id)
+
         onos = self.get_fabric_onos_info(o)
 
         si = ServiceInstance.objects.get(id=o.id)
 
-        s_tag = si.get_westbound_service_instance_properties("s_tag")
-        dpid = si.get_westbound_service_instance_properties("switch_datapath_id")
-        west_port = si.get_westbound_service_instance_properties("switch_port")
+        if (o.s_tag is None):
+            raise Exception("Cannot sync FabricCrossconnectServiceInstance if s_tag is None on fcsi %s" % o.id)
 
-        bng_mapping = self.find_bng(s_tag = s_tag)
+        if (o.source_port is None):
+            raise Exception("Cannot sync FabricCrossconnectServiceInstance if source_port is None on fcsi %s" % o.id)
+
+        if (not o.switch_datapath_id):
+            raise Exception("Cannot sync FabricCrossconnectServiceInstance if switch_datapath_id is unset on fcsi %s" % o.id)
+
+        bng_mapping = self.find_bng(s_tag = o.s_tag)
         if not bng_mapping:
-            raise Exception("Unable to determine BNG port for s_tag %s" % s_tag)
+            raise Exception("Unable to determine BNG port for s_tag %s" % o.s_tag)
         east_port = bng_mapping.switch_port
 
-        data = { "deviceId": dpid,
-                 "vlanId": s_tag,
-                 "ports": [ int(west_port), int(east_port) ] } 
+        data = { "deviceId": o.switch_datapath_id,
+                 "vlanId": o.s_tag,
+                 "ports": [ int(o.source_port), int(east_port) ] }
 
         url = onos['url'] + '/onos/segmentrouting/xconnect'
 
@@ -128,7 +136,12 @@
         if r.status_code != 200:
             raise Exception("Failed to create fabric crossconnect in ONOS: %s" % r.text)
 
-        o.backend_handle = self.make_handle(s_tag, dpid)
+        # TODO(smbaker): If the o.backend_handle changed, then someone must have changed the
+        #   FabricCrossconnectServiceInstance. If so, then we potentially need to clean up the old
+        #   entry in ONOS. Furthermore, we might want to also save the two port numbers that we used,
+        #   to detect someone changing those.
+
+        o.backend_handle = self.make_handle(o.s_tag, o.switch_datapath_id)
         o.save(update_fields=["backend_handle"])
 
         self.log.info("ONOS response", res=r.text)
@@ -147,9 +160,9 @@
                 return
 
             # backend_handle has everything we need in it to delete this entry.
-            (s_tag, dpid) = self.extract_handle(o.backend_handle)
+            (s_tag, switch_datapath_id) = self.extract_handle(o.backend_handle)
 
-            data = { "deviceId": dpid,
+            data = { "deviceId": switch_datapath_id,
                      "vlanId": s_tag }
 
             url = onos['url'] + '/onos/segmentrouting/xconnect'
diff --git a/xos/synchronizer/steps/test_sync_fabric_crossconnect_service_instance.py b/xos/synchronizer/steps/test_sync_fabric_crossconnect_service_instance.py
old mode 100755
new mode 100644
index a1b3e23..8e4c5a8
--- a/xos/synchronizer/steps/test_sync_fabric_crossconnect_service_instance.py
+++ b/xos/synchronizer/steps/test_sync_fabric_crossconnect_service_instance.py
@@ -111,10 +111,10 @@
 
     def test_make_handle_extract_handle(self):
         h = self.sync_step().make_handle(222, "of:0000000000000201")
-        (s_tag, dpid) = self.sync_step().extract_handle(h)
+        (s_tag, switch_datapath_id) = self.sync_step().extract_handle(h)
 
         self.assertEqual(s_tag, 222)
-        self.assertEqual(dpid, "of:0000000000000201")
+        self.assertEqual(switch_datapath_id, "of:0000000000000201")
 
     def test_get_fabric_onos_init(self):
         fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service)
@@ -180,10 +180,10 @@
             patch.object(BNGPortMapping.objects, "get_items") as bng_objects, \
             patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
 
-            fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service)
+            fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=111, source_port=3,
+                                                    switch_datapath_id="of:0000000000000201", updated=1, policed=2)
 
-            si = self.mock_westbound(fsi, s_tag=111, switch_datapath_id = "of:0000000000000201", switch_port = 3)
-            serviceinstance_objects.return_value = [si]
+            serviceinstance_objects.return_value = [fsi]
 
             bngmapping = BNGPortMapping(s_tag="111", switch_port=4)
             bng_objects.return_value = [bngmapping]
@@ -206,16 +206,72 @@
         with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
             patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
 
-            fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service)
+            fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=111, source_port=3,
+                                                    switch_datapath_id="of:0000000000000201", updated=1, policed=2)
 
-            si = self.mock_westbound(fsi, s_tag="111", switch_datapath_id = "of:0000000000000201", switch_port = 3)
-            serviceinstance_objects.return_value = [si]
+            serviceinstance_objects.return_value = [fsi]
 
             with self.assertRaises(Exception) as e:
                 self.sync_step().sync_record(fsi)
 
             self.assertEqual(e.exception.message, "Unable to determine BNG port for s_tag 111")
 
+    def test_sync_not_policed(self):
+        with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
+            patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
+
+            fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, source_port=3,
+                                                    switch_datapath_id="of:0000000000000201", updated=1, policed=0)
+
+            serviceinstance_objects.return_value = [fsi]
+
+            with self.assertRaises(Exception) as e:
+                self.sync_step().sync_record(fsi)
+
+            self.assertEqual(e.exception.message, "Waiting for model_policy to run on fcsi 7777")
+
+    def test_sync_no_s_tag(self):
+        with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
+            patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
+
+            fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, source_port=3,
+                                                    switch_datapath_id="of:0000000000000201", updated=1, policed=2)
+
+            serviceinstance_objects.return_value = [fsi]
+
+            with self.assertRaises(Exception) as e:
+                self.sync_step().sync_record(fsi)
+
+            self.assertEqual(e.exception.message, "Cannot sync FabricCrossconnectServiceInstance if s_tag is None on fcsi 7777")
+
+    def test_sync_no_switch_datapath_id(self):
+        with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
+            patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
+
+            fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, source_port=3, s_tag=111,
+                                                    updated=1, policed=2)
+
+            serviceinstance_objects.return_value = [fsi]
+
+            with self.assertRaises(Exception) as e:
+                self.sync_step().sync_record(fsi)
+
+            self.assertEqual(e.exception.message, "Cannot sync FabricCrossconnectServiceInstance if switch_datapath_id is unset on fcsi 7777")
+
+    def test_sync_no_source_port(self):
+        with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \
+            patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save:
+
+            fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=111,
+                                                    switch_datapath_id="of:0000000000000201", updated=1, policed=2)
+
+            serviceinstance_objects.return_value = [fsi]
+
+            with self.assertRaises(Exception) as e:
+                self.sync_step().sync_record(fsi)
+
+            self.assertEqual(e.exception.message, "Cannot sync FabricCrossconnectServiceInstance if source_port is None on fcsi 7777")
+
     @requests_mock.Mocker()
     def test_delete(self, m):
         with patch.object(FabricCrossconnectServiceInstance.objects, "get_items") as fcsi_objects, \
@@ -244,7 +300,7 @@
                                                     backend_handle="111/of:0000000000000201",
                                                     enacted=True)
 
-            # Another subscriber using the same (s_tag, dpid) pair
+            # Another subscriber using the same (s_tag, switch_datapath_id) pair
             fsi2 = FabricCrossconnectServiceInstance(id=7778, owner=self.service,
                                                      backend_handle="111/of:0000000000000201",
                                                      enacted=True)