Matteo Scandolo | 41ed099 | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 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 Parlakisik | 66fde4d | 2016-09-22 13:28:13 -0700 | [diff] [blame] | 17 | # models.py - vSGW Models |
| 18 | |
Saleil Bhat | 42e8c5e | 2017-02-01 15:59:54 -0800 | [diff] [blame] | 19 | from core.models import Service, TenantWithContainer, Image |
JianHao | ce45e04 | 2017-01-16 11:06:29 +0000 | [diff] [blame] | 20 | from django.db import models, transaction |
| 21 | |
| 22 | MCORD_KIND = 'EPC' |
| 23 | |
Murat Parlakisik | 66fde4d | 2016-09-22 13:28:13 -0700 | [diff] [blame] | 24 | SERVICE_NAME = 'vsgw' |
| 25 | SERVICE_NAME_VERBOSE = 'Virtual SGW Service' |
| 26 | SERVICE_NAME_VERBOSE_PLURAL = 'Virtual SGW Services' |
| 27 | TENANT_NAME_VERBOSE = 'Virtual SGW Tenant' |
| 28 | TENANT_NAME_VERBOSE_PLURAL = 'Virtual SGW Tenants' |
| 29 | |
| 30 | class VSGWService(Service): |
| 31 | |
| 32 | KIND = SERVICE_NAME |
| 33 | |
| 34 | class Meta: |
JianHao | ce45e04 | 2017-01-16 11:06:29 +0000 | [diff] [blame] | 35 | proxy = True |
Murat Parlakisik | 66fde4d | 2016-09-22 13:28:13 -0700 | [diff] [blame] | 36 | app_label = SERVICE_NAME |
| 37 | verbose_name = SERVICE_NAME_VERBOSE |
| 38 | |
Murat Parlakisik | 66fde4d | 2016-09-22 13:28:13 -0700 | [diff] [blame] | 39 | class 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 Bhat | 42e8c5e | 2017-02-01 15:59:54 -0800 | [diff] [blame] | 47 | image_name = models.CharField(max_length=254, help_text="Name of VM image") |
Murat Parlakisik | 66fde4d | 2016-09-22 13:28:13 -0700 | [diff] [blame] | 48 | |
| 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 |
JianHao | ce45e04 | 2017-01-16 11:06:29 +0000 | [diff] [blame] | 53 | super(VSGWTenant, self).__init__(*args, **kwargs) |
Murat Parlakisik | 66fde4d | 2016-09-22 13:28:13 -0700 | [diff] [blame] | 54 | |
| 55 | def save(self, *args, **kwargs): |
| 56 | super(VSGWTenant, self).save(*args, **kwargs) |
JianHao | ce45e04 | 2017-01-16 11:06:29 +0000 | [diff] [blame] | 57 | model_policy_vsgwtenant(self.pk) |
Murat Parlakisik | 66fde4d | 2016-09-22 13:28:13 -0700 | [diff] [blame] | 58 | |
| 59 | def delete(self, *args, **kwargs): |
| 60 | self.cleanup_container() |
| 61 | super(VSGWTenant, self).delete(*args, **kwargs) |
| 62 | |
Saleil Bhat | 42e8c5e | 2017-02-01 15:59:54 -0800 | [diff] [blame] | 63 | @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 | |
JianHao | ce45e04 | 2017-01-16 11:06:29 +0000 | [diff] [blame] | 71 | def model_policy_vsgwtenant(pk): |
Murat Parlakisik | 66fde4d | 2016-09-22 13:28:13 -0700 | [diff] [blame] | 72 | with transaction.atomic(): |
| 73 | tenant = VSGWTenant.objects.select_for_update().filter(pk=pk) |
| 74 | if not tenant: |
| 75 | return |
| 76 | tenant = tenant[0] |
JianHao | ce45e04 | 2017-01-16 11:06:29 +0000 | [diff] [blame] | 77 | tenant.manage_container() |