-
Notifications
You must be signed in to change notification settings - Fork 0
/
port_count.py
651 lines (558 loc) · 29.5 KB
/
port_count.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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Copyright 2023, 2024 Consoli Solutions, LLC. All rights reserved.
**License**
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may also obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
The license is free for single customer use (internal applications). Use of this module in the production,
redistribution, or service delivery for commerce requires an additional license. Contact [email protected] for
details.
**Description**
Creates a port count & type report in Excel Workbook format from a brcddb project
**Version Control**
+-----------+---------------+-----------------------------------------------------------------------------------+
| Version | Last Edit | Description |
+===========+===============+===================================================================================+
| 4.0.0 | 04 Aug 2023 | Re-Launch |
+-----------+---------------+-----------------------------------------------------------------------------------+
| 4.0.1 | 06 Mar 2024 | Improved error messaging. |
+-----------+---------------+-----------------------------------------------------------------------------------+
| 4.0.2 | 03 Apr 2024 | Added version numbers of imported libraries. |
+-----------+---------------+-----------------------------------------------------------------------------------+
"""
__author__ = 'Jack Consoli'
__copyright__ = 'Copyright 2023, 2024 Consoli Solutions, LLC'
__date__ = '03 Apr 2024'
__license__ = 'Apache License, Version 2.0'
__email__ = '[email protected]'
__maintainer__ = 'Jack Consoli'
__status__ = 'Released'
__version__ = '4.0.2'
import os
import copy
import openpyxl as xl
import openpyxl.utils.cell as xl_util
import collections
import brcdapi.log as brcdapi_log
import brcdapi.gen_util as gen_util
import brcdapi.excel_fonts as excel_fonts
import brcdapi.excel_util as excel_util
import brcdapi.file as brcdapi_file
import brcddb.brcddb_project as brcddb_project
import brcddb.util.search as brcddb_search
import brcddb.brcddb_common as brcddb_common
import brcddb.brcddb_port as brcddb_port
import brcddb.brcddb_fabric as brcddb_fabric
import brcddb.brcddb_switch as brcddb_switch
_version_d = dict(
brcdapi_log=brcdapi_log.__version__,
gen_util=gen_util.__version__,
brcdapi_file=brcdapi_file.__version__,
excel_util=excel_util.__version__,
excel_fonts=excel_fonts.__version__,
brcddb_project=brcddb_project.__version__,
brcddb_search=brcddb_search.__version__,
brcddb_common=brcddb_common.__version__,
brcddb_port=brcddb_port.__version__,
brcddb_fabric=brcddb_fabric.__version__,
brcddb_switch=brcddb_switch.__version__,
)
_DOC_STRING = False # Should always be False. Prohibits any code execution. Only useful for building documentation
# _STAND_ALONE: True: Executes as a standalone module taking input from the command line. False: Does not automatically
# execute. This is useful when importing this module into another module that calls psuedo_main().
_STAND_ALONE = True # See note above
_input_d = dict(
i=dict(h='Required. Name of input file generated by capture.py, combine.py, or multi_capture.py'),
o=dict(h='Required. Name of output file. ".xlsx" is automatically appended.')
)
_input_d.update(gen_util.parseargs_log_d.copy())
# _switch_d is a template copied for each switch to track the number of login types, speed, and SFP type. _speed and
# 0 in _speed is used for unknown login speeds.
_speed = {0: 0, 1: 0, 2: 0, 4: 0, 8: 0, 16: 0, 32: 0, 64: 0}
_speed_keys = [int(i) for i in _speed.keys()]
_speed_keys.sort()
# _fc4_types are templates copied in _switch_d.
_fc4_types = collections.OrderedDict() # Ordered so that all reports are consistent
_fc4_types['isl'] = dict(other=dict(h='Other', s=()))
_fc4_types['targets'] = dict(
ibm=dict(h='IBM', s=('ibm',)),
hv=dict(h='Hitachi-Vantara', s=('hitachi', 'hv')),
dell=dict(h='Dell-EMC', s=('dell', 'emc')),
pure=dict(h='Pure', s=('pure',)),
netapp=dict(h='NetApp', s=('netapp',)),
hpe=dict(h='HPE', s=('hp', '3par')),
other=dict(h='Other', s=tuple())
)
_fc4_types['initiators'] = dict(
e=dict(h='Emulex', s=('emulex', 'lpe')),
q=dict(h='QLogic', s=('qlog', 'qle')),
ucs=dict(h='UCS', s=('cisco', 'fcoe')),
other=dict(h='Other', s=tuple())
)
_fc4_types['unknown'] = dict(other=dict(h='Other', s=tuple()))
_node_symb_key = 'brocade-name-server/fibrechannel-name-server/node-symbolic-name'
_switch_d = dict()
for _fc4_k, _fc4_d in _fc4_types.items():
_d0 = dict()
_switch_d.update({_fc4_k: _d0})
for _type in ('lwl', 'swl'):
_d1 = dict()
_d0.update({_type: _d1})
for _key in _fc4_d.keys():
_d1.update({_key: copy.copy(_speed)})
_border = excel_fonts.border_type('thin')
_wrap = excel_fonts.align_type('wrap')
_wrap_center = excel_fonts.align_type('wrap_center')
_text_font = excel_fonts.font_type('std')
_bold_font = excel_fonts.font_type('bold')
_hdr_font = excel_fonts.font_type('hdr_1')
def _sheet_for_report(wb, sheet_index, sname, stitle):
"""Creates a work sheet with margins and header
:param wb: Workbook object
:type wb: class
:param sheet_index: Where to insert the sheet in the workbook
:type sheet_index: int
:param sname: Sheet tab name
:type sname: str
:param stitle: Sheet title
:type stitle: str
"""
global _speed_keys, _border, _wrap, _wrap_center, _hdr_font
# Create the worksheet
sheet = wb.create_sheet(index=sheet_index, title=sname)
sheet.page_setup.paperSize = sheet.PAPERSIZE_LETTER
sheet.page_setup.orientation = sheet.ORIENTATION_LANDSCAPE
sheet.freeze_panes = sheet['A2']
# Set up the column widths and add the headers
row = col = 1
for col_width in (5, 30):
sheet.column_dimensions[xl_util.get_column_letter(col)].width = col_width
excel_util.cell_update(sheet, row, col, None, font=_hdr_font, align=_wrap, border=_border)
col += 1
sheet.merge_cells(start_row=row, start_column=1, end_row=row, end_column=2)
sheet['A1'] = stitle
for i in _speed_keys:
sheet.column_dimensions[xl_util.get_column_letter(col)].width = 6
excel_util.cell_update(sheet, row, col, i, font=_hdr_font, align=_wrap, border=_border)
col += 1
sheet.column_dimensions[xl_util.get_column_letter(col)].width = 8
excel_util.cell_update(sheet, row, col, 'Total', font=_hdr_font, align=_wrap_center, border=_border)
return sheet
def _summary_sheet(proj_obj, wb, summary_l):
"""Creates a summary sheet
:param proj_obj: Project object
:type proj_obj: brcddb.classes.project.ProjectObj
:param wb: openpyxl workbook object
:type wb: Workbook
:param summary_l: List of fabric dictionaries. See fab_sum_l in _create_report()
:type summary_l: list()
"""
global _speed, _speed_keys, _fc4_types, _border, _wrap, _text_font, _bold_font
# Initialize local variables
sheet = _sheet_for_report(wb, 0, 'Summary', 'Summary')
total_sum = list()
row = 2
# Get a summary of all the brands for each FC4 type. Note that there may be different brands on different switches.
sum_d = dict()
for fc4_type in _fc4_types.keys():
sum_d.update({fc4_type: dict()})
for fc4_type in _fc4_types.keys():
brand_l = list()
for fab_d in summary_l:
for switch_d in fab_d['switch_l']:
brand_l.extend([str(b) for b in switch_d[fc4_type].keys()])
for brand in gen_util.remove_duplicates(brand_l):
sum_d[fc4_type].update({brand: dict()})
for speed in _speed_keys:
sum_d[fc4_type][brand].update({speed: list()})
# Now get all the cell references
for fab_d in summary_l:
for switch_d in fab_d['switch_l']:
for fc4_type in _fc4_types.keys():
fc4_type_d = switch_d[fc4_type]
for brand in sum_d[fc4_type].keys():
for speed in _speed_keys:
try:
sum_d[fc4_type][brand][speed].append(fab_d['sheet'] + '!' + fc4_type_d[brand][speed])
except KeyError:
pass # Not all fabrics have the same list of brands
for fc4_type in _fc4_types:
# Add the FC4 type header
row, col = row + 1, 1
sheet.merge_cells(start_row=row, start_column=col, end_row=row, end_column=col+1)
excel_util.cell_update(sheet, row, col, fc4_type, font=_bold_font, align=_wrap, border=_border)
row += 1
type_start_row = row
# Add the brands
fc4_type_d = sum_d[fc4_type]
for brand in fc4_type_d.keys():
brand_d = fc4_type_d.get(brand)
if brand_d is None:
continue
# Add the brand name to the worksheet
col = 2
excel_util.cell_update(sheet, row, col, str(brand), font=_text_font, align=_wrap, border=_border)
# Add each speed within the brand
for speed in _speed_keys:
col += 1
val = 0 if len(brand_d[speed]) == 0 else '=' + '+'.join(brand_d[speed])
excel_util.cell_update(sheet, row, col, val, font=_text_font, align=_wrap, border=_border)
buf = '=sum(C' + str(row) + ':' + xl_util.get_column_letter(col) + str(row) + ')'
excel_util.cell_update(sheet, row, col+1, buf, font=_text_font, align=_wrap, border=_border)
row += 1
# Add the sub-total for each FC4 type
type_end_row, col = row, 2
row += 1
total_sum.append(row)
excel_util.cell_update(sheet, row, col, 'sub-total', font=_text_font, align=_wrap, border=_border)
for k3 in range(0, len(_speed_keys)):
col += 1
col_letter = xl_util.get_column_letter(col)
buf = '=sum(' + col_letter + str(type_start_row) + ':' + col_letter + str(type_end_row) + ')'
excel_util.cell_update(sheet, row, col, buf, font=_text_font, align=_wrap, border=_border)
buf = '=sum(C' + str(row) + ':' + xl_util.get_column_letter(col) + str(row) + ')'
excel_util.cell_update(sheet, row, col+1, buf, font=_text_font, align=_wrap, border=_border)
row += 1
# Add a project summary
row, col = row + 1, 1
sheet.merge_cells(start_row=row, start_column=col, end_row=row, end_column=col+1)
excel_util.cell_update(sheet, row, col, 'Project total', font=_bold_font, align=_wrap)
buf = '=$' + str(total_sum.pop(0))
for i in total_sum:
buf += '+$' + str(i)
col += 2
for k3 in range(0, len(_speed_keys)):
excel_util.cell_update(sheet, row, col, buf.replace('$', xl_util.get_column_letter(col)), font=_bold_font,
align=_wrap, border=_border)
col += 1
buf = '=sum(C' + str(row) + ':' + xl_util.get_column_letter(col-1) + str(row) + ')'
excel_util.cell_update(sheet, row, col, buf, font=_bold_font, align=_wrap, border=_border)
# Add a login summary
row, col = row + 2, 1
excel_util.cell_update(sheet, row, col, 'Total Logins', font=_bold_font, align=_wrap, border=_border)
sheet.merge_cells(start_row=row, start_column=1, end_row=row, end_column=2)
col += 2
excel_util.cell_update(sheet, row, col, len(proj_obj.r_login_keys()), font=_bold_font, align=_wrap, border=_border)
def _update_switch_in_fab_sheet(row, sheet, sub_hdr, switch_d):
"""Adds the port counts for a switch in a fabric worksheet
:param row: Starting row
:type row: int
:param sheet: Worksheet object
:type sheet: class
:param sub_hdr: Section header
:type sub_hdr: str
:param switch_d: Dictionary of port counts by port and login type
:type switch_d: dict
:return row: Last row used on worksheet
:rtype row: int
:return rd: Fabric level dictionary. See fab_sum_l in _create_report()
:rtype rd: dict
"""
global _speed_keys, _fc4_types, _border, _wrap, _text_font, _bold_font
switch_sum = list() # A list of row numbers for each type summary
rd = collections.OrderedDict()
col = 1
# Add the sub-header. Typically, the switch name
sheet.merge_cells(start_row=row, start_column=col, end_row=row, end_column=2+len(_speed_keys))
excel_util.cell_update(sheet, row, col, sub_hdr, font=excel_fonts.font_type('hdr_2'), align=_wrap)
row += 1
# The report is a list of ports based on the FC4 type (ISL, target, initiator, and unknown) associated with the
# login for each switch. Within each FC4 type the sub-categories are the SFP type (SWL or LWL) then brand. Note that
# SFP type and brand are combined into a single cell. The next category is the speed which is displayed in the
# columns.
for k0 in _fc4_types.keys():
d0 = switch_d.get(k0)
if d0 is None:
continue # When I wrote this, d0 should never be None. This is just future proofing.
col, row = 1, row + 1
rd.update({k0: dict()})
excel_util.cell_update(sheet, row, col, str(k0), font=_bold_font, align=_wrap)
sheet.merge_cells(start_row=row, start_column=col, end_row=row, end_column=col+1)
row += 1
type_start_row = row # Used for the formula in the type sub-total
for k1, d1 in d0.items(): # Wavelength (LWL & SWL)
for k2, d2 in d1.items(): # Brand
col = 2
buf = str(k1).upper() + ', ' + _fc4_types[k0][k2]['h']
excel_util.cell_update(sheet, row, col, buf, font=_text_font, align=_wrap, border=_border)
brand_d = dict()
rd[k0].update({buf: brand_d})
for k3 in _speed_keys: # Login speed
col += 1
excel_util.cell_update(sheet, row, col, d2[k3], font=_text_font, align=_wrap, border=_border)
brand_d.update({k3: xl_util.get_column_letter(col) + str(row)})
buf = '=sum(C' + str(row) + ':' + xl_util.get_column_letter(col) + str(row) + ')'
excel_util.cell_update(sheet, row, col+1, buf, font=_text_font, align=_wrap, border=_border)
row += 1
# Add the sub-total for each FC4 type
type_end_row, row, col = row, row + 1, 2
switch_sum.append(row)
excel_util.cell_update(sheet, row, col, 'sub-total', font=_text_font, align=_wrap, border=_border)
for k3 in range(0, len(_speed_keys)):
col += 1
col_letter = xl_util.get_column_letter(col)
buf = '=sum(' + col_letter + str(type_start_row) + ':' + col_letter + str(type_end_row) + ')'
excel_util.cell_update(sheet, row, col, buf, font=_text_font, align=_wrap, border=_border)
buf = '=sum(C' + str(row) + ':' + xl_util.get_column_letter(col) + str(row) + ')'
excel_util.cell_update(sheet, row, col+1, buf, font=_text_font, align=_wrap, border=_border)
row += 1
# Add the totals for the switch
row, col = row + 1, 1
excel_util.cell_update(sheet, row, col, 'Switch total', font=_bold_font, align=_wrap)
sheet.merge_cells(start_row=row, start_column=col, end_row=row, end_column=col+1)
buf = '=$' + str(switch_sum.pop(0))
for i in switch_sum:
buf += '+$' + str(i)
col += 2
for col in range(col, len(_speed_keys)+col):
col_letter = xl_util.get_column_letter(col)
excel_util.cell_update(sheet, row, col, buf.replace('$', col_letter), font=_bold_font, align=_wrap,
border=_border)
buf = '=sum(C' + str(row) + ':' + xl_util.get_column_letter(col-1) + str(row) + ')'
excel_util.cell_update(sheet, row, col+1, buf, font=_text_font, align=_wrap, border=_border)
return row, rd
def _create_report(proj_obj, file_name):
""" Create an Excel workbook with the port counts by fabric
:param proj_obj: Project object
:type proj_obj: brcddb.classes.project.ProjectObj
:param file_name: Name of Excel workbook
:type file_name: str
:return: Exit code
:rtype: int
"""
global _speed_keys, _fc4_types, _border, _wrap, _bold_font, _text_font
wb = xl.Workbook()
sheet_index = 0
ec = brcddb_common.EXIT_STATUS_OK
fab_sum_l = list() # Used to reference cells for summaries and totals. A list of dictionaries as follows:
"""
+---------------+-------+-----------------------------------------------------------+
| sheet | str | Sheet name |
+---------------+-------+-----------------------------------------------------------+
| switch_l | list | List of switch dictionaries. |
+---------------+-------+-----------------------------------------------------------+
Switch dictionaries:
+---------------+-------+-----------------------------------------------------------+
| isl | dict | Key is the brand. The value is a dictionary as follows: |
| | | Key Value |
| | | 0 Cell reference for speed 0 (unknown speed) |
| | | 1 Cell reference for speed 1 Gbps |
| | | etc. - one entry for each speed in _speed |
+---------------+-------+-----------------------------------------------------------+
| target | dict | Same as isl |
+---------------+-------+-----------------------------------------------------------+
| initiators | dict | Same as isl |
+---------------+-------+-----------------------------------------------------------+
| unknown | dict | Same as isl |
+---------------+-------+-----------------------------------------------------------+
"""
for fab_obj in proj_obj.r_fabric_objects(): # Add a sheet for each fabric
# Create the fabric sheet
sname = excel_util.valid_sheet_name.sub('', brcddb_fabric.best_fab_name(fab_obj).replace(' ', '_'))[:22] + \
'_' + str(sheet_index)
fab_name = brcddb_fabric.best_fab_name(fab_obj, wwn=False)
sheet = _sheet_for_report(wb, sheet_index, sname, fab_name)
row = 3
# Set up the tracker for the summary sheet
fab_sum = list() # A list of row numbers for each type summary
# Add the switches
switch_l, fab_switch_ref_d = list(), collections.OrderedDict()
fab_d = dict(sheet=sname, switch_l=switch_l, fab_switch_ref_d=fab_switch_ref_d)
for switch_obj in fab_obj.r_switch_objects():
row, cell_d = _update_switch_in_fab_sheet(row,
sheet,
brcddb_switch.best_switch_name(switch_obj, wwn=True),
switch_obj.r_get('switch_d'))
fab_sum.append(row)
switch_l.append(cell_d)
row += 2
fab_sum_l.append(fab_d)
# Add the header for the fabric summary at the bottom of the page.
col = 1
excel_util.cell_update(sheet, row, col, 'Fabric total', font=_bold_font, align=_wrap)
sheet.merge_cells(start_row=row, start_column=col, end_row=row, end_column=col+1)
# Adding the same level of detail to the fabric section as is with switches was an afterthought. Creating a
# dict that matches the switch dict with a list of switch references kept it simple.
cell_d_l = fab_d['switch_l']
if len(cell_d_l) > 0:
cell_d = cell_d_l[0]
for k, switch_type_d in cell_d.items():
fab_type_d = dict()
fab_switch_ref_d.update({k: fab_type_d})
for k0, switch_ref_d in switch_type_d.items():
fab_ref_d = dict()
fab_type_d.update({k0: fab_ref_d})
for k1, cell in switch_ref_d.items():
fab_ref_d.update({k1: '=' + cell})
# Get the remaining switch cell references for the fabric
if len(cell_d_l) > 1:
for cell_d in cell_d_l[1:]:
for k, switch_type_d in cell_d.items():
fab_type_d = fab_switch_ref_d[k]
for k0, switch_ref_d in switch_type_d.items():
fab_ref_d = fab_type_d[k0]
for k1, cell in switch_ref_d.items():
fab_ref_d[k1] += '+' + cell
# Add the fabric summaries to the worksheet
# fab_total_d is a list of cell references to be added together for the fabric total
fab_total_d = collections.OrderedDict()
for k in _speed_keys:
fab_total_d.update({k: list()})
# Fill in the fabric section
for k, fab_type_d in fab_switch_ref_d.items():
row, col = row+2, 1
excel_util.cell_update(sheet, row, col, str(k), font=_bold_font, align=_wrap)
sheet.merge_cells(start_row=row, start_column=col, end_row=row, end_column=col+1)
row, col = row+1, 2
start_row = row
for k0, cell_d in fab_type_d.items():
excel_util.cell_update(sheet, row, col, str(k0), font=_text_font, align=_wrap, border=_border)
for buf in [cell_d[k1] for k1 in _speed_keys]:
col += 1
excel_util.cell_update(sheet, row, col, buf, font=_text_font, align=_wrap, border=_border)
buf = '=sum(C' + str(row) + ':' + xl_util.get_column_letter(col) + str(row) + ')'
excel_util.cell_update(sheet, row, col+1, buf, font=_text_font, align=_wrap, border=_border)
row, col = row+1, 2
# Add the sub-total
row, col = row+1, 2
excel_util.cell_update(sheet, row, col, 'fabric sub-total', font=_text_font, align=_wrap, border=_border)
for speed_k in _speed_keys:
col += 1
col_letter = xl_util.get_column_letter(col)
buf = '=sum(' + col_letter + str(start_row) + ':' + col_letter + str(row-1) + ')'
excel_util.cell_update(sheet, row, col, buf, font=_text_font, align=_wrap, border=_border)
fab_total_d[speed_k].append(col_letter + str(row))
buf = '=sum(C' + str(row) + ':' + xl_util.get_column_letter(col) + str(row) + ')'
excel_util.cell_update(sheet, row, col+1, buf, font=_text_font, align=_wrap, border=_border)
# Add the fabric total
row, col = row+2, 1
excel_util.cell_update(sheet, row, col, 'Fabric total', font=_bold_font, align=_wrap)
sheet.merge_cells(start_row=row, start_column=col, end_row=row, end_column=col+1)
col = 2
for k, fab_ref_d in fab_total_d.items():
col += 1
buf = '=' + fab_ref_d[0] + '+' + '+'.join(fab_ref_d[1:])
excel_util.cell_update(sheet, row, col, buf, font=_text_font, align=_wrap, border=_border)
buf = '=sum(C' + str(row) + ':' + xl_util.get_column_letter(col) + str(row) + ')'
excel_util.cell_update(sheet, row, col+1, buf, font=_text_font, align=_wrap, border=_border)
sheet_index += 1
# Add a summary sheet
_summary_sheet(proj_obj, wb, fab_sum_l)
try:
wb.save(file_name)
except PermissionError:
buf = 'Write access permission for ' + file_name + ' denied. This typically occurs when the file is open'
brcdapi_log.log(['', buf], echo=True)
ec = brcddb_common.EXIT_STATUS_USER_ERROR
except FileExistsError:
brcdapi_log.log(['', 'The folder in the path for ' + file_name + ' does not exist.'], echo=True)
return ec
def psuedo_main(inf, outf):
"""Basically the main(). Did it this way so that it can easily be used as a standalone module or called externally.
:param inf: Input file name
:type inf: str
:param outf: Output file name
:type outf: str
:return: Exit code. See exist codes in brcddb.brcddb_common
:rtype: int
"""
global __version__, _speed, _switch_d, _node_symb_key
# Get a project object
try:
proj_obj = brcddb_project.read_from(inf)
except FileNotFoundError:
brcdapi_log.log(['', inf + ' not found.'], echo=True)
return brcddb_common.EXIT_STATUS_USER_ERROR
if proj_obj is None:
brcdapi_log.log(['', 'Unknown error reading: ' + inf], echo=True)
return brcddb_common.EXIT_STATUS_ERROR
brcddb_project.build_xref(proj_obj)
brcddb_project.add_custom_search_terms(proj_obj)
# Determine the number of logins for each media type and speed
for fab_obj in proj_obj.r_fabric_objects():
# Set up the speed tracking for each switch in each fabric
for switch_obj in fab_obj.r_switch_objects():
switch_d = copy.deepcopy(_switch_d)
switch_obj.s_new_key('switch_d', switch_d)
# Now figure out which port dictionary to use
for port_obj in switch_obj.r_port_objects():
# Default keys
k0 = 'unknown' # Basic FC4 type: isl, targets, initiators, or unknown
wave_length = port_obj.r_get('media-rdp/wavelength')
k1 = 'lwl' if isinstance(wave_length, int) and wave_length > 1300 else 'swl'
k2 = 'other' # brand
k3 = 0 # Speed
# k0 - Login type
buf = brcddb_port.port_type(port_obj)
if 'E-Port' in buf:
k0 = 'isl'
elif 'F-Port' in buf or 'N-Port' in buf:
# Is it a target or initiator?
all_logins_l = port_obj.r_login_objects()
if len(brcddb_search.match_test(all_logins_l, brcddb_search.target)) > 0:
k0 = 'targets'
elif len(brcddb_search.match_test(all_logins_l, brcddb_search.initiator)) > 0:
k0 = 'initiators'
# Determine the brand, k2.
fc4_d = _fc4_types.get(k0)
if isinstance(fc4_d, dict):
for login_obj in all_logins_l:
if k2 != 'other':
break
buf = login_obj.r_get(_node_symb_key)
if isinstance(buf, str):
buf = buf.lower()
for k, d in fc4_d.items():
if k2 != 'other':
break
for search_term in d['s']:
if search_term in buf:
k2 = k
break
# k3 - Speed
login_speed = port_obj.r_get('cs_search/speed')
if isinstance(login_speed, int) and login_speed in _speed:
k3 = login_speed
# Increment the port counter
switch_d[k0][k1][k2][k3] += 1
# Generate the report and return
return _create_report(proj_obj, outf)
def _get_input():
"""Parses the module load command line
:return: Exit code. See exist codes in brcddb.brcddb_common
:rtype: int
"""
global __version__, _input_d, _version_d
# Get command line input
args_d = gen_util.get_input('Creates a port count report in Excel ', _input_d)
# Set up logging
brcdapi_log.open_log(folder=args_d['log'], supress=args_d['sup'], no_log=args_d['nl'], version_d=_version_d)
# Command line feedback
ml = [
os.path.basename(__file__) + ', ' + __version__,
'In file: ' + args_d['i'],
'Out file: ' + args_d['o'],
'Log, -log: ' + str(args_d['log']),
'No log, -nl: ' + str(args_d['nl']),
'Supress, -sup: ' + str(args_d['sup']),
'',
]
brcdapi_log.log(ml, echo=True)
return psuedo_main(brcdapi_file.full_file_name(args_d['i'], '.json'),
brcdapi_file.full_file_name(args_d['o'], '.xlsx'))
##################################################################
#
# Main Entry Point
#
###################################################################
if _DOC_STRING:
print('_DOC_STRING is True. No processing')
exit(0)
if _STAND_ALONE:
_ec = _get_input()
brcdapi_log.close_log(['', 'Processing Complete. Exit code: ' + str(_ec)], echo=True)
exit(_ec)