Omar Abdelkader | 1d9c319 | 2017-08-03 14:54:25 -0700 | [diff] [blame] | 1 | from core.models.plcorebase import * |
| 2 | from models_decl import VMMEService_decl |
| 3 | from models_decl import VMMETenant_decl |
| 4 | |
Omar Abdelkader | 2c16284 | 2017-08-07 19:41:59 -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 |
Omar Abdelkader | d7fc4a0 | 2017-08-15 16:25:53 -0600 | [diff] [blame] | 15 | from services.vsgw.models import VSGWService, VSGWTenant |
| 16 | from services.vpgwc.models import VPGWCService, VPGWCTenant |
| 17 | from services.vhss.models import VHSSService, VHSSTenant |
Omar Abdelkader | 2c16284 | 2017-08-07 19:41:59 -0600 | [diff] [blame] | 18 | import traceback |
| 19 | from xos.exceptions import * |
| 20 | from xos.config import Config |
| 21 | from django.contrib.contenttypes.models import ContentType |
| 22 | from django.contrib.contenttypes.fields import GenericForeignKey |
| 23 | |
Omar Abdelkader | 1d9c319 | 2017-08-03 14:54:25 -0700 | [diff] [blame] | 24 | class VMMEService(VMMEService_decl): |
| 25 | class Meta: |
| 26 | proxy = True |
| 27 | |
| 28 | class VMMETenant(VMMETenant_decl): |
| 29 | class Meta: |
| 30 | proxy = True |
| 31 | |
| 32 | def __init__(self, *args, **kwargs): |
Omar Abdelkader | d7fc4a0 | 2017-08-15 16:25:53 -0600 | [diff] [blame] | 33 | vmmeservices = VMMEService.get_service_objects().all() |
| 34 | if vmmeservices: |
| 35 | self._meta.get_field("provider_service").default = vmmeservices[0].id |
Omar Abdelkader | 1d9c319 | 2017-08-03 14:54:25 -0700 | [diff] [blame] | 36 | super(VMMETenant, self).__init__(*args, **kwargs) |
Omar Abdelkader | d7fc4a0 | 2017-08-15 16:25:53 -0600 | [diff] [blame] | 37 | self.cached_vsgw = None |
| 38 | self.cached_vpgwc = None |
| 39 | self.cached_vhss = None |
| 40 | |
| 41 | @property |
| 42 | def vsgw(self): |
| 43 | vsgw = self.get_newest_subscribed_tenant(VSGWTenant) |
| 44 | if not vsgw: |
| 45 | return None |
| 46 | |
| 47 | if (self.cached_vsgw) and (self.cached_vsgw.id == vsgw.id): |
| 48 | return self.cached_vsgw |
| 49 | |
| 50 | vsgw.caller = self.creator |
| 51 | self.cached_vsgw = vsgw |
| 52 | return vsgw |
| 53 | |
| 54 | @vsgw.setter |
| 55 | def vsgw(self, value): |
| 56 | raise XOSConfigurationError("VMMETenant.vsgw setter is not implemeneted") |
| 57 | |
| 58 | @property |
| 59 | def vpgwc(self): |
| 60 | vpgwc = self.get_newest_subscribed_tenant(VPGWCTenant) |
| 61 | if not vpgwc: |
| 62 | return None |
| 63 | |
| 64 | if (self.cached_vpgwc) and (self.cached_vpgwc.id == vpgwc.id): |
| 65 | return self.cached_vpgwc |
| 66 | |
| 67 | vpgwc.caller = self.creator |
| 68 | self.cached_vpgwc = vpgwc |
| 69 | return vpgwc |
| 70 | |
| 71 | @vpgwc.setter |
| 72 | def vpgwc(self, value): |
| 73 | raise XOSConfigurationError("VMMETenant.vpgwc setter is not implemeneted") |
| 74 | |
| 75 | @property |
| 76 | def vhss(self): |
| 77 | vhss = self.get_newest_subscribed_tenant(VHSSTenant) |
| 78 | if not vhss: |
| 79 | return None |
| 80 | |
| 81 | if (self.cached_vhss) and (self.cached_vhss.id == vhss.id): |
| 82 | return self.cached_vhss |
| 83 | |
| 84 | vhss.caller = self.creator |
| 85 | self.cached_vhss = vhss |
| 86 | return vhss |
| 87 | |
| 88 | @vhss.setter |
| 89 | def vhss(self, value): |
| 90 | raise XOSConfigurationError("VMMETenant.vhss setter is not implemeneted") |
| 91 | |
| 92 | # This model breaks down if there are multiple service objects |
| 93 | def get_vsgw_service(self): |
| 94 | vsgwservices = VSGWService.get_service_objects().all() |
| 95 | if not vsgwservices: |
| 96 | raise XOSConfigurationError("No VSGW Services available") |
| 97 | return vsgwservices[0] |
| 98 | |
| 99 | def get_vpgwc_service(self): |
| 100 | vpgwcservices = VPGWCService.get_service_objects().all() |
| 101 | if not vpgwcservices: |
| 102 | raise XOSConfigurationError("No VPGWC Services available") |
| 103 | return vpgwcservices[0] |
| 104 | |
| 105 | def get_vhss_service(self): |
| 106 | vhssservices = VHSSService.get_service_objects().all() |
| 107 | if not vhssservices: |
| 108 | raise XOSConfigurationError("No VHSS Services available") |
| 109 | return vhssservices[0] |
| 110 | |
| 111 | def manage_vsgw(self): |
| 112 | # Each vMME object owns exactly one VSGWTenant object |
| 113 | if self.deleted: |
| 114 | return |
| 115 | |
| 116 | if self.vsgw is None: |
| 117 | vsgw = self.get_vsgw_service().create_tenant(subscriber_tenant=self, creator=self.creator) |
| 118 | |
| 119 | def manage_vpgwc(self): |
| 120 | # Each vMME object owns exactly one VPGWCTenant object |
| 121 | if self.deleted: |
| 122 | return |
| 123 | |
| 124 | if self.vpgwc is None: |
| 125 | vpgwc = self.get_vpgwc_service().create_tenant(subscriber_tenant=self, creator=self.creator) |
| 126 | |
| 127 | def manage_vhss(self): |
| 128 | # Each vMME object owns exactly one VHSSTenant object |
| 129 | if self.deleted: |
| 130 | return |
| 131 | |
| 132 | if self.vhss is None: |
| 133 | vhss = self.get_vhss_service().create_tenant(subscriber_tenant=self, creator=self.creator) |
| 134 | |
| 135 | def cleanup_vsgw(self): |
| 136 | if self.vsgw: |
| 137 | self.vsgw.delete() |
| 138 | |
| 139 | def cleanup_vpgwc(self): |
| 140 | if self.vpgwc: |
| 141 | self.vpgwc.delete() |
| 142 | |
| 143 | def cleanup_vhss(self): |
| 144 | if self.vhss: |
| 145 | self.vhss.delete() |
| 146 | |
| 147 | def cleanup_orphans(self): |
| 148 | # ensure vMME only has one vSGW, vPGWC, and vHSS |
| 149 | cur_vsgw = self.vsgw |
| 150 | cur_vpgwc = self.vpgwc |
| 151 | cur_vhss = self.vhss |
| 152 | |
| 153 | for vsgw in list(self.get_subscribed_tenants(VSGWTenant)): |
| 154 | if (not cur_vsgw) or (vsgw.id != cur_vsgw.id): |
| 155 | vsgw.delete() |
| 156 | |
| 157 | for vpgwc in list(self.get_subscribed_tenants(VPGWCTenant)): |
| 158 | if (not cur_vpgwc) or (vpgwc.id != cur_vpgwc.id): |
| 159 | vpgwc.delete() |
| 160 | |
| 161 | for vhss in list(self.get_subscribed_tenants(VHSSTenant)): |
| 162 | if (not cur_vhss) or (vhss.id != cur_vhss.id): |
| 163 | vhss.delete() |
| 164 | |
| 165 | if self.orig_instance_id and (self.orig_instance_id != self.get_attribute("instance_id")): |
| 166 | instances = Instance.objects.filter(id=self.orig_instance.id) |
| 167 | if instances: |
| 168 | instances[0].delete() |
Omar Abdelkader | 1d9c319 | 2017-08-03 14:54:25 -0700 | [diff] [blame] | 169 | |
| 170 | def save(self, *args, **kwargs): |
| 171 | super(VMMETenant, self).save(*args, **kwargs) |
| 172 | # This call needs to happen so that an instance is created for this |
| 173 | # tenant is created in the slice. One instance is created per tenant. |
| 174 | model_policy_vmmetenant(self.pk) |
| 175 | |
| 176 | def delete(self, *args, **kwargs): |
| 177 | # Delete the instance that was created for this tenant |
Omar Abdelkader | d7fc4a0 | 2017-08-15 16:25:53 -0600 | [diff] [blame] | 178 | self.cleanup_vsgw() |
| 179 | self.cleanup_vpgwc() |
| 180 | self.cleanup_vhss() |
Omar Abdelkader | 1d9c319 | 2017-08-03 14:54:25 -0700 | [diff] [blame] | 181 | self.cleanup_container() |
| 182 | super(VMMETenant, self).delete(*args, **kwargs) |
| 183 | |
| 184 | def model_policy_vmmetenant(pk): |
| 185 | # This section of code is atomic to prevent race conditions |
| 186 | with transaction.atomic(): |
| 187 | # We find all of the tenants that are waiting to update |
| 188 | tenant = VMMETenant.objects.select_for_update().filter(pk=pk) |
| 189 | if not tenant: |
| 190 | return |
| 191 | # Since this code is atomic it is safe to always use the first tenant |
| 192 | tenant = tenant[0] |
Omar Abdelkader | 2c16284 | 2017-08-07 19:41:59 -0600 | [diff] [blame] | 193 | tenant.manage_container() |
Omar Abdelkader | d7fc4a0 | 2017-08-15 16:25:53 -0600 | [diff] [blame] | 194 | tenant.manage_vsgw() |
| 195 | tenant.manage_vpgwc() |
| 196 | tenant.manage_vhss() |
| 197 | tenant.cleanup_orphans() |