importreimportyamlfrompprintimportpprintfromcollections.abcimportIterabledefextract_topology_info(file_path):topology_result={}# YAMLファイルからtopology_resultを取得withopen(file_path,'r')asfile:yaml_dict=yaml.safe_load(file)# 'devices'セクションを取得devices_section=yaml_dict['devices']# 'topology'セクションを取得topology_section=yaml_dict['topology']fordevice,detailsintopology_section.items():interfaces=details['interfaces']topology_result[device]={interface['link']:nameforname,interfaceininterfaces.items()}# 'devices'セクションからホスト名を追加forhostname,detailsindevices_section.items():alias=details['alias']topology_result[alias]['hostname']=hostnamereturntopology_resultdefreplace_template_variables(data,testnumber,variables):forkey,valueindata.items():ifisinstance(value,dict):replace_template_variables(value,testnumber,variables)else:# Step 1: Replacing the pattern with the value from procedure_vars_dictforvar_key,var_valueinvariables.items():value=value.replace(f"%{{vars_{testnumber}.nodes.{var_key}}}",var_value)# Step 2: Replacing the remaining patternforvar_key,var_valueinvariables.items():value=value.replace(f"%VARIABLES{{runtime.testbed.devices[{var_value}].name}}",var_value)data[key]=value# Additional Step: Replacing other variables such as '%VARIABLES{DUT_if1}'forvar_key,var_valueinvariables.items():value=value.replace(f"%VARIABLES{{{var_key}}}",var_value)data[key]=valuedeffind_key(data,target_key,results=None):ifresultsisNone:results=[]ifisinstance(data,dict):forkey,valueindata.items():ifkey==target_key:results.append(value)find_key(value,target_key,results)elifisinstance(data,Iterable)andnotisinstance(data,str):foritemindata:find_key(item,target_key,results)returnresultsfile_path='testbed_topology_mylab_copy.yaml'topology_result=extract_topology_info(file_path)trigger_datafile_path='trigger_datafile_copy2.yaml'testnumber='003-001-009'testnumber_under=re.sub('-','_',testnumber)#YAMLファイルからvars_003_006_001セクションを解析withopen(trigger_datafile_path,'r')asfile:trigger_yaml_dict=yaml.safe_load(file)vars_section=trigger_yaml_dict[f'vars_{testnumber_under}']test_sections_section=trigger_yaml_dict.get(f'TriggerBlitz_tc{testnumber}',{}).get('test_sections',{})nodes=vars_section['nodes']links=vars_section['links']# nodes 変数の変換transformed_nodes={}forvariable_name,node_aliasinnodes.items():ifnode_alias=='IXIA':continuetransformed_nodes[variable_name]=topology_result[node_alias]['hostname']# links 変数の変換transformed_links={}configure_by_jinja2_data={}procedure_dict={}forsectionintest_sections_section:forstep_name,step_detailsinsection.items():# 変数定義セクションの解析if'init_configuration'instep_name:forstep_detailinstep_details:if'api'instep_detailandstep_detail['api'].get('function')=='get_single_interface':arguments=step_detail['api']['arguments']device_var=arguments['device'].split('.')[-1].strip('}')link_var=arguments['link_name'].split('.')[-1].strip('}')device_alias=vars_section['nodes'][device_var]link_alias=vars_section['links'][link_var]if'save'instep_detail['api']:forsave_iteminstep_detail['api']['save']:variable_name=save_item['variable_name']# Transforming variable_name based on topology_resulttransformed_value=topology_result[device_alias][link_alias]transformed_links[variable_name]=transformed_valueif'api'instep_detailandstep_detail['api'].get('function')=='configure_by_jinja2':template_name=step_detail['api']['arguments'].pop('template_name')templates_dir=step_detail['api']['arguments'].pop('templates_dir')# templates_dir は特に使用しない予定configure_by_jinja2_data[template_name]=step_detail['api']['arguments']else:# 試験手順部分を抽出する処理procedure_dict[step_name]=[]execute_blocks=[(execute_block['device'],execute_block['command'])forexecute_blockinfind_key(step_details,'execute')]configure_blocks=[(configure_block['device'],configure_block['command'])forconfigure_blockinfind_key(step_details,'configure')]tgn_blocks=find_key(step_details,'tgn')# TODO:iftgn_blocks!=[]:pprint(tgn_blocks)ifexecute_blocks!=[]:foriinexecute_blocks:procedure_dict[step_name].append(i)ifconfigure_blocks!=[]:foriinconfigure_blocks:procedure_dict[step_name].append(i)# topology_result# transformed_nodes# transformed_linksprocedure_vars_dict={**transformed_nodes,**transformed_links}replace_template_variables(configure_by_jinja2_data,testnumber_under,procedure_vars_dict)# 'dev_IXIA': '%VARIABLES{runtime.testbed.devices[%{vars_003_001_009.nodes.IXIA}].name}', を編集するようにpprint(configure_by_jinja2_data)