Omar Abdelkader | 6048031 | 2017-08-03 14:55:20 -0700 | [diff] [blame] | 1 | from core.models.plcorebase import * |
| 2 | from models_decl import VSGWService_decl |
| 3 | from models_decl import VSGWTenant_decl |
| 4 | |
Omar Abdelkader | 6b01723 | 2017-08-07 19:48:57 -0600 | [diff] [blame] | 5 | from django.db import models |
| 6 | from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool |
| 7 | from core.models.plcorebase import StrippedCharField |
| 8 | import os |
| 9 | from django.db import models, transaction |
| 10 | from django.forms.models import model_to_dict |
| 11 | from django.db.models import * |
| 12 | from operator import itemgetter, attrgetter, methodcaller |
| 13 | from core.models import Tag |
| 14 | from core.models.service import LeastLoadedNodeScheduler |
| 15 | import traceback |
| 16 | from xos.exceptions import * |
| 17 | from xos.config import Config |
| 18 | from django.contrib.contenttypes.models import ContentType |
| 19 | from django.contrib.contenttypes.fields import GenericForeignKey |
| 20 | |
Omar Abdelkader | 6048031 | 2017-08-03 14:55:20 -0700 | [diff] [blame] | 21 | class VSGWService(VSGWService_decl): |
| 22 | class Meta: |
| 23 | proxy = True |
| 24 | |
Omar Abdelkader | 21b7541 | 2017-08-15 16:59:06 -0600 | [diff] [blame^] | 25 | def create_tenant(self, **kwargs): |
| 26 | t = VSGWTenant(kind="vEPC", provider_service=self, connect_method="na", tenant_message="vsgw tenant in service chain", **kwargs) |
| 27 | t.save() |
| 28 | return t |
| 29 | |
Omar Abdelkader | 6048031 | 2017-08-03 14:55:20 -0700 | [diff] [blame] | 30 | class VSGWTenant(VSGWTenant_decl): |
| 31 | class Meta: |
| 32 | proxy = True |
| 33 | |
| 34 | def __init__(self, *args, **kwargs): |
| 35 | vsgwservice = VSGWService.get_service_objects().all() |
| 36 | if vsgwservice: |
| 37 | self._meta.get_field( |
| 38 | "provider_service").default = vsgwservice[0].id |
| 39 | super(VSGWTenant, self).__init__(*args, **kwargs) |
| 40 | |
| 41 | def save(self, *args, **kwargs): |
Omar Abdelkader | 21b7541 | 2017-08-15 16:59:06 -0600 | [diff] [blame^] | 42 | if not self.creator: |
| 43 | if not getattr(self, "caller", None): |
| 44 | raise XOSProgrammingError("VSGWTenant's self.caller was not set") |
| 45 | self.creator = self.caller |
| 46 | if not self.creator: |
| 47 | raise XOSProgrammingError("VSGWTenant's self.creator was not set") |
| 48 | |
Omar Abdelkader | 6048031 | 2017-08-03 14:55:20 -0700 | [diff] [blame] | 49 | super(VSGWTenant, self).save(*args, **kwargs) |
| 50 | # This call needs to happen so that an instance is created for this |
| 51 | # tenant is created in the slice. One instance is created per tenant. |
| 52 | model_policy_vsgwtenant(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(VSGWTenant, self).delete(*args, **kwargs) |
| 58 | |
| 59 | def model_policy_vsgwtenant(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 = VSGWTenant.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() |
| 69 | |