Matteo Scandolo | 5b7a5d4 | 2017-08-08 13:05:26 -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 | |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 17 | # models.py - ExampleService Models |
| 18 | |
Saleil Bhat | 22afba8 | 2017-01-21 23:10:09 -0800 | [diff] [blame] | 19 | from core.models import Service, TenantWithContainer, Image |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 20 | from django.db import models, transaction |
| 21 | |
| 22 | |
| 23 | MCORD_KIND = "EPC" #added from vBBU |
| 24 | |
| 25 | #these macros are currently not used, names hard-coded manually |
| 26 | SERVICE_NAME = 'vmme' |
| 27 | SERVICE_NAME_VERBOSE = 'VMME Service' |
| 28 | SERVICE_NAME_VERBOSE_PLURAL = 'VMME Services' |
| 29 | TENANT_NAME_VERBOSE = 'VMME Service Tenant' |
| 30 | TENANT_NAME_VERBOSE_PLURAL = 'VMME Service Tenants' |
| 31 | |
| 32 | class VMMEService(Service): |
| 33 | |
| 34 | KIND = MCORD_KIND |
| 35 | |
| 36 | class Meta: |
| 37 | proxy = True |
| 38 | app_label = "vmme" |
| 39 | verbose_name = "VMME Service" |
| 40 | |
| 41 | class VMMETenant(TenantWithContainer): |
| 42 | |
| 43 | KIND = 'vmme' |
| 44 | class Meta: |
| 45 | verbose_name = "VMME Service Tenant" |
| 46 | |
| 47 | tenant_message = models.CharField(max_length=254, help_text="vMME message") |
Saleil Bhat | 22afba8 | 2017-01-21 23:10:09 -0800 | [diff] [blame] | 48 | image_name = models.CharField(max_length=254, help_text="Name of VM image") |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 49 | |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 50 | def __init__(self, *args, **kwargs): |
| 51 | vmme_services = VMMEService.get_service_objects().all() |
| 52 | if vmme_services: |
| 53 | self._meta.get_field('provider_service').default = vmme_services[0].id |
| 54 | super(VMMETenant, self).__init__(*args, **kwargs) |
| 55 | |
| 56 | def save(self, *args, **kwargs): |
| 57 | super(VMMETenant, self).save(*args, **kwargs) |
| 58 | model_policy_vmmetenant(self.pk) #defined below |
| 59 | |
| 60 | def delete(self, *args, **kwargs): |
| 61 | self.cleanup_container() |
| 62 | super(VMMETenant, self).delete(*args, **kwargs) |
| 63 | |
Saleil Bhat | 22afba8 | 2017-01-21 23:10:09 -0800 | [diff] [blame] | 64 | @property |
| 65 | def image(self): |
| 66 | img = self.image_name.strip() |
| 67 | if img.lower() != "default": |
| 68 | return Image.objects.get(name=img) |
| 69 | else: |
| 70 | return super(VMMETenant, self).image |
| 71 | |
| 72 | |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 73 | |
| 74 | def model_policy_vmmetenant(pk): |
| 75 | with transaction.atomic(): |
| 76 | tenant = VMMETenant.objects.select_for_update().filter(pk=pk) |
| 77 | if not tenant: |
| 78 | return |
| 79 | tenant = tenant[0] |
| 80 | tenant.manage_container() |
| 81 | |