Matteo Scandolo | 5b7a5d4 | 2017-08-08 13:05:26 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Pingping Lin | 7ea29aa | 2017-10-03 16:39:23 -0700 | [diff] [blame^] | 15 | from core.models.plcorebase import * |
| 16 | from models_decl import VMMEService_decl |
| 17 | from models_decl import VMMEVendor_decl |
| 18 | from models_decl import VMMETenant_decl |
Matteo Scandolo | 5b7a5d4 | 2017-08-08 13:05:26 -0700 | [diff] [blame] | 19 | |
Pingping Lin | 7ea29aa | 2017-10-03 16:39:23 -0700 | [diff] [blame^] | 20 | from django.db import models |
| 21 | from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool |
| 22 | from core.models.plcorebase import StrippedCharField |
| 23 | import os |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 24 | from django.db import models, transaction |
Pingping Lin | 7ea29aa | 2017-10-03 16:39:23 -0700 | [diff] [blame^] | 25 | from django.forms.models import model_to_dict |
| 26 | from django.db.models import * |
| 27 | from operator import itemgetter, attrgetter, methodcaller |
| 28 | from core.models import Tag |
| 29 | from core.models.service import LeastLoadedNodeScheduler |
| 30 | import traceback |
| 31 | from xos.exceptions import * |
| 32 | from xos.config import Config |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 33 | |
Pingping Lin | 7ea29aa | 2017-10-03 16:39:23 -0700 | [diff] [blame^] | 34 | class VMMEService(VMMEService_decl): |
| 35 | class Meta: |
| 36 | proxy = True |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 37 | |
Pingping Lin | 7ea29aa | 2017-10-03 16:39:23 -0700 | [diff] [blame^] | 38 | def create_tenant(self, **kwargs): |
| 39 | t = VMMETenant(kind="vEPC", provider_service=self, connect_method="na", **kwargs) |
| 40 | t.save() |
| 41 | return t |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 42 | |
Pingping Lin | 7ea29aa | 2017-10-03 16:39:23 -0700 | [diff] [blame^] | 43 | class VMMEVendor(VMMEVendor_decl): |
| 44 | class Meta: |
| 45 | proxy = True |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 46 | |
Pingping Lin | 7ea29aa | 2017-10-03 16:39:23 -0700 | [diff] [blame^] | 47 | class VMMETenant(VMMETenant_decl): |
| 48 | class Meta: |
| 49 | proxy = True |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 50 | |
Pingping Lin | 7ea29aa | 2017-10-03 16:39:23 -0700 | [diff] [blame^] | 51 | def __init__(self, *args, **kwargs): |
| 52 | vmmeservices = VMMEService.get_service_objects().all() |
| 53 | if vmmeservices: |
| 54 | self._meta.get_field("provider_service").default = vmmeservices[0].id |
| 55 | super(VMMETenant, self).__init__(*args, **kwargs) |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 56 | |
Pingping Lin | 7ea29aa | 2017-10-03 16:39:23 -0700 | [diff] [blame^] | 57 | @property |
| 58 | def image(self): |
| 59 | if not self.vmme_vendor: |
| 60 | return super(VMMETenant, self).image |
| 61 | return self.vmme_vendor.image |
| 62 | |
| 63 | def save_instance(self, instance): |
| 64 | if self.vmme_vendor: |
| 65 | instance.flavor = self.vmme_vendor.flavor |
| 66 | super(VMMETenant, self).save_instance(instance) |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 67 | |
Pingping Lin | 7ea29aa | 2017-10-03 16:39:23 -0700 | [diff] [blame^] | 68 | def save(self, *args, **kwargs): |
| 69 | if not self.creator: |
| 70 | if not getattr(self, "caller", None): |
| 71 | raise XOSProgrammingError("VMMETenant's self.caller was not set") |
| 72 | self.creator = self.caller |
| 73 | if not self.creator: |
| 74 | raise XOSProgrammingError("VMMETenant's self.creator was not set") |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 75 | |
Pingping Lin | 7ea29aa | 2017-10-03 16:39:23 -0700 | [diff] [blame^] | 76 | super(VMMETenant, self).save(*args, **kwargs) |
| 77 | # This call needs to happen so that an instance is created for this |
| 78 | # tenant is created in the slice. One instance is created per tenant. |
| 79 | model_policy_vmmetenant(self.pk) |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 80 | |
Pingping Lin | 7ea29aa | 2017-10-03 16:39:23 -0700 | [diff] [blame^] | 81 | def delete(self, *args, **kwargs): |
| 82 | # Delete the instance that was created for this tenant |
| 83 | self.cleanup_container() |
| 84 | super(VMMETenant, self).delete(*args, **kwargs) |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 85 | |
| 86 | def model_policy_vmmetenant(pk): |
Pingping Lin | 7ea29aa | 2017-10-03 16:39:23 -0700 | [diff] [blame^] | 87 | # This section of code is atomic to prevent race conditions |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 88 | with transaction.atomic(): |
Pingping Lin | 7ea29aa | 2017-10-03 16:39:23 -0700 | [diff] [blame^] | 89 | # We find all of the tenants that are waiting to update |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 90 | tenant = VMMETenant.objects.select_for_update().filter(pk=pk) |
| 91 | if not tenant: |
| 92 | return |
Pingping Lin | 7ea29aa | 2017-10-03 16:39:23 -0700 | [diff] [blame^] | 93 | # Since this code is atomic it is safe to always use the first tenant |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 94 | tenant = tenant[0] |
| 95 | tenant.manage_container() |
| 96 | |