device_components.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.core.exceptions import ObjectDoesNotExist, ValidationError
  4. from django.core.validators import MaxValueValidator, MinValueValidator
  5. from django.db import models
  6. from django.db.models import Sum
  7. from django.urls import reverse
  8. from mptt.models import MPTTModel, TreeForeignKey
  9. from taggit.managers import TaggableManager
  10. from dcim.choices import *
  11. from dcim.constants import *
  12. from dcim.fields import MACAddressField
  13. from extras.models import TaggedItem
  14. from extras.utils import extras_features
  15. from netbox.models import PrimaryModel
  16. from utilities.fields import NaturalOrderingField
  17. from utilities.mptt import TreeManager
  18. from utilities.ordering import naturalize_interface
  19. from utilities.querysets import RestrictedQuerySet
  20. from utilities.query_functions import CollateAsChar
  21. __all__ = (
  22. 'BaseInterface',
  23. 'CableTermination',
  24. 'ConsolePort',
  25. 'ConsoleServerPort',
  26. 'DeviceBay',
  27. 'FrontPort',
  28. 'Interface',
  29. 'InventoryItem',
  30. 'PathEndpoint',
  31. 'PowerOutlet',
  32. 'PowerPort',
  33. 'RearPort',
  34. )
  35. class ComponentModel(PrimaryModel):
  36. """
  37. An abstract model inherited by any model which has a parent Device.
  38. """
  39. device = models.ForeignKey(
  40. to='dcim.Device',
  41. on_delete=models.CASCADE,
  42. related_name='%(class)ss'
  43. )
  44. name = models.CharField(
  45. max_length=64
  46. )
  47. _name = NaturalOrderingField(
  48. target_field='name',
  49. max_length=100,
  50. blank=True
  51. )
  52. label = models.CharField(
  53. max_length=64,
  54. blank=True,
  55. help_text="Physical label"
  56. )
  57. description = models.CharField(
  58. max_length=200,
  59. blank=True
  60. )
  61. objects = RestrictedQuerySet.as_manager()
  62. class Meta:
  63. abstract = True
  64. def __str__(self):
  65. if self.label:
  66. return f"{self.name} ({self.label})"
  67. return self.name
  68. def to_objectchange(self, action):
  69. # Annotate the parent Device
  70. try:
  71. device = self.device
  72. except ObjectDoesNotExist:
  73. # The parent Device has already been deleted
  74. device = None
  75. return super().to_objectchange(action, related_object=device)
  76. @property
  77. def parent_object(self):
  78. return self.device
  79. class CableTermination(models.Model):
  80. """
  81. An abstract model inherited by all models to which a Cable can terminate (certain device components, PowerFeed, and
  82. CircuitTermination instances). The `cable` field indicates the Cable instance which is terminated to this instance.
  83. `_cable_peer` is a GenericForeignKey used to cache the far-end CableTermination on the local instance; this is a
  84. shortcut to referencing `cable.termination_b`, for example. `_cable_peer` is set or cleared by the receivers in
  85. dcim.signals when a Cable instance is created or deleted, respectively.
  86. """
  87. cable = models.ForeignKey(
  88. to='dcim.Cable',
  89. on_delete=models.SET_NULL,
  90. related_name='+',
  91. blank=True,
  92. null=True
  93. )
  94. _cable_peer_type = models.ForeignKey(
  95. to=ContentType,
  96. on_delete=models.SET_NULL,
  97. related_name='+',
  98. blank=True,
  99. null=True
  100. )
  101. _cable_peer_id = models.PositiveIntegerField(
  102. blank=True,
  103. null=True
  104. )
  105. _cable_peer = GenericForeignKey(
  106. ct_field='_cable_peer_type',
  107. fk_field='_cable_peer_id'
  108. )
  109. mark_connected = models.BooleanField(
  110. default=False,
  111. help_text="Treat as if a cable is connected"
  112. )
  113. # Generic relations to Cable. These ensure that an attached Cable is deleted if the terminated object is deleted.
  114. _cabled_as_a = GenericRelation(
  115. to='dcim.Cable',
  116. content_type_field='termination_a_type',
  117. object_id_field='termination_a_id'
  118. )
  119. _cabled_as_b = GenericRelation(
  120. to='dcim.Cable',
  121. content_type_field='termination_b_type',
  122. object_id_field='termination_b_id'
  123. )
  124. class Meta:
  125. abstract = True
  126. def clean(self):
  127. super().clean()
  128. if self.mark_connected and self.cable_id:
  129. raise ValidationError("Cannot set mark_connected with a cable connected.")
  130. def get_cable_peer(self):
  131. return self._cable_peer
  132. @property
  133. def _occupied(self):
  134. return bool(self.mark_connected or self.cable_id)
  135. @property
  136. def parent_object(self):
  137. raise NotImplementedError("CableTermination models must implement parent_object()")
  138. class PathEndpoint(models.Model):
  139. """
  140. An abstract model inherited by any CableTermination subclass which represents the end of a CablePath; specifically,
  141. these include ConsolePort, ConsoleServerPort, PowerPort, PowerOutlet, Interface, PowerFeed, and CircuitTermination.
  142. `_path` references the CablePath originating from this instance, if any. It is set or cleared by the receivers in
  143. dcim.signals in response to changes in the cable path, and complements the `origin` GenericForeignKey field on the
  144. CablePath model. `_path` should not be accessed directly; rather, use the `path` property.
  145. `connected_endpoint()` is a convenience method for returning the destination of the associated CablePath, if any.
  146. """
  147. _path = models.ForeignKey(
  148. to='dcim.CablePath',
  149. on_delete=models.SET_NULL,
  150. null=True,
  151. blank=True
  152. )
  153. class Meta:
  154. abstract = True
  155. def trace(self):
  156. if self._path is None:
  157. return []
  158. # Construct the complete path
  159. path = [self, *self._path.get_path()]
  160. while (len(path) + 1) % 3:
  161. # Pad to ensure we have complete three-tuples (e.g. for paths that end at a RearPort)
  162. path.append(None)
  163. path.append(self._path.destination)
  164. # Return the path as a list of three-tuples (A termination, cable, B termination)
  165. return list(zip(*[iter(path)] * 3))
  166. @property
  167. def path(self):
  168. return self._path
  169. @property
  170. def connected_endpoint(self):
  171. """
  172. Caching accessor for the attached CablePath's destination (if any)
  173. """
  174. if not hasattr(self, '_connected_endpoint'):
  175. self._connected_endpoint = self._path.destination if self._path else None
  176. return self._connected_endpoint
  177. #
  178. # Console ports
  179. #
  180. @extras_features('custom_fields', 'export_templates', 'webhooks')
  181. class ConsolePort(ComponentModel, CableTermination, PathEndpoint):
  182. """
  183. A physical console port within a Device. ConsolePorts connect to ConsoleServerPorts.
  184. """
  185. type = models.CharField(
  186. max_length=50,
  187. choices=ConsolePortTypeChoices,
  188. blank=True,
  189. help_text='Physical port type'
  190. )
  191. speed = models.PositiveSmallIntegerField(
  192. choices=ConsolePortSpeedChoices,
  193. blank=True,
  194. null=True,
  195. help_text='Port speed in bits per second'
  196. )
  197. tags = TaggableManager(through=TaggedItem)
  198. csv_headers = ['device', 'name', 'label', 'type', 'speed', 'mark_connected', 'description']
  199. class Meta:
  200. ordering = ('device', '_name')
  201. unique_together = ('device', 'name')
  202. def get_absolute_url(self):
  203. return reverse('dcim:consoleport', kwargs={'pk': self.pk})
  204. def to_csv(self):
  205. return (
  206. self.device.identifier,
  207. self.name,
  208. self.label,
  209. self.type,
  210. self.speed,
  211. self.mark_connected,
  212. self.description,
  213. )
  214. #
  215. # Console server ports
  216. #
  217. @extras_features('custom_fields', 'export_templates', 'webhooks')
  218. class ConsoleServerPort(ComponentModel, CableTermination, PathEndpoint):
  219. """
  220. A physical port within a Device (typically a designated console server) which provides access to ConsolePorts.
  221. """
  222. type = models.CharField(
  223. max_length=50,
  224. choices=ConsolePortTypeChoices,
  225. blank=True,
  226. help_text='Physical port type'
  227. )
  228. speed = models.PositiveSmallIntegerField(
  229. choices=ConsolePortSpeedChoices,
  230. blank=True,
  231. null=True,
  232. help_text='Port speed in bits per second'
  233. )
  234. tags = TaggableManager(through=TaggedItem)
  235. csv_headers = ['device', 'name', 'label', 'type', 'speed', 'mark_connected', 'description']
  236. class Meta:
  237. ordering = ('device', '_name')
  238. unique_together = ('device', 'name')
  239. def get_absolute_url(self):
  240. return reverse('dcim:consoleserverport', kwargs={'pk': self.pk})
  241. def to_csv(self):
  242. return (
  243. self.device.identifier,
  244. self.name,
  245. self.label,
  246. self.type,
  247. self.speed,
  248. self.mark_connected,
  249. self.description,
  250. )
  251. #
  252. # Power ports
  253. #
  254. @extras_features('custom_fields', 'export_templates', 'webhooks')
  255. class PowerPort(ComponentModel, CableTermination, PathEndpoint):
  256. """
  257. A physical power supply (intake) port within a Device. PowerPorts connect to PowerOutlets.
  258. """
  259. type = models.CharField(
  260. max_length=50,
  261. choices=PowerPortTypeChoices,
  262. blank=True,
  263. help_text='Physical port type'
  264. )
  265. maximum_draw = models.PositiveSmallIntegerField(
  266. blank=True,
  267. null=True,
  268. validators=[MinValueValidator(1)],
  269. help_text="Maximum power draw (watts)"
  270. )
  271. allocated_draw = models.PositiveSmallIntegerField(
  272. blank=True,
  273. null=True,
  274. validators=[MinValueValidator(1)],
  275. help_text="Allocated power draw (watts)"
  276. )
  277. tags = TaggableManager(through=TaggedItem)
  278. csv_headers = [
  279. 'device', 'name', 'label', 'type', 'mark_connected', 'maximum_draw', 'allocated_draw', 'description',
  280. ]
  281. class Meta:
  282. ordering = ('device', '_name')
  283. unique_together = ('device', 'name')
  284. def get_absolute_url(self):
  285. return reverse('dcim:powerport', kwargs={'pk': self.pk})
  286. def to_csv(self):
  287. return (
  288. self.device.identifier,
  289. self.name,
  290. self.label,
  291. self.get_type_display(),
  292. self.mark_connected,
  293. self.maximum_draw,
  294. self.allocated_draw,
  295. self.description,
  296. )
  297. def clean(self):
  298. super().clean()
  299. if self.maximum_draw is not None and self.allocated_draw is not None:
  300. if self.allocated_draw > self.maximum_draw:
  301. raise ValidationError({
  302. 'allocated_draw': f"Allocated draw cannot exceed the maximum draw ({self.maximum_draw}W)."
  303. })
  304. def get_power_draw(self):
  305. """
  306. Return the allocated and maximum power draw (in VA) and child PowerOutlet count for this PowerPort.
  307. """
  308. # Calculate aggregate draw of all child power outlets if no numbers have been defined manually
  309. if self.allocated_draw is None and self.maximum_draw is None:
  310. poweroutlet_ct = ContentType.objects.get_for_model(PowerOutlet)
  311. outlet_ids = PowerOutlet.objects.filter(power_port=self).values_list('pk', flat=True)
  312. utilization = PowerPort.objects.filter(
  313. _cable_peer_type=poweroutlet_ct,
  314. _cable_peer_id__in=outlet_ids
  315. ).aggregate(
  316. maximum_draw_total=Sum('maximum_draw'),
  317. allocated_draw_total=Sum('allocated_draw'),
  318. )
  319. ret = {
  320. 'allocated': utilization['allocated_draw_total'] or 0,
  321. 'maximum': utilization['maximum_draw_total'] or 0,
  322. 'outlet_count': len(outlet_ids),
  323. 'legs': [],
  324. }
  325. # Calculate per-leg aggregates for three-phase feeds
  326. if getattr(self._cable_peer, 'phase', None) == PowerFeedPhaseChoices.PHASE_3PHASE:
  327. for leg, leg_name in PowerOutletFeedLegChoices:
  328. outlet_ids = PowerOutlet.objects.filter(power_port=self, feed_leg=leg).values_list('pk', flat=True)
  329. utilization = PowerPort.objects.filter(
  330. _cable_peer_type=poweroutlet_ct,
  331. _cable_peer_id__in=outlet_ids
  332. ).aggregate(
  333. maximum_draw_total=Sum('maximum_draw'),
  334. allocated_draw_total=Sum('allocated_draw'),
  335. )
  336. ret['legs'].append({
  337. 'name': leg_name,
  338. 'allocated': utilization['allocated_draw_total'] or 0,
  339. 'maximum': utilization['maximum_draw_total'] or 0,
  340. 'outlet_count': len(outlet_ids),
  341. })
  342. return ret
  343. # Default to administratively defined values
  344. return {
  345. 'allocated': self.allocated_draw or 0,
  346. 'maximum': self.maximum_draw or 0,
  347. 'outlet_count': PowerOutlet.objects.filter(power_port=self).count(),
  348. 'legs': [],
  349. }
  350. #
  351. # Power outlets
  352. #
  353. @extras_features('custom_fields', 'export_templates', 'webhooks')
  354. class PowerOutlet(ComponentModel, CableTermination, PathEndpoint):
  355. """
  356. A physical power outlet (output) within a Device which provides power to a PowerPort.
  357. """
  358. type = models.CharField(
  359. max_length=50,
  360. choices=PowerOutletTypeChoices,
  361. blank=True,
  362. help_text='Physical port type'
  363. )
  364. power_port = models.ForeignKey(
  365. to='dcim.PowerPort',
  366. on_delete=models.SET_NULL,
  367. blank=True,
  368. null=True,
  369. related_name='poweroutlets'
  370. )
  371. feed_leg = models.CharField(
  372. max_length=50,
  373. choices=PowerOutletFeedLegChoices,
  374. blank=True,
  375. help_text="Phase (for three-phase feeds)"
  376. )
  377. tags = TaggableManager(through=TaggedItem)
  378. csv_headers = ['device', 'name', 'label', 'type', 'mark_connected', 'power_port', 'feed_leg', 'description']
  379. class Meta:
  380. ordering = ('device', '_name')
  381. unique_together = ('device', 'name')
  382. def get_absolute_url(self):
  383. return reverse('dcim:poweroutlet', kwargs={'pk': self.pk})
  384. def to_csv(self):
  385. return (
  386. self.device.identifier,
  387. self.name,
  388. self.label,
  389. self.get_type_display(),
  390. self.mark_connected,
  391. self.power_port.name if self.power_port else None,
  392. self.get_feed_leg_display(),
  393. self.description,
  394. )
  395. def clean(self):
  396. super().clean()
  397. # Validate power port assignment
  398. if self.power_port and self.power_port.device != self.device:
  399. raise ValidationError(
  400. "Parent power port ({}) must belong to the same device".format(self.power_port)
  401. )
  402. #
  403. # Interfaces
  404. #
  405. class BaseInterface(models.Model):
  406. """
  407. Abstract base class for fields shared by dcim.Interface and virtualization.VMInterface.
  408. """
  409. enabled = models.BooleanField(
  410. default=True
  411. )
  412. mac_address = MACAddressField(
  413. null=True,
  414. blank=True,
  415. verbose_name='MAC Address'
  416. )
  417. mtu = models.PositiveIntegerField(
  418. blank=True,
  419. null=True,
  420. validators=[MinValueValidator(1), MaxValueValidator(65536)],
  421. verbose_name='MTU'
  422. )
  423. mode = models.CharField(
  424. max_length=50,
  425. choices=InterfaceModeChoices,
  426. blank=True
  427. )
  428. class Meta:
  429. abstract = True
  430. def save(self, *args, **kwargs):
  431. # Remove untagged VLAN assignment for non-802.1Q interfaces
  432. if not self.mode:
  433. self.untagged_vlan = None
  434. # Only "tagged" interfaces may have tagged VLANs assigned. ("tagged all" implies all VLANs are assigned.)
  435. if self.pk and self.mode != InterfaceModeChoices.MODE_TAGGED:
  436. self.tagged_vlans.clear()
  437. return super().save(*args, **kwargs)
  438. @extras_features('custom_fields', 'export_templates', 'webhooks')
  439. class Interface(ComponentModel, BaseInterface, CableTermination, PathEndpoint):
  440. """
  441. A network interface within a Device. A physical Interface can connect to exactly one other Interface.
  442. """
  443. # Override ComponentModel._name to specify naturalize_interface function
  444. _name = NaturalOrderingField(
  445. target_field='name',
  446. naturalize_function=naturalize_interface,
  447. max_length=100,
  448. blank=True
  449. )
  450. lag = models.ForeignKey(
  451. to='self',
  452. on_delete=models.SET_NULL,
  453. related_name='member_interfaces',
  454. null=True,
  455. blank=True,
  456. verbose_name='Parent LAG'
  457. )
  458. type = models.CharField(
  459. max_length=50,
  460. choices=InterfaceTypeChoices
  461. )
  462. mgmt_only = models.BooleanField(
  463. default=False,
  464. verbose_name='Management only',
  465. help_text='This interface is used only for out-of-band management'
  466. )
  467. untagged_vlan = models.ForeignKey(
  468. to='ipam.VLAN',
  469. on_delete=models.SET_NULL,
  470. related_name='interfaces_as_untagged',
  471. null=True,
  472. blank=True,
  473. verbose_name='Untagged VLAN'
  474. )
  475. tagged_vlans = models.ManyToManyField(
  476. to='ipam.VLAN',
  477. related_name='interfaces_as_tagged',
  478. blank=True,
  479. verbose_name='Tagged VLANs'
  480. )
  481. ip_addresses = GenericRelation(
  482. to='ipam.IPAddress',
  483. content_type_field='assigned_object_type',
  484. object_id_field='assigned_object_id',
  485. related_query_name='interface'
  486. )
  487. tags = TaggableManager(through=TaggedItem)
  488. csv_headers = [
  489. 'device', 'name', 'label', 'lag', 'type', 'enabled', 'mark_connected', 'mac_address', 'mtu', 'mgmt_only',
  490. 'description', 'mode',
  491. ]
  492. class Meta:
  493. ordering = ('device', CollateAsChar('_name'))
  494. unique_together = ('device', 'name')
  495. def get_absolute_url(self):
  496. return reverse('dcim:interface', kwargs={'pk': self.pk})
  497. def to_csv(self):
  498. return (
  499. self.device.identifier if self.device else None,
  500. self.name,
  501. self.label,
  502. self.lag.name if self.lag else None,
  503. self.get_type_display(),
  504. self.enabled,
  505. self.mark_connected,
  506. self.mac_address,
  507. self.mtu,
  508. self.mgmt_only,
  509. self.description,
  510. self.get_mode_display(),
  511. )
  512. def clean(self):
  513. super().clean()
  514. # Virtual interfaces cannot be connected
  515. if self.type in NONCONNECTABLE_IFACE_TYPES and (
  516. self.cable or getattr(self, 'circuit_termination', False)
  517. ):
  518. raise ValidationError({
  519. 'type': "Virtual and wireless interfaces cannot be connected to another interface or circuit. "
  520. "Disconnect the interface or choose a suitable type."
  521. })
  522. # An interface's LAG must belong to the same device or virtual chassis
  523. if self.lag and self.lag.device != self.device:
  524. if self.device.virtual_chassis is None:
  525. raise ValidationError({
  526. 'lag': f"The selected LAG interface ({self.lag}) belongs to a different device ({self.lag.device})."
  527. })
  528. elif self.lag.device.virtual_chassis != self.device.virtual_chassis:
  529. raise ValidationError({
  530. 'lag': f"The selected LAG interface ({self.lag}) belongs to {self.lag.device}, which is not part "
  531. f"of virtual chassis {self.device.virtual_chassis}."
  532. })
  533. # A virtual interface cannot have a parent LAG
  534. if self.type == InterfaceTypeChoices.TYPE_VIRTUAL and self.lag is not None:
  535. raise ValidationError({'lag': "Virtual interfaces cannot have a parent LAG interface."})
  536. # A LAG interface cannot be its own parent
  537. if self.pk and self.lag_id == self.pk:
  538. raise ValidationError({'lag': "A LAG interface cannot be its own parent."})
  539. # Validate untagged VLAN
  540. if self.untagged_vlan and self.untagged_vlan.site not in [self.device.site, None]:
  541. raise ValidationError({
  542. 'untagged_vlan': "The untagged VLAN ({}) must belong to the same site as the interface's parent "
  543. "device, or it must be global".format(self.untagged_vlan)
  544. })
  545. @property
  546. def is_connectable(self):
  547. return self.type not in NONCONNECTABLE_IFACE_TYPES
  548. @property
  549. def is_virtual(self):
  550. return self.type in VIRTUAL_IFACE_TYPES
  551. @property
  552. def is_wireless(self):
  553. return self.type in WIRELESS_IFACE_TYPES
  554. @property
  555. def is_lag(self):
  556. return self.type == InterfaceTypeChoices.TYPE_LAG
  557. @property
  558. def count_ipaddresses(self):
  559. return self.ip_addresses.count()
  560. #
  561. # Pass-through ports
  562. #
  563. @extras_features('custom_fields', 'export_templates', 'webhooks')
  564. class FrontPort(ComponentModel, CableTermination):
  565. """
  566. A pass-through port on the front of a Device.
  567. """
  568. type = models.CharField(
  569. max_length=50,
  570. choices=PortTypeChoices
  571. )
  572. rear_port = models.ForeignKey(
  573. to='dcim.RearPort',
  574. on_delete=models.CASCADE,
  575. related_name='frontports'
  576. )
  577. rear_port_position = models.PositiveSmallIntegerField(
  578. default=1,
  579. validators=[
  580. MinValueValidator(REARPORT_POSITIONS_MIN),
  581. MaxValueValidator(REARPORT_POSITIONS_MAX)
  582. ]
  583. )
  584. tags = TaggableManager(through=TaggedItem)
  585. csv_headers = [
  586. 'device', 'name', 'label', 'type', 'mark_connected', 'rear_port', 'rear_port_position', 'description',
  587. ]
  588. class Meta:
  589. ordering = ('device', '_name')
  590. unique_together = (
  591. ('device', 'name'),
  592. ('rear_port', 'rear_port_position'),
  593. )
  594. def get_absolute_url(self):
  595. return reverse('dcim:frontport', kwargs={'pk': self.pk})
  596. def to_csv(self):
  597. return (
  598. self.device.identifier,
  599. self.name,
  600. self.label,
  601. self.get_type_display(),
  602. self.mark_connected,
  603. self.rear_port.name,
  604. self.rear_port_position,
  605. self.description,
  606. )
  607. def clean(self):
  608. super().clean()
  609. # Validate rear port assignment
  610. if self.rear_port.device != self.device:
  611. raise ValidationError({
  612. "rear_port": f"Rear port ({self.rear_port}) must belong to the same device"
  613. })
  614. # Validate rear port position assignment
  615. if self.rear_port_position > self.rear_port.positions:
  616. raise ValidationError({
  617. "rear_port_position": f"Invalid rear port position ({self.rear_port_position}): Rear port "
  618. f"{self.rear_port.name} has only {self.rear_port.positions} positions"
  619. })
  620. @extras_features('custom_fields', 'export_templates', 'webhooks')
  621. class RearPort(ComponentModel, CableTermination):
  622. """
  623. A pass-through port on the rear of a Device.
  624. """
  625. type = models.CharField(
  626. max_length=50,
  627. choices=PortTypeChoices
  628. )
  629. positions = models.PositiveSmallIntegerField(
  630. default=1,
  631. validators=[
  632. MinValueValidator(REARPORT_POSITIONS_MIN),
  633. MaxValueValidator(REARPORT_POSITIONS_MAX)
  634. ]
  635. )
  636. tags = TaggableManager(through=TaggedItem)
  637. csv_headers = ['device', 'name', 'label', 'type', 'mark_connected', 'positions', 'description']
  638. class Meta:
  639. ordering = ('device', '_name')
  640. unique_together = ('device', 'name')
  641. def get_absolute_url(self):
  642. return reverse('dcim:rearport', kwargs={'pk': self.pk})
  643. def clean(self):
  644. super().clean()
  645. # Check that positions count is greater than or equal to the number of associated FrontPorts
  646. frontport_count = self.frontports.count()
  647. if self.positions < frontport_count:
  648. raise ValidationError({
  649. "positions": f"The number of positions cannot be less than the number of mapped front ports "
  650. f"({frontport_count})"
  651. })
  652. def to_csv(self):
  653. return (
  654. self.device.identifier,
  655. self.name,
  656. self.label,
  657. self.get_type_display(),
  658. self.mark_connected,
  659. self.positions,
  660. self.description,
  661. )
  662. #
  663. # Device bays
  664. #
  665. @extras_features('custom_fields', 'export_templates', 'webhooks')
  666. class DeviceBay(ComponentModel):
  667. """
  668. An empty space within a Device which can house a child device
  669. """
  670. installed_device = models.OneToOneField(
  671. to='dcim.Device',
  672. on_delete=models.SET_NULL,
  673. related_name='parent_bay',
  674. blank=True,
  675. null=True
  676. )
  677. tags = TaggableManager(through=TaggedItem)
  678. csv_headers = ['device', 'name', 'label', 'installed_device', 'description']
  679. class Meta:
  680. ordering = ('device', '_name')
  681. unique_together = ('device', 'name')
  682. def get_absolute_url(self):
  683. return reverse('dcim:devicebay', kwargs={'pk': self.pk})
  684. def to_csv(self):
  685. return (
  686. self.device.identifier,
  687. self.name,
  688. self.label,
  689. self.installed_device.identifier if self.installed_device else None,
  690. self.description,
  691. )
  692. def clean(self):
  693. super().clean()
  694. # Validate that the parent Device can have DeviceBays
  695. if not self.device.device_type.is_parent_device:
  696. raise ValidationError("This type of device ({}) does not support device bays.".format(
  697. self.device.device_type
  698. ))
  699. # Cannot install a device into itself, obviously
  700. if self.device == self.installed_device:
  701. raise ValidationError("Cannot install a device into itself.")
  702. # Check that the installed device is not already installed elsewhere
  703. if self.installed_device:
  704. current_bay = DeviceBay.objects.filter(installed_device=self.installed_device).first()
  705. if current_bay and current_bay != self:
  706. raise ValidationError({
  707. 'installed_device': "Cannot install the specified device; device is already installed in {}".format(
  708. current_bay
  709. )
  710. })
  711. #
  712. # Inventory items
  713. #
  714. @extras_features('custom_fields', 'export_templates', 'webhooks')
  715. class InventoryItem(MPTTModel, ComponentModel):
  716. """
  717. An InventoryItem represents a serialized piece of hardware within a Device, such as a line card or power supply.
  718. InventoryItems are used only for inventory purposes.
  719. """
  720. parent = TreeForeignKey(
  721. to='self',
  722. on_delete=models.CASCADE,
  723. related_name='child_items',
  724. blank=True,
  725. null=True,
  726. db_index=True
  727. )
  728. manufacturer = models.ForeignKey(
  729. to='dcim.Manufacturer',
  730. on_delete=models.PROTECT,
  731. related_name='inventory_items',
  732. blank=True,
  733. null=True
  734. )
  735. part_id = models.CharField(
  736. max_length=50,
  737. verbose_name='Part ID',
  738. blank=True,
  739. help_text='Manufacturer-assigned part identifier'
  740. )
  741. serial = models.CharField(
  742. max_length=50,
  743. verbose_name='Serial number',
  744. blank=True
  745. )
  746. asset_tag = models.CharField(
  747. max_length=50,
  748. unique=True,
  749. blank=True,
  750. null=True,
  751. verbose_name='Asset tag',
  752. help_text='A unique tag used to identify this item'
  753. )
  754. discovered = models.BooleanField(
  755. default=False,
  756. help_text='This item was automatically discovered'
  757. )
  758. tags = TaggableManager(through=TaggedItem)
  759. objects = TreeManager()
  760. csv_headers = [
  761. 'device', 'name', 'label', 'manufacturer', 'part_id', 'serial', 'asset_tag', 'discovered', 'description',
  762. ]
  763. class Meta:
  764. ordering = ('device__id', 'parent__id', '_name')
  765. unique_together = ('device', 'parent', 'name')
  766. def get_absolute_url(self):
  767. return reverse('dcim:inventoryitem', kwargs={'pk': self.pk})
  768. def to_csv(self):
  769. return (
  770. self.device.name or '{{{}}}'.format(self.device.pk),
  771. self.name,
  772. self.label,
  773. self.manufacturer.name if self.manufacturer else None,
  774. self.part_id,
  775. self.serial,
  776. self.asset_tag,
  777. self.discovered,
  778. self.description,
  779. )