vrfs.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from django.db import models
  2. from django.utils.translation import gettext_lazy as _
  3. from ipam.constants import *
  4. from netbox.models import PrimaryModel
  5. __all__ = (
  6. 'RouteTarget',
  7. 'VRF',
  8. )
  9. class VRF(PrimaryModel):
  10. """
  11. A virtual routing and forwarding (VRF) table represents a discrete layer three forwarding domain (e.g. a routing
  12. table). Prefixes and IPAddresses can optionally be assigned to VRFs. (Prefixes and IPAddresses not assigned to a VRF
  13. are said to exist in the "global" table.)
  14. """
  15. name = models.CharField(
  16. verbose_name=_('name'),
  17. max_length=100,
  18. db_collation="natural_sort"
  19. )
  20. rd = models.CharField(
  21. max_length=VRF_RD_MAX_LENGTH,
  22. unique=True,
  23. blank=True,
  24. null=True,
  25. verbose_name=_('route distinguisher'),
  26. help_text=_('Unique route distinguisher (as defined in RFC 4364)')
  27. )
  28. tenant = models.ForeignKey(
  29. to='tenancy.Tenant',
  30. on_delete=models.PROTECT,
  31. related_name='vrfs',
  32. blank=True,
  33. null=True
  34. )
  35. enforce_unique = models.BooleanField(
  36. default=True,
  37. verbose_name=_('enforce unique space'),
  38. help_text=_('Prevent duplicate prefixes/IP addresses within this VRF')
  39. )
  40. import_targets = models.ManyToManyField(
  41. to='ipam.RouteTarget',
  42. related_name='importing_vrfs',
  43. blank=True
  44. )
  45. export_targets = models.ManyToManyField(
  46. to='ipam.RouteTarget',
  47. related_name='exporting_vrfs',
  48. blank=True
  49. )
  50. clone_fields = (
  51. 'tenant', 'enforce_unique', 'description',
  52. )
  53. class Meta:
  54. ordering = ('name', 'rd', 'pk') # (name, rd) may be non-unique
  55. verbose_name = _('VRF')
  56. verbose_name_plural = _('VRFs')
  57. def __str__(self):
  58. if self.rd:
  59. return f'{self.name} ({self.rd})'
  60. return self.name
  61. class RouteTarget(PrimaryModel):
  62. """
  63. A BGP extended community used to control the redistribution of routes among VRFs, as defined in RFC 4364.
  64. """
  65. name = models.CharField(
  66. verbose_name=_('name'),
  67. max_length=VRF_RD_MAX_LENGTH, # Same format options as VRF RD (RFC 4360 section 4)
  68. unique=True,
  69. help_text=_('Route target value (formatted in accordance with RFC 4360)'),
  70. db_collation="natural_sort"
  71. )
  72. tenant = models.ForeignKey(
  73. to='tenancy.Tenant',
  74. on_delete=models.PROTECT,
  75. related_name='route_targets',
  76. blank=True,
  77. null=True
  78. )
  79. class Meta:
  80. ordering = ['name']
  81. verbose_name = _('route target')
  82. verbose_name_plural = _('route targets')
  83. def __str__(self):
  84. return self.name