blob: 7acdc6ec42aff6653af7f77bb61c61fb4ea11ab9 [file] [log] [blame]
Matteo Scandolo5b7a5d42017-08-08 13:05:26 -07001# 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 Linece8a742017-10-03 16:39:23 -070015from core.models.plcorebase import *
16from models_decl import VMMEService_decl
17from models_decl import VMMEVendor_decl
18from models_decl import VMMETenant_decl
Matteo Scandolo5b7a5d42017-08-08 13:05:26 -070019
Pingping Linece8a742017-10-03 16:39:23 -070020from django.db import models
21from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool
22from core.models.plcorebase import StrippedCharField
23import os
Saleil Bhat1c5593d2017-01-06 15:25:57 -080024from django.db import models, transaction
Pingping Linece8a742017-10-03 16:39:23 -070025from django.forms.models import model_to_dict
26from django.db.models import *
27from operator import itemgetter, attrgetter, methodcaller
28from core.models import Tag
29from core.models.service import LeastLoadedNodeScheduler
30import traceback
31from xos.exceptions import *
32from xos.config import Config
Saleil Bhat1c5593d2017-01-06 15:25:57 -080033
Pingping Linece8a742017-10-03 16:39:23 -070034class VMMEService(VMMEService_decl):
35 class Meta:
36 proxy = True
Saleil Bhat1c5593d2017-01-06 15:25:57 -080037
Pingping Linece8a742017-10-03 16:39:23 -070038 def create_tenant(self, **kwargs):
39 t = VMMETenant(kind="vEPC", provider_service=self, connect_method="na", **kwargs)
40 t.save()
41 return t
Saleil Bhat1c5593d2017-01-06 15:25:57 -080042
Pingping Linece8a742017-10-03 16:39:23 -070043class VMMEVendor(VMMEVendor_decl):
44 class Meta:
45 proxy = True
Saleil Bhat1c5593d2017-01-06 15:25:57 -080046
Pingping Linece8a742017-10-03 16:39:23 -070047class VMMETenant(VMMETenant_decl):
48 class Meta:
49 proxy = True
Saleil Bhat1c5593d2017-01-06 15:25:57 -080050
Pingping Linece8a742017-10-03 16:39:23 -070051 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 Bhat1c5593d2017-01-06 15:25:57 -080056
Pingping Linece8a742017-10-03 16:39:23 -070057 @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 Bhat1c5593d2017-01-06 15:25:57 -080067
Pingping Linece8a742017-10-03 16:39:23 -070068 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 Bhat1c5593d2017-01-06 15:25:57 -080075
Pingping Linece8a742017-10-03 16:39:23 -070076 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 Bhat1c5593d2017-01-06 15:25:57 -080080
Pingping Linece8a742017-10-03 16:39:23 -070081 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 Bhat1c5593d2017-01-06 15:25:57 -080085
86def model_policy_vmmetenant(pk):
Pingping Linece8a742017-10-03 16:39:23 -070087 # This section of code is atomic to prevent race conditions
Saleil Bhat1c5593d2017-01-06 15:25:57 -080088 with transaction.atomic():
Pingping Linece8a742017-10-03 16:39:23 -070089 # We find all of the tenants that are waiting to update
Saleil Bhat1c5593d2017-01-06 15:25:57 -080090 tenant = VMMETenant.objects.select_for_update().filter(pk=pk)
91 if not tenant:
92 return
Pingping Linece8a742017-10-03 16:39:23 -070093 # Since this code is atomic it is safe to always use the first tenant
Saleil Bhat1c5593d2017-01-06 15:25:57 -080094 tenant = tenant[0]
95 tenant.manage_container()
96