Matteo Scandolo | f0446ed | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 1 | |
| 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 | |
Scott Baker | 4eb615b | 2017-05-05 16:55:22 -0700 | [diff] [blame] | 17 | # 'simple_attributes' will be expanded into properties and setters that |
| 18 | # store the attribute using self.set_attribute / self.get_attribute. |
| 19 | |
| 20 | simple_attributes = ( ("devices", []), ) |
| 21 | |
| 22 | sync_attributes = ("firewall_enable", |
| 23 | "firewall_rules", |
| 24 | "url_filter_enable", |
| 25 | "url_filter_rules", |
| 26 | "cdn_enable", |
| 27 | "uplink_speed", |
| 28 | "downlink_speed", |
| 29 | "enable_uverse", |
| 30 | "status") |
| 31 | |
| 32 | def __init__(self, *args, **kwargs): |
Scott Baker | c37e7a0 | 2017-07-17 17:32:20 -0700 | [diff] [blame] | 33 | # TODO: Should probably make an RCORDService, rather than filtering by name |
| 34 | rcord_services = Service.objects.filter(name="rcord") |
| 35 | if not rcord_services: |
| 36 | # this isn't going to end well... |
| 37 | raise Exception("There is no rcord service to use as the default service for CordSubscriberRoot") |
| 38 | |
| 39 | self._meta.get_field("owner").default = rcord_services[0].id |
| 40 | |
Scott Baker | 4eb615b | 2017-05-05 16:55:22 -0700 | [diff] [blame] | 41 | super(CordSubscriberRoot, self).__init__(*args, **kwargs) |
| 42 | |
| 43 | def find_device(self, mac): |
| 44 | for device in self.devices: |
| 45 | if device["mac"] == mac: |
| 46 | return device |
| 47 | return None |
| 48 | |
| 49 | def update_device(self, mac, **kwargs): |
| 50 | # kwargs may be "level" or "mac" |
| 51 | # Setting one of these to None will cause None to be stored in the db |
| 52 | devices = self.devices |
| 53 | for device in devices: |
| 54 | if device["mac"] == mac: |
| 55 | for arg in kwargs.keys(): |
| 56 | device[arg] = kwargs[arg] |
| 57 | self.devices = devices |
| 58 | return device |
| 59 | raise ValueError("Device with mac %s not found" % mac) |
| 60 | |
| 61 | def create_device(self, **kwargs): |
| 62 | if "mac" not in kwargs: |
| 63 | raise XOSMissingField("The mac field is required") |
| 64 | |
| 65 | if self.find_device(kwargs['mac']): |
| 66 | raise XOSDuplicateKey("Device with mac %s already exists" % kwargs["mac"]) |
| 67 | |
| 68 | device = kwargs.copy() |
| 69 | |
| 70 | devices = self.devices |
| 71 | devices.append(device) |
| 72 | self.devices = devices |
| 73 | |
| 74 | return device |
| 75 | |
| 76 | def delete_device(self, mac): |
| 77 | devices = self.devices |
| 78 | for device in devices: |
| 79 | if device["mac"]==mac: |
| 80 | devices.remove(device) |
| 81 | self.devices = devices |
| 82 | return |
| 83 | |
| 84 | raise ValueError("Device with mac %s not found" % mac) |
| 85 | |
| 86 | #-------------------------------------------------------------------------- |
| 87 | # Deprecated -- devices used to be called users |
| 88 | |
| 89 | def find_user(self, uid): |
| 90 | return self.find_device(uid) |
| 91 | |
| 92 | def update_user(self, uid, **kwargs): |
| 93 | return self.update_device(uid, **kwargs) |
| 94 | |
| 95 | def create_user(self, **kwargs): |
| 96 | return self.create_device(**kwargs) |
| 97 | |
| 98 | def delete_user(self, uid): |
| 99 | return self.delete_user(uid) |
| 100 | |
| 101 | # ------------------------------------------------------------------------ |
| 102 | |
| 103 | @property |
| 104 | def services(self): |
| 105 | return {"cdn": self.cdn_enable, |
| 106 | "url_filter": self.url_filter_enable, |
| 107 | "firewall": self.firewall_enable} |
| 108 | |
| 109 | @services.setter |
| 110 | def services(self, value): |
| 111 | pass |
| 112 | |
| 113 | def invalidate_related_objects(self): |
Scott Baker | c37e7a0 | 2017-07-17 17:32:20 -0700 | [diff] [blame] | 114 | # Dirty all vSGs related to this subscriber, so the vSG synchronizer |
| 115 | # will run. |
| 116 | |
Scott Baker | 617314f | 2017-09-20 12:15:16 -0700 | [diff] [blame] | 117 | # TODO: This should be reimplemented when multiple-objects-per-synchronizer is implemented. |
Scott Baker | c37e7a0 | 2017-07-17 17:32:20 -0700 | [diff] [blame] | 118 | |
| 119 | for link in self.subscribed_links.all(): |
Scott Baker | 617314f | 2017-09-20 12:15:16 -0700 | [diff] [blame] | 120 | outer_service_instance = link.provider_service_instance |
| 121 | for link in outer_service_instance.subscribed_links.all(): |
| 122 | inner_service_instance = link.provider_service_instance |
| 123 | inner_service_instance.save(update_fields = ["updated"]) |
Scott Baker | c37e7a0 | 2017-07-17 17:32:20 -0700 | [diff] [blame] | 124 | |
Sapan Bhatia | 3d4cb87 | 2017-09-20 06:42:38 -0700 | [diff] [blame] | 125 | def __xos_save_base(self, *args, **kwargs): |
Scott Baker | c37e7a0 | 2017-07-17 17:32:20 -0700 | [diff] [blame] | 126 | self.validate_unique_service_specific_id(none_okay=True) |
| 127 | if (not hasattr(self, 'caller') or not self.caller.is_admin): |
| 128 | if (self.has_field_changed("service_specific_id")): |
| 129 | raise XOSPermissionDenied("You do not have permission to change service_specific_id") |
Scott Baker | 617314f | 2017-09-20 12:15:16 -0700 | [diff] [blame] | 130 | |
Scott Baker | c37e7a0 | 2017-07-17 17:32:20 -0700 | [diff] [blame] | 131 | super(CordSubscriberRoot, self).save(*args, **kwargs) |
| 132 | |
Scott Baker | 4eb615b | 2017-05-05 16:55:22 -0700 | [diff] [blame] | 133 | self.invalidate_related_objects() |
| 134 | |
Sapan Bhatia | 3d4cb87 | 2017-09-20 06:42:38 -0700 | [diff] [blame] | 135 | return True # Indicate that we called super.save() |