Sapan Bhatia | 46482ee | 2017-04-21 17:59:48 +0200 | [diff] [blame] | 1 | sync_attributes = ("wan_container_ip", "wan_container_mac", "wan_container_netbits", |
| 2 | "wan_container_gateway_ip", "wan_container_gateway_mac", |
| 3 | "wan_vm_ip", "wan_vm_mac") |
| 4 | |
| 5 | def __init__(self, *args, **kwargs): |
| 6 | super(VSGTenant, self).__init__(*args, **kwargs) |
| 7 | self.cached_vrouter=None |
| 8 | |
| 9 | @property |
| 10 | def vrouter(self): |
| 11 | vrouter = self.get_newest_subscribed_tenant(VRouterTenant) |
| 12 | if not vrouter: |
| 13 | return None |
| 14 | |
| 15 | # always return the same object when possible |
| 16 | if (self.cached_vrouter) and (self.cached_vrouter.id == vrouter.id): |
| 17 | return self.cached_vrouter |
| 18 | |
| 19 | vrouter.caller = self.creator |
| 20 | self.cached_vrouter = vrouter |
| 21 | return vrouter |
| 22 | |
| 23 | @vrouter.setter |
| 24 | def vrouter(self, value): |
| 25 | raise XOSConfigurationError("VSGTenant.vrouter setter is not implemented") |
| 26 | |
| 27 | @property |
| 28 | def volt(self): |
| 29 | from services.volt.models import VOLTTenant |
| 30 | if not self.subscriber_tenant: |
| 31 | return None |
| 32 | volts = VOLTTenant.objects.filter(id=self.subscriber_tenant.id) |
| 33 | if not volts: |
| 34 | return None |
| 35 | return volts[0] |
| 36 | |
| 37 | @volt.setter |
| 38 | def volt(self, value): |
| 39 | raise XOSConfigurationError("VSGTenant.volt setter is not implemented") |
| 40 | |
| 41 | @property |
| 42 | def ssh_command(self): |
| 43 | if self.instance: |
| 44 | return self.instance.get_ssh_command() |
| 45 | else: |
| 46 | return "no-instance" |
| 47 | |
| 48 | def get_vrouter_field(self, name, default=None): |
| 49 | if self.vrouter: |
| 50 | return getattr(self.vrouter, name, default) |
| 51 | else: |
| 52 | return default |
| 53 | |
| 54 | @property |
| 55 | def wan_container_ip(self): |
| 56 | return self.get_vrouter_field("public_ip", None) |
| 57 | |
| 58 | @property |
| 59 | def wan_container_mac(self): |
| 60 | return self.get_vrouter_field("public_mac", None) |
| 61 | |
| 62 | @property |
| 63 | def wan_container_netbits(self): |
| 64 | return self.get_vrouter_field("netbits", None) |
| 65 | |
| 66 | @property |
| 67 | def wan_container_gateway_ip(self): |
| 68 | return self.get_vrouter_field("gateway_ip", None) |
| 69 | |
| 70 | @property |
| 71 | def wan_container_gateway_mac(self): |
| 72 | return self.get_vrouter_field("gateway_mac", None) |
| 73 | |
| 74 | @property |
| 75 | def wan_vm_ip(self): |
Scott Baker | 248ed75 | 2017-05-24 16:40:28 -0700 | [diff] [blame] | 76 | tags = Tag.objects.filter(content_type=self.instance.get_content_type_key(), object_id=self.instance.id, name="vm_vrouter_tenant") |
Sapan Bhatia | 46482ee | 2017-04-21 17:59:48 +0200 | [diff] [blame] | 77 | if tags: |
| 78 | tenant = VRouterTenant.objects.get(id=tags[0].value) |
| 79 | return tenant.public_ip |
| 80 | else: |
| 81 | raise Exception("no vm_vrouter_tenant tag for instance %s" % o.instance) |
| 82 | |
| 83 | @property |
| 84 | def wan_vm_mac(self): |
Scott Baker | 248ed75 | 2017-05-24 16:40:28 -0700 | [diff] [blame] | 85 | tags = Tag.objects.filter(content_type=self.instance.get_content_type_key(), object_id=self.instance.id, name="vm_vrouter_tenant") |
Sapan Bhatia | 46482ee | 2017-04-21 17:59:48 +0200 | [diff] [blame] | 86 | if tags: |
| 87 | tenant = VRouterTenant.objects.get(id=tags[0].value) |
| 88 | return tenant.public_mac |
| 89 | else: |
| 90 | raise Exception("no vm_vrouter_tenant tag for instance %s" % o.instance) |
| 91 | |
| 92 | @property |
| 93 | def is_synced(self): |
| 94 | return (self.enacted is not None) and (self.enacted >= self.updated) |
| 95 | |
| 96 | @is_synced.setter |
| 97 | def is_synced(self, value): |
| 98 | pass |
| 99 | |
Sapan Bhatia | 46482ee | 2017-04-21 17:59:48 +0200 | [diff] [blame] | 100 | def save(self, *args, **kwargs): |
| 101 | if not self.creator: |
| 102 | if not getattr(self, "caller", None): |
| 103 | # caller must be set when creating a vCPE since it creates a slice |
| 104 | raise XOSProgrammingError("VSGTenant's self.caller was not set") |
| 105 | self.creator = self.caller |
| 106 | if not self.creator: |
| 107 | raise XOSProgrammingError("VSGTenant's self.creator was not set") |
| 108 | |
| 109 | super(VSGTenant, self).save(*args, **kwargs) |
Sapan Bhatia | 46482ee | 2017-04-21 17:59:48 +0200 | [diff] [blame] | 110 | |
| 111 | def delete(self, *args, **kwargs): |
Sapan Bhatia | 46482ee | 2017-04-21 17:59:48 +0200 | [diff] [blame] | 112 | super(VSGTenant, self).delete(*args, **kwargs) |
| 113 | |