CORD-1253 move from VRouterTenant to AddressManagerServiceInstance

Change-Id: I159c81a5ea4dc9c0d4e5c54cc92c3a6a669c1a48
diff --git a/xos/synchronizer/model_policies/model_policy_vsgtenant.py b/xos/synchronizer/model_policies/model_policy_vsgtenant.py
index f8600ce..86f6257 100644
--- a/xos/synchronizer/model_policies/model_policy_vsgtenant.py
+++ b/xos/synchronizer/model_policies/model_policy_vsgtenant.py
@@ -26,32 +26,31 @@
 
     def handle_update(self, tenant):
         self.manage_container(tenant)
-        self.manage_vrouter(tenant)
+        self.manage_address_service_instance(tenant)
         self.cleanup_orphans(tenant)
 
     def handle_delete(self, tenant):
-        if tenant.vrouter:
-            tenant.vrouter.delete()
+        if tenant.address_service_instance:
+            tenant.address_service_instance.delete()
 
-    def manage_vrouter(self, tenant):
+    def manage_address_service_instance(self, tenant):
         if tenant.deleted:
             return
 
-        if tenant.vrouter is None:
-            vrouter = self.allocate_public_service_instance(address_pool_name="addresses_vsg", subscriber_tenant=tenant)
-            vrouter.save()
+        if tenant.address_service_instance is None:
+            address_service_instance = self.allocate_public_service_instance(address_pool_name="addresses_vsg", subscriber_tenant=tenant)
+            address_service_instance.save()
 
     def cleanup_orphans(self, tenant):
-        # ensure vSG only has one vRouter
-        cur_vrouter = tenant.vrouter
+        # ensure vSG only has one AddressManagerServiceInstance
+        cur_asi = tenant.address_service_instance
         for link in tenant.subscribed_links.all():
             # TODO: hardcoded dependency
-            # cast from ServiceInstance to VRouterTenant
-            vrouters = VRouterTenant.objects.filter(id = link.provider_service_instance.id)
-            for vrouter in vrouters:
-                if (not cur_vrouter) or (vrouter.id != cur_vrouter.id):
-                    # print "XXX clean up orphaned vrouter", vrouter
-                    vrouter.delete()
+            # cast from ServiceInstance to AddressManagerServiceInstance
+            asis = AddressManagerServiceInstance.objects.filter(id = link.provider_service_instance.id)
+            for asi in asis:
+                if (not cur_asi) or (asi.id != cur_asi.id):
+                    asi.delete()
 
     def get_vsg_service(self, tenant):
         return VSGService.objects.get(id=tenant.owner.id)
@@ -177,12 +176,12 @@
             # be configured.
             tags = Tag.objects.filter(content_type=instance.self_content_type_id, object_id=instance.id, name="vm_vrouter_tenant")
             if not tags:
-                vrouter = self.allocate_public_service_instance(address_pool_name="addresses_vsg",
-                                                                subscriber_service=tenant.owner)
-                vrouter.set_attribute("tenant_for_instance_id", instance.id)
-                vrouter.save()
+                address_service_instance = self.allocate_public_service_instance(address_pool_name="addresses_vsg",
+                                                                                 subscriber_service=tenant.owner)
+                address_service_instance.set_attribute("tenant_for_instance_id", instance.id)
+                address_service_instance.save()
                 # TODO: potential partial failure
-                tag = Tag(service=tenant.owner, content_type=instance.self_content_type_id, object_id=instance.id, name="vm_vrouter_tenant", value="%d" % vrouter.id)
+                tag = Tag(service=tenant.owner, content_type=instance.self_content_type_id, object_id=instance.id, name="vm_vrouter_tenant", value="%d" % address_service_instance.id)
                 tag.save()
 
             instance.no_sync = False   # allow the synchronizer to run now
diff --git a/xos/synchronizer/model_policies/test_model_policy_vsgtenant.py b/xos/synchronizer/model_policies/test_model_policy_vsgtenant.py
index f647bc9..4951ee8 100644
--- a/xos/synchronizer/model_policies/test_model_policy_vsgtenant.py
+++ b/xos/synchronizer/model_policies/test_model_policy_vsgtenant.py
@@ -176,9 +176,9 @@
     def set_parameter(self, name, value):
         pass
 
-class MockVRouterTenantObjects(MockObjectStore): pass
-class MockVRouterTenant(MockObject):
-    objects = get_MockObjectStore("VRouterTenant")
+class MockAddressManagerServiceInstanceObjects(MockObjectStore): pass
+class MockAddressManagerServiceInstance(MockObject):
+    objects = get_MockObjectStore("AddressManagerServiceInstance")
     public_ip = None
     public_mac = None
     address_pool_id = None
@@ -243,45 +243,45 @@
         model_policy_vsgtenant.NetworkParameter = MockNetworkParameter
 
     @patch.object(VSGTenantPolicy, "manage_container")
-    @patch.object(VSGTenantPolicy, "manage_vrouter")
+    @patch.object(VSGTenantPolicy, "manage_address_service_instance")
     @patch.object(VSGTenantPolicy, "cleanup_orphans")
-    def test_handle_create(self, cleanup_orphans, manage_vrouter, manage_container):
+    def test_handle_create(self, cleanup_orphans, manage_address_service_instance, manage_container):
         self.policy.handle_create(self.tenant)
         manage_container.assert_called_with(self.tenant)
