blob: f343cc33226431ad7588419a195cad6089b3fb42 [file] [log] [blame]
Matteo Scandoloaca86652017-08-08 13:05:27 -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 Bhatia46482ee2017-04-21 17:59:48 +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
21def __init__(self, *args, **kwargs):
22 super(VSGTenant, self).__init__(*args, **kwargs)
23 self.cached_vrouter=None
24
25@property
26def vrouter(self):
27 vrouter = self.get_newest_subscribed_tenant(VRouterTenant)
28 if not vrouter:
29 return None
30
31 # always return the same object when possible
32 if (self.cached_vrouter) and (self.cached_vrouter.id == vrouter.id):
33 return self.cached_vrouter
34
35 vrouter.caller = self.creator
36 self.cached_vrouter = vrouter
37 return vrouter
38
39@vrouter.setter
40def vrouter(self, value):
41 raise XOSConfigurationError("VSGTenant.vrouter setter is not implemented")
42
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("VSGTenant.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
64def get_vrouter_field(self, name, default=None):
65 if self.vrouter:
66 return getattr(self.vrouter, name, default)
67 else:
68 return default
69
70@property
71def wan_container_ip(self):
72 return self.get_vrouter_field("public_ip", None)
73
74@property
75def wan_container_mac(self):
76 return self.get_vrouter_field("public_mac", None)
77
78@property
79def wan_container_netbits(self):
80 return self.get_vrouter_field("netbits", None)
81
82@property
83def wan_container_gateway_ip(self):
84 return self.get_vrouter_field("gateway_ip", None)
85
86@property
87def wan_container_gateway_mac(self):
88 return self.get_vrouter_field("gateway_mac", None)
89
90@property
91def wan_vm_ip(self):
Scott Baker248ed752017-05-24 16:40:28 -070092 tags = Tag.objects.filter(content_type=self.instance.get_content_type_key(), object_id=self.instance.id, name="vm_vrouter_tenant")
Sapan Bhatia46482ee2017-04-21 17:59:48 +020093 if tags:
94 tenant = VRouterTenant.objects.get(id=tags[0].value)
95 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):
Scott Baker248ed752017-05-24 16:40:28 -0700101 tags = Tag.objects.filter(content_type=self.instance.get_content_type_key(), object_id=self.instance.id, name="vm_vrouter_tenant")
Sapan Bhatia46482ee2017-04-21 17:59:48 +0200102 if tags:
103 tenant = VRouterTenant.objects.get(id=tags[0].value)
104 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
115
Sapan Bhatia46482ee2017-04-21 17:59:48 +0200116def save(self, *args, **kwargs):
117 if not self.creator:
118 if not getattr(self, "caller", None):
119 # caller must be set when creating a vCPE since it creates a slice
120 raise XOSProgrammingError("VSGTenant's self.caller was not set")
121 self.creator = self.caller
122 if not self.creator:
123 raise XOSProgrammingError("VSGTenant's self.creator was not set")
124
125 super(VSGTenant, self).save(*args, **kwargs)
Sapan Bhatia46482ee2017-04-21 17:59:48 +0200126
127def delete(self, *args, **kwargs):
Sapan Bhatia46482ee2017-04-21 17:59:48 +0200128 super(VSGTenant, self).delete(*args, **kwargs)
129