blob: 3f2b5222e27401be05faa4b198f6101fe5849d18 [file] [log] [blame]
Woojoong Kime30f6062017-10-05 19:38:56 -05001# 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
15from core.models.plcorebase import *
16from models_decl import VHSSService_decl
17from models_decl import VHSSVendor_decl
18from models_decl import VHSSTenant_decl
19
20from 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
24from django.db import models, transaction
25from 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
33
34class 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
43class VHSSVendor(VHSSVendor_decl):
44 class Meta:
45 proxy = True
46
47class 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
87def 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()