blob: 1c1d15ae87840fad31c6e3a4e84e13c5622eb1fb [file] [log] [blame]
Matteo Scandolo6288d5a2017-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
Sapan Bhatia51a804c2017-05-03 17:24:37 +020017sync_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
22def __init__(self, *args, **kwargs):
23 super(VEGTenant, self).__init__(*args, **kwargs)
Andrea Campanella2a2df422017-08-30 16:59:17 +020024 self.cached_address_service_instance = None
Sapan Bhatia51a804c2017-05-03 17:24:37 +020025@property
Andrea Campanella2a2df422017-08-30 16:59:17 +020026def address_service_instance(self):
27 address_service_instance = self.get_newest_subscribed_tenant(AddressManagerServiceInstance)
28 if not address_service_instance:
Sapan Bhatia51a804c2017-05-03 17:24:37 +020029 return None
30
31 # always return the same object when possible
Andrea Campanella2a2df422017-08-30 16:59:17 +020032 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 Bhatia51a804c2017-05-03 17:24:37 +020034
Andrea Campanella2a2df422017-08-30 16:59:17 +020035 address_service_instance.caller = self.creator
36 self.cached_address_service_instance = address_service_instance
37 return address_service_instance
Sapan Bhatia51a804c2017-05-03 17:24:37 +020038
Andrea Campanella2a2df422017-08-30 16:59:17 +020039@address_service_instance.setter
40def address_service_instance(self, value):
41 raise XOSConfigurationError("VEGTenant.address_service_instance setter is not implemented")
Sapan Bhatia51a804c2017-05-03 17:24:37 +020042
43@property
44def 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
54def volt(self, value):
55 raise XOSConfigurationError("VEGTenant.volt setter is not implemented")
56
57@property
58def ssh_command(self):
59 if self.instance:
60 return self.instance.get_ssh_command()
61 else:
62 return "no-instance"
63
Andrea Campanella2a2df422017-08-30 16:59:17 +020064def 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 Bhatia51a804c2017-05-03 17:24:37 +020067 else:
68 return default
69
70@property
71def wan_container_ip(self):
Andrea Campanella2a2df422017-08-30 16:59:17 +020072 return self.get_address_service_instance_field("public_ip", None)
Sapan Bhatia51a804c2017-05-03 17:24:37 +020073
74@property
75def wan_container_mac(self):
Andrea Campanella2a2df422017-08-30 16:59:17 +020076 return self.get_address_service_instance_field("public_mac", None)
Sapan Bhatia51a804c2017-05-03 17:24:37 +020077
78@property
79def wan_container_netbits(self):
Andrea Campanella2a2df422017-08-30 16:59:17 +020080 return self.get_address_service_instance_field("netbits", None)
Sapan Bhatia51a804c2017-05-03 17:24:37 +020081
82@property
83def wan_container_gateway_ip(self):
Andrea Campanella2a2df422017-08-30 16:59:17 +020084 return self.get_address_service_instance_field("gateway_ip", None)
Sapan Bhatia51a804c2017-05-03 17:24:37 +020085
86@property
87def wan_container_gateway_mac(self):
Andrea Campanella2a2df422017-08-30 16:59:17 +020088 return self.get_address_service_instance_field("gateway_mac", None)
Sapan Bhatia51a804c2017-05-03 17:24:37 +020089
90@property
91def wan_vm_ip(self):
Andrea Campanella2a2df422017-08-30 16:59:17 +020092 tags = Tag.objects.filter(content_type=self.instance.get_content_type_key(), object_id=self.instance.id, name="vm_vrouter_tenant")
Sapan Bhatia51a804c2017-05-03 17:24:37 +020093 if tags:
Andrea Campanella2a2df422017-08-30 16:59:17 +020094 tenant = AddressManagerServiceInstance.objects.get(id=tags[0].value)
Sapan Bhatia51a804c2017-05-03 17:24:37 +020095 return tenant.public_ip
96 else:
97 raise Exception("no vm_vrouter_tenant tag for instance %s" % o.instance)
98
99@property
100def wan_vm_mac(self):
Andrea Campanella2a2df422017-08-30 16:59:17 +0200101 tags = Tag.objects.filter(content_type=self.instance.get_content_type_key(), object_id=self.instance.id, name="vm_vrouter_tenant")
Sapan Bhatia51a804c2017-05-03 17:24:37 +0200102 if tags:
Andrea Campanella2a2df422017-08-30 16:59:17 +0200103 tenant = AddressManagerServiceInstance.objects.get(id=tags[0].value)
Sapan Bhatia51a804c2017-05-03 17:24:37 +0200104 return tenant.public_mac
105 else:
106 raise Exception("no vm_vrouter_tenant tag for instance %s" % o.instance)
107
108@property
109def is_synced(self):
110 return (self.enacted is not None) and (self.enacted >= self.updated)
111
112@is_synced.setter
113def is_synced(self, value):
114 pass
Andrea Campanella2a2df422017-08-30 16:59:17 +0200115def save(self, *args, **kwargs):
Sapan Bhatia51a804c2017-05-03 17:24:37 +0200116 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 Campanella2a2df422017-08-30 16:59:17 +0200125
Sapan Bhatia51a804c2017-05-03 17:24:37 +0200126
127def delete(self, *args, **kwargs):
Sapan Bhatia51a804c2017-05-03 17:24:37 +0200128 super(VEGTenant, self).delete(*args, **kwargs)