[SEBA-164] Removing subscriber chain if not authenitcated

Change-Id: I9cd5b1058076f34392e89291b7ce4882f6e29376
diff --git a/xos/synchronizer/model_policies/model_policy_rcordsubscriber.py b/xos/synchronizer/model_policies/model_policy_rcordsubscriber.py
index f50c7d7..b2de202 100644
--- a/xos/synchronizer/model_policies/model_policy_rcordsubscriber.py
+++ b/xos/synchronizer/model_policies/model_policy_rcordsubscriber.py
@@ -31,26 +31,33 @@
         chain = si.subscribed_links.all()
 
         # Already has a chain
-        if len(chain) > 0 and not si.is_new:
+        if len(chain) > 0:
             self.logger.debug("MODEL_POLICY: RCORDSubscriber %s is already part of a chain" % si.id)
-            return
+            if si.status == "awaiting-auth" or si.status == "auth-failed" or si.status == "disabled":
+                # delete chain
+                self.logger.debug("MODEL_POLICY: deleting RCORDSubscriber chain from %s" % si.id, status=si.status)
+                for link in chain:
+                    self.logger.debug("Removing link %s" % link.id, provider_service=link.provider_service_instance.leaf_model, subscriber_service=link.subscriber_service_instance.leaf_model)
+                    link.delete()
+                    link.provider_service_instance.leaf_model.delete()
 
-        # if it does not have a chain,
-        # Find links to the next element in the service chain
-        # and create one
+        else:
+            self.logger.debug("MODEL_POLICY: creating RCORDSubscriber chain for %s" % si.id, status=si.status)
+            # if it does not have a chain,
+            # Find links to the next element in the service chain
+            # and create one
+            links = si.owner.subscribed_dependencies.all()
 
-        links = si.owner.subscribed_dependencies.all()
+            for link in links:
+                si_class = link.provider_service.get_service_instance_class_name()
+                self.logger.info("MODEL_POLICY: RCORDSubscriber %s creating %s" % (si, si_class))
 
-        for link in links:
-            si_class = link.provider_service.get_service_instance_class_name()
-            self.logger.info("MODEL_POLICY: RCORDSubscriber %s creating %s" % (si, si_class))
-
-            eastbound_si_class = model_accessor.get_model_class(si_class)
-            eastbound_si = eastbound_si_class()
-            eastbound_si.owner_id = link.provider_service_id
-            eastbound_si.save()
-            link = ServiceInstanceLink(provider_service_instance=eastbound_si, subscriber_service_instance=si)
-            link.save()
+                eastbound_si_class = model_accessor.get_model_class(si_class)
+                eastbound_si = eastbound_si_class()
+                eastbound_si.owner_id = link.provider_service_id
+                eastbound_si.save()
+                link = ServiceInstanceLink(provider_service_instance=eastbound_si, subscriber_service_instance=si)
+                link.save()
 
     def handle_delete(self, si):
         pass
diff --git a/xos/synchronizer/model_policies/test_model_policy_rcordsubscriber.py b/xos/synchronizer/model_policies/test_model_policy_rcordsubscriber.py
index 895a254..cd92964 100644
--- a/xos/synchronizer/model_policies/test_model_policy_rcordsubscriber.py
+++ b/xos/synchronizer/model_policies/test_model_policy_rcordsubscriber.py
@@ -99,7 +99,7 @@
             self.assertEqual(save_link.call_count, 0)
             self.assertEqual(save_volt.call_count, 0)
 
-    def test_create(self):
+    def test_create_chain(self):
         volt = Mock()
         volt.get_service_instance_class_name.return_value = "VOLTServiceInstance"
 
@@ -118,6 +118,28 @@
             self.assertEqual(save_link.call_count, 1)
             self.assertEqual(save_volt.call_count, 1)
 
+    def test_remove_chain(self):
+        volt = VOLTServiceInstance()
+        volt.name = "volt"
+
+        link = ServiceInstanceLink()
+        link.subscriber_service_instance= self.si
+        link.provider_service_instance = volt
+        link.provider_service_instance.leaf_model = volt
+
+
+        si = self.si
+        si.is_new = False
+        si.status = "awaiting-auth"
+        si.subscribed_links.all.return_value = [link]
+
+        with patch.object(VOLTServiceInstance, "delete", autospec=True) as delete_volt, \
+             patch.object(ServiceInstanceLink, "delete", autospec=True) as delete_link:
+
+            self.policy.handle_create(si)
+            self.assertEqual(delete_link.call_count, 1)
+            self.assertEqual(delete_volt.call_count, 1)
+
 
 if __name__ == '__main__':
     unittest.main()