blob: 555dd0152430e4a11669458c573e4b48771999fa [file] [log] [blame]
Omar Abdelkader7e5b3d32017-08-03 14:50:55 -07001from core.models.plcorebase import *
2from models_decl import VBBUService_decl
3from models_decl import VBBUTenant_decl
4
Omar Abdelkader9bd2c8d2017-08-07 19:21:02 -06005from core.models import Service, TenantWithContainer
6from django.db import transaction
7from django.db.models import *
8
Omar Abdelkader7e5b3d32017-08-03 14:50:55 -07009class VBBUService(VBBUService_decl):
10 class Meta:
11 proxy = True
12
Omar Abdelkader17f6eec2017-08-30 10:39:42 -060013 def create_tenant(self, **kwargs):
14 t = VBBUTenant(kind="vEPC", provider_service=self, connect_method="na", **kwargs)
15 t.save()
16 return t
17
Omar Abdelkader7e5b3d32017-08-03 14:50:55 -070018class VBBUTenant(VBBUTenant_decl):
19 class Meta:
20 proxy = True
21
22 def __init__(self, *args, **kwargs):
23 vbbuservice = VBBUService.get_service_objects().all()
24 if vbbuservice:
25 self._meta.get_field(
26 "provider_service").default = vbbuservice[0].id
27 super(VBBUTenant, self).__init__(*args, **kwargs)
28
29 def save(self, *args, **kwargs):
Omar Abdelkader17f6eec2017-08-30 10:39:42 -060030 if not self.creator:
31 if not getattr(self, "caller", None):
32 raise XOSProgrammingError("VBBUTenant's self.caller was not set")
33 self.creator = self.caller
34 if not self.creator:
35 raise XOSProgrammingError("VBBUTenant's self.creator was not set")
36
Omar Abdelkader7e5b3d32017-08-03 14:50:55 -070037 super(VBBUTenant, self).save(*args, **kwargs)
38 # This call needs to happen so that an instance is created for this
39 # tenant is created in the slice. One instance is created per tenant.
40 model_policy_vbbutenant(self.pk)
41
42 def delete(self, *args, **kwargs):
43 # Delete the instance that was created for this tenant
44 self.cleanup_container()
45 super(VBBUTenant, self).delete(*args, **kwargs)
46
47def model_policy_vbbutenant(pk):
48 # This section of code is atomic to prevent race conditions
49 with transaction.atomic():
50 # We find all of the tenants that are waiting to update
51 tenant = VBBUTenant.objects.select_for_update().filter(pk=pk)
52 if not tenant:
53 return
54 # Since this code is atomic it is safe to always use the first tenant
55 tenant = tenant[0]
56 tenant.manage_container()