models.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. from django.core.exceptions import ValidationError
  2. from django.db import models
  3. from django.utils.translation import gettext_lazy as _
  4. from dcim.choices import LinkStatusChoices
  5. from dcim.constants import WIRELESS_IFACE_TYPES
  6. from dcim.models.mixins import CachedScopeMixin
  7. from netbox.models import NestedGroupModel, PrimaryModel
  8. from netbox.models.mixins import DistanceMixin
  9. from .choices import *
  10. from .constants import *
  11. __all__ = (
  12. 'WirelessLAN',
  13. 'WirelessLANGroup',
  14. 'WirelessLink',
  15. )
  16. class WirelessAuthenticationBase(models.Model):
  17. """
  18. Abstract model for attaching attributes related to wireless authentication.
  19. """
  20. auth_type = models.CharField(
  21. max_length=50,
  22. choices=WirelessAuthTypeChoices,
  23. blank=True,
  24. null=True,
  25. verbose_name=_("authentication type"),
  26. )
  27. auth_cipher = models.CharField(
  28. verbose_name=_('authentication cipher'),
  29. max_length=50,
  30. choices=WirelessAuthCipherChoices,
  31. blank=True,
  32. null=True
  33. )
  34. auth_psk = models.CharField(
  35. max_length=PSK_MAX_LENGTH,
  36. blank=True,
  37. verbose_name=_('pre-shared key')
  38. )
  39. class Meta:
  40. abstract = True
  41. class WirelessLANGroup(NestedGroupModel):
  42. """
  43. A nested grouping of WirelessLANs
  44. """
  45. name = models.CharField(
  46. verbose_name=_('name'),
  47. max_length=100,
  48. unique=True,
  49. db_collation="natural_sort"
  50. )
  51. slug = models.SlugField(
  52. verbose_name=_('slug'),
  53. max_length=100,
  54. unique=True
  55. )
  56. class Meta:
  57. ordering = ('name', 'pk')
  58. constraints = (
  59. models.UniqueConstraint(
  60. fields=('parent', 'name'),
  61. name='%(app_label)s_%(class)s_unique_parent_name'
  62. ),
  63. )
  64. verbose_name = _('wireless LAN group')
  65. verbose_name_plural = _('wireless LAN groups')
  66. class WirelessLAN(WirelessAuthenticationBase, CachedScopeMixin, PrimaryModel):
  67. """
  68. A wireless network formed among an arbitrary number of access point and clients.
  69. """
  70. ssid = models.CharField(
  71. max_length=SSID_MAX_LENGTH,
  72. verbose_name=_('SSID')
  73. )
  74. group = models.ForeignKey(
  75. to='wireless.WirelessLANGroup',
  76. on_delete=models.SET_NULL,
  77. related_name='wireless_lans',
  78. blank=True,
  79. null=True
  80. )
  81. status = models.CharField(
  82. max_length=50,
  83. choices=WirelessLANStatusChoices,
  84. default=WirelessLANStatusChoices.STATUS_ACTIVE,
  85. verbose_name=_('status')
  86. )
  87. vlan = models.ForeignKey(
  88. to='ipam.VLAN',
  89. on_delete=models.PROTECT,
  90. blank=True,
  91. null=True,
  92. verbose_name=_('VLAN')
  93. )
  94. tenant = models.ForeignKey(
  95. to='tenancy.Tenant',
  96. on_delete=models.PROTECT,
  97. related_name='wireless_lans',
  98. blank=True,
  99. null=True
  100. )
  101. clone_fields = ('ssid', 'group', 'scope_type', 'scope_id', 'tenant', 'description')
  102. class Meta:
  103. ordering = ('ssid', 'pk')
  104. verbose_name = _('wireless LAN')
  105. verbose_name_plural = _('wireless LANs')
  106. def __str__(self):
  107. return self.ssid
  108. def get_status_color(self):
  109. return WirelessLANStatusChoices.colors.get(self.status)
  110. def get_wireless_interface_types():
  111. # Wrap choices in a callable to avoid generating dummy migrations
  112. # when the choices are updated.
  113. return {'type__in': WIRELESS_IFACE_TYPES}
  114. class WirelessLink(WirelessAuthenticationBase, DistanceMixin, PrimaryModel):
  115. """
  116. A point-to-point connection between two wireless Interfaces.
  117. """
  118. interface_a = models.ForeignKey(
  119. to='dcim.Interface',
  120. limit_choices_to=get_wireless_interface_types,
  121. on_delete=models.PROTECT,
  122. related_name='+',
  123. verbose_name=_('interface A'),
  124. )
  125. interface_b = models.ForeignKey(
  126. to='dcim.Interface',
  127. limit_choices_to=get_wireless_interface_types,
  128. on_delete=models.PROTECT,
  129. related_name='+',
  130. verbose_name=_('interface B'),
  131. )
  132. ssid = models.CharField(
  133. max_length=SSID_MAX_LENGTH,
  134. blank=True,
  135. verbose_name=_('SSID')
  136. )
  137. status = models.CharField(
  138. verbose_name=_('status'),
  139. max_length=50,
  140. choices=LinkStatusChoices,
  141. default=LinkStatusChoices.STATUS_CONNECTED
  142. )
  143. tenant = models.ForeignKey(
  144. to='tenancy.Tenant',
  145. on_delete=models.PROTECT,
  146. related_name='wireless_links',
  147. blank=True,
  148. null=True
  149. )
  150. # Cache the associated device for the A and B interfaces. This enables filtering of WirelessLinks by their
  151. # associated Devices.
  152. _interface_a_device = models.ForeignKey(
  153. to='dcim.Device',
  154. on_delete=models.CASCADE,
  155. related_name='+',
  156. blank=True,
  157. null=True
  158. )
  159. _interface_b_device = models.ForeignKey(
  160. to='dcim.Device',
  161. on_delete=models.CASCADE,
  162. related_name='+',
  163. blank=True,
  164. null=True
  165. )
  166. clone_fields = ('ssid', 'status')
  167. class Meta:
  168. ordering = ['pk']
  169. constraints = (
  170. models.UniqueConstraint(
  171. fields=('interface_a', 'interface_b'),
  172. name='%(app_label)s_%(class)s_unique_interfaces'
  173. ),
  174. )
  175. verbose_name = _('wireless link')
  176. verbose_name_plural = _('wireless links')
  177. def __str__(self):
  178. return self.ssid or f'#{self.pk}'
  179. def get_status_color(self):
  180. return LinkStatusChoices.colors.get(self.status)
  181. def clean(self):
  182. super().clean()
  183. # Validate interface types
  184. if self.interface_a.type not in WIRELESS_IFACE_TYPES:
  185. raise ValidationError({
  186. 'interface_a': _(
  187. "{type} is not a wireless interface."
  188. ).format(type=self.interface_a.get_type_display())
  189. })
  190. if self.interface_b.type not in WIRELESS_IFACE_TYPES:
  191. raise ValidationError({
  192. 'interface_a': _(
  193. "{type} is not a wireless interface."
  194. ).format(type=self.interface_b.get_type_display())
  195. })
  196. def save(self, *args, **kwargs):
  197. # Store the parent Device for the A and B interfaces
  198. self._interface_a_device = self.interface_a.device
  199. self._interface_b_device = self.interface_b.device
  200. super().save(*args, **kwargs)