blob: 64d7374093729f158be746046b7f69d154107519 [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 models_decl import VMMEService_decl
16from models_decl import VMMEVendor_decl
17from models_decl import VMMETenant_decl
Matteo Scandolo5b7a5d42017-08-08 13:05:26 -070018
Pingping Linece8a742017-10-03 16:39:23 -070019from django.db import models
Pingping Lina83e3772017-10-04 11:29:50 -070020from core.models import Service, XOSBase, Slice, Instance, ServiceInstance, TenantWithContainer, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool
Pingping Linece8a742017-10-03 16:39:23 -070021import os
Saleil Bhat1c5593d2017-01-06 15:25:57 -080022from django.db import models, transaction
Pingping Linece8a742017-10-03 16:39:23 -070023from django.forms.models import model_to_dict
24from django.db.models import *
25from operator import itemgetter, attrgetter, methodcaller
26from core.models import Tag
27from core.models.service import LeastLoadedNodeScheduler
28import traceback
29from xos.exceptions import *
Saleil Bhat1c5593d2017-01-06 15:25:57 -080030
Pingping Linece8a742017-10-03 16:39:23 -070031class VMMEService(VMMEService_decl):
32 class Meta:
Andy Bavierc67937c2017-10-05 14:43:47 -070033 proxy = True
Saleil Bhat1c5593d2017-01-06 15:25:57 -080034
Pingping Linece8a742017-10-03 16:39:23 -070035 def create_tenant(self, **kwargs):
Andy Bavierc67937c2017-10-05 14:43:47 -070036 t = VMMETenant(kind="vEPC", owner=self, connect_method="na", **kwargs)
Pingping Linece8a742017-10-03 16:39:23 -070037 t.save()
38 return t
Saleil Bhat1c5593d2017-01-06 15:25:57 -080039
Pingping Linece8a742017-10-03 16:39:23 -070040class VMMEVendor(VMMEVendor_decl):
41 class Meta:
Andy Bavierc67937c2017-10-05 14:43:47 -070042 proxy = True
Saleil Bhat1c5593d2017-01-06 15:25:57 -080043
Pingping Linece8a742017-10-03 16:39:23 -070044class VMMETenant(VMMETenant_decl):
45 class Meta:
Andy Bavierc67937c2017-10-05 14:43:47 -070046 proxy = True
Saleil Bhat1c5593d2017-01-06 15:25:57 -080047
Pingping Linece8a742017-10-03 16:39:23 -070048 def __init__(self, *args, **kwargs):
49 vmmeservices = VMMEService.get_service_objects().all()
50 if vmmeservices:
Andy Bavierc67937c2017-10-05 14:43:47 -070051 self._meta.get_field("owner").default = vmmeservices[0].id
Pingping Linece8a742017-10-03 16:39:23 -070052 super(VMMETenant, self).__init__(*args, **kwargs)
Saleil Bhat1c5593d2017-01-06 15:25:57 -080053
Pingping Linece8a742017-10-03 16:39:23 -070054 @property
55 def image(self):
56 if not self.vmme_vendor:
57 return super(VMMETenant, self).image
58 return self.vmme_vendor.image
Andy Bavierc67937c2017-10-05 14:43:47 -070059
Pingping Linece8a742017-10-03 16:39:23 -070060 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 Bhat1c5593d2017-01-06 15:25:57 -080064
Pingping Linece8a742017-10-03 16:39:23 -070065 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 Bhat1c5593d2017-01-06 15:25:57 -080072
Pingping Linece8a742017-10-03 16:39:23 -070073 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 Bhat1c5593d2017-01-06 15:25:57 -080077
Pingping Linece8a742017-10-03 16:39:23 -070078 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 Bhat1c5593d2017-01-06 15:25:57 -080082
83def model_policy_vmmetenant(pk):
Pingping Linece8a742017-10-03 16:39:23 -070084 # This section of code is atomic to prevent race conditions
Saleil Bhat1c5593d2017-01-06 15:25:57 -080085 with transaction.atomic():
Pingping Linece8a742017-10-03 16:39:23 -070086 # We find all of the tenants that are waiting to update
Saleil Bhat1c5593d2017-01-06 15:25:57 -080087 tenant = VMMETenant.objects.select_for_update().filter(pk=pk)
88 if not tenant:
89 return
Pingping Linece8a742017-10-03 16:39:23 -070090 # Since this code is atomic it is safe to always use the first tenant
Saleil Bhat1c5593d2017-01-06 15:25:57 -080091 tenant = tenant[0]
92 tenant.manage_container()
93