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 | ece8a74 | 2017-10-03 16:39:23 -0700 | [diff] [blame] | 15 | from models_decl import VMMEService_decl |
| 16 | from models_decl import VMMEVendor_decl |
| 17 | from models_decl import VMMETenant_decl |
Matteo Scandolo | 5b7a5d4 | 2017-08-08 13:05:26 -0700 | [diff] [blame] | 18 | |
Pingping Lin | ece8a74 | 2017-10-03 16:39:23 -0700 | [diff] [blame] | 19 | from django.db import models |
Pingping Lin | a83e377 | 2017-10-04 11:29:50 -0700 | [diff] [blame^] | 20 | from core.models import Service, XOSBase, Slice, Instance, ServiceInstance, TenantWithContainer, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool |
Pingping Lin | ece8a74 | 2017-10-03 16:39:23 -0700 | [diff] [blame] | 21 | import os |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 22 | from django.db import models, transaction |
Pingping Lin | ece8a74 | 2017-10-03 16:39:23 -0700 | [diff] [blame] | 23 | from django.forms.models import model_to_dict |
| 24 | from django.db.models import * |
| 25 | from operator import itemgetter, attrgetter, methodcaller |
| 26 | from core.models import Tag |
| 27 | from core.models.service import LeastLoadedNodeScheduler |
| 28 | import traceback |
| 29 | from xos.exceptions import * |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 30 | |
Pingping Lin | ece8a74 | 2017-10-03 16:39:23 -0700 | [diff] [blame] | 31 | class VMMEService(VMMEService_decl): |
| 32 | class Meta: |
| 33 | proxy = True |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 34 | |
Pingping Lin | ece8a74 | 2017-10-03 16:39:23 -0700 | [diff] [blame] | 35 | def create_tenant(self, **kwargs): |
| 36 | t = VMMETenant(kind="vEPC", provider_service=self, connect_method="na", **kwargs) |
| 37 | t.save() |
| 38 | return t |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 39 | |
Pingping Lin | ece8a74 | 2017-10-03 16:39:23 -0700 | [diff] [blame] | 40 | class VMMEVendor(VMMEVendor_decl): |
| 41 | class Meta: |
| 42 | proxy = True |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 43 | |
Pingping Lin | ece8a74 | 2017-10-03 16:39:23 -0700 | [diff] [blame] | 44 | class VMMETenant(VMMETenant_decl): |
| 45 | class Meta: |
| 46 | proxy = True |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 47 | |
Pingping Lin | ece8a74 | 2017-10-03 16:39:23 -0700 | [diff] [blame] | 48 | def __init__(self, *args, **kwargs): |
| 49 | vmmeservices = VMMEService.get_service_objects().all() |
| 50 | if vmmeservices: |
| 51 | self._meta.get_field("provider_service").default = vmmeservices[0].id |
| 52 | super(VMMETenant, self).__init__(*args, **kwargs) |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 53 | |
Pingping Lin | ece8a74 | 2017-10-03 16:39:23 -0700 | [diff] [blame] | 54 | @property |
| 55 | def image(self): |
| 56 | if not self.vmme_vendor: |
| 57 | return super(VMMETenant, self).image |
| 58 | return self.vmme_vendor.image |
| 59 | |
| 60 | def save_instance(self, instance): |
| 61 | if self.vmme_vendor: |
| 62 | instance.flavor = self.vmme_vendor.flavor |
| 63 | super(VMMETenant, self).save_instance(instance) |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 64 | |
Pingping Lin | ece8a74 | 2017-10-03 16:39:23 -0700 | [diff] [blame] | 65 | def save(self, *args, **kwargs): |
| 66 | if not self.creator: |
| 67 | if not getattr(self, "caller", None): |
| 68 | raise XOSProgrammingError("VMMETenant's self.caller was not set") |
| 69 | self.creator = self.caller |
| 70 | if not self.creator: |
| 71 | raise XOSProgrammingError("VMMETenant's self.creator was not set") |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 72 | |
Pingping Lin | ece8a74 | 2017-10-03 16:39:23 -0700 | [diff] [blame] | 73 | super(VMMETenant, self).save(*args, **kwargs) |
| 74 | # This call needs to happen so that an instance is created for this |
| 75 | # tenant is created in the slice. One instance is created per tenant. |
| 76 | model_policy_vmmetenant(self.pk) |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 77 | |
Pingping Lin | ece8a74 | 2017-10-03 16:39:23 -0700 | [diff] [blame] | 78 | def delete(self, *args, **kwargs): |
| 79 | # Delete the instance that was created for this tenant |
| 80 | self.cleanup_container() |
| 81 | super(VMMETenant, self).delete(*args, **kwargs) |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 82 | |
| 83 | def model_policy_vmmetenant(pk): |
Pingping Lin | ece8a74 | 2017-10-03 16:39:23 -0700 | [diff] [blame] | 84 | # This section of code is atomic to prevent race conditions |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 85 | with transaction.atomic(): |
Pingping Lin | ece8a74 | 2017-10-03 16:39:23 -0700 | [diff] [blame] | 86 | # We find all of the tenants that are waiting to update |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 87 | tenant = VMMETenant.objects.select_for_update().filter(pk=pk) |
| 88 | if not tenant: |
| 89 | return |
Pingping Lin | ece8a74 | 2017-10-03 16:39:23 -0700 | [diff] [blame] | 90 | # 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] | 91 | tenant = tenant[0] |
| 92 | tenant.manage_container() |
| 93 | |