-
-
Notifications
You must be signed in to change notification settings - Fork 287
Expand file tree
/
Copy pathmodels.py
More file actions
290 lines (263 loc) · 10.6 KB
/
models.py
File metadata and controls
290 lines (263 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
from ipaddress import ip_network
import swapper
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator
from django.db import models, transaction
from django.utils.module_loading import import_string
from django.utils.translation import gettext_lazy as _
from openwisp_users.mixins import OrgMixin
from openwisp_utils.base import TimeStampedEditableModel
from .. import settings as app_settings
class AbstractSubnetDivisionRule(TimeStampedEditableModel, OrgMixin):
_subnet_division_rule_update_queue = dict()
# It is used to monitor changes in fields of a SubnetDivisionRule object
# An entry is added to the queue from pre_save signal in the following format
#
# '<rule-uid>: {
# '<field-name>': '<old-value>',
# }
#
# In post_save signal, it is checked whether entry for SubnetDivisionRule object
# exists in this queue. If it exists changes are made to related objects.
type = models.CharField(max_length=200, choices=app_settings.SUBNET_DIVISION_TYPES)
master_subnet = models.ForeignKey(
swapper.get_model_name("openwisp_ipam", "Subnet"), on_delete=models.CASCADE
)
label = models.CharField(
max_length=30,
help_text=_("Label used to calculate the configuration variables"),
)
number_of_subnets = models.PositiveSmallIntegerField(
verbose_name=_("Number of Subnets"),
help_text=_(
"Indicates how many subnets will be created. "
"Set to 0 to assign IP addresses directly "
"from the main subnet.
),
validators=[MinValueValidator(0)],
)
size = models.PositiveSmallIntegerField(
verbose_name=_("Size of subnets"),
help_text=_("Indicates the size of each created subnet"),
)
number_of_ips = models.PositiveSmallIntegerField(
verbose_name=_("Number of IPs"),
help_text=_("Indicates how many IP addresses will be created for each subnet"),
validators=[MinValueValidator(1)],
)
class Meta:
abstract = True
constraints = [
models.UniqueConstraint(
fields=["organization", "label"],
name="unique_subnet_division_rule_label",
),
models.UniqueConstraint(
fields=["organization", "label", "type", "master_subnet"],
name="unique_subnet_division_rule",
),
]
def __str__(self):
return f"{self.label}"
@property
def rule_class(self):
return import_string(self.type)
def clean(self):
# Auto-fill organization from master subnet
if (
self.master_subnet_id
and self.master_subnet.organization_id is not None
and not self.organization_id
):
self.organization_id = self.master_subnet.organization_id
super().clean()
self._validate_label()
self._validate_master_subnet_validity()
self._validate_master_subnet_consistency()
self._validate_ip_address_consistency()
if not self._state.adding:
self._validate_existing_fields()
def _validate_label(self):
if not self.label.isidentifier():
raise ValidationError(
{
"label": _(
"Only alphanumeric characters and underscores are allowed."
)
}
)
def _validate_master_subnet_validity(self):
if not self.master_subnet.subnet:
raise ValidationError({"master_subnet": _("Invalid master subnet.")})
def _validate_existing_fields(self):
db_instance = self._meta.model.objects.get(id=self.id)
# The size field should not be changed
if self.size != db_instance.size:
raise ValidationError({"size": _("Subnet size cannot be changed")})
# Number of IPs should not decreased
if self.number_of_ips < db_instance.number_of_ips:
raise ValidationError(
{"number_of_ips": _("Number of IPs cannot be decreased")}
)
# Number of subnets should not be changed
if self.number_of_subnets != db_instance.number_of_subnets:
raise ValidationError(
{"number_of_subnets": _("Number of Subnets cannot be changed")}
)
def _validate_master_subnet_consistency(self):
master_subnet = self.master_subnet.subnet
# Try to generate subnets and validate size
try:
next(master_subnet.subnets(new_prefix=self.size))
# if size is not consistent, raise error
except ValueError:
raise ValidationError(
{
"size": _(
"Master subnet cannot accommodate subnets of size /{0}".format(
self.size
)
)
}
)
# Ensure that master subnet can accommodate
# the required number of generated subnets
available = 2 ** (self.size - master_subnet.prefixlen)
# Account for the reserved subnet
available -= 1
if self.number_of_subnets >= available:
raise ValidationError(
{
"number_of_subnets": _(
"The master subnet is too small to acommodate the "
'requested "number of subnets" plus the reserved '
"subnet, please increase the size of the master "
'subnet or decrease the "size of subnets" field.'
)
}
)
# Validate organization of master subnet
if (
self.master_subnet.organization is not None
and self.master_subnet.organization != self.organization
):
raise ValidationError(
{"organization": _("Organization should be same as the subnet")}
)
def _validate_ip_address_consistency(self):
try:
next(
ip_network(str(self.master_subnet.subnet)).subnets(new_prefix=self.size)
)[self.number_of_ips - 1]
except IndexError:
raise ValidationError(
{
"number_of_ips": _(
f"Generated subnets of size /{self.size} cannot accommodate "
f"{self.number_of_ips} IP Addresses."
)
}
)
def check_and_queue_modified_fields(self):
try:
db_instance = self._meta.model.objects.get(id=self.id)
except self._meta.model.DoesNotExist:
# This rule does not exists in database.
# No operation is needed to be performed.
return
else:
# Check which fields of instance is modified
# NOTE: Open-ended implementation to allow change in all
# fields of SubnetDivisionRule in future.
# Currently only changing label and number of IPs is allowed.
modified_fields = {}
for field in db_instance._meta.fields:
instance_value = getattr(self, field.name)
db_value = getattr(db_instance, field.name)
if instance_value != db_value:
modified_fields[field.name] = db_value
if modified_fields:
self._subnet_division_rule_update_queue[str(self.id)] = modified_fields
def update_related_objects(self):
from .. import tasks
# Update related objects appropriately.
# NOTE: Currently only changing label and number of IPs is implemented/allowed.
try:
modified_fields = self._subnet_division_rule_update_queue.pop(str(self.id))
except KeyError:
return
else:
if "label" in modified_fields:
tasks.update_subnet_division_index.delay(rule_id=str(self.id))
tasks.update_subnet_name_description(rule_id=str(self.id))
if "number_of_ips" in modified_fields:
tasks.provision_extra_ips.delay(
rule_id=str(self.id),
old_number_of_ips=modified_fields["number_of_ips"],
)
def delete_provisioned_subnets(self):
# Deleting an object of SubnetDivisionRule will set the rule field
# of related SubnetDivisionIndex to "None" due to "on_delete=SET_NULL".
# These indexes are used delete subnets that were provisioned by the
# deleted rule. Deleting a Subnet object will automatically delete
# related IpAddress objects.
Subnet = swapper.load_model("openwisp_ipam", "Subnet")
SubnetDivisionIndex = swapper.load_model(
"subnet_division", "SubnetDivisionIndex"
)
Subnet.objects.filter(
id__in=SubnetDivisionIndex.objects.filter(rule_id=None).values("subnet_id")
).delete()
@classmethod
def pre_save(cls, instance, **kwargs):
instance.check_and_queue_modified_fields()
@classmethod
def post_save(cls, instance, created, **kwargs):
from ..tasks import provision_subnet_ip_for_existing_devices
if created:
transaction.on_commit(
lambda: provision_subnet_ip_for_existing_devices.delay(
rule_id=instance.id
)
)
else:
transaction.on_commit(instance.update_related_objects)
@classmethod
def post_delete(cls, instance, **kwargs):
transaction.on_commit(instance.delete_provisioned_subnets)
class AbstractSubnetDivisionIndex(models.Model):
keyword = models.CharField(max_length=30)
subnet = models.ForeignKey(
swapper.get_model_name("openwisp_ipam", "Subnet"),
on_delete=models.CASCADE,
null=True,
blank=True,
)
ip = models.ForeignKey(
swapper.get_model_name("openwisp_ipam", "IpAddress"),
on_delete=models.CASCADE,
null=True,
blank=True,
)
rule = models.ForeignKey(
swapper.get_model_name("subnet_division", "SubnetDivisionRule"),
null=True,
on_delete=models.SET_NULL,
)
config = models.ForeignKey(
swapper.get_model_name("config", "Config"),
on_delete=models.CASCADE,
null=True,
blank=True,
)
class Meta:
abstract = True
indexes = [
models.Index(fields=["keyword"]),
]
constraints = [
models.UniqueConstraint(
fields=["keyword", "subnet", "ip", "config"],
name="unique_subnet_division_index",
),
]