racks.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. from collections import OrderedDict
  2. from django.conf import settings
  3. from django.contrib.auth.models import User
  4. from django.contrib.contenttypes.fields import GenericRelation
  5. from django.contrib.contenttypes.models import ContentType
  6. from django.contrib.postgres.fields import ArrayField
  7. from django.core.exceptions import ValidationError
  8. from django.core.validators import MaxValueValidator, MinValueValidator
  9. from django.db import models
  10. from django.db.models import Count, Sum
  11. from django.urls import reverse
  12. from mptt.models import TreeForeignKey
  13. from dcim.choices import *
  14. from dcim.constants import *
  15. from dcim.elevations import RackElevationSVG
  16. from extras.utils import extras_features
  17. from netbox.models import NestedGroupModel, OrganizationalModel, PrimaryModel
  18. from utilities.choices import ColorChoices
  19. from utilities.fields import ColorField, NaturalOrderingField
  20. from utilities.querysets import RestrictedQuerySet
  21. from utilities.utils import array_to_string
  22. from .device_components import PowerOutlet, PowerPort
  23. from .devices import Device
  24. from .power import PowerFeed
  25. __all__ = (
  26. 'Rack',
  27. 'Location',
  28. 'RackReservation',
  29. 'RackRole',
  30. )
  31. #
  32. # Racks
  33. #
  34. @extras_features('custom_fields', 'export_templates', 'webhooks')
  35. class Location(NestedGroupModel):
  36. """
  37. A Location represents a subgroup of Racks and/or Devices within a Site. A Location may represent a building within a
  38. site, or a room within a building, for example.
  39. """
  40. name = models.CharField(
  41. max_length=100
  42. )
  43. slug = models.SlugField(
  44. max_length=100
  45. )
  46. site = models.ForeignKey(
  47. to='dcim.Site',
  48. on_delete=models.CASCADE,
  49. related_name='locations'
  50. )
  51. parent = TreeForeignKey(
  52. to='self',
  53. on_delete=models.CASCADE,
  54. related_name='children',
  55. blank=True,
  56. null=True,
  57. db_index=True
  58. )
  59. description = models.CharField(
  60. max_length=200,
  61. blank=True
  62. )
  63. csv_headers = ['site', 'parent', 'name', 'slug', 'description']
  64. class Meta:
  65. ordering = ['site', 'name']
  66. unique_together = [
  67. ['site', 'name'],
  68. ['site', 'slug'],
  69. ]
  70. def get_absolute_url(self):
  71. return "{}?location_id={}".format(reverse('dcim:rack_list'), self.pk)
  72. def to_csv(self):
  73. return (
  74. self.site,
  75. self.parent.name if self.parent else '',
  76. self.name,
  77. self.slug,
  78. self.description,
  79. )
  80. def clean(self):
  81. super().clean()
  82. # Parent Location (if any) must belong to the same Site
  83. if self.parent and self.parent.site != self.site:
  84. raise ValidationError(f"Parent location ({self.parent}) must belong to the same site ({self.site})")
  85. @extras_features('custom_fields', 'export_templates', 'webhooks')
  86. class RackRole(OrganizationalModel):
  87. """
  88. Racks can be organized by functional role, similar to Devices.
  89. """
  90. name = models.CharField(
  91. max_length=100,
  92. unique=True
  93. )
  94. slug = models.SlugField(
  95. max_length=100,
  96. unique=True
  97. )
  98. color = ColorField(
  99. default=ColorChoices.COLOR_GREY
  100. )
  101. description = models.CharField(
  102. max_length=200,
  103. blank=True,
  104. )
  105. objects = RestrictedQuerySet.as_manager()
  106. csv_headers = ['name', 'slug', 'color', 'description']
  107. class Meta:
  108. ordering = ['name']
  109. def __str__(self):
  110. return self.name
  111. def get_absolute_url(self):
  112. return "{}?role={}".format(reverse('dcim:rack_list'), self.slug)
  113. def to_csv(self):
  114. return (
  115. self.name,
  116. self.slug,
  117. self.color,
  118. self.description,
  119. )
  120. @extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
  121. class Rack(PrimaryModel):
  122. """
  123. Devices are housed within Racks. Each rack has a defined height measured in rack units, and a front and rear face.
  124. Each Rack is assigned to a Site and (optionally) a Location.
  125. """
  126. name = models.CharField(
  127. max_length=100
  128. )
  129. _name = NaturalOrderingField(
  130. target_field='name',
  131. max_length=100,
  132. blank=True
  133. )
  134. facility_id = models.CharField(
  135. max_length=50,
  136. blank=True,
  137. null=True,
  138. verbose_name='Facility ID',
  139. help_text='Locally-assigned identifier'
  140. )
  141. site = models.ForeignKey(
  142. to='dcim.Site',
  143. on_delete=models.PROTECT,
  144. related_name='racks'
  145. )
  146. location = models.ForeignKey(
  147. to='dcim.Location',
  148. on_delete=models.SET_NULL,
  149. related_name='racks',
  150. blank=True,
  151. null=True
  152. )
  153. tenant = models.ForeignKey(
  154. to='tenancy.Tenant',
  155. on_delete=models.PROTECT,
  156. related_name='racks',
  157. blank=True,
  158. null=True
  159. )
  160. status = models.CharField(
  161. max_length=50,
  162. choices=RackStatusChoices,
  163. default=RackStatusChoices.STATUS_ACTIVE
  164. )
  165. role = models.ForeignKey(
  166. to='dcim.RackRole',
  167. on_delete=models.PROTECT,
  168. related_name='racks',
  169. blank=True,
  170. null=True,
  171. help_text='Functional role'
  172. )
  173. serial = models.CharField(
  174. max_length=50,
  175. blank=True,
  176. verbose_name='Serial number'
  177. )
  178. asset_tag = models.CharField(
  179. max_length=50,
  180. blank=True,
  181. null=True,
  182. unique=True,
  183. verbose_name='Asset tag',
  184. help_text='A unique tag used to identify this rack'
  185. )
  186. type = models.CharField(
  187. choices=RackTypeChoices,
  188. max_length=50,
  189. blank=True,
  190. verbose_name='Type'
  191. )
  192. width = models.PositiveSmallIntegerField(
  193. choices=RackWidthChoices,
  194. default=RackWidthChoices.WIDTH_19IN,
  195. verbose_name='Width',
  196. help_text='Rail-to-rail width'
  197. )
  198. u_height = models.PositiveSmallIntegerField(
  199. default=RACK_U_HEIGHT_DEFAULT,
  200. verbose_name='Height (U)',
  201. validators=[MinValueValidator(1), MaxValueValidator(100)],
  202. help_text='Height in rack units'
  203. )
  204. desc_units = models.BooleanField(
  205. default=False,
  206. verbose_name='Descending units',
  207. help_text='Units are numbered top-to-bottom'
  208. )
  209. outer_width = models.PositiveSmallIntegerField(
  210. blank=True,
  211. null=True,
  212. help_text='Outer dimension of rack (width)'
  213. )
  214. outer_depth = models.PositiveSmallIntegerField(
  215. blank=True,
  216. null=True,
  217. help_text='Outer dimension of rack (depth)'
  218. )
  219. outer_unit = models.CharField(
  220. max_length=50,
  221. choices=RackDimensionUnitChoices,
  222. blank=True,
  223. )
  224. comments = models.TextField(
  225. blank=True
  226. )
  227. images = GenericRelation(
  228. to='extras.ImageAttachment'
  229. )
  230. objects = RestrictedQuerySet.as_manager()
  231. csv_headers = [
  232. 'site', 'location', 'name', 'facility_id', 'tenant', 'status', 'role', 'type', 'serial', 'asset_tag', 'width',
  233. 'u_height', 'desc_units', 'outer_width', 'outer_depth', 'outer_unit', 'comments',
  234. ]
  235. clone_fields = [
  236. 'site', 'location', 'tenant', 'status', 'role', 'type', 'width', 'u_height', 'desc_units', 'outer_width',
  237. 'outer_depth', 'outer_unit',
  238. ]
  239. class Meta:
  240. ordering = ('site', 'location', '_name', 'pk') # (site, location, name) may be non-unique
  241. unique_together = (
  242. # Name and facility_id must be unique *only* within a Location
  243. ('location', 'name'),
  244. ('location', 'facility_id'),
  245. )
  246. def __str__(self):
  247. return self.display_name or super().__str__()
  248. def get_absolute_url(self):
  249. return reverse('dcim:rack', args=[self.pk])
  250. def clean(self):
  251. super().clean()
  252. # Validate location/site assignment
  253. if self.site and self.location and self.location.site != self.site:
  254. raise ValidationError(f"Assigned location must belong to parent site ({self.site}).")
  255. # Validate outer dimensions and unit
  256. if (self.outer_width is not None or self.outer_depth is not None) and not self.outer_unit:
  257. raise ValidationError("Must specify a unit when setting an outer width/depth")
  258. elif self.outer_width is None and self.outer_depth is None:
  259. self.outer_unit = ''
  260. if self.pk:
  261. # Validate that Rack is tall enough to house the installed Devices
  262. top_device = Device.objects.filter(
  263. rack=self
  264. ).exclude(
  265. position__isnull=True
  266. ).order_by('-position').first()
  267. if top_device:
  268. min_height = top_device.position + top_device.device_type.u_height - 1
  269. if self.u_height < min_height:
  270. raise ValidationError({
  271. 'u_height': "Rack must be at least {}U tall to house currently installed devices.".format(
  272. min_height
  273. )
  274. })
  275. # Validate that Rack was assigned a Location of its same site, if applicable
  276. if self.location:
  277. if self.location.site != self.site:
  278. raise ValidationError({
  279. 'location': f"Location must be from the same site, {self.site}."
  280. })
  281. def to_csv(self):
  282. return (
  283. self.site.name,
  284. self.location.name if self.location else None,
  285. self.name,
  286. self.facility_id,
  287. self.tenant.name if self.tenant else None,
  288. self.get_status_display(),
  289. self.role.name if self.role else None,
  290. self.get_type_display() if self.type else None,
  291. self.serial,
  292. self.asset_tag,
  293. self.width,
  294. self.u_height,
  295. self.desc_units,
  296. self.outer_width,
  297. self.outer_depth,
  298. self.outer_unit,
  299. self.comments,
  300. )
  301. @property
  302. def units(self):
  303. if self.desc_units:
  304. return range(1, self.u_height + 1)
  305. else:
  306. return reversed(range(1, self.u_height + 1))
  307. @property
  308. def display_name(self):
  309. if self.facility_id:
  310. return f'{self.name} ({self.facility_id})'
  311. return self.name
  312. def get_status_class(self):
  313. return RackStatusChoices.CSS_CLASSES.get(self.status)
  314. def get_rack_units(self, user=None, face=DeviceFaceChoices.FACE_FRONT, exclude=None, expand_devices=True):
  315. """
  316. Return a list of rack units as dictionaries. Example: {'device': None, 'face': 0, 'id': 48, 'name': 'U48'}
  317. Each key 'device' is either a Device or None. By default, multi-U devices are repeated for each U they occupy.
  318. :param face: Rack face (front or rear)
  319. :param user: User instance to be used for evaluating device view permissions. If None, all devices
  320. will be included.
  321. :param exclude: PK of a Device to exclude (optional); helpful when relocating a Device within a Rack
  322. :param expand_devices: When True, all units that a device occupies will be listed with each containing a
  323. reference to the device. When False, only the bottom most unit for a device is included and that unit
  324. contains a height attribute for the device
  325. """
  326. elevation = OrderedDict()
  327. for u in self.units:
  328. elevation[u] = {
  329. 'id': u,
  330. 'name': f'U{u}',
  331. 'face': face,
  332. 'device': None,
  333. 'occupied': False
  334. }
  335. # Add devices to rack units list
  336. if self.pk:
  337. # Retrieve all devices installed within the rack
  338. queryset = Device.objects.prefetch_related(
  339. 'device_type',
  340. 'device_type__manufacturer',
  341. 'device_role'
  342. ).annotate(
  343. devicebay_count=Count('devicebays')
  344. ).exclude(
  345. pk=exclude
  346. ).filter(
  347. rack=self,
  348. position__gt=0,
  349. device_type__u_height__gt=0
  350. ).filter(
  351. Q(face=face) | Q(device_type__is_full_depth=True)
  352. )
  353. # Determine which devices the user has permission to view
  354. permitted_device_ids = []
  355. if user is not None:
  356. permitted_device_ids = self.devices.restrict(user, 'view').values_list('pk', flat=True)
  357. for device in queryset:
  358. if expand_devices:
  359. for u in range(device.position, device.position + device.device_type.u_height):
  360. if user is None or device.pk in permitted_device_ids:
  361. elevation[u]['device'] = device
  362. elevation[u]['occupied'] = True
  363. else:
  364. if user is None or device.pk in permitted_device_ids:
  365. elevation[device.position]['device'] = device
  366. elevation[device.position]['occupied'] = True
  367. elevation[device.position]['height'] = device.device_type.u_height
  368. for u in range(device.position + 1, device.position + device.device_type.u_height):
  369. elevation.pop(u, None)
  370. return [u for u in elevation.values()]
  371. def get_available_units(self, u_height=1, rack_face=None, exclude=None):
  372. """
  373. Return a list of units within the rack available to accommodate a device of a given U height (default 1).
  374. Optionally exclude one or more devices when calculating empty units (needed when moving a device from one
  375. position to another within a rack).
  376. :param u_height: Minimum number of contiguous free units required
  377. :param rack_face: The face of the rack (front or rear) required; 'None' if device is full depth
  378. :param exclude: List of devices IDs to exclude (useful when moving a device within a rack)
  379. """
  380. # Gather all devices which consume U space within the rack
  381. devices = self.devices.prefetch_related('device_type').filter(position__gte=1)
  382. if exclude is not None:
  383. devices = devices.exclude(pk__in=exclude)
  384. # Initialize the rack unit skeleton
  385. units = list(range(1, self.u_height + 1))
  386. # Remove units consumed by installed devices
  387. for d in devices:
  388. if rack_face is None or d.face == rack_face or d.device_type.is_full_depth:
  389. for u in range(d.position, d.position + d.device_type.u_height):
  390. try:
  391. units.remove(u)
  392. except ValueError:
  393. # Found overlapping devices in the rack!
  394. pass
  395. # Remove units without enough space above them to accommodate a device of the specified height
  396. available_units = []
  397. for u in units:
  398. if set(range(u, u + u_height)).issubset(units):
  399. available_units.append(u)
  400. return list(reversed(available_units))
  401. def get_reserved_units(self):
  402. """
  403. Return a dictionary mapping all reserved units within the rack to their reservation.
  404. """
  405. reserved_units = {}
  406. for r in self.reservations.all():
  407. for u in r.units:
  408. reserved_units[u] = r
  409. return reserved_units
  410. def get_elevation_svg(
  411. self,
  412. face=DeviceFaceChoices.FACE_FRONT,
  413. user=None,
  414. unit_width=settings.RACK_ELEVATION_DEFAULT_UNIT_WIDTH,
  415. unit_height=settings.RACK_ELEVATION_DEFAULT_UNIT_HEIGHT,
  416. legend_width=RACK_ELEVATION_LEGEND_WIDTH_DEFAULT,
  417. include_images=True,
  418. base_url=None
  419. ):
  420. """
  421. Return an SVG of the rack elevation
  422. :param face: Enum of [front, rear] representing the desired side of the rack elevation to render
  423. :param user: User instance to be used for evaluating device view permissions. If None, all devices
  424. will be included.
  425. :param unit_width: Width in pixels for the rendered drawing
  426. :param unit_height: Height of each rack unit for the rendered drawing. Note this is not the total
  427. height of the elevation
  428. :param legend_width: Width of the unit legend, in pixels
  429. :param include_images: Embed front/rear device images where available
  430. :param base_url: Base URL for links and images. If none, URLs will be relative.
  431. """
  432. elevation = RackElevationSVG(self, user=user, include_images=include_images, base_url=base_url)
  433. return elevation.render(face, unit_width, unit_height, legend_width)
  434. def get_0u_devices(self):
  435. return self.devices.filter(position=0)
  436. def get_utilization(self):
  437. """
  438. Determine the utilization rate of the rack and return it as a percentage. Occupied and reserved units both count
  439. as utilized.
  440. """
  441. # Determine unoccupied units
  442. available_units = self.get_available_units()
  443. # Remove reserved units
  444. for u in self.get_reserved_units():
  445. if u in available_units:
  446. available_units.remove(u)
  447. occupied_unit_count = self.u_height - len(available_units)
  448. percentage = int(float(occupied_unit_count) / self.u_height * 100)
  449. return percentage
  450. def get_power_utilization(self):
  451. """
  452. Determine the utilization rate of power in the rack and return it as a percentage.
  453. """
  454. powerfeeds = PowerFeed.objects.filter(rack=self)
  455. available_power_total = sum(pf.available_power for pf in powerfeeds)
  456. if not available_power_total:
  457. return 0
  458. pf_powerports = PowerPort.objects.filter(
  459. _cable_peer_type=ContentType.objects.get_for_model(PowerFeed),
  460. _cable_peer_id__in=powerfeeds.values_list('id', flat=True)
  461. )
  462. poweroutlets = PowerOutlet.objects.filter(power_port_id__in=pf_powerports)
  463. allocated_draw_total = PowerPort.objects.filter(
  464. _cable_peer_type=ContentType.objects.get_for_model(PowerOutlet),
  465. _cable_peer_id__in=poweroutlets.values_list('id', flat=True)
  466. ).aggregate(Sum('allocated_draw'))['allocated_draw__sum'] or 0
  467. return int(allocated_draw_total / available_power_total * 100)
  468. @extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
  469. class RackReservation(PrimaryModel):
  470. """
  471. One or more reserved units within a Rack.
  472. """
  473. rack = models.ForeignKey(
  474. to='dcim.Rack',
  475. on_delete=models.CASCADE,
  476. related_name='reservations'
  477. )
  478. units = ArrayField(
  479. base_field=models.PositiveSmallIntegerField()
  480. )
  481. tenant = models.ForeignKey(
  482. to='tenancy.Tenant',
  483. on_delete=models.PROTECT,
  484. related_name='rackreservations',
  485. blank=True,
  486. null=True
  487. )
  488. user = models.ForeignKey(
  489. to=User,
  490. on_delete=models.PROTECT
  491. )
  492. description = models.CharField(
  493. max_length=200
  494. )
  495. objects = RestrictedQuerySet.as_manager()
  496. csv_headers = ['site', 'location', 'rack', 'units', 'tenant', 'user', 'description']
  497. class Meta:
  498. ordering = ['created', 'pk']
  499. def __str__(self):
  500. return "Reservation for rack {}".format(self.rack)
  501. def get_absolute_url(self):
  502. return reverse('dcim:rackreservation', args=[self.pk])
  503. def clean(self):
  504. super().clean()
  505. if hasattr(self, 'rack') and self.units:
  506. # Validate that all specified units exist in the Rack.
  507. invalid_units = [u for u in self.units if u not in self.rack.units]
  508. if invalid_units:
  509. raise ValidationError({
  510. 'units': "Invalid unit(s) for {}U rack: {}".format(
  511. self.rack.u_height,
  512. ', '.join([str(u) for u in invalid_units]),
  513. ),
  514. })
  515. # Check that none of the units has already been reserved for this Rack.
  516. reserved_units = []
  517. for resv in self.rack.reservations.exclude(pk=self.pk):
  518. reserved_units += resv.units
  519. conflicting_units = [u for u in self.units if u in reserved_units]
  520. if conflicting_units:
  521. raise ValidationError({
  522. 'units': 'The following units have already been reserved: {}'.format(
  523. ', '.join([str(u) for u in conflicting_units]),
  524. )
  525. })
  526. def to_csv(self):
  527. return (
  528. self.rack.site.name,
  529. self.rack.location if self.rack.location else None,
  530. self.rack.name,
  531. ','.join([str(u) for u in self.units]),
  532. self.tenant.name if self.tenant else None,
  533. self.user.username,
  534. self.description
  535. )
  536. @property
  537. def unit_list(self):
  538. return array_to_string(self.units)