Matteo Scandolo | 6288d5a | 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 | |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 17 | sync_attributes = ("wan_container_ip", "wan_container_mac", "wan_container_netbits", |
| 18 | "wan_container_gateway_ip", "wan_container_gateway_mac", |
| 19 | "wan_vm_ip", "wan_vm_mac") |
| 20 | |
| 21 | |
| 22 | def __init__(self, *args, **kwargs): |
| 23 | super(VEGTenant, self).__init__(*args, **kwargs) |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 24 | self.cached_address_service_instance = None |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 25 | @property |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 26 | def address_service_instance(self): |
| 27 | address_service_instance = self.get_newest_subscribed_tenant(AddressManagerServiceInstance) |
| 28 | if not address_service_instance: |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 29 | return None |
| 30 | |
| 31 | # always return the same object when possible |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 32 | if (self.cached_address_service_instance) and (self.cached_address_service_instance.id == address_service_instance.id): |
| 33 | return self.cached_address_service_instance |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 34 | |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 35 | address_service_instance.caller = self.creator |
| 36 | self.cached_address_service_instance = address_service_instance |
| 37 | return address_service_instance |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 38 | |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 39 | @address_service_instance.setter |
| 40 | def address_service_instance(self, value): |
| 41 | raise XOSConfigurationError("VEGTenant.address_service_instance setter is not implemented") |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 42 | |
| 43 | @property |
| 44 | def volt(self): |
| 45 | from services.volt.models import VOLTTenant |
| 46 | if not self.subscriber_tenant: |
| 47 | return None |
| 48 | volts = VOLTTenant.objects.filter(id=self.subscriber_tenant.id) |
| 49 | if not volts: |
| 50 | return None |
| 51 | return volts[0] |
| 52 | |
| 53 | @volt.setter |
| 54 | def volt(self, value): |
| 55 | raise XOSConfigurationError("VEGTenant.volt setter is not implemented") |
| 56 | |
| 57 | @property |
| 58 | def ssh_command(self): |
| 59 | if self.instance: |
| 60 | return self.instance.get_ssh_command() |
| 61 | else: |
| 62 | return "no-instance" |
| 63 | |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 64 | def get_address_service_instance_field(self, name, default=None): |
| 65 | if self.address_service_instance: |
| 66 | return getattr(self.address_service_instance, name, default) |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 67 | else: |
| 68 | return default |
| 69 | |
| 70 | @property |
| 71 | def wan_container_ip(self): |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 72 | return self.get_address_service_instance_field("public_ip", None) |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 73 | |
| 74 | @property |
| 75 | def wan_container_mac(self): |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 76 | return self.get_address_service_instance_field("public_mac", None) |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 77 | |
| 78 | @property |
| 79 | def wan_container_netbits(self): |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 80 | return self.get_address_service_instance_field("netbits", None) |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 81 | |
| 82 | @property |
| 83 | def wan_container_gateway_ip(self): |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 84 | return self.get_address_service_instance_field("gateway_ip", None) |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 85 | |
| 86 | @property |
| 87 | def wan_container_gateway_mac(self): |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 88 | return self.get_address_service_instance_field("gateway_mac", None) |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 89 | |
| 90 | @property |
| 91 | def wan_vm_ip(self): |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 92 | tags = Tag.objects.filter(content_type=self.instance.get_content_type_key(), object_id=self.instance.id, name="vm_vrouter_tenant") |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 93 | if tags: |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 94 | tenant = AddressManagerServiceInstance.objects.get(id=tags[0].value) |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 95 | return tenant.public_ip |
| 96 | else: |
| 97 | raise Exception("no vm_vrouter_tenant tag for instance %s" % o.instance) |
| 98 | |
| 99 | @property |
| 100 | def wan_vm_mac(self): |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 101 | tags = Tag.objects.filter(content_type=self.instance.get_content_type_key(), object_id=self.instance.id, name="vm_vrouter_tenant") |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 102 | if tags: |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 103 | tenant = AddressManagerServiceInstance.objects.get(id=tags[0].value) |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 104 | return tenant.public_mac |
| 105 | else: |
| 106 | raise Exception("no vm_vrouter_tenant tag for instance %s" % o.instance) |
| 107 | |
| 108 | @property |
| 109 | def is_synced(self): |
| 110 | return (self.enacted is not None) and (self.enacted >= self.updated) |
| 111 | |
| 112 | @is_synced.setter |
| 113 | def is_synced(self, value): |
| 114 | pass |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 115 | def save(self, *args, **kwargs): |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 116 | if not self.creator: |
| 117 | if not getattr(self, "caller", None): |
| 118 | # caller must be set when creating a vEG since it creates a slice |
| 119 | raise XOSProgrammingError("VEGTenant's self.caller was not set") |
| 120 | self.creator = self.caller |
| 121 | if not self.creator: |
| 122 | raise XOSProgrammingError("VEGTenant's self.creator was not set") |
| 123 | |
| 124 | super(VEGTenant, self).save(*args, **kwargs) |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 125 | |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 126 | |
| 127 | def delete(self, *args, **kwargs): |
Sapan Bhatia | 51a804c | 2017-05-03 17:24:37 +0200 | [diff] [blame] | 128 | super(VEGTenant, self).delete(*args, **kwargs) |