racks.py 18 KB

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