blob: ef174c430da52da484098c8f26900ade8714abbd [file] [log] [blame]
Matteo Scandolof0446ed2017-08-08 13:05:24 -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
Scott Baker4eb615b2017-05-05 16:55:22 -070017# 'simple_attributes' will be expanded into properties and setters that
18# store the attribute using self.set_attribute / self.get_attribute.
19
20simple_attributes = ( ("devices", []), )
21
22sync_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
32def __init__(self, *args, **kwargs):
Scott Bakerc37e7a02017-07-17 17:32:20 -070033 # 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 Baker4eb615b2017-05-05 16:55:22 -070041 super(CordSubscriberRoot, self).__init__(*args, **kwargs)
42
43def find_device(self, mac):
44 for device in self.devices:
45 if device["mac"] == mac:
46 return device
47 return None
48
49def 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
61def 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
76def 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
89def find_user(self, uid):
90 return self.find_device(uid)
91
92def update_user(self, uid, **kwargs):
93 return self.update_device(uid, **kwargs)
94
95def create_user(self, **kwargs):
96 return self.create_device(**kwargs)
97
98def delete_user(self, uid):
99 return self.delete_user(uid)
100
101# ------------------------------------------------------------------------
102
103@property
104def services(self):
105 return {"cdn": self.cdn_enable,
106 "url_filter": self.url_filter_enable,
107 "firewall": self.firewall_enable}
108
109@services.setter
110def services(self, value):
111 pass
112
113def invalidate_related_objects(self):
Scott Bakerc37e7a02017-07-17 17:32:20 -0700114 # Dirty all vSGs related to this subscriber, so the vSG synchronizer
115 # will run.
116
Scott Baker617314f2017-09-20 12:15:16 -0700117 # TODO: This should be reimplemented when multiple-objects-per-synchronizer is implemented.
Scott Bakerc37e7a02017-07-17 17:32:20 -0700118
119 for link in self.subscribed_links.all():
Scott Baker617314f2017-09-20 12:15:16 -0700120 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 Bakerc37e7a02017-07-17 17:32:20 -0700124
Sapan Bhatia3d4cb872017-09-20 06:42:38 -0700125def __xos_save_base(self, *args, **kwargs):
Scott Bakerc37e7a02017-07-17 17:32:20 -0700126 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 Baker617314f2017-09-20 12:15:16 -0700130
Scott Bakerc37e7a02017-07-17 17:32:20 -0700131 super(CordSubscriberRoot, self).save(*args, **kwargs)
132
Scott Baker4eb615b2017-05-05 16:55:22 -0700133 self.invalidate_related_objects()
134
Sapan Bhatia3d4cb872017-09-20 06:42:38 -0700135 return True # Indicate that we called super.save()