blob: fe905020dc6c0c25c427eaaaaa0c2a0f39c6b5c5 [file] [log] [blame]
Yunpeng Zhang0048cf42017-08-15 23:01:28 -04001from core.models.plcorebase import *
2from models_decl import VPGWUService_decl
Omar Abdelkader56121752017-09-06 19:40:17 -05003from models_decl import VPGWUVendor_decl
Yunpeng Zhang0048cf42017-08-15 23:01:28 -04004from models_decl import VPGWUTenant_decl
5
6from django.db import models
7from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool, SlicePrivilege, SitePrivilege
8from core.models.plcorebase import StrippedCharField
9import os
10from django.db import models, transaction
11from django.forms.models import model_to_dict
12from django.db.models import *
13from operator import itemgetter, attrgetter, methodcaller
14from core.models import Tag
15from core.models.service import LeastLoadedNodeScheduler
16import traceback
17from xos.exceptions import *
18from sets import Set
19from xos.config import Config
Yunpeng Zhang0048cf42017-08-15 23:01:28 -040020
21class VPGWUService(VPGWUService_decl):
22 class Meta:
23 proxy = True
24
Omar Abdelkadercbcd9432017-08-30 10:45:07 -060025 def create_tenant(self, **kwargs):
26 t = VPGWUTenant(kind="vEPC", provider_service=self, connect_method="na", **kwargs)
27 t.save()
28 return t
29
Omar Abdelkader56121752017-09-06 19:40:17 -050030class VPGWUVendor(VPGWUVendor_decl):
31 class Meta:
32 proxy = True
33
Yunpeng Zhang0048cf42017-08-15 23:01:28 -040034class VPGWUTenant(VPGWUTenant_decl):
35 class Meta:
36 proxy = True
37
38 def __init__(self, *args, **kwargs):
39 vpgwuservice = VPGWUService.get_service_objects().all()
40 # When the tenant is created the default service in the form is set
41 # to be the first created HelloWorldServiceComplete
42 if vpgwuservice:
43 self._meta.get_field(
44 "provider_service").default = vpgwuservice[0].id
45 super(VPGWUTenant, self).__init__(*args, **kwargs)
46
Omar Abdelkader56121752017-09-06 19:40:17 -050047 @property
48 def image(self):
49 if not self.vpgwu_vendor:
50 return super(VPGWUTenant, self).image
51 return self.vpgwu_vendor.image
52
53 def save_instance(self, instance):
54 if self.vpgwu_vendor:
55 instance.flavor = self.vpgwu_vendor.flavor
56 super(VPGWUTenant, self).save_instance(instance)
57
Yunpeng Zhang0048cf42017-08-15 23:01:28 -040058 def save(self, *args, **kwargs):
Omar Abdelkadercbcd9432017-08-30 10:45:07 -060059 if not self.creator:
60 if not getattr(self, "caller", None):
61 raise XOSProgrammingError("VPGWUTenant's self.caller was not set")
62 self.creator = self.caller
63 if not self.creator:
64 raise XOSProgrammingError("VPGWUTenant's self.creator was not set")
65
Yunpeng Zhang0048cf42017-08-15 23:01:28 -040066 # Update the instance that was created for this tenant
67 super(VPGWUTenant, self).save(*args, **kwargs)
68 model_policy_vpgwutenant(self.pk)
69
70 def delete(self, *args, **kwargs):
71 # Delete the instance that was created for this tenant
72 self.cleanup_container()
73 super(VPGWUTenant, self).delete(*args, **kwargs)
74
75def model_policy_vpgwutenant(pk):
76 # This section of code is atomic to prevent race conditions
77 with transaction.atomic():
78 # We find all of the tenants that are waiting to update
79 tenant = VPGWUTenant.objects.select_for_update().filter(pk=pk)
80 if not tenant:
81 return
82 # Since this code is atomic it is safe to always use the first tenant
83 tenant = tenant[0]
84 tenant.manage_container()