Scott Baker | 5a96314 | 2016-03-06 16:50:25 -0800 | [diff] [blame] | 1 | from django.db import models |
| 2 | from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, Subscriber, NetworkParameter, NetworkParameterType, Port, AddressPool |
| 3 | from core.models.plcorebase import StrippedCharField |
| 4 | import os |
| 5 | from django.db import models, transaction |
| 6 | from django.forms.models import model_to_dict |
| 7 | from django.db.models import Q |
| 8 | from operator import itemgetter, attrgetter, methodcaller |
| 9 | from core.models import Tag |
| 10 | from core.models.service import LeastLoadedNodeScheduler |
| 11 | from services.cord.models import CordSubscriberRoot |
| 12 | import traceback |
| 13 | from xos.exceptions import * |
| 14 | from xos.config import Config |
| 15 | |
| 16 | class ConfigurationError(Exception): |
| 17 | pass |
| 18 | |
| 19 | VTR_KIND = "vTR" |
| 20 | |
| 21 | CORD_USE_VTN = getattr(Config(), "networking_use_vtn", False) |
| 22 | |
| 23 | # ------------------------------------------- |
| 24 | # VOLT |
| 25 | # ------------------------------------------- |
| 26 | |
| 27 | class VTRService(Service): |
| 28 | KIND = VTR_KIND |
| 29 | |
| 30 | class Meta: |
| 31 | app_label = "vtr" |
| 32 | verbose_name = "vTR Service" |
| 33 | proxy = True |
| 34 | |
| 35 | class VTRTenant(Tenant): |
| 36 | class Meta: |
| 37 | proxy = True |
| 38 | |
| 39 | KIND = VTR_KIND |
| 40 | |
| 41 | TEST_CHOICES = ( ("ping", "Ping"), ("traceroute", "Trace Route"), ("tcpdump", "Tcp Dump") ) |
Scott Baker | edb93f4 | 2016-03-07 14:44:22 -0800 | [diff] [blame] | 42 | SCOPE_CHOICES = ( ("container", "Container"), ("vm", "VM") ) |
Scott Baker | 5a96314 | 2016-03-06 16:50:25 -0800 | [diff] [blame] | 43 | |
| 44 | simple_attributes = ( ("test", None), |
| 45 | ("argument", None), |
Scott Baker | 27662d5 | 2016-03-06 22:13:39 -0800 | [diff] [blame] | 46 | ("result", None), |
Scott Baker | edb93f4 | 2016-03-07 14:44:22 -0800 | [diff] [blame] | 47 | ("target_id", None), |
| 48 | ("scope", "container") ) |
Scott Baker | 5a96314 | 2016-03-06 16:50:25 -0800 | [diff] [blame] | 49 | |
Scott Baker | edb93f4 | 2016-03-07 14:44:22 -0800 | [diff] [blame] | 50 | sync_attributes = ( 'test', 'argument', "scope" ) |
Scott Baker | 5a96314 | 2016-03-06 16:50:25 -0800 | [diff] [blame] | 51 | |
| 52 | def __init__(self, *args, **kwargs): |
| 53 | vtr_services = VTRService.get_service_objects().all() |
| 54 | if vtr_services: |
| 55 | self._meta.get_field("provider_service").default = vtr_services[0].id |
| 56 | super(VTRTenant, self).__init__(*args, **kwargs) |
| 57 | |
| 58 | @property |
| 59 | def target(self): |
| 60 | if getattr(self, "cached_target", None): |
| 61 | return self.cached_target |
Scott Baker | 27662d5 | 2016-03-06 22:13:39 -0800 | [diff] [blame] | 62 | target_id=self.target_id |
Scott Baker | 5a96314 | 2016-03-06 16:50:25 -0800 | [diff] [blame] | 63 | if not target_id: |
| 64 | return None |
| 65 | users=CordSubscriberRoot.objects.filter(id=target_id) |
| 66 | if not users: |
| 67 | return None |
| 68 | user=users[0] |
| 69 | self.cached_target = users[0] |
| 70 | return user |
| 71 | |
| 72 | @target.setter |
| 73 | def target(self, value): |
| 74 | if value: |
| 75 | value = value.id |
| 76 | if (value != self.get_attribute("target_id", None)): |
| 77 | self.cached_target=None |
Scott Baker | 27662d5 | 2016-03-06 22:13:39 -0800 | [diff] [blame] | 78 | self.target_id = value |
Scott Baker | 5a96314 | 2016-03-06 16:50:25 -0800 | [diff] [blame] | 79 | |
| 80 | def save(self, *args, **kwargs): |
| 81 | super(VTRTenant, self).save(*args, **kwargs) |
| 82 | |
| 83 | def delete(self, *args, **kwargs): |
| 84 | super(VTRTenant, self).delete(*args, **kwargs) |
| 85 | |
| 86 | |
| 87 | VTRTenant.setup_simple_attributes() |
| 88 | |