blob: 355ee8fbcc7b515c9378491d321248c36997ddb0 [file] [log] [blame]
Matteo Scandolo41ed0992017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Murat Parlakisik66fde4d2016-09-22 13:28:13 -070017# models.py - vSGW Models
18
Saleil Bhat42e8c5e2017-02-01 15:59:54 -080019from core.models import Service, TenantWithContainer, Image
JianHaoce45e042017-01-16 11:06:29 +000020from django.db import models, transaction
21
22MCORD_KIND = 'EPC'
23
Murat Parlakisik66fde4d2016-09-22 13:28:13 -070024SERVICE_NAME = 'vsgw'
25SERVICE_NAME_VERBOSE = 'Virtual SGW Service'
26SERVICE_NAME_VERBOSE_PLURAL = 'Virtual SGW Services'
27TENANT_NAME_VERBOSE = 'Virtual SGW Tenant'
28TENANT_NAME_VERBOSE_PLURAL = 'Virtual SGW Tenants'
29
30class VSGWService(Service):
31
32 KIND = SERVICE_NAME
33
34 class Meta:
JianHaoce45e042017-01-16 11:06:29 +000035 proxy = True
Murat Parlakisik66fde4d2016-09-22 13:28:13 -070036 app_label = SERVICE_NAME
37 verbose_name = SERVICE_NAME_VERBOSE
38
Murat Parlakisik66fde4d2016-09-22 13:28:13 -070039class VSGWTenant(TenantWithContainer):
40
41 KIND = SERVICE_NAME
42
43 class Meta:
44 verbose_name = TENANT_NAME_VERBOSE
45
46 tenant_message = models.CharField(max_length=254, help_text="Tenant Message to Display")
Saleil Bhat42e8c5e2017-02-01 15:59:54 -080047 image_name = models.CharField(max_length=254, help_text="Name of VM image")
Murat Parlakisik66fde4d2016-09-22 13:28:13 -070048
49 def __init__(self, *args, **kwargs):
50 vsgw_service = VSGWService.get_service_objects().all()
51 if vsgw_service:
52 self._meta.get_field('provider_service').default = vsgw_service[0].id
JianHaoce45e042017-01-16 11:06:29 +000053 super(VSGWTenant, self).__init__(*args, **kwargs)
Murat Parlakisik66fde4d2016-09-22 13:28:13 -070054
55 def save(self, *args, **kwargs):
56 super(VSGWTenant, self).save(*args, **kwargs)
JianHaoce45e042017-01-16 11:06:29 +000057 model_policy_vsgwtenant(self.pk)
Murat Parlakisik66fde4d2016-09-22 13:28:13 -070058
59 def delete(self, *args, **kwargs):
60 self.cleanup_container()
61 super(VSGWTenant, self).delete(*args, **kwargs)
62
Saleil Bhat42e8c5e2017-02-01 15:59:54 -080063 @property
64 def image(self):
65 img = self.image_name.strip()
66 if img.lower() != "default":
67 return Image.objects.get(name=img)
68 else:
69 return super(VSGWTenant, self).image
70
JianHaoce45e042017-01-16 11:06:29 +000071def model_policy_vsgwtenant(pk):
Murat Parlakisik66fde4d2016-09-22 13:28:13 -070072 with transaction.atomic():
73 tenant = VSGWTenant.objects.select_for_update().filter(pk=pk)
74 if not tenant:
75 return
76 tenant = tenant[0]
JianHaoce45e042017-01-16 11:06:29 +000077 tenant.manage_container()