Scott Baker | 4eb615b | 2017-05-05 16:55:22 -0700 | [diff] [blame] | 1 | # 'simple_attributes' will be expanded into properties and setters that |
| 2 | # store the attribute using self.set_attribute / self.get_attribute. |
| 3 | |
| 4 | simple_attributes = ( ("devices", []), ) |
| 5 | |
| 6 | sync_attributes = ("firewall_enable", |
| 7 | "firewall_rules", |
| 8 | "url_filter_enable", |
| 9 | "url_filter_rules", |
| 10 | "cdn_enable", |
| 11 | "uplink_speed", |
| 12 | "downlink_speed", |
| 13 | "enable_uverse", |
| 14 | "status") |
| 15 | |
| 16 | def __init__(self, *args, **kwargs): |
| 17 | super(CordSubscriberRoot, self).__init__(*args, **kwargs) |
| 18 | |
| 19 | def find_device(self, mac): |
| 20 | for device in self.devices: |
| 21 | if device["mac"] == mac: |
| 22 | return device |
| 23 | return None |
| 24 | |
| 25 | def update_device(self, mac, **kwargs): |
| 26 | # kwargs may be "level" or "mac" |
| 27 | # Setting one of these to None will cause None to be stored in the db |
| 28 | devices = self.devices |
| 29 | for device in devices: |
| 30 | if device["mac"] == mac: |
| 31 | for arg in kwargs.keys(): |
| 32 | device[arg] = kwargs[arg] |
| 33 | self.devices = devices |
| 34 | return device |
| 35 | raise ValueError("Device with mac %s not found" % mac) |
| 36 | |
| 37 | def create_device(self, **kwargs): |
| 38 | if "mac" not in kwargs: |
| 39 | raise XOSMissingField("The mac field is required") |
| 40 | |
| 41 | if self.find_device(kwargs['mac']): |
| 42 | raise XOSDuplicateKey("Device with mac %s already exists" % kwargs["mac"]) |
| 43 | |
| 44 | device = kwargs.copy() |
| 45 | |
| 46 | devices = self.devices |
| 47 | devices.append(device) |
| 48 | self.devices = devices |
| 49 | |
| 50 | return device |
| 51 | |
| 52 | def delete_device(self, mac): |
| 53 | devices = self.devices |
| 54 | for device in devices: |
| 55 | if device["mac"]==mac: |
| 56 | devices.remove(device) |
| 57 | self.devices = devices |
| 58 | return |
| 59 | |
| 60 | raise ValueError("Device with mac %s not found" % mac) |
| 61 | |
| 62 | #-------------------------------------------------------------------------- |
| 63 | # Deprecated -- devices used to be called users |
| 64 | |
| 65 | def find_user(self, uid): |
| 66 | return self.find_device(uid) |
| 67 | |
| 68 | def update_user(self, uid, **kwargs): |
| 69 | return self.update_device(uid, **kwargs) |
| 70 | |
| 71 | def create_user(self, **kwargs): |
| 72 | return self.create_device(**kwargs) |
| 73 | |
| 74 | def delete_user(self, uid): |
| 75 | return self.delete_user(uid) |
| 76 | |
| 77 | # ------------------------------------------------------------------------ |
| 78 | |
| 79 | @property |
| 80 | def services(self): |
| 81 | return {"cdn": self.cdn_enable, |
| 82 | "url_filter": self.url_filter_enable, |
| 83 | "firewall": self.firewall_enable} |
| 84 | |
| 85 | @services.setter |
| 86 | def services(self, value): |
| 87 | pass |
| 88 | |
| 89 | def invalidate_related_objects(self): |
| 90 | # Dirty all vSGs related to this subscriber, so the vSG synchronizer
|
| 91 | # will run.
|
| 92 |
|
| 93 | # TODO: This should be reimplemented to use a watcher instead.
|
| 94 |
|
| 95 | # NOTE: "vOLT" and "vCPE" are hardcoded below. They had better agree
|
| 96 | # with the kinds defined in the vOLT and vCPE models.
|
| 97 |
|
| 98 | for tenant in self.subscribed_tenants.all():
|
| 99 | if tenant.kind == "vOLT":
|
| 100 | for inner_tenant in tenant.subscribed_tenants.all():
|
| 101 | if inner_tenant.kind == "vCPE":
|
| 102 | inner_tenant.save()
|
| 103 |
|
| 104 | def save(self, *args, **kwargs):
|
| 105 | self.validate_unique_service_specific_id(none_okay=True)
|
| 106 | if (not hasattr(self, 'caller') or not self.caller.is_admin):
|
| 107 | if (self.has_field_changed("service_specific_id")):
|
| 108 | raise XOSPermissionDenied("You do not have permission to change service_specific_id")
|
| 109 | super(CordSubscriberRoot, self).save(*args, **kwargs)
|
| 110 |
|
| 111 | self.invalidate_related_objects() |
| 112 | |