-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetbox_optical_setup.py
440 lines (388 loc) · 18.7 KB
/
netbox_optical_setup.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
from django.contrib.contenttypes.models import ContentType
from extras.choices import CustomFieldTypeChoices
from extras.models.customfields import CustomField
from extras.scripts import Script
name = 'Netbox Optical Setup (0.8)'
# We'll need the IDs of the content types we will attach the custom
# fields to.
cable_id = ContentType.objects.get(model='cable').id
interface_id = ContentType.objects.get(model='interface').id
frontport_id = ContentType.objects.get(model='frontport').id
rearport_id = ContentType.objects.get(model='rearport').id
circuit_id = ContentType.objects.get(model='circuit').id
# Based on FOA link below and OTT CONA, CONE manuals
# https://www.thefoa.org/tech/smf.htm
# TODO: Add specialty fibers
FIBER_TYPES = (
'G.651.1',
'G.652',
'G.652.B',
'G.652.D',
'G.653',
'G.653.A',
'G.653.B',
'G.654',
'G.654.A',
'G.654.B',
'G.654.C',
'G.654.D',
'G.654.E',
'G.655',
'G.655.C',
'G.655.D',
'G.655.E',
'G.656',
'G.657',
'G.657.A1',
'G.657.A2',
'G.657.B2',
'G.657.B3'
)
def get_optical_fields():
fields = CustomField.objects.filter(group_name='Optical Settings')
return fields
class CreateCustomFieldsScript(Script):
class Meta:
name = "Create Optical Networking Custom Fields"
description = "Creates the optical networking custom fields"
commit_default = True
def run(self, data, commit):
# TODO: Add exception check to ensure these fields don't already exist
if CustomField.objects.filter(name='fiber_type').exists():
self.log_failure('fiber_type custom field already exists')
else:
fiber_type = CustomField.objects.create(
name='fiber_type',
label='Fiber Type',
group_name='Optical Settings',
type=CustomFieldTypeChoices.TYPE_SELECT,
# TODO: Add choices, defaults
description="The ITU fiber type of the cable.",
choices=FIBER_TYPES,
# TODO: Is there a better way to reference this?
# TODO: Any other MM?
default="G.652"
)
fiber_type.content_types.set([cable_id])
fiber_type.save()
self.log_info("fiber_type custom field created")
if CustomField.objects.filter(name='attenuation_coeff').exists():
self.log_failure('attenuation_coeff custom field already exists')
else:
attenuation_coeff = CustomField.objects.create(
name='attenuation_coeff',
label='Attn Coeff',
group_name='Optical Settings',
type=CustomFieldTypeChoices.TYPE_TEXT,
description="The attenuation coefficient of the cable per km",
choices=FIBER_TYPES,
# TODO: Convert unit as needed, but always based on km
default="0.4", # TODO: Make configurable in config
)
attenuation_coeff.content_types.set([cable_id])
attenuation_coeff.save()
self.log_info("attenuation_coeff custom field created")
if CustomField.objects.filter(name='power_loss').exists():
self.log_failure('power_loss custom field already exists')
else:
power_loss = CustomField.objects.create(
name='power_loss',
label='Pwr Loss',
group_name='Optical Settings',
# FIXME: Update to Decimal for Netbox 3.4
type=CustomFieldTypeChoices.TYPE_TEXT,
description="The overall power loss on the fiber across the cable's length.",
validation_minimum=-60, # Loss stops at -60 dB (0.000001)
validation_maximum=0 # Loss starts at 0 dB (anything higher
# isn't loss!
)
power_loss.content_types.set([cable_id])
power_loss.save()
self.log_info("power_loss custom field created")
if CustomField.objects.filter(name='attenuator_loss').exists():
self.log_failure('attenuator_loss custom field already exists')
else:
attenuator_loss = CustomField.objects.create(
name='attenuator_loss',
label='Attn Loss',
group_name='Optical Settings',
# FIXME: Update to Decimal for Netbox 3.4
type=CustomFieldTypeChoices.TYPE_INTEGER,
description="The intentionally-added optical loss on this port from a fixed or variable attenuator.",
validation_minimum=-60, # Loss stops at -60 dB (0.000001)
validation_maximum=0 # Loss starts at 0 dB (anything higher
# isn't loss!
)
attenuator_loss.content_types.set([frontport_id, rearport_id])
attenuator_loss.save()
self.log_info("attenuator_loss custom field created")
if CustomField.objects.filter(name='insertion_loss').exists():
self.log_failure('insertion_loss custom field already exists')
else:
insertion_loss = CustomField.objects.create(
name='insertion_loss',
label='Ins Loss',
group_name='Optical Settings',
# FIXME: Update to Decimal for Netbox 3.4
type=CustomFieldTypeChoices.TYPE_TEXT,
description="The optical loss caused by passing through the port",
validation_minimum=-60, # Loss stops at -60 dB (0.000001)
validation_maximum=0 # Loss starts at 0 dB (anything higher
# isn't loss!
)
insertion_loss.content_types.set([frontport_id, rearport_id])
insertion_loss.save()
self.log_info("insertion_loss custom field created")
if CustomField.objects.filter(name='return_loss').exists():
self.log_failure('return_loss custom field already exists')
else:
return_loss = CustomField.objects.create(
name='return_loss',
label='Rtn Loss',
group_name='Optical Settings',
# FIXME: Update to Decimal for Netbox 3.4
type=CustomFieldTypeChoices.TYPE_TEXT,
description="The optical loss from reflection at the port",
validation_minimum=-60, # Loss stops at -60 dB (0.000001)
validation_maximum=0 # Loss starts at 0 dB (anything higher
# isn't loss!
)
return_loss.content_types.set([frontport_id, rearport_id])
return_loss.save()
self.log_info("return_loss custom field created")
if CustomField.objects.filter(name='max_tx_power').exists():
self.log_failure('max_tx_power custom field already exists')
else:
max_tx_power = CustomField.objects.create(
name='max_tx_power',
label='Max TX Pwr',
group_name='Optical Settings',
# FIXME: Update to Decimal for Netbox 3.4
type=CustomFieldTypeChoices.TYPE_TEXT,
description="The maximum transmission power of the transceiver"
# TODO: Add validation
)
max_tx_power.content_types.set([interface_id])
max_tx_power.save()
self.log_info("max_tx_power custom field created")
if CustomField.objects.filter(name='min_tx_power').exists():
self.log_failure('min_tx_power custom field already exists')
else:
min_tx_power = CustomField.objects.create(
name='min_tx_power',
label='Min TX Pwr',
group_name='Optical Settings',
# FIXME: Update to Decimal for Netbox 3.4
type=CustomFieldTypeChoices.TYPE_TEXT,
description="The minimum transmission power of the transceiver"
# TODO: Add validation
)
min_tx_power.content_types.set([interface_id])
min_tx_power.save()
self.log_info("min_tx_power custom field created")
if CustomField.objects.filter(name='rx_overload').exists():
self.log_failure('rx_overload custom field already exists')
else:
rx_overload = CustomField.objects.create(
name='rx_overload',
label='RX Ovld',
group_name='Optical Settings',
# FIXME: Update to Decimal for Netbox 3.4
type=CustomFieldTypeChoices.TYPE_TEXT,
description="The overload power threshold of the transceiver"
# TODO: Add validation
)
rx_overload.content_types.set([interface_id])
rx_overload.save()
self.log_info("rx_overload custom field created")
if CustomField.objects.filter(name='rx_sensitivity').exists():
self.log_failure('rx_sensitivity custom field already exists')
else:
rx_sensitivity = CustomField.objects.create(
name='rx_sensitivity',
label='RX Sen',
group_name='Optical Settings',
# FIXME: Update to Decimal for Netbox 3.4
type=CustomFieldTypeChoices.TYPE_TEXT,
description="The sensitivity power threshold of the transceiver"
# TODO: Add validation
)
rx_sensitivity.content_types.set([interface_id])
rx_sensitivity.save()
self.log_info("rx_sensitivity custom field created")
if CustomField.objects.filter(name='tx_wavelength').exists():
self.log_failure('tx_wavelength custom field already exists')
else:
tx_wavelength = CustomField.objects.create(
name='tx_wavelength',
label='TX Wave',
group_name='Optical Settings',
# FIXME: Update to Decimal for Netbox 3.4
type=CustomFieldTypeChoices.TYPE_TEXT,
description="The specific TX wavelength of the transceiver",
# FIXME: Integer fields are a limitation here; need <= option
# and/or Decimal support
validation_minimum=1260, # Use start of the O-band
# End right after the U-band (maybe we can prevent 1676 somehow?)
validation_maximum=1676
)
tx_wavelength.content_types.set([interface_id])
tx_wavelength.save()
self.log_info("tx_wavelength custom field created")
if CustomField.objects.filter(name='rx_wavelength').exists():
self.log_failure('rx_wavelength custom field already exists')
else:
rx_wavelength = CustomField.objects.create(
name='rx_wavelength',
label='RX Wave',
group_name='Optical Settings',
# FIXME: Update to Decimal for Netbox 3.4
type=CustomFieldTypeChoices.TYPE_TEXT,
description="The specific RX wavelength of the transceiver",
# FIXME: Integer fields are a limitation here; need <= option
# and/or Decimal support
# TODO: Add MM (850)
validation_minimum=1260, # Use start of the O-band
# End right after the U-band (maybe we can prevent 1676 somehow?)
validation_maximum=1676
)
rx_wavelength.content_types.set([interface_id])
rx_wavelength.save()
self.log_info("rx_wavelength custom field created")
# Return the output to the script page
self.log_success('Generation of custom fields complete')
class UpdateCustomFieldsScript(Script):
class Meta:
name = "Update Optical Networking Custom Fields"
description = "Updates the optical networking custom fields"
commit_default = False
def run(self, data, commit):
fiber_type = CustomField.objects.get(name='fiber_type')
fiber_type.label = 'Fiber Type'
fiber_type.group_name = 'Optical Settings'
fiber_type.type = CustomFieldTypeChoices.TYPE_SELECT
fiber_type.description = "The ITU fiber type of the cable."
fiber_type.choices = FIBER_TYPES
fiber_type.default = "G.652"
fiber_type.content_types.set([cable_id])
fiber_type.save()
self.log_info("fiber_type custom field updated")
attenuation_coeff = CustomField.objects.get(name='attenuation_coeff')
attenuation_coeff.label = 'Attn Coeff'
attenuation_coeff.group_name = 'Optical Settings'
attenuation_coeff.type = CustomFieldTypeChoices.TYPE_TEXT
attenuation_coeff.description = "The attenuation coefficient of the cable per km"
attenuation_coeff.choices = FIBER_TYPES
attenuation_coeff.default = "0.4"
attenuation_coeff.content_types.set([cable_id])
attenuation_coeff.save()
self.log_info("attenuation_coeff custom field updated")
power_loss = CustomField.objects.get(name='power_loss')
power_loss.label = 'Pwr Loss'
power_loss.group_name = 'Optical Settings'
power_loss.type = CustomFieldTypeChoices.TYPE_TEXT
power_loss.description = "The overall power loss on the fiber across the cable's length."
power_loss.validation_minimum = -60
power_loss.validation_maximum = 0
power_loss.content_types.set([cable_id])
power_loss.save()
self.log_info("power_loss custom field updated")
attenuator_loss = CustomField.objects.get(name='attenuator_loss')
attenuator_loss.label = 'Attn Loss'
attenuator_loss.group_name = 'Optical Settings'
attenuator_loss.type = CustomFieldTypeChoices.TYPE_INTEGER
attenuator_loss.description = "The intentionally-added optical loss on this port from a fixed or variable attenuator."
attenuator_loss.validation_minimum = -60
attenuator_loss.validation_maximum = 0
attenuator_loss.content_types.set([frontport_id, rearport_id])
attenuator_loss.save()
self.log_info("attenuator_loss custom field updated")
insertion_loss = CustomField.objects.get(name='insertion_loss')
insertion_loss.label = 'Ins Loss'
insertion_loss.group_name = 'Optical Settings'
insertion_loss.type = CustomFieldTypeChoices.TYPE_TEXT
insertion_loss.description = "The optical loss caused by passing through the port"
insertion_loss.validation_minimum = -60
insertion_loss.validation_maximum = 0
insertion_loss.content_types.set([frontport_id, rearport_id])
insertion_loss.save()
self.log_info("insertion_loss custom field updated")
return_loss = CustomField.objects.get(name='return_loss')
return_loss.label = 'Rtn Loss'
return_loss.group_name = 'Optical Settings'
return_loss.type = CustomFieldTypeChoices.TYPE_TEXT
return_loss.description = "The optical loss from reflection at the port"
return_loss.validation_minimum = -60
return_loss.validation_maximum = 0
return_loss.content_types.set([frontport_id, rearport_id])
return_loss.save()
self.log_info("return_loss custom field updated")
max_tx_power = CustomField.objects.get(name='max_tx_power')
max_tx_power.label = 'Max TX Pwr'
max_tx_power.group_name = 'Optical Settings'
max_tx_power.type = CustomFieldTypeChoices.TYPE_TEXT
max_tx_power.description = "The maximum transmission power of the transceiver"
max_tx_power.content_types.set([interface_id])
max_tx_power.save()
self.log_info("max_tx_power custom field updated")
min_tx_power = CustomField.objects.get(name='min_tx_power')
min_tx_power.label = 'Min TX Pwr'
min_tx_power.group_name = 'Optical Settings'
min_tx_power.type = CustomFieldTypeChoices.TYPE_TEXT
min_tx_power.description = "The minimum transmission power of the transceiver"
min_tx_power.content_types.set([interface_id])
min_tx_power.save()
self.log_info("min_tx_power custom field updated")
rx_overload = CustomField.objects.get(name='rx_overload')
rx_overload.label = 'RX Ovld'
rx_overload.group_name = 'Optical Settings'
rx_overload.type = CustomFieldTypeChoices.TYPE_TEXT
rx_overload.description = "The overload power threshold of the transceiver"
rx_overload.content_types.set([interface_id])
rx_overload.save()
self.log_info("rx_overload custom field updated")
rx_sensitivity = CustomField.objects.get(name='rx_sensitivity')
rx_sensitivity.label = 'RX Sen'
rx_sensitivity.group_name = 'Optical Settings'
rx_sensitivity.type = CustomFieldTypeChoices.TYPE_TEXT
rx_sensitivity.description = "The sensitivity power threshold of the transceiver"
rx_sensitivity.content_types.set([interface_id])
rx_sensitivity.save()
self.log_info("rx_sensitivity custom field updated")
tx_wavelength = CustomField.objects.get(name='tx_wavelength')
tx_wavelength.label = 'TX Wave'
tx_wavelength.group_name = 'Optical Settings'
tx_wavelength.type = CustomFieldTypeChoices.TYPE_TEXT
tx_wavelength.description = "The specific TX wavelength of the transceiver"
tx_wavelength.validation_minimum = 850
tx_wavelength.validation_maximum = 1676
tx_wavelength.content_types.set([interface_id])
tx_wavelength.save()
self.log_info("tx_wavelength custom field updated")
rx_wavelength = CustomField.objects.get(name='rx_wavelength')
rx_wavelength.label = 'RX Wave'
rx_wavelength.group_name = 'Optical Settings'
rx_wavelength.type = CustomFieldTypeChoices.TYPE_TEXT
rx_wavelength.description = "The specific RX wavelength of the transceiver"
rx_wavelength.validation_minimum = 850
rx_wavelength.validation_maximum = 1676
rx_wavelength.content_types.set([interface_id])
rx_wavelength.save()
self.log_info("rx_wavelength custom field updated")
self.log_success('All optical fields updated')
class RemoveCustomFieldsScript(Script):
class Meta:
name = "Remove Optical Networking Custom Fields"
description = "Removes the optical networking custom fields"
commit_default = False
def run(self, data, commit):
fields = get_optical_fields()
for field in fields:
CustomField.objects.filter(id=field.id).delete()
self.log_info(f"{field.name} removed")
self.log_success('All optical fields removed')
script_order = (
CreateCustomFieldsScript,
UpdateCustomFieldsScript,
RemoveCustomFieldsScript
)