blob: bba7aac7bff9765b53f8c17955f698992487b99e [file] [log] [blame]
Matteo Scandolo5b7a5d42017-08-08 13:05:26 -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
Saleil Bhat1c5593d2017-01-06 15:25:57 -080017# models.py - ExampleService Models
18
Saleil Bhat22afba82017-01-21 23:10:09 -080019from core.models import Service, TenantWithContainer, Image
Saleil Bhat1c5593d2017-01-06 15:25:57 -080020from django.db import models, transaction
21
22
23MCORD_KIND = "EPC" #added from vBBU
24
25#these macros are currently not used, names hard-coded manually
26SERVICE_NAME = 'vmme'
27SERVICE_NAME_VERBOSE = 'VMME Service'
28SERVICE_NAME_VERBOSE_PLURAL = 'VMME Services'
29TENANT_NAME_VERBOSE = 'VMME Service Tenant'
30TENANT_NAME_VERBOSE_PLURAL = 'VMME Service Tenants'
31
32class VMMEService(Service):
33
34 KIND = MCORD_KIND
35
36 class Meta:
37 proxy = True
38 app_label = "vmme"
39 verbose_name = "VMME Service"
40
41class 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 Bhat22afba82017-01-21 23:10:09 -080048 image_name = models.CharField(max_length=254, help_text="Name of VM image")
Saleil Bhat1c5593d2017-01-06 15:25:57 -080049
Saleil Bhat1c5593d2017-01-06 15:25:57 -080050 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 Bhat22afba82017-01-21 23:10:09 -080064 @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 Bhat1c5593d2017-01-06 15:25:57 -080073
74def 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