[SEBA-516] Using acquire_service_instance method in model_policy
Change-Id: I44f2d9b354d7d5223279daa26cd7dcdc00d4ef4d
diff --git a/xos/synchronizer/model_policies/model_policy_rcordsubscriber.py b/xos/synchronizer/model_policies/model_policy_rcordsubscriber.py
index 43f7cfb..38a13c3 100644
--- a/xos/synchronizer/model_policies/model_policy_rcordsubscriber.py
+++ b/xos/synchronizer/model_policies/model_policy_rcordsubscriber.py
@@ -25,48 +25,36 @@
def handle_update(self, si):
- # FIXME if the status is now pre-provisioned but the subscriber had a service-chain it needs to be removed
- if si.status == "pre-provisioned":
- self.logger.debug(
- "MODEL_POLICY: Skipping chain creation as RCORDSubscriber %s is in 'pre-provisioned' state" %
- si.id)
- return
-
chain = si.subscribed_links.all()
# Already has a chain
- if len(chain) > 0:
- self.logger.debug("MODEL_POLICY: RCORDSubscriber %s is already part of a chain" % si.id)
- 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 si.status != "enabled" and len(chain) > 0:
+ # 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()
- else:
- if si.status != "enabled":
- self.logger.debug("MODEL_POLICY: NOT creating RCORDSubscriber chain for %s" % si.id, status=si.status)
- 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()
+ elif si.status == "enabled":
- 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))
+ 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()
- 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()
+ 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))
+
+ provider_service = link.provider_service.leaf_model
+
+ valid_provider_service_instance = provider_service.validate_links(si)
+ if not valid_provider_service_instance:
+ provider_service.acquire_service_instance(si)
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 3405c6a..374e9be 100644
--- a/xos/synchronizer/model_policies/test_model_policy_rcordsubscriber.py
+++ b/xos/synchronizer/model_policies/test_model_policy_rcordsubscriber.py
@@ -60,13 +60,15 @@
self.policy = RCORDSubscriberPolicy(model_accessor=self.model_accessor)
self.si = Mock(name="myTestSubscriber")
+ self.si.subscribed_links.all.return_value = []
def tearDown(self):
sys.path = self.sys_path_save
- def test_update_pre_provisione(self):
+ def test_update_pre_provisioned(self):
si = self.si
si.status = "pre-provisioned"
+
self.policy.handle_create(si)
with patch.object(VOLTServiceInstance, "save", autospec=True) as save_volt, \
@@ -79,34 +81,41 @@
def test_update_and_do_nothing(self):
si = self.si
si.is_new = False
- si.subscribed_links.all.return_value = ["already", "have", "a", "chain"]
+ si.status = "enabled"
- with patch.object(VOLTServiceInstance, "save", autospec=True) as save_volt, \
- patch.object(ServiceInstanceLink, "save", autospec=True) as save_link:
+ next_si = Mock()
- self.policy.handle_create(si)
- self.assertEqual(save_link.call_count, 0)
- self.assertEqual(save_volt.call_count, 0)
+ link = Mock()
+ link.provider_service.validate_links = Mock(return_value=next_si)
+ link.provider_service.acquire_service_instance = Mock()
+ link.provider_service.leaf_model = link.provider_service
+
+ si.owner.subscribed_dependencies.all.return_value = [link]
+
+ self.policy.handle_create(si)
+
+ link.provider_service.validate_links.assert_called_with(si)
+ link.provider_service.acquire_service_instance.assert_not_called()
def test_create_chain(self):
- volt = Mock()
- volt.get_service_instance_class_name.return_value = "VOLTServiceInstance"
-
- service_dependency = Mock()
- service_dependency.provider_service = volt
-
si = self.si
si.is_new = True
si.status = "enabled"
- si.subscribed_links.all.return_value = []
- si.owner.subscribed_dependencies.all.return_value = [service_dependency]
- with patch.object(VOLTServiceInstance, "save", autospec=True) as save_volt, \
- patch.object(ServiceInstanceLink, "save", autospec=True) as save_link:
+ next_si = Mock()
- self.policy.handle_create(si)
- self.assertEqual(save_link.call_count, 1)
- self.assertEqual(save_volt.call_count, 1)
+ link = Mock()
+ link.provider_service.validate_links = Mock(return_value=None)
+ link.provider_service.acquire_service_instance = Mock()
+ link.provider_service.leaf_model = link.provider_service
+ link.provider_service.get_service_instance_class_name.return_value = "TestClass"
+
+ si.owner.subscribed_dependencies.all.return_value = [link]
+
+ self.policy.handle_create(si)
+
+ link.provider_service.validate_links.assert_called_with(si)
+ link.provider_service.acquire_service_instance.assert_called_with(si)
def test_remove_chain(self):
volt = VOLTServiceInstance()