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): |
Scott Baker | c37e7a0 | 2017-07-17 17:32:20 -0700 | [diff] [blame^] | 17 | # TODO: Should probably make an RCORDService, rather than filtering by name |
| 18 | rcord_services = Service.objects.filter(name="rcord") |
| 19 | if not rcord_services: |
| 20 | # this isn't going to end well... |
| 21 | raise Exception("There is no rcord service to use as the default service for CordSubscriberRoot") |
| 22 | |
| 23 | self._meta.get_field("owner").default = rcord_services[0].id |
| 24 | |
Scott Baker | 4eb615b | 2017-05-05 16:55:22 -0700 | [diff] [blame] | 25 | super(CordSubscriberRoot, self).__init__(*args, **kwargs) |
| 26 | |
| 27 | def find_device(self, mac): |
| 28 | for device in self.devices: |
| 29 | if device["mac"] == mac: |
| 30 | return device |
| 31 | return None |
| 32 | |
| 33 | def update_device(self, mac, **kwargs): |
| 34 | # kwargs may be "level" or "mac" |
| 35 | # Setting one of these to None will cause None to be stored in the db |
| 36 | devices = self.devices |
| 37 | for device in devices: |
| 38 | if device["mac"] == mac: |
| 39 | for arg in kwargs.keys(): |
| 40 | device[arg] = kwargs[arg] |
| 41 | self.devices = devices |
| 42 | return device |
| 43 | raise ValueError("Device with mac %s not found" % mac) |
| 44 | |
| 45 | def create_device(self, **kwargs): |
| 46 | if "mac" not in kwargs: |
| 47 | raise XOSMissingField("The mac field is required") |
| 48 | |
| 49 | if self.find_device(kwargs['mac']): |
| 50 | raise XOSDuplicateKey("Device with mac %s already exists" % kwargs["mac"]) |
| 51 | |
| 52 | device = kwargs.copy() |
| 53 | |
| 54 | devices = self.devices |
| 55 | devices.append(device) |
| 56 | self.devices = devices |
| 57 | |
| 58 | return device |
| 59 | |
| 60 | def delete_device(self, mac): |
| 61 | devices = self.devices |
| 62 | for device in devices: |
| 63 | if device["mac"]==mac: |
| 64 | devices.remove(device) |
| 65 | self.devices = devices |
| 66 | return |
| 67 | |
| 68 | raise ValueError("Device with mac %s not found" % mac) |
| 69 | |
| 70 | #-------------------------------------------------------------------------- |
| 71 | # Deprecated -- devices used to be called users |
| 72 | |
| 73 | def find_user(self, uid): |
| 74 | return self.find_device(uid) |
| 75 | |
| 76 | def update_user(self, uid, **kwargs): |
| 77 | return self.update_device(uid, **kwargs) |
| 78 | |
| 79 | def create_user(self, **kwargs): |
| 80 | return self.create_device(**kwargs) |
| 81 | |
| 82 | def delete_user(self, uid): |
| 83 | return self.delete_user(uid) |
| 84 | |
| 85 | # ------------------------------------------------------------------------ |
| 86 | |
| 87 | @property |
| 88 | def services(self): |
| 89 | return {"cdn": self.cdn_enable, |
| 90 | "url_filter": self.url_filter_enable, |
| 91 | "firewall": self.firewall_enable} |
| 92 | |
| 93 | @services.setter |
| 94 | def services(self, value): |
| 95 | pass |
| 96 | |
| 97 | def invalidate_related_objects(self): |
Scott Baker | c37e7a0 | 2017-07-17 17:32:20 -0700 | [diff] [blame^] | 98 | # Dirty all vSGs related to this subscriber, so the vSG synchronizer |
| 99 | # will run. |
| 100 | |
| 101 | # TODO: This should be reimplemented to use a watcher instead. |
| 102 | |
| 103 | # TODO: Hardcoded service dependency |
| 104 | |
| 105 | from services.volt.models import VOLTTenant |
| 106 | from services.vsg.models import VSGTenant |
| 107 | |
| 108 | for link in self.subscribed_links.all(): |
| 109 | # cast from base class to derived class |
| 110 | volts = VOLTTenant.objects.filter(serviceinstance_ptr = link.provider_service_instance) |
| 111 | for volt in volts: |
| 112 | for inner_link in volt.subscribed_links.all(): |
| 113 | # cast from base class to derived class |
| 114 | vsgs = VSGTenant.objects.filter(serviceinstance_ptr = inner_link.provider_service_instance) |
| 115 | for vsg in vsgs: |
| 116 | vsg.save() |
| 117 | |
| 118 | def save(self, *args, **kwargs): |
| 119 | self.validate_unique_service_specific_id(none_okay=True) |
| 120 | if (not hasattr(self, 'caller') or not self.caller.is_admin): |
| 121 | if (self.has_field_changed("service_specific_id")): |
| 122 | raise XOSPermissionDenied("You do not have permission to change service_specific_id") |
| 123 | super(CordSubscriberRoot, self).save(*args, **kwargs) |
| 124 | |
Scott Baker | 4eb615b | 2017-05-05 16:55:22 -0700 | [diff] [blame] | 125 | self.invalidate_related_objects() |
| 126 | |