blob: a2924ca69c5b45b048932f8fa9480006ff81835e [file] [log] [blame]
Yunpeng Zhang0048cf42017-08-15 23:01:28 -04001from core.models.plcorebase import *
2from models_decl import VPGWUService_decl
3from models_decl import VPGWUTenant_decl
4
5from django.db import models
6from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool, SlicePrivilege, SitePrivilege
7from core.models.plcorebase import StrippedCharField
8import os
9from django.db import models, transaction
10from django.forms.models import model_to_dict
11from django.db.models import *
12from operator import itemgetter, attrgetter, methodcaller
13from core.models import Tag
14from core.models.service import LeastLoadedNodeScheduler
15import traceback
16from xos.exceptions import *
17from sets import Set
18from xos.config import Config
Yunpeng Zhang0048cf42017-08-15 23:01:28 -040019
20class VPGWUService(VPGWUService_decl):
21 class Meta:
22 proxy = True
23
Omar Abdelkadercbcd9432017-08-30 10:45:07 -060024 def create_tenant(self, **kwargs):
25 t = VPGWUTenant(kind="vEPC", provider_service=self, connect_method="na", **kwargs)
26 t.save()
27 return t
28
Yunpeng Zhang0048cf42017-08-15 23:01:28 -040029class VPGWUTenant(VPGWUTenant_decl):
30 class Meta:
31 proxy = True
32
33 def __init__(self, *args, **kwargs):
34 vpgwuservice = VPGWUService.get_service_objects().all()
35 # When the tenant is created the default service in the form is set
36 # to be the first created HelloWorldServiceComplete
37 if vpgwuservice:
38 self._meta.get_field(
39 "provider_service").default = vpgwuservice[0].id
40 super(VPGWUTenant, self).__init__(*args, **kwargs)
41
42 def save(self, *args, **kwargs):
Omar Abdelkadercbcd9432017-08-30 10:45:07 -060043 if not self.creator:
44 if not getattr(self, "caller", None):
45 raise XOSProgrammingError("VPGWUTenant's self.caller was not set")
46 self.creator = self.caller
47 if not self.creator:
48 raise XOSProgrammingError("VPGWUTenant's self.creator was not set")
49
Yunpeng Zhang0048cf42017-08-15 23:01:28 -040050 # Update the instance that was created for this tenant
51 super(VPGWUTenant, self).save(*args, **kwargs)
52 model_policy_vpgwutenant(self.pk)
53
54 def delete(self, *args, **kwargs):
55 # Delete the instance that was created for this tenant
56 self.cleanup_container()
57 super(VPGWUTenant, self).delete(*args, **kwargs)
58
59def model_policy_vpgwutenant(pk):
60 # This section of code is atomic to prevent race conditions
61 with transaction.atomic():
62 # We find all of the tenants that are waiting to update
63 tenant = VPGWUTenant.objects.select_for_update().filter(pk=pk)
64 if not tenant:
65 return
66 # Since this code is atomic it is safe to always use the first tenant
67 tenant = tenant[0]
68 tenant.manage_container()