Yunpeng Zhang | a2c2733 | 2017-08-15 23:00:11 -0400 | [diff] [blame] | 1 | from core.models.plcorebase import * |
| 2 | from models_decl import VSGWUService_decl |
Omar Abdelkader | f8e5188 | 2017-09-06 19:52:57 -0500 | [diff] [blame^] | 3 | from models_decl import VSGWUVendor_decl |
Yunpeng Zhang | a2c2733 | 2017-08-15 23:00:11 -0400 | [diff] [blame] | 4 | from models_decl import VSGWUTenant_decl |
| 5 | |
| 6 | from django.db import models |
| 7 | from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool |
| 8 | from core.models.plcorebase import StrippedCharField |
| 9 | import os |
| 10 | from django.db import models, transaction |
| 11 | from django.forms.models import model_to_dict |
| 12 | from django.db.models import * |
| 13 | from operator import itemgetter, attrgetter, methodcaller |
| 14 | from core.models import Tag |
| 15 | from core.models.service import LeastLoadedNodeScheduler |
| 16 | import traceback |
| 17 | from xos.exceptions import * |
| 18 | from xos.config import Config |
Yunpeng Zhang | a2c2733 | 2017-08-15 23:00:11 -0400 | [diff] [blame] | 19 | |
| 20 | class VSGWUService(VSGWUService_decl): |
| 21 | class Meta: |
| 22 | proxy = True |
| 23 | |
Omar Abdelkader | 187b1da | 2017-08-30 10:48:38 -0600 | [diff] [blame] | 24 | def create_tenant(self, **kwargs): |
| 25 | t = VSGWUTenant(kind="vEPC", provider_service=self, connect_method="na", **kwargs) |
| 26 | t.save() |
| 27 | return t |
| 28 | |
Omar Abdelkader | f8e5188 | 2017-09-06 19:52:57 -0500 | [diff] [blame^] | 29 | class VSGWUVendor(VSGWUVendor_decl): |
| 30 | class Meta: |
| 31 | proxy = True |
| 32 | |
Yunpeng Zhang | a2c2733 | 2017-08-15 23:00:11 -0400 | [diff] [blame] | 33 | class VSGWUTenant(VSGWUTenant_decl): |
| 34 | class Meta: |
| 35 | proxy = True |
| 36 | |
| 37 | def __init__(self, *args, **kwargs): |
| 38 | vsgwuservice = VSGWUService.get_service_objects().all() |
| 39 | if vsgwuservice: |
| 40 | self._meta.get_field( |
| 41 | "provider_service").default = vsgwuservice[0].id |
| 42 | super(VSGWUTenant, self).__init__(*args, **kwargs) |
| 43 | |
Omar Abdelkader | f8e5188 | 2017-09-06 19:52:57 -0500 | [diff] [blame^] | 44 | @property |
| 45 | def image(self): |
| 46 | if not self.vsgwu_vendor: |
| 47 | return super(VSGWUTenant, self).image |
| 48 | return self.vsgwu_vendor.image |
| 49 | |
| 50 | def save_instance(self, instance): |
| 51 | if self.vsgwu_vendor: |
| 52 | instance.flavor = self.vsgwu_vendor.flavor |
| 53 | super(VSGWUTenant, self).save_instance(instance) |
| 54 | |
Yunpeng Zhang | a2c2733 | 2017-08-15 23:00:11 -0400 | [diff] [blame] | 55 | def save(self, *args, **kwargs): |
Omar Abdelkader | 187b1da | 2017-08-30 10:48:38 -0600 | [diff] [blame] | 56 | if not self.creator: |
| 57 | if not getattr(self, "caller", None): |
| 58 | raise XOSProgrammingError("VSGWUTenant's self.caller was not set") |
| 59 | self.creator = self.caller |
| 60 | if not self.creator: |
| 61 | raise XOSProgrammingError("VSGWUTenant's self.creator was not set") |
| 62 | |
Yunpeng Zhang | a2c2733 | 2017-08-15 23:00:11 -0400 | [diff] [blame] | 63 | super(VSGWUTenant, self).save(*args, **kwargs) |
| 64 | # This call needs to happen so that an instance is created for this |
| 65 | # tenant is created in the slice. One instance is created per tenant. |
| 66 | model_policy_vsgwutenant(self.pk) |
| 67 | |
| 68 | def delete(self, *args, **kwargs): |
| 69 | # Delete the instance that was created for this tenant |
| 70 | self.cleanup_container() |
| 71 | super(VSGWUTenant, self).delete(*args, **kwargs) |
| 72 | |
| 73 | def model_policy_vsgwutenant(pk): |
| 74 | # This section of code is atomic to prevent race conditions |
| 75 | with transaction.atomic(): |
| 76 | # We find all of the tenants that are waiting to update |
| 77 | tenant = VSGWUTenant.objects.select_for_update().filter(pk=pk) |
| 78 | if not tenant: |
| 79 | return |
| 80 | # Since this code is atomic it is safe to always use the first tenant |
| 81 | tenant = tenant[0] |
| 82 | tenant.manage_container() |
| 83 | |