[MCORD-183] Fix Bug Causing Instance not Created when Tenant Made

Change-Id: I32622a09bcf463774c94cacba658d338c00d16de
diff --git a/xos/header.py b/xos/header.py
index ba07ed8..9f497cd 100644
--- a/xos/header.py
+++ b/xos/header.py
@@ -1,5 +1,5 @@
 from django.db import models
-from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool
+from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool, SlicePrivilege, SitePrivilege
 from core.models.plcorebase import StrippedCharField
 import os
 from django.db import models, transaction
@@ -10,12 +10,7 @@
 from core.models.service import LeastLoadedNodeScheduler
 import traceback
 from xos.exceptions import *
+from sets import Set
 from xos.config import Config
 from django.contrib.contenttypes.models import ContentType
 from django.contrib.contenttypes.fields import GenericForeignKey
-
-MCORD_KIND = "vEPC"
-SERVICE_NAME_VERBOSE = 'Virtual Packet Gateway -- Control Service'
-SERVICE_NAME_VERBOSE_PLURAL = 'Virtual Packet Gateway -- Control Services'
-TENANT_NAME_VERBOSE = 'Virtual Packet Gateway -- Control Tenant'
-TENANT_NAME_VERBOSE_PLURAL = 'Virtual Packet Gateway -- Control Tenants'
diff --git a/xos/models.py b/xos/models.py
new file mode 100644
index 0000000..1ee0440
--- /dev/null
+++ b/xos/models.py
@@ -0,0 +1,41 @@
+from core.models.plcorebase import *
+from models_decl import VPGWCService_decl
+from models_decl import VPGWCTenant_decl
+
+class VPGWCService(VPGWCService_decl):
+   class Meta:
+        proxy = True 
+
+class VPGWCTenant(VPGWCTenant_decl):
+   class Meta:
+        proxy = True 
+
+   def __init__(self, *args, **kwargs):
+       vpgwcservice = VPGWCService.get_service_objects().all()
+       # When the tenant is created the default service in the form is set
+       # to be the first created HelloWorldServiceComplete
+       if vpgwcservice:
+           self._meta.get_field(
+                "provider_service").default = vpgwcservice[0].id
+       super(VPGWCTenant, self).__init__(*args, **kwargs)
+
+   def save(self, *args, **kwargs):
+       # Update the instance that was created for this tenant
+       super(VPGWCTenant, self).save(*args, **kwargs)
+       model_policy_vpgwctenant(self.pk)   
+
+   def delete(self, *args, **kwargs):
+       # Delete the instance that was created for this tenant
+       self.cleanup_container()
+       super(VPGWCTenant, self).delete(*args, **kwargs)
+
+def model_policy_vpgwctenant(pk):
+    # This section of code is atomic to prevent race conditions
+    with transaction.atomic():
+        # We find all of the tenants that are waiting to update
+        tenant = VPGWCTenant.objects.select_for_update().filter(pk=pk)
+        if not tenant:
+            return
+        # Since this code is atomic it is safe to always use the first tenant
+        tenant = tenant[0]
+        tenant.manage_container()
diff --git a/xos/vpgwc.xproto b/xos/vpgwc.xproto
index dac3c17..4b6ff74 100644
--- a/xos/vpgwc.xproto
+++ b/xos/vpgwc.xproto
@@ -1,6 +1,7 @@
 option name = "vPGWC";
 option verbose_name = "Virtual Packet Gateway -- Control";
 option kind = "vEPC";
+option legacy = "True";
 
 message VPGWCService (Service) {
     option name = "VPGWCService";