/
physical.py
553 lines (484 loc) · 17.7 KB
/
physical.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# -*- coding: utf-8 -*-
import re
from collections import namedtuple
from itertools import chain
from django import forms
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.db import models, transaction
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _
from ralph.accounts.models import Region
from ralph.admin.autocomplete import AutocompleteTooltipMixin
from ralph.admin.sites import ralph_site
from ralph.admin.widgets import AutocompleteWidget
from ralph.assets.models.assets import Asset, NamedMixin
from ralph.assets.models.choices import AssetSource
from ralph.assets.utils import move_parents_models
from ralph.back_office.models import BackOfficeAsset, Warehouse
from ralph.data_center.models.choices import (
ConnectionType,
DataCenterAssetStatus,
Orientation,
RackOrientation
)
from ralph.lib.mixins.fields import (
NullableCharField,
NullableGenericIPAddressField
)
from ralph.lib.mixins.models import AdminAbsoluteUrlMixin
from ralph.lib.transitions.decorators import transition_action
from ralph.lib.transitions.fields import TransitionField
# i.e. number in range 1-16 and optional postfix 'A' or 'B'
VALID_SLOT_NUMBER_FORMAT = re.compile('^([1-9][A,B]?|1[0-6][A,B]?)$')
ACCESSORY_DATA = [
'brush', 'patch_panel_fc', 'patch_panel_utp', 'organizer', 'power_socket'
]
class Gap(object):
"""A placeholder that represents a gap in a blade chassis"""
id = 0
barcode = '-'
sn = '-'
service = namedtuple('Service', ['name'])('-')
model = namedtuple('Model', ['name'])('-')
linked_device = None
def __init__(self, slot_no, orientation):
self.slot_no = slot_no
self.orientation = orientation
def get_orientation_desc(self):
return self.orientation
def get_absolute_url(self):
return ''
@classmethod
def generate_gaps(cls, items):
def get_number(slot_no):
"""Returns the integer part of slot number"""
m = re.match(r'(\d+)', slot_no)
return (m and int(m.group(0))) or 0
if not items:
return []
max_slot_no = max([
get_number(asset.slot_no)
for asset in items
])
first_asset_slot_no = items[0].slot_no
ab = first_asset_slot_no and first_asset_slot_no[-1] in {'A', 'B'}
slot_nos = {asset.slot_no for asset in items}
def handle_missing(slot_no):
if slot_no not in slot_nos:
items.append(Gap(slot_no, items[0].get_orientation_desc()))
for slot_no in range(1, max_slot_no + 1):
if ab:
for letter in ['A', 'B']:
handle_missing(str(slot_no) + letter)
else:
handle_missing(str(slot_no))
return items
class DataCenter(AdminAbsoluteUrlMixin, NamedMixin, models.Model):
_allow_in_dashboard = True
visualization_cols_num = models.PositiveIntegerField(
verbose_name=_('visualization grid columns number'),
default=20,
)
visualization_rows_num = models.PositiveIntegerField(
verbose_name=_('visualization grid rows number'),
default=20,
)
@property
def rack_set(self):
return Rack.objects.select_related(
'server_room'
).filter(server_room__data_center=self)
@property
def server_rooms(self):
return ServerRoom.objects.filter(data_center=self)
def __str__(self):
return self.name
class ServerRoom(NamedMixin.NonUnique, models.Model):
_allow_in_dashboard = True
data_center = models.ForeignKey(DataCenter, verbose_name=_("data center"))
data_center._autocomplete = False
data_center._filter_title = _('data center')
def __str__(self):
return '{} ({})'.format(self.name, self.data_center.name)
class Accessory(NamedMixin):
class Meta:
verbose_name = _('accessory')
verbose_name_plural = _('accessories')
class RackAccessory(AdminAbsoluteUrlMixin, models.Model):
accessory = models.ForeignKey(Accessory)
rack = models.ForeignKey('Rack')
orientation = models.PositiveIntegerField(
choices=Orientation(),
default=Orientation.front.id,
)
position = models.IntegerField(null=True, blank=False)
remarks = models.CharField(
verbose_name='Additional remarks',
max_length=1024,
blank=True,
)
class Meta:
verbose_name_plural = _('rack accessories')
def get_orientation_desc(self):
return Orientation.name_from_id(self.orientation)
def __str__(self):
rack_name = self.rack.name if self.rack else ''
accessory_name = self.accessory.name if self.accessory else ''
return '{rack_name} - {accessory_name}'.format(
rack_name=rack_name, accessory_name=accessory_name,
)
class Rack(AdminAbsoluteUrlMixin, NamedMixin.NonUnique, models.Model):
_allow_in_dashboard = True
server_room = models.ForeignKey(
ServerRoom, verbose_name=_('server room'),
null=True,
blank=True,
)
server_room._autocomplete = False
server_room._filter_title = _('server room')
description = models.CharField(
_('description'), max_length=250, blank=True
)
orientation = models.PositiveIntegerField(
choices=RackOrientation(),
default=RackOrientation.top.id,
)
max_u_height = models.IntegerField(default=48)
visualization_col = models.PositiveIntegerField(
verbose_name=_('column number on visualization grid'),
default=0,
)
visualization_row = models.PositiveIntegerField(
verbose_name=_('row number on visualization grid'),
default=0,
)
accessories = models.ManyToManyField(Accessory, through='RackAccessory')
require_position = models.BooleanField(
default=True,
help_text=_(
'Uncheck if position is optional for this rack (ex. when rack '
'has warehouse-kind role'
)
)
class Meta:
unique_together = ('name', 'server_room')
def __str__(self):
if self.server_room:
return "{} ({}/{})".format(
self.name,
self.server_room.data_center,
self.server_room.name,
)
return self.name
def get_orientation_desc(self):
return RackOrientation.name_from_id(self.orientation)
def get_root_assets(self, side=None):
filter_kwargs = {
'rack': self,
}
if side:
filter_kwargs['orientation'] = side
else:
filter_kwargs['orientation__in'] = [
Orientation.front, Orientation.back
]
return DataCenterAsset.objects.select_related(
'model', 'model__category'
).filter(
Q(slot_no='') | Q(slot_no=None), **filter_kwargs
).exclude(model__has_parent=True)
def get_free_u(self):
u_list = [True] * self.max_u_height
accessories = RackAccessory.objects.values_list(
'position').filter(rack=self)
dc_assets = self.get_root_assets().values_list(
'position', 'model__height_of_device'
)
def fill_u_list(objects, height_of_device=lambda obj: 1):
for obj in objects:
# if position is None when objects simply does not have
# (assigned) position and position 0 is for some
# accessories (pdu) with left-right orientation and
# should not be included in free/filled space.
if obj[0] == 0 or obj[0] is None:
continue
start = obj[0] - 1
end = min(
self.max_u_height, obj[0] + int(height_of_device(obj)) - 1
)
height = end - start
if height:
u_list[start:end] = [False] * height
fill_u_list(accessories)
fill_u_list(dc_assets, lambda obj: obj[1])
return sum(u_list)
def get_pdus(self):
return DataCenterAsset.objects.select_related('model').filter(
rack=self,
orientation__in=(Orientation.left, Orientation.right),
position=0,
)
class DataCenterAsset(AutocompleteTooltipMixin, Asset):
_allow_in_dashboard = True
rack = models.ForeignKey(Rack, null=True, blank=True)
status = TransitionField(
default=DataCenterAssetStatus.new.id,
choices=DataCenterAssetStatus(),
)
position = models.IntegerField(null=True, blank=True)
orientation = models.PositiveIntegerField(
choices=Orientation(),
default=Orientation.front.id,
)
slot_no = models.CharField(
blank=True,
help_text=_('Fill it if asset is blade server'),
max_length=3,
null=True,
validators=[
RegexValidator(
regex=VALID_SLOT_NUMBER_FORMAT,
message=_(
"Slot number should be a number from range 1-16 with "
"an optional postfix 'A' or 'B' (e.g. '16A')"
),
code='invalid_slot_no'
)
],
verbose_name=_('slot number'),
)
connections = models.ManyToManyField(
'self',
through='Connection',
symmetrical=False,
)
source = models.PositiveIntegerField(
blank=True,
choices=AssetSource(),
db_index=True,
null=True,
verbose_name=_("source"),
)
delivery_date = models.DateField(null=True, blank=True)
production_year = models.PositiveSmallIntegerField(null=True, blank=True)
production_use_date = models.DateField(null=True, blank=True)
# Temporary solution until core functionality will not be fully migrated to
# NG
management_ip = NullableGenericIPAddressField(
verbose_name=_('Management IP address'),
help_text=_('Presented as string.'),
unique=True,
blank=True,
null=True,
default=None,
)
management_hostname = NullableCharField(
max_length=100, unique=True, null=True, blank=True
)
# @property
# def management_ip(self):
# """A property that gets management IP of a asset."""
# management_ip = self.ipaddress_set.filter(
# is_management=True
# ).order_by('-address').first()
# return management_ip.address if management_ip else ''
autocomplete_tooltip_fields = [
'rack',
'barcode',
'sn',
]
class Meta:
verbose_name = _('data center asset')
verbose_name_plural = _('data center assets')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Saved current rack value to check if changed.
self._rack_id = self.rack_id
def __str__(self):
return '{} (BC: {} / SN: {})'.format(
self.hostname or '-', self.barcode or '-', self.sn or '-'
)
def __repr__(self):
return '<DataCenterAsset: {}>'.format(self.id)
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
# When changing rack we search and save all descendants
if self.pk and self._rack_id != self.rack_id:
DataCenterAsset.objects.filter(parent=self).update(rack=self.rack)
def get_orientation_desc(self):
return Orientation.name_from_id(self.orientation)
@property
def cores_count(self):
"""Returns cores count assigned to device in Ralph"""
asset_cores_count = self.model.cores_count if self.model else 0
return asset_cores_count
def _validate_orientation(self):
"""
Validate if orientation is valid for given position.
"""
if self.position is None:
return
if self.position == 0 and not Orientation.is_width(self.orientation):
msg = 'Valid orientations for picked position are: {}'.format(
', '.join(
choice.desc for choice in Orientation.WIDTH.choices
)
)
raise ValidationError({'orientation': [msg]})
if self.position > 0 and not Orientation.is_depth(self.orientation):
msg = 'Valid orientations for picked position are: {}'.format(
', '.join(
choice.desc for choice in Orientation.DEPTH.choices
)
)
raise ValidationError({'orientation': [msg]})
def _validate_position(self):
"""
Validate if position not empty when rack requires it.
"""
if (
self.rack and
self.position is None and
self.rack.require_position
):
msg = 'Position is required for this rack'
raise ValidationError({'position': [msg]})
def _validate_position_in_rack(self):
"""
Validate if position is in rack height range.
"""
if (
self.rack and
self.position is not None and
self.position > self.rack.max_u_height
):
msg = 'Position is higher than "max u height" = {}'.format(
self.rack.max_u_height,
)
raise ValidationError({'position': [msg]})
if self.position is not None and self.position < 0:
msg = 'Position should be 0 or greater'
raise ValidationError({'position': msg})
def _validate_slot_no(self):
if self.model_id:
if self.model.has_parent and not self.slot_no:
raise ValidationError({
'slot_no': 'Slot number is required when asset is blade'
})
if not self.model.has_parent and self.slot_no:
raise ValidationError({
'slot_no': (
'Slot number cannot be filled when asset is not blade'
)
})
def clean(self):
# TODO: this should be default logic of clean method;
# we could register somehow validators (or take each func with
# _validate prefix) and call it here
errors = {}
for validator in [
super().clean,
self._validate_orientation,
self._validate_position,
self._validate_position_in_rack,
self._validate_slot_no
]:
try:
validator()
except ValidationError as e:
e.update_error_dict(errors)
if errors:
raise ValidationError(errors)
def get_related_assets(self):
"""Returns the children of a blade chassis"""
orientations = [Orientation.front, Orientation.back]
assets_by_orientation = []
for orientation in orientations:
assets_by_orientation.append(list(
DataCenterAsset.objects.select_related('model').filter(
parent=self,
orientation=orientation,
model__has_parent=True,
).exclude(id=self.id)
))
assets = [
Gap.generate_gaps(assets) for assets in assets_by_orientation
]
return chain(*assets)
@classmethod
def get_autocomplete_queryset(cls):
return cls._default_manager.exclude(
status=DataCenterAssetStatus.liquidated.id
)
@classmethod
@transition_action(
verbose_name=_('Change rack'),
form_fields={
'rack': {
'field': forms.CharField(widget=AutocompleteWidget(
field=rack, admin_site=ralph_site
)),
}
}
)
def change_rack(cls, instances, request, **kwargs):
rack = Rack.objects.get(pk=kwargs['rack'])
for instance in instances:
instance.rack = rack
@classmethod
@transition_action(
verbose_name=_('Convert to BackOffice Asset'),
disable_save_object=True,
only_one_action=True,
form_fields={
'warehouse': {
'field': forms.CharField(label=_('Warehouse')),
'autocomplete_field': 'warehouse',
'autocomplete_model': 'back_office.BackOfficeAsset'
},
'region': {
'field': forms.CharField(label=_('Region')),
'autocomplete_field': 'region',
'autocomplete_model': 'back_office.BackOfficeAsset'
}
}
)
def convert_to_backoffice_asset(cls, instances, request, **kwargs):
with transaction.atomic():
for i, instance in enumerate(instances):
back_office_asset = BackOfficeAsset()
back_office_asset.region = Region.objects.get(
pk=kwargs['region']
)
back_office_asset.warehouse = Warehouse.objects.get(
pk=kwargs['warehouse']
)
move_parents_models(instance, back_office_asset)
# Save new asset to list, required to redirect url.
# RunTransitionView.get_success_url()
instances[i] = back_office_asset
class Connection(models.Model):
outbound = models.ForeignKey(
'DataCenterAsset',
verbose_name=_('connected to device'),
on_delete=models.PROTECT,
related_name='outbound_connections',
)
inbound = models.ForeignKey(
'DataCenterAsset',
verbose_name=_('connected device'),
on_delete=models.PROTECT,
related_name='inbound_connections',
)
# TODO: discuss
connection_type = models.PositiveIntegerField(
verbose_name=_('connection type'),
choices=ConnectionType()
)
def __str__(self):
return '%s -> %s (%s)' % (
self.outbound,
self.inbound,
self.connection_type
)