-        manage_vrouter.assert_called_with(self.tenant)
+        manage_address_service_instance.assert_called_with(self.tenant)
         cleanup_orphans.assert_called_with(self.tenant)
 
     @patch.object(VSGTenantPolicy, "manage_container")
-    @patch.object(VSGTenantPolicy, "manage_vrouter")
+    @patch.object(VSGTenantPolicy, "manage_address_service_instance")
     @patch.object(VSGTenantPolicy, "cleanup_orphans")
-    def test_handle_update(self, cleanup_orphans, manage_vrouter, manage_container):
+    def test_handle_update(self, cleanup_orphans, manage_address_service_instance, manage_container):
         self.policy.handle_create(self.tenant)
         manage_container.assert_called_with(self.tenant)
-        manage_vrouter.assert_called_with(self.tenant)
+        manage_address_service_instance.assert_called_with(self.tenant)
         cleanup_orphans.assert_called_with(self.tenant)
 
-    @patch.object(MockVRouterTenant, "delete")
-    def test_handle_delete_vrouter_exist(self, vroutertenant_delete):
-        vrtenant = MockVRouterTenant()
-        self.tenant.vrouter = vrtenant
+    @patch.object(MockAddressManagerServiceInstance, "delete")
+    def test_handle_delete_asi_exist(self, amsi_delete):
+        vrtenant = MockAddressManagerServiceInstance()
+        self.tenant.address_service_instance = vrtenant
         self.policy.handle_delete(self.tenant)
-        vroutertenant_delete.assert_called()
+        amsi_delete.assert_called()
 
-    @patch.object(MockVRouterTenant, "delete")
-    def test_handle_delete_vrouter_noexist(self, vroutertenant_delete):
-        self.tenant.vrouter = None
+    @patch.object(MockAddressManagerServiceInstance, "delete")
+    def test_handle_delete_asi_noexist(self, amsi_delete):
+        self.tenant.address_service_instance = None
         self.policy.handle_delete(self.tenant)
-        vroutertenant_delete.assert_not_called()
+        amsi_delete.assert_not_called()
 
-    @patch.object(MockVRouterTenantObjects, "get_items")
-    @patch.object(MockVRouterTenant, "delete")
-    def test_cleanup_orphans(self, vroutertenant_delete, vroutertenant_objects):
-        vrtenant = MockVRouterTenant(id=1)
-        self.tenant.vrouter = vrtenant
-        some_other_vrtenant = MockVRouterTenant(id=2, subscriber_tenant_id = self.tenant.id)
-        vroutertenant_objects.get_items = [some_other_vrtenant]
+    @patch.object(MockAddressManagerServiceInstanceObjects, "get_items")
+    @patch.object(MockAddressManagerServiceInstance, "delete")
+    def test_cleanup_orphans(self, amsi_delete, amsi_objects):
+        vrtenant = MockAddressManagerServiceInstance(id=1)
+        self.tenant.address_service_instance = vrtenant
+        some_other_vrtenant = MockAddressManagerServiceInstance(id=2, subscriber_tenant_id = self.tenant.id)
+        amsi_objects.get_items = [some_other_vrtenant]
         self.policy.handle_delete(self.tenant)
-        vroutertenant_delete.assert_called()
+        amsi_delete.assert_called()
 
     @patch.object(MockTag, "objects")
     def test_find_instance_for_s_tag_noexist(self, tag_objects):
@@ -430,7 +430,7 @@
                               pick, get_psi, get_image, tenant_save, volt,
                               vsgservice_objects, flavor_objects, node_objects, npt_objects):
         # setup mocks
-        vrtenant = MockVRouterTenant(public_ip="1.2.3.4", public_mac="01:02:03:04:05:06")
+        vrtenant = MockAddressManagerServiceInstance(public_ip="1.2.3.4", public_mac="01:02:03:04:05:06")
         vsgservice=MockVSGService(name="myvsgservice", id=1, slices=MockObjectList(initial=[self.slice]))
         vsgservice_objects.return_value = [vsgservice]
         self.tenant.owner = vsgservice
@@ -474,7 +474,7 @@
         self.assertEqual(tag.value, "222")
         self.assertEqual(tag.object_id, instance.id)
 
-        # The instance should have a tag pointing to its vrouter
+        # The instance should have a tag pointing to its address_service_instance
         tag = MockTag.objects.get(name="vm_vrouter_tenant")
         self.assertNotEqual(tag.value, vrtenant.id)
         self.assertEqual(tag.object_id, instance.id)
@@ -483,13 +483,13 @@
         get_psi.assert_called()
 
     @patch.object(VSGTenantPolicy, "allocate_public_service_instance")
-    def test_manage_vrouter(self, get_psi):
-        vrtenant = MockVRouterTenant(public_ip="1.2.3.4", public_mac="01:02:03:04:05:06")
+    def test_manage_address_service_instance(self, get_psi):
+        vrtenant = MockAddressManagerServiceInstance(public_ip="1.2.3.4", public_mac="01:02:03:04:05:06")
         get_psi.return_value = vrtenant
 
-        self.tenant.vrouter = None
+        self.tenant.address_service_instance = None
 
-        self.policy.manage_vrouter(self.tenant)
+        self.policy.manage_address_service_instance(self.tenant)
 
         get_psi.assert_called_with(address_pool_name="addresses_vsg", subscriber_tenant=self.tenant)