|
24 | 24 | import numpy as np
|
25 | 25 | import networkx as nx
|
26 | 26 |
|
27 |
| -class flow(): |
| 27 | +class flow_report(): |
| 28 | + def create_device_flow_table(self,full_filepath_master,device_name): |
| 29 | + print('--- create_device_flow_table ---',full_filepath_master,device_name) |
| 30 | + ## check Flow_Data sheet exists in Master file |
| 31 | + input_excel_master = openpyxl.load_workbook(full_filepath_master) |
| 32 | + ws_list_master = input_excel_master.sheetnames |
| 33 | + input_excel_master.close() |
| 34 | + |
| 35 | + ws_flow_name = 'Flow_Data' |
| 36 | + if ws_flow_name in ws_list_master: |
| 37 | + master_flow_array = ns_def.convert_excel_to_array(ws_flow_name, full_filepath_master, 3) |
| 38 | + master_flow_array = master_flow_array[:-1] |
| 39 | + #print(master_flow_array) |
| 40 | + target_flow_array = [] |
| 41 | + for tmp_master_flow_array in master_flow_array: |
| 42 | + tmp_routing_path = '' |
| 43 | + if tmp_master_flow_array[1][6] != '': |
| 44 | + tmp_routing_path = tmp_master_flow_array[1][6] |
| 45 | + elif tmp_master_flow_array[1][6] == '' and tmp_master_flow_array[1][6] != ' ': |
| 46 | + tmp_routing_path = tmp_master_flow_array[1][7] |
| 47 | + |
| 48 | + if tmp_master_flow_array[1][1] == device_name or tmp_master_flow_array[1][2] == device_name or device_name in tmp_routing_path: |
| 49 | + target_flow_array.append([tmp_master_flow_array[1][0],tmp_master_flow_array[1][1],tmp_master_flow_array[1][2],tmp_master_flow_array[1][3],tmp_master_flow_array[1][4],tmp_master_flow_array[1][5],tmp_routing_path]) |
| 50 | + #print(target_flow_array) |
| 51 | + |
| 52 | + |
| 53 | + ''' |
| 54 | + export flow report |
| 55 | + ''' |
| 56 | + excel_maseter_file = full_filepath_master |
| 57 | + iDir = os.path.abspath(os.path.dirname(excel_maseter_file)) |
| 58 | + |
| 59 | + basename_without_ext = os.path.splitext(os.path.basename(excel_maseter_file))[0] |
| 60 | + self.outFileTxt_11_3.delete(0, tkinter.END) |
| 61 | + self.outFileTxt_11_3.insert(tk.END, iDir + ns_def.return_os_slash() + '[FLOW_REPORT]' + basename_without_ext.replace('[MASTER]', '') + '.xlsx') |
| 62 | + |
| 63 | + ## check file open |
| 64 | + ns_def.check_file_open(self.outFileTxt_11_3.get()) |
| 65 | + |
| 66 | + # flag exist flow file |
| 67 | + flag_flow_table_exist = False |
| 68 | + if os.path.isfile(self.outFileTxt_11_3.get()) == True: |
| 69 | + #os.remove(self.outFileTxt_11_3.get()) |
| 70 | + flag_flow_table_exist = True |
| 71 | + |
| 72 | + self.excel_flow_file = self.outFileTxt_11_3.get() |
| 73 | + |
| 74 | + ## check Flow_Data sheet exists in Master file |
| 75 | + input_excel_master = openpyxl.load_workbook(excel_maseter_file) |
| 76 | + ws_list_master = input_excel_master.sheetnames |
| 77 | + input_excel_master.close() |
| 78 | + |
| 79 | + ''' |
| 80 | + MAKE Flows Table List |
| 81 | + ''' |
| 82 | + master_device_table_tuple = {} |
| 83 | + flow_list_array = [] |
| 84 | + egt_maker_width_array = ['5','25', '25','25', '25','15', '20', '25', '40'] # for Network Sketcher Ver 2.0 |
| 85 | + flow_list_array.append([1, ['<RANGE>', '1','1', '1', '1', '1', '1', '1', '1', '1', '<END>']]) |
| 86 | + flow_list_array.append([2, ['<HEADER>', 'No','Source Device Name', 'Destination Device Name','Source IP Address', 'Destination IP Address','TCP/UDP/ICMP','Service name(Port)', 'Max. bandwidth(Mbps)', 'Routing path settings', '<END>']]) |
| 87 | + current_row_num = 3 |
| 88 | + |
| 89 | + '''add flow table list''' |
| 90 | + #print(self.show_l3_interface) |
| 91 | + # Initialize a dictionary to hold devices and their IP addresses |
| 92 | + device_ips = {} |
| 93 | + |
| 94 | + # Iterate through the list of interfaces |
| 95 | + for tmp_show_l3_interface in self.show_l3_interface: |
| 96 | + tmp_device_name = tmp_show_l3_interface[0] |
| 97 | + tmp_ip_address = tmp_show_l3_interface[3] |
| 98 | + |
| 99 | + # Add the IP address to the corresponding device in the dictionary |
| 100 | + if tmp_device_name not in device_ips: |
| 101 | + device_ips[tmp_device_name] = [] |
| 102 | + device_ips[tmp_device_name].append(tmp_ip_address) |
| 103 | + |
| 104 | + for tmp_target_flow_array in target_flow_array: |
| 105 | + tmp_target_flow_array = list(map(str, tmp_target_flow_array)) |
| 106 | + tmp_target_flow_array.insert(0, '') |
| 107 | + tmp_target_flow_array.append('<END>') |
| 108 | + source_ip_array = device_ips[tmp_target_flow_array[2]] |
| 109 | + destination_ip_array = device_ips[tmp_target_flow_array[3]] |
| 110 | + str_source_ip = ', '.join(map(str, source_ip_array )) |
| 111 | + str_destination_ip = ', '.join(map(str, destination_ip_array)) |
| 112 | + tmp_target_flow_array.insert(4, str_source_ip) |
| 113 | + tmp_target_flow_array.insert(5, str_destination_ip) |
| 114 | + flow_list_array.append([current_row_num,tmp_target_flow_array]) |
| 115 | + current_row_num += 1 |
| 116 | + |
| 117 | + #add last <EMD> |
| 118 | + flow_list_array.append([current_row_num, ['<END>']]) |
| 119 | + #print(flow_list_array) |
| 120 | + |
| 121 | + #print(flow_list_array) |
| 122 | + ### Convert to tuple |
| 123 | + master_device_table_tuple = ns_def.convert_array_to_tuple(flow_list_array) |
| 124 | + |
| 125 | + ''' |
| 126 | + Create temp input data file |
| 127 | + ''' |
| 128 | + # List of characters not allowed in Excel worksheet names |
| 129 | + forbidden_chars = [':', '\\', '/', '?', '*', '[', ']'] |
| 130 | + # Remove forbidden characters using a list comprehension |
| 131 | + cleaned_device_name = ''.join(char for char in device_name if char not in forbidden_chars) |
| 132 | + |
| 133 | + |
| 134 | + ### Create the flow table excel file or add sheet |
| 135 | + self.worksheet_name = cleaned_device_name |
| 136 | + if flag_flow_table_exist == False: |
| 137 | + wb = openpyxl.Workbook() |
| 138 | + sheet = wb.active |
| 139 | + sheet.title = self.worksheet_name |
| 140 | + wb.save(self.excel_flow_file) |
| 141 | + else: |
| 142 | + wb = openpyxl.load_workbook(self.excel_flow_file) |
| 143 | + if self.worksheet_name in wb.sheetnames: |
| 144 | + # Remove the existing worksheet |
| 145 | + sheet_to_remove = wb[self.worksheet_name] |
| 146 | + wb.remove(sheet_to_remove) |
| 147 | + |
| 148 | + wb.create_sheet(title=self.worksheet_name) |
| 149 | + wb.save(self.excel_flow_file) |
| 150 | + |
| 151 | + ''' |
| 152 | + Create [FLOW_REPORT] file |
| 153 | + ''' |
| 154 | + tmp_master_data_array = [] |
| 155 | + tmp_master_data_array.append([1, [self.worksheet_name]]) |
| 156 | + #print(tmp_master_data_array) |
| 157 | + |
| 158 | + template_master_data_tuple = {} |
| 159 | + template_master_data_tuple = ns_def.convert_array_to_tuple(tmp_master_data_array) |
| 160 | + |
| 161 | + #print('Create --- template_master_data_tuple---') |
| 162 | + #print(template_master_data_tuple) |
| 163 | + offset_row = 0 |
| 164 | + offset_column = 0 |
| 165 | + write_to_section = '_template_' |
| 166 | + ns_def.write_excel_meta(template_master_data_tuple, self.excel_flow_file, self.worksheet_name, write_to_section, offset_row, offset_column) |
| 167 | + |
| 168 | + ### |
| 169 | + input_excel_name = self.excel_flow_file |
| 170 | + output_excel_name = self.outFileTxt_11_3.get() |
| 171 | + if flag_flow_table_exist == False: |
| 172 | + NEW_OR_ADD = 'NEW' |
| 173 | + else: |
| 174 | + NEW_OR_ADD = 'ADD_OPTION1' |
| 175 | + ns_egt_maker.create_excel_gui_tree(input_excel_name,output_excel_name,NEW_OR_ADD, egt_maker_width_array) |
| 176 | + |
| 177 | + ''' |
| 178 | + Add FLOW_List table from meta |
| 179 | + ''' |
| 180 | + # Write normal tuple to excel |
| 181 | + tmp_ws_name = '_tmp_' |
| 182 | + master_excel_meta = master_device_table_tuple |
| 183 | + ppt_meta_file = output_excel_name |
| 184 | + excel_file_path = ppt_meta_file |
| 185 | + worksheet_name = tmp_ws_name |
| 186 | + section_write_to = '<<N/A>>' |
| 187 | + offset_row = 0 |
| 188 | + offset_column = 0 |
| 189 | + ns_def.create_excel_sheet(ppt_meta_file, tmp_ws_name) |
| 190 | + ns_def.write_excel_meta(master_excel_meta, excel_file_path, worksheet_name, section_write_to, offset_row, offset_column) |
| 191 | + |
| 192 | + #print(output_excel_name) |
| 193 | + self.input_tree_excel = openpyxl.load_workbook(output_excel_name) |
| 194 | + worksheet_name = cleaned_device_name |
| 195 | + start_row = 1 |
| 196 | + start_column = 0 |
| 197 | + custom_table_name = ppt_meta_file |
| 198 | + self.input_tree_excel = ns_egt_maker.insert_custom_excel_table(self.input_tree_excel, worksheet_name, start_row, start_column, custom_table_name) |
| 199 | + self.input_tree_excel.save(output_excel_name) |
| 200 | + |
| 201 | + # Remove _tmp_ sheet from excel master self.worksheet_name |
| 202 | + ns_def.remove_excel_sheet(ppt_meta_file, tmp_ws_name) |
| 203 | + |
| 204 | + |
| 205 | +class flow(): |
28 | 206 | def add_routing_path_to_flow(self,full_filepath_master,flow_list_array):
|
29 | 207 | print('--- Routing path calculation ---')
|
30 | 208 | argv_array = ['show', 'l3_broadcast_domain']
|
@@ -434,13 +612,13 @@ def export_ip_report(self,dummy):
|
434 | 612 | # SET IP Address report file patch
|
435 | 613 | basename_without_ext = os.path.splitext(os.path.basename(excel_maseter_file))[0]
|
436 | 614 | self.outFileTxt_11_3.delete(0, tkinter.END)
|
437 |
| - self.outFileTxt_11_3.insert(tk.END, iDir + ns_def.return_os_slash() + '[IP_TABLE]' + basename_without_ext.replace('[MASTER]', '') + '.xlsx') |
438 |
| - self.excel_file_path = iDir + ns_def.return_os_slash() + '_template_[IP_TABLE]' + basename_without_ext.replace('[MASTER]', '') + '.xlsx' |
| 615 | + self.outFileTxt_11_3.insert(tk.END, iDir + ns_def.return_os_slash() + '[IP_REPORT]' + basename_without_ext.replace('[MASTER]', '') + '.xlsx') #change IP_TABLE to IP_REPORT at ver 2.5.1 |
| 616 | + self.excel_file_path = iDir + ns_def.return_os_slash() + '_template_[IP_REPORT]' + basename_without_ext.replace('[MASTER]', '') + '.xlsx' #change IP_TABLE to IP_REPORT at ver 2.5.1 |
439 | 617 |
|
440 | 618 | ## check file open
|
441 | 619 | ns_def.check_file_open(self.outFileTxt_11_3.get())
|
442 | 620 |
|
443 |
| - # remove exist device file |
| 621 | + # remove exist ip table file |
444 | 622 | if os.path.isfile(self.outFileTxt_11_3.get()) == True:
|
445 | 623 | os.remove(self.outFileTxt_11_3.get())
|
446 | 624 |
|
|
0 commit comments