CORD-1253 move from VRouterTenant to AddressManagerServiceInstance

Change-Id: I159c81a5ea4dc9c0d4e5c54cc92c3a6a669c1a48
diff --git a/xos/attic/header.py b/xos/attic/header.py
index d3d30ed..7883c2d 100644
--- a/xos/attic/header.py
+++ b/xos/attic/header.py
@@ -24,7 +24,7 @@
 from operator import itemgetter, attrgetter, methodcaller
 from core.models import Tag
 from core.models.service import LeastLoadedNodeScheduler
-from services.vrouter.models import VRouterService, VRouterTenant
+from services.addressmanager.models import AddressManagerService, AddressManagerServiceInstance
 import traceback
 from xos.exceptions import *
 from django.contrib.contenttypes.models import ContentType
diff --git a/xos/attic/vsgtenant_model.py b/xos/attic/vsgtenant_model.py
index f343cc3..22a1a9a 100644
--- a/xos/attic/vsgtenant_model.py
+++ b/xos/attic/vsgtenant_model.py
@@ -20,25 +20,25 @@
 
 def __init__(self, *args, **kwargs):
     super(VSGTenant, self).__init__(*args, **kwargs)
-    self.cached_vrouter=None
+    self.cached_address_service_instance=None
 
 @property
-def vrouter(self):
-    vrouter = self.get_newest_subscribed_tenant(VRouterTenant)
-    if not vrouter:
+def address_service_instance(self):
+    address_service_instance = self.get_newest_subscribed_tenant(AddressManagerServiceInstance)
+    if not address_service_instance:
         return None
 
     # always return the same object when possible
-    if (self.cached_vrouter) and (self.cached_vrouter.id == vrouter.id):
-        return self.cached_vrouter
+    if (self.cached_address_service_instance) and (self.cached_address_service_instance.id == address_service_instance.id):
+        return self.cached_address_service_instance
 
-    vrouter.caller = self.creator
-    self.cached_vrouter = vrouter
-    return vrouter
+    address_service_instance.caller = self.creator
+    self.cached_address_service_instance = address_service_instance
+    return address_service_instance
 
-@vrouter.setter
-def vrouter(self, value):
-    raise XOSConfigurationError("VSGTenant.vrouter setter is not implemented")
+@address_service_instance.setter
+def address_service_instance(self, value):
+    raise XOSConfigurationError("VSGTenant.address_service_instance setter is not implemented")
 
 @property
 def volt(self):
@@ -61,37 +61,37 @@
     else:
         return "no-instance"
 
-def get_vrouter_field(self, name, default=None):
-    if self.vrouter:
-        return getattr(self.vrouter, name, default)
+def get_address_service_instance_field(self, name, default=None):
+    if self.address_service_instance:
+        return getattr(self.address_service_instance, name, default)
     else:
         return default
 
 @property
 def wan_container_ip(self):
-    return self.get_vrouter_field("public_ip", None)
+    return self.get_address_service_instance_field("public_ip", None)
 
 @property
 def wan_container_mac(self):
-    return self.get_vrouter_field("public_mac", None)
+    return self.get_address_service_instance_field("public_mac", None)
 
 @property
 def wan_container_netbits(self):
-    return self.get_vrouter_field("netbits", None)
+    return self.get_address_service_instance_field("netbits", None)
 
 @property
 def wan_container_gateway_ip(self):
-    return self.get_vrouter_field("gateway_ip", None)
+    return self.get_address_service_instance_field("gateway_ip", None)
 
 @property
 def wan_container_gateway_mac(self):
-    return self.get_vrouter_field("gateway_mac", None)
+    return self.get_address_service_instance_field("gateway_mac", None)
 
 @property
 def wan_vm_ip(self):
     tags = Tag.objects.filter(content_type=self.instance.get_content_type_key(), object_id=self.instance.id, name="vm_vrouter_tenant")
     if tags:
-        tenant = VRouterTenant.objects.get(id=tags[0].value)
+        tenant = AddressManagerServiceInstance.objects.get(id=tags[0].value)
         return tenant.public_ip
     else:
         raise Exception("no vm_vrouter_tenant tag for instance %s" % o.instance)
@@ -100,7 +100,7 @@
 def wan_vm_mac(self):
     tags = Tag.objects.filter(content_type=self.instance.get_content_type_key(), object_id=self.instance.id, name="vm_vrouter_tenant")
     if tags:
-        tenant = VRouterTenant.objects.get(id=tags[0].value)
+        tenant = AddressManagerServiceInstance.objects.get(id=tags[0].value)
         return tenant.public_mac
     else:
         raise Exception("no vm_vrouter_tenant tag for instance %s" % o.instance)
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)