Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 1 | from django.db import models |
Scott Baker | ac60d5f | 2015-05-14 21:48:53 -0700 | [diff] [blame] | 2 | from core.models import Service, PlCoreBase, Slice, Sliver, Tenant, Node, Image, User, Flavor |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 3 | from core.models.plcorebase import StrippedCharField |
| 4 | import os |
| 5 | from django.db import models |
| 6 | from django.forms.models import model_to_dict |
| 7 | from django.db.models import Q |
| 8 | from operator import itemgetter, attrgetter, methodcaller |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 9 | import traceback |
Scott Baker | d921e1c | 2015-04-20 14:24:29 -0700 | [diff] [blame] | 10 | from xos.exceptions import * |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 11 | |
| 12 | """ |
| 13 | import os |
| 14 | import sys |
| 15 | sys.path.append("/opt/xos") |
| 16 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings") |
| 17 | import django |
| 18 | from core.models import *
|
| 19 | from hpc.models import *
|
| 20 | from cord.models import *
|
| 21 | django.setup() |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 22 | |
Scott Baker | 557aada | 2015-04-20 09:48:34 -0700 | [diff] [blame] | 23 | t = VOLTTenant() |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 24 | t.caller = User.objects.all()[0] |
| 25 | t.save() |
| 26 | |
Scott Baker | 2b29a31 | 2015-05-11 08:31:24 -0700 | [diff] [blame] | 27 | for v in VOLTTenant.get_tenant_objects().all(): |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 28 | v.caller = User.objects.all()[0] |
| 29 | v.delete() |
| 30 | |
Scott Baker | 2b29a31 | 2015-05-11 08:31:24 -0700 | [diff] [blame] | 31 | for v in VCPETenant.get_tenant_objects().all(): |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 32 | v.caller = User.objects.all()[0] |
| 33 | v.delete() |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 34 | |
Scott Baker | 2b29a31 | 2015-05-11 08:31:24 -0700 | [diff] [blame] | 35 | for v in VOLTTenant.get_tenant_objects().all(): |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 36 | v.caller = User.objects.all()[0] |
| 37 | v.delete() |
Scott Baker | 9c8a2c7 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 38 | |
Scott Baker | 2b29a31 | 2015-05-11 08:31:24 -0700 | [diff] [blame] | 39 | for v in VOLTTenant.get_tenant_objects().all(): |
Scott Baker | 9c8a2c7 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 40 | if not v.creator: |
| 41 | v.creator= User.objects.all()[0] |
| 42 | v.save() |
| 43 | |
Scott Baker | 2b29a31 | 2015-05-11 08:31:24 -0700 | [diff] [blame] | 44 | for v in VCPETenant.get_tenant_objects().all(): |
Scott Baker | 9c8a2c7 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 45 | if not v.creator: |
| 46 | v.creator= User.objects.all()[0] |
| 47 | v.save() |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 48 | """ |
| 49 | |
| 50 | class ConfigurationError(Exception): |
| 51 | pass |
| 52 | |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 53 | # ------------------------------------------- |
| 54 | # VOLT |
| 55 | # ------------------------------------------- |
| 56 | |
| 57 | class VOLTService(Service): |
| 58 | KIND = "vOLT" |
| 59 | |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 60 | class Meta: |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 61 | app_label = "cord" |
| 62 | verbose_name = "vOLT Service" |
| 63 | proxy = True |
| 64 | |
| 65 | class VOLTTenant(Tenant): |
| 66 | class Meta: |
| 67 | proxy = True |
| 68 | |
| 69 | KIND = "vOLT" |
| 70 | |
Scott Baker | 664c428 | 2015-05-28 17:12:00 -0700 | [diff] [blame] | 71 | default_attributes = {"vlan_id": None, |
| 72 | "is_demo_user": False } |
Scott Baker | e744c7b | 2015-04-20 11:50:09 -0700 | [diff] [blame] | 73 | |
Scott Baker | 557aada | 2015-04-20 09:48:34 -0700 | [diff] [blame] | 74 | def __init__(self, *args, **kwargs): |
| 75 | volt_services = VOLTService.get_service_objects().all() |
| 76 | if volt_services: |
| 77 | self._meta.get_field("provider_service").default = volt_services[0].id |
| 78 | super(VOLTTenant, self).__init__(*args, **kwargs) |
| 79 | |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 80 | @property |
Scott Baker | e744c7b | 2015-04-20 11:50:09 -0700 | [diff] [blame] | 81 | def vlan_id(self): |
| 82 | return self.get_attribute("vlan_id", self.default_attributes["vlan_id"]) |
| 83 | |
| 84 | @vlan_id.setter |
| 85 | def vlan_id(self, value): |
| 86 | self.set_attribute("vlan_id", value) |
| 87 | |
| 88 | @property |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 89 | def vcpe(self): |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 90 | if getattr(self, "cached_vcpe", None): |
| 91 | return self.cached_vcpe |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 92 | vcpe_id=self.get_attribute("vcpe_id") |
| 93 | if not vcpe_id: |
| 94 | return None |
| 95 | vcpes=VCPETenant.objects.filter(id=vcpe_id) |
| 96 | if not vcpes: |
| 97 | return None |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 98 | vcpe=vcpes[0] |
Scott Baker | 9c8a2c7 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 99 | vcpe.caller = self.creator |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 100 | self.cached_vcpe = vcpe |
| 101 | return vcpe |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 102 | |
| 103 | @vcpe.setter |
| 104 | def vcpe(self, value): |
| 105 | if value: |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 106 | value = value.id |
| 107 | if (value != self.get_attribute("vcpe_id", None)): |
| 108 | self.cached_vcpe=None |
| 109 | self.set_attribute("vcpe_id", value) |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 110 | |
Scott Baker | 9c8a2c7 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 111 | @property |
| 112 | def creator(self): |
| 113 | if getattr(self, "cached_creator", None): |
| 114 | return self.cached_creator |
| 115 | creator_id=self.get_attribute("creator_id") |
| 116 | if not creator_id: |
| 117 | return None |
| 118 | users=User.objects.filter(id=creator_id) |
| 119 | if not users: |
| 120 | return None |
| 121 | user=users[0] |
| 122 | self.cached_creator = users[0] |
| 123 | return user |
| 124 | |
| 125 | @creator.setter |
| 126 | def creator(self, value): |
| 127 | if value: |
| 128 | value = value.id |
| 129 | if (value != self.get_attribute("creator_id", None)): |
| 130 | self.cached_creator=None |
| 131 | self.set_attribute("creator_id", value) |
| 132 | |
Scott Baker | 664c428 | 2015-05-28 17:12:00 -0700 | [diff] [blame] | 133 | @property |
| 134 | def is_demo_user(self): |
| 135 | return self.get_attribute("is_demo_user", self.default_attributes["is_demo_user"]) |
| 136 | |
| 137 | @is_demo_user.setter |
| 138 | def is_demo_user(self, value): |
| 139 | self.set_attribute("is_demo_user", value) |
| 140 | |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 141 | def manage_vcpe(self): |
| 142 | # Each VOLT object owns exactly one VCPE object |
| 143 | |
| 144 | if self.deleted: |
| 145 | return |
| 146 | |
| 147 | if self.vcpe is None: |
| 148 | vcpeServices = VCPEService.get_service_objects().all() |
| 149 | if not vcpeServices: |
Scott Baker | d921e1c | 2015-04-20 14:24:29 -0700 | [diff] [blame] | 150 | raise XOSConfigurationError("No VCPE Services available") |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 151 | |
| 152 | vcpe = VCPETenant(provider_service = vcpeServices[0], |
| 153 | subscriber_tenant = self) |
Scott Baker | 9c8a2c7 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 154 | vcpe.caller = self.creator |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 155 | vcpe.save() |
| 156 | |
| 157 | try: |
| 158 | self.vcpe = vcpe |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 159 | super(VOLTTenant, self).save() |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 160 | except: |
| 161 | vcpe.delete() |
| 162 | raise |
| 163 | |
| 164 | def cleanup_vcpe(self): |
| 165 | if self.vcpe: |
| 166 | self.vcpe.delete() |
| 167 | self.vcpe = None |
| 168 | |
| 169 | def save(self, *args, **kwargs): |
Scott Baker | d921e1c | 2015-04-20 14:24:29 -0700 | [diff] [blame] | 170 | self.validate_unique_service_specific_id() |
| 171 | |
Scott Baker | 9c8a2c7 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 172 | if not self.creator: |
| 173 | if not getattr(self, "caller", None): |
| 174 | # caller must be set when creating a vCPE since it creates a slice |
| 175 | raise XOSProgrammingError("VOLTTenant's self.caller was not set") |
| 176 | self.creator = self.caller |
| 177 | if not self.creator: |
| 178 | raise XOSProgrammingError("VOLTTenant's self.creator was not set") |
| 179 | |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 180 | super(VOLTTenant, self).save(*args, **kwargs) |
| 181 | self.manage_vcpe() |
| 182 | |
| 183 | def delete(self, *args, **kwargs): |
| 184 | self.cleanup_vcpe() |
| 185 | super(VOLTTenant, self).delete(*args, **kwargs) |
| 186 | |
| 187 | # ------------------------------------------- |
| 188 | # VCPE |
| 189 | # ------------------------------------------- |
| 190 | |
| 191 | class VCPEService(Service): |
| 192 | KIND = "vCPE" |
| 193 | |
| 194 | class Meta: |
| 195 | app_label = "cord" |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 196 | verbose_name = "vCPE Service" |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 197 | proxy = True |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 198 | |
Scott Baker | 5c8abf8 | 2015-05-20 20:45:11 -0700 | [diff] [blame] | 199 | def allocate_bbs_account(self): |
| 200 | vcpes = VCPETenant.get_tenant_objects().all() |
| 201 | bbs_accounts = [vcpe.bbs_account for vcpe in vcpes] |
| 202 | |
| 203 | # There's a bit of a race here; some other user could be trying to |
| 204 | # allocate a bbs_account at the same time we are. |
| 205 | |
Scott Baker | f4f61ca | 2015-06-09 12:03:56 -0700 | [diff] [blame] | 206 | for i in range(2,21): |
Scott Baker | 5c8abf8 | 2015-05-20 20:45:11 -0700 | [diff] [blame] | 207 | account_name = "bbs%02d@onlab.us" % i |
| 208 | if (account_name not in bbs_accounts): |
| 209 | return account_name |
| 210 | |
| 211 | raise XOSConfigurationError("We've run out of available broadbandshield accounts. Delete some vcpe and try again.") |
| 212 | |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 213 | class VCPETenant(Tenant): |
| 214 | class Meta: |
| 215 | proxy = True |
| 216 | |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 217 | KIND = "vCPE" |
| 218 | |
Scott Baker | 9c8a2c7 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 219 | sync_attributes = ("firewall_enable", |
| 220 | "firewall_rules", |
| 221 | "url_filter_enable", |
| 222 | "url_filter_rules", |
Scott Baker | c7a96c8 | 2015-05-20 08:41:38 -0700 | [diff] [blame] | 223 | "cdn_enable", |
| 224 | "nat_ip", |
| 225 | "lan_ip", |
| 226 | "wan_ip", |
Scott Baker | 710ad05 | 2015-06-04 10:26:44 -0700 | [diff] [blame] | 227 | "private_ip", |
Scott Baker | 3f4fe40 | 2015-06-05 12:08:34 -0700 | [diff] [blame] | 228 | "hpc_client_ip", |
| 229 | "wan_mac") |
Scott Baker | 9c8a2c7 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 230 | |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 231 | default_attributes = {"firewall_enable": False, |
Scott Baker | 0bcbb96 | 2015-04-16 21:54:50 -0700 | [diff] [blame] | 232 | "firewall_rules": "accept all anywhere anywhere", |
| 233 | "url_filter_enable": False, |
| 234 | "url_filter_rules": "allow all", |
Scott Baker | 8d13a98 | 2015-05-14 20:29:04 -0700 | [diff] [blame] | 235 | "url_filter_level": "PG", |
Scott Baker | 0bcbb96 | 2015-04-16 21:54:50 -0700 | [diff] [blame] | 236 | "cdn_enable": False, |
Scott Baker | a0cf961 | 2015-05-20 16:31:58 -0700 | [diff] [blame] | 237 | "sliver_id": None, |
Scott Baker | 5c8abf8 | 2015-05-20 20:45:11 -0700 | [diff] [blame] | 238 | "users": [], |
| 239 | "bbs_account": None} |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 240 | |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 241 | def __init__(self, *args, **kwargs): |
| 242 | super(VCPETenant, self).__init__(*args, **kwargs) |
| 243 | self.cached_vbng=None |
| 244 | self.cached_sliver=None |
| 245 | |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 246 | @property |
| 247 | def image(self): |
Scott Baker | fd1f696 | 2015-06-04 10:02:09 -0700 | [diff] [blame] | 248 | LOOK_FOR_IMAGES=["ubuntu-vcpe2", # ONOS demo machine -- preferred vcpe image |
Scott Baker | 2c4f1eb | 2015-06-02 16:03:30 -0700 | [diff] [blame] | 249 | "Ubuntu 14.04 LTS", # portal |
Scott Baker | 4f751bd | 2015-04-20 20:12:59 -0700 | [diff] [blame] | 250 | "Ubuntu-14.04-LTS", # ONOS demo machine |
| 251 | ] |
| 252 | for image_name in LOOK_FOR_IMAGES: |
| 253 | images = Image.objects.filter(name = image_name) |
| 254 | if images: |
| 255 | return images[0] |
| 256 | |
| 257 | raise XOSProgrammingError("No VPCE image (looked for %s)" % str(LOOK_FOR_IMAGES)) |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 258 | |
| 259 | @property |
| 260 | def sliver(self): |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 261 | if getattr(self, "cached_sliver", None): |
| 262 | return self.cached_sliver |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 263 | sliver_id=self.get_attribute("sliver_id") |
| 264 | if not sliver_id: |
| 265 | return None |
| 266 | slivers=Sliver.objects.filter(id=sliver_id) |
| 267 | if not slivers: |
| 268 | return None |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 269 | sliver=slivers[0] |
Scott Baker | 9c8a2c7 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 270 | sliver.caller = self.creator |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 271 | self.cached_sliver = sliver |
| 272 | return sliver |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 273 | |
| 274 | @sliver.setter |
| 275 | def sliver(self, value): |
| 276 | if value: |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 277 | value = value.id |
| 278 | if (value != self.get_attribute("sliver_id", None)): |
| 279 | self.cached_sliver=None |
| 280 | self.set_attribute("sliver_id", value) |
| 281 | |
| 282 | @property |
Scott Baker | 9c8a2c7 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 283 | def creator(self): |
| 284 | if getattr(self, "cached_creator", None): |
| 285 | return self.cached_creator |
| 286 | creator_id=self.get_attribute("creator_id") |
| 287 | if not creator_id: |
| 288 | return None |
| 289 | users=User.objects.filter(id=creator_id) |
| 290 | if not users: |
| 291 | return None |
| 292 | user=users[0] |
| 293 | self.cached_creator = users[0] |
| 294 | return user |
| 295 | |
| 296 | @creator.setter |
| 297 | def creator(self, value): |
| 298 | if value: |
| 299 | value = value.id |
| 300 | if (value != self.get_attribute("creator_id", None)): |
| 301 | self.cached_creator=None |
| 302 | self.set_attribute("creator_id", value) |
| 303 | |
| 304 | @property |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 305 | def vbng(self): |
| 306 | if getattr(self, "cached_vbng", None): |
| 307 | return self.cached_vbng |
| 308 | vbng_id=self.get_attribute("vbng_id") |
| 309 | if not vbng_id: |
| 310 | return None |
| 311 | vbngs=VBNGTenant.objects.filter(id=vbng_id) |
| 312 | if not vbngs: |
| 313 | return None |
| 314 | vbng=vbngs[0] |
Scott Baker | 9c8a2c7 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 315 | vbng.caller = self.creator |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 316 | self.cached_vbng = vbng |
| 317 | return vbng |
| 318 | |
| 319 | @vbng.setter |
| 320 | def vbng(self, value): |
| 321 | if value: |
| 322 | value = value.id |
| 323 | if (value != self.get_attribute("vbng_id", None)): |
| 324 | self.cached_vbng=None |
| 325 | self.set_attribute("vbng_id", value) |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 326 | |
| 327 | @property |
| 328 | def firewall_enable(self): |
| 329 | return self.get_attribute("firewall_enable", self.default_attributes["firewall_enable"]) |
| 330 | |
| 331 | @firewall_enable.setter |
| 332 | def firewall_enable(self, value): |
| 333 | self.set_attribute("firewall_enable", value) |
| 334 | |
Scott Baker | 0bcbb96 | 2015-04-16 21:54:50 -0700 | [diff] [blame] | 335 | @property |
| 336 | def firewall_rules(self): |
| 337 | return self.get_attribute("firewall_rules", self.default_attributes["firewall_rules"]) |
| 338 | |
| 339 | @firewall_rules.setter |
| 340 | def firewall_rules(self, value): |
| 341 | self.set_attribute("firewall_rules", value) |
| 342 | |
| 343 | @property |
| 344 | def url_filter_enable(self): |
| 345 | return self.get_attribute("url_filter_enable", self.default_attributes["url_filter_enable"]) |
| 346 | |
| 347 | @url_filter_enable.setter |
| 348 | def url_filter_enable(self, value): |
| 349 | self.set_attribute("url_filter_enable", value) |
| 350 | |
| 351 | @property |
Scott Baker | 8d13a98 | 2015-05-14 20:29:04 -0700 | [diff] [blame] | 352 | def url_filter_level(self): |
| 353 | return self.get_attribute("url_filter_level", self.default_attributes["url_filter_level"]) |
| 354 | |
| 355 | @url_filter_level.setter |
| 356 | def url_filter_level(self, value): |
| 357 | self.set_attribute("url_filter_level", value) |
| 358 | |
| 359 | @property |
Scott Baker | 0bcbb96 | 2015-04-16 21:54:50 -0700 | [diff] [blame] | 360 | def url_filter_rules(self): |
| 361 | return self.get_attribute("url_filter_rules", self.default_attributes["url_filter_rules"]) |
| 362 | |
| 363 | @url_filter_rules.setter |
| 364 | def url_filter_rules(self, value): |
| 365 | self.set_attribute("url_filter_rules", value) |
| 366 | |
| 367 | @property |
| 368 | def cdn_enable(self): |
| 369 | return self.get_attribute("cdn_enable", self.default_attributes["cdn_enable"]) |
| 370 | |
| 371 | @cdn_enable.setter |
| 372 | def cdn_enable(self, value): |
| 373 | self.set_attribute("cdn_enable", value) |
| 374 | |
Scott Baker | 8d13a98 | 2015-05-14 20:29:04 -0700 | [diff] [blame] | 375 | @property |
| 376 | def users(self): |
Scott Baker | a0cf961 | 2015-05-20 16:31:58 -0700 | [diff] [blame] | 377 | return self.get_attribute("users", self.default_attributes["users"]) |
Scott Baker | 8d13a98 | 2015-05-14 20:29:04 -0700 | [diff] [blame] | 378 | |
| 379 | @users.setter |
| 380 | def users(self, value): |
Scott Baker | a0cf961 | 2015-05-20 16:31:58 -0700 | [diff] [blame] | 381 | self.set_attribute("users", value) |
| 382 | |
Scott Baker | 5c8abf8 | 2015-05-20 20:45:11 -0700 | [diff] [blame] | 383 | @property |
| 384 | def bbs_account(self): |
| 385 | return self.get_attribute("bbs_account", self.default_attributes["bbs_account"]) |
| 386 | |
| 387 | @bbs_account.setter |
| 388 | def bbs_account(self, value): |
| 389 | return self.set_attribute("bbs_account", value) |
| 390 | |
Scott Baker | 11981a9 | 2015-06-04 16:24:20 -0700 | [diff] [blame] | 391 | @property |
| 392 | def ssh_command(self): |
| 393 | if self.sliver: |
| 394 | return self.sliver.get_ssh_command() |
| 395 | else: |
| 396 | return "no-sliver" |
| 397 | |
| 398 | @ssh_command.setter |
| 399 | def ssh_command(self, value): |
| 400 | pass |
| 401 | |
Scott Baker | a0cf961 | 2015-05-20 16:31:58 -0700 | [diff] [blame] | 402 | def find_user(self, uid): |
| 403 | uid = int(uid) |
| 404 | for user in self.users: |
| 405 | if user["id"] == uid: |
| 406 | return user |
| 407 | return None |
| 408 | |
| 409 | def update_user(self, uid, **kwargs): |
| 410 | # kwargs may be "level" or "mac" |
| 411 | # Setting one of these to None will cause None to be stored in the db |
| 412 | uid = int(uid) |
| 413 | users = self.users |
| 414 | for user in users: |
| 415 | if user["id"] == uid: |
| 416 | for arg in kwargs.keys(): |
| 417 | user[arg] = kwargs[arg] |
| 418 | self.users = users |
Scott Baker | ac73170 | 2015-06-01 20:34:03 -0700 | [diff] [blame] | 419 | return user |
Scott Baker | a0cf961 | 2015-05-20 16:31:58 -0700 | [diff] [blame] | 420 | raise ValueError("User %d not found" % uid) |
| 421 | |
| 422 | def create_user(self, **kwargs): |
Scott Baker | 7c79630 | 2015-05-28 19:44:20 -0700 | [diff] [blame] | 423 | if "name" not in kwargs: |
| 424 | raise XOSMissingField("The name field is required") |
| 425 | |
| 426 | for user in self.users: |
| 427 | if kwargs["name"] == user["name"]: |
| 428 | raise XOSDuplicateKey("User %s already exists" % kwargs["name"]) |
| 429 | |
Scott Baker | a0cf961 | 2015-05-20 16:31:58 -0700 | [diff] [blame] | 430 | uids = [x["id"] for x in self.users] |
| 431 | if uids: |
| 432 | uid = max(uids)+1 |
| 433 | else: |
| 434 | uid = 0 |
| 435 | newuser = kwargs.copy() |
| 436 | newuser["id"] = uid |
| 437 | |
| 438 | users = self.users |
| 439 | users.append(newuser) |
| 440 | self.users = users |
| 441 | |
| 442 | return newuser |
| 443 | |
| 444 | def delete_user(self, uid): |
| 445 | uid = int(uid) |
| 446 | users = self.users |
| 447 | for user in users: |
| 448 | if user["id"]==uid: |
| 449 | users.remove(user) |
| 450 | self.users = users |
| 451 | return |
| 452 | |
| 453 | raise ValueError("Users %d not found" % uid) |
Scott Baker | 8d13a98 | 2015-05-14 20:29:04 -0700 | [diff] [blame] | 454 | |
| 455 | @property |
| 456 | def services(self): |
Scott Baker | f4c3ad4 | 2015-05-15 14:59:47 -0700 | [diff] [blame] | 457 | return {"cdn": self.cdn_enable, |
| 458 | "url_filter": self.url_filter_enable, |
| 459 | "firewall": self.firewall_enable} |
Scott Baker | 8d13a98 | 2015-05-14 20:29:04 -0700 | [diff] [blame] | 460 | |
| 461 | @services.setter |
| 462 | def services(self, value): |
| 463 | pass |
| 464 | |
Scott Baker | 706bf97 | 2015-05-20 08:19:25 -0700 | [diff] [blame] | 465 | @property |
| 466 | def addresses(self): |
| 467 | if not self.sliver: |
| 468 | return {} |
| 469 | |
| 470 | addresses = {} |
| 471 | for ns in self.sliver.networkslivers.all(): |
| 472 | if "lan" in ns.network.name.lower(): |
| 473 | addresses["lan"] = ns.ip |
| 474 | elif "wan" in ns.network.name.lower(): |
| 475 | addresses["wan"] = ns.ip |
| 476 | elif "private" in ns.network.name.lower(): |
| 477 | addresses["private"] = ns.ip |
| 478 | elif "nat" in ns.network.name.lower(): |
| 479 | addresses["nat"] = ns.ip |
Scott Baker | 710ad05 | 2015-06-04 10:26:44 -0700 | [diff] [blame] | 480 | elif "hpc_client" in ns.network.name.lower(): |
| 481 | addresses["hpc_client"] = ns.ip |
Scott Baker | 706bf97 | 2015-05-20 08:19:25 -0700 | [diff] [blame] | 482 | return addresses |
| 483 | |
Scott Baker | a6a7e03 | 2015-05-20 08:25:29 -0700 | [diff] [blame] | 484 | @property |
| 485 | def nat_ip(self): |
| 486 | return self.addresses.get("nat",None) |
| 487 | |
| 488 | @property |
| 489 | def lan_ip(self): |
| 490 | return self.addresses.get("lan",None) |
| 491 | |
| 492 | @property |
| 493 | def wan_ip(self): |
| 494 | return self.addresses.get("wan",None) |
| 495 | |
| 496 | @property |
Scott Baker | 3f4fe40 | 2015-06-05 12:08:34 -0700 | [diff] [blame] | 497 | def wan_mac(self): |
| 498 | ip = self.wan_ip |
| 499 | if not ip: |
| 500 | return None |
| 501 | try: |
| 502 | (a,b,c,d) = ip.split('.') |
| 503 | wan_mac = "02:42:%2x:%2x:%2x:%2x" % (int(a), int(b), int(c), int(d)) |
| 504 | except: |
| 505 | wan_mac = "Exception" |
| 506 | return wan_mac |
| 507 | |
| 508 | @property |
Scott Baker | a6a7e03 | 2015-05-20 08:25:29 -0700 | [diff] [blame] | 509 | def private_ip(self): |
| 510 | return self.addresses.get("private",None) |
| 511 | |
Scott Baker | 710ad05 | 2015-06-04 10:26:44 -0700 | [diff] [blame] | 512 | @property |
| 513 | def hpc_client_ip(self): |
| 514 | return self.addresses.get("hpc_client",None) |
| 515 | |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 516 | def pick_node(self): |
| 517 | nodes = list(Node.objects.all()) |
| 518 | # TODO: logic to filter nodes by which nodes are up, and which |
| 519 | # nodes the slice can instantiate on. |
| 520 | nodes = sorted(nodes, key=lambda node: node.slivers.all().count()) |
| 521 | return nodes[0] |
| 522 | |
| 523 | def manage_sliver(self): |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 524 | # Each VCPE object owns exactly one sliver. |
| 525 | |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 526 | if self.deleted: |
| 527 | return |
| 528 | |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 529 | if (self.sliver is not None) and (self.sliver.image != self.image): |
| 530 | self.sliver.delete() |
| 531 | self.sliver = None |
Scott Baker | 9c8a2c7 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 532 | |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 533 | if self.sliver is None: |
| 534 | if not self.provider_service.slices.count(): |
Scott Baker | ee0c280 | 2015-05-06 15:46:34 -0700 | [diff] [blame] | 535 | raise XOSConfigurationError("The VCPE service has no slices") |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 536 | |
Scott Baker | ac60d5f | 2015-05-14 21:48:53 -0700 | [diff] [blame] | 537 | flavors = Flavor.objects.filter(name="m1.small") |
| 538 | if not flavors: |
| 539 | raise XOSConfigurationError("No m1.small flavor") |
| 540 | |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 541 | node =self.pick_node() |
| 542 | sliver = Sliver(slice = self.provider_service.slices.all()[0], |
| 543 | node = node, |
| 544 | image = self.image, |
Scott Baker | 9c8a2c7 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 545 | creator = self.creator, |
Scott Baker | ac60d5f | 2015-05-14 21:48:53 -0700 | [diff] [blame] | 546 | deployment = node.site_deployment.deployment, |
| 547 | flavor = flavors[0]) |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 548 | sliver.save() |
| 549 | |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 550 | try: |
| 551 | self.sliver = sliver |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 552 | super(VCPETenant, self).save() |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 553 | except: |
| 554 | sliver.delete() |
| 555 | raise |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 556 | |
| 557 | def cleanup_sliver(self): |
| 558 | if self.sliver: |
| 559 | self.sliver.delete() |
| 560 | self.sliver = None |
| 561 | |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 562 | def manage_vbng(self): |
| 563 | # Each vCPE object owns exactly one vBNG object |
| 564 | |
| 565 | if self.deleted: |
| 566 | return |
| 567 | |
| 568 | if self.vbng is None: |
| 569 | vbngServices = VBNGService.get_service_objects().all() |
| 570 | if not vbngServices: |
Scott Baker | d921e1c | 2015-04-20 14:24:29 -0700 | [diff] [blame] | 571 | raise XOSConfigurationError("No VBNG Services available") |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 572 | |
| 573 | vbng = VBNGTenant(provider_service = vbngServices[0], |
| 574 | subscriber_tenant = self) |
Scott Baker | 9c8a2c7 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 575 | vbng.caller = self.creator |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 576 | vbng.save() |
| 577 | |
| 578 | try: |
| 579 | self.vbng = vbng |
| 580 | super(VCPETenant, self).save() |
| 581 | except: |
| 582 | vbng.delete() |
| 583 | raise |
| 584 | |
| 585 | def cleanup_vbng(self): |
| 586 | if self.vbng: |
| 587 | self.vbng.delete() |
| 588 | self.vbng = None |
| 589 | |
Scott Baker | 5c8abf8 | 2015-05-20 20:45:11 -0700 | [diff] [blame] | 590 | def manage_bbs_account(self): |
| 591 | if self.deleted: |
| 592 | return |
| 593 | |
Scott Baker | e257011 | 2015-05-20 20:57:28 -0700 | [diff] [blame] | 594 | if self.url_filter_enable: |
| 595 | if not self.bbs_account: |
| 596 | # make sure we use the proxied VCPEService object, not the generic Service object |
| 597 | vcpe_service = VCPEService.objects.get(id=self.provider_service.id) |
Scott Baker | f4f61ca | 2015-06-09 12:03:56 -0700 | [diff] [blame] | 598 | if self.service_specific_id=="SYNCME": |
| 599 | self.bbs_account = "bbs01@onlab.us" |
| 600 | else: |
| 601 | self.bbs_account = vcpe_service.allocate_bbs_account() |
Scott Baker | e257011 | 2015-05-20 20:57:28 -0700 | [diff] [blame] | 602 | super(VCPETenant, self).save() |
| 603 | else: |
| 604 | if self.bbs_account: |
| 605 | self.bbs_account = None |
| 606 | super(VCPETenant, self).save() |
Scott Baker | 5c8abf8 | 2015-05-20 20:45:11 -0700 | [diff] [blame] | 607 | |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 608 | def save(self, *args, **kwargs): |
Scott Baker | 9c8a2c7 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 609 | if not self.creator: |
| 610 | if not getattr(self, "caller", None): |
| 611 | # caller must be set when creating a vCPE since it creates a slice |
Scott Baker | ee0c280 | 2015-05-06 15:46:34 -0700 | [diff] [blame] | 612 | raise XOSProgrammingError("VCPETenant's self.caller was not set") |
Scott Baker | 9c8a2c7 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 613 | self.creator = self.caller |
| 614 | if not self.creator: |
Scott Baker | ee0c280 | 2015-05-06 15:46:34 -0700 | [diff] [blame] | 615 | raise XOSProgrammingError("VCPETenant's self.creator was not set") |
Scott Baker | 9c8a2c7 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 616 | |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 617 | super(VCPETenant, self).save(*args, **kwargs) |
Scott Baker | 39b0d89 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 618 | self.manage_sliver() |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 619 | self.manage_vbng() |
Scott Baker | 5c8abf8 | 2015-05-20 20:45:11 -0700 | [diff] [blame] | 620 | self.manage_bbs_account() |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 621 | |
| 622 | def delete(self, *args, **kwargs): |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 623 | self.cleanup_vbng() |
Scott Baker | 1b24161 | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 624 | self.cleanup_sliver() |
| 625 | super(VCPETenant, self).delete(*args, **kwargs) |
| 626 | |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 627 | #---------------------------------------------------------------------------- |
| 628 | # vBNG |
| 629 | #---------------------------------------------------------------------------- |
| 630 | |
| 631 | class VBNGService(Service): |
| 632 | KIND = "vBNG" |
| 633 | |
| 634 | class Meta: |
| 635 | app_label = "cord" |
| 636 | verbose_name = "vBNG Service" |
| 637 | proxy = True |
| 638 | |
| 639 | class VBNGTenant(Tenant): |
| 640 | class Meta: |
| 641 | proxy = True |
| 642 | |
| 643 | KIND = "vBNG" |
| 644 | |
Scott Baker | 99ff551 | 2015-06-02 14:34:04 -0700 | [diff] [blame] | 645 | default_attributes = {"routeable_subnet": "", |
Scott Baker | 790613c | 2015-06-08 19:09:53 -0700 | [diff] [blame] | 646 | "mapped_ip": "", |
| 647 | "mapped_mac": "", |
| 648 | "mapped_hostname": ""} |
Scott Baker | 697fb0b | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 649 | |
| 650 | @property |
| 651 | def routeable_subnet(self): |
| 652 | return self.get_attribute("routeable_subnet", self.default_attributes["routeable_subnet"]) |
| 653 | |
| 654 | @routeable_subnet.setter |
| 655 | def routeable_subnet(self, value): |
| 656 | self.set_attribute("routeable_subnet", value) |
Scott Baker | 99ff551 | 2015-06-02 14:34:04 -0700 | [diff] [blame] | 657 | |
| 658 | @property |
| 659 | def mapped_ip(self): |
| 660 | return self.get_attribute("mapped_ip", self.default_attributes["mapped_ip"]) |
| 661 | |
| 662 | @mapped_ip.setter |
| 663 | def mapped_ip(self, value): |
| 664 | self.set_attribute("mapped_ip", value) |
Scott Baker | 790613c | 2015-06-08 19:09:53 -0700 | [diff] [blame] | 665 | |
| 666 | @property |
| 667 | def mapped_mac(self): |
| 668 | return self.get_attribute("mapped_mac", self.default_attributes["mapped_mac"]) |
| 669 | |
| 670 | @mapped_mac.setter |
| 671 | def mapped_mac(self, value): |
| 672 | self.set_attribute("mapped_mac", value) |
| 673 | |
| 674 | @property |
| 675 | def mapped_hostname(self): |
| 676 | return self.get_attribute("mapped_hostname", self.default_attributes["mapped_hostname"]) |
| 677 | |
| 678 | @mapped_hostname.setter |
| 679 | def mapped_hostname(self, value): |
| 680 | self.set_attribute("mapped_hostname", value) |