Woojoong Kim | e30f606 | 2017-10-05 19:38:56 -0500 | [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 | |
| 15 | from core.models.plcorebase import * |
| 16 | from models_decl import VHSSService_decl |
| 17 | from models_decl import VHSSVendor_decl |
| 18 | from models_decl import VHSSTenant_decl |
| 19 | |
| 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 |
| 24 | from django.db import models, transaction |
| 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 |
| 33 | |
| 34 | class VHSSService(VHSSService_decl): |
| 35 | class Meta: |
| 36 | proxy = True |
| 37 | |
| 38 | def create_tenant(self, **kwargs): |
| 39 | t = VHSSTenant(kind="vEPC", provider_service=self, connect_method="na", **kwargs) |
| 40 | t.save() |
| 41 | return t |
| 42 | |
| 43 | class VHSSVendor(VHSSVendor_decl): |
| 44 | class Meta: |
| 45 | proxy = True |
| 46 | |
| 47 | class VHSSTenant(VHSSTenant_decl): |
| 48 | class Meta: |
| 49 | proxy = True |
| 50 | |
| 51 | def __init__(self, *args, **kwargs): |
| 52 | vhssservice = VHSSService.get_service_objects().all() |
| 53 | if vhssservice: |
| 54 | self._meta.get_field( |
| 55 | "provider_service").default = vhssservice[0].id |
| 56 | super(VHSSTenant, self).__init__(*args, **kwargs) |
| 57 | |
| 58 | @property |
| 59 | def image(self): |
| 60 | if not self.vhss_vendor: |
| 61 | return super(VHSSTenant, self).image |
| 62 | return self.vhss_vendor.image |
| 63 | |
| 64 | def save_instance(self, instance): |
| 65 | if self.vhss_vendor: |
| 66 | instance.flavor = self.vhss_vendor.flavor |
| 67 | super(VHSSTenant, self).save_instance(instance) |
| 68 | |
| 69 | def save(self, *args, **kwargs): |
| 70 | if not self.creator: |
| 71 | if not getattr(self, "caller", None): |
| 72 | raise XOSProgrammingError("VHSSTenant's self.caller was not set") |
| 73 | self.creator = self.caller |
| 74 | if not self.creator: |
| 75 | raise XOSProgrammingError("VHSSTenant's self.creator was not set") |
| 76 | |
| 77 | super(VHSSTenant, self).save(*args, **kwargs) |
| 78 | # This call needs to happen so that an instance is created for this |
| 79 | # tenant is created in the slice. One instance is created per tenant. |
| 80 | model_policy_vhsstenant(self.pk) |
| 81 | |
| 82 | def delete(self, *args, **kwargs): |
| 83 | # Delete the instance that was created for this tenant |
| 84 | self.cleanup_container() |
| 85 | super(VHSSTenant, self).delete(*args, **kwargs) |
| 86 | |
| 87 | def model_policy_vhsstenant(pk): |
| 88 | # This section of code is atomic to prevent race conditions |
| 89 | with transaction.atomic(): |
| 90 | # We find all of the tenants that are waiting to update |
| 91 | tenant = VHSSTenant.objects.select_for_update().filter(pk=pk) |
| 92 | if not tenant: |
| 93 | return |
| 94 | # Since this code is atomic it is safe to always use the first tenant |
| 95 | tenant = tenant[0] |
| 96 | tenant.manage_container() |