def load_config(session, file=''): try: ixnetwork = session.Ixnetwork if file == '': file = sys.argv[0].split('/')[-1].split('.')[0] + '.ixncfg' ixnetwork.LoadConfig(Files(file, local_file=True)) #ixNetwork.ResourceManager.ImportConfigFile(Files(jsonConfigFile, local_file=True), Arg3=True) except Exception as err: logger.error(err) logger_msg(u'加载配置文件失败,请检查配置', 'ERROR')
def logger_msg(msg, level='INFO'): # support info level and error level if level.lower() == 'info': try: logger.info(msg.encode('utf-8')) except Exception as err: logger.info(msg) elif level.lower() == 'error': try: logger.error(msg.encode('utf-8')) except Exception as err: logger.error(msg)
def stop_traffic(session): """ This function stops all the IxNetwork traffic items configured on all the ports. Args: session (obj): IxNetwork session object. Returns: None. """ try: ixnetwork = session.Ixnetwork ixnetwork.Traffic.StopStatelessTrafficBlocking() except Exception as e: logger.error(e) logger_msg(u'停止流量失败,请检查配置', 'ERROR')
def stop_protocols(session): """This function stops all the protocols configured on the IxNetwork protocol stack (e.g., IP and Ethernet). Args: session (obj) : IxNetwork session object. Returns: None """ try: ixnetwork = session.Ixnetwork ixnetwork.StopAllProtocols(Arg1='sync') except Exception as e: logger.error(e) logger_msg(u'停止协议失败,请检查配置', 'ERROR')
def start_traffic(session): """ This function starts all the IxNetwork traffic items configured on all the ports. Args: session (obj): IxNetwork session object. Returns: None. """ try: ixnetwork = session.Ixnetwork """ Apply traffic to hardware """ ixnetwork.Traffic.Apply() """ Run traffic """ ixnetwork.Traffic.StartStatelessTrafficBlocking() except Exception as e: logger.error(e) logger_msg(u'流量开启失败,请检查配置', 'ERROR')
def get_statistics(session, stat_view_name='Protocols Summary'): """This function fetches the traffic statistics information. Args: session (obj) : IxNetwork session object. stat_view_name (str, optional): Statistics view name. Default value is 'Flow Statistics' Returns: traffic statistics dictionary. """ try: ixnetwork = session.Ixnetwork statistics = session.StatViewAssistant(stat_view_name) return statistics except Exception as e: logger.error(e) logger_msg(u'获取结果数据失败,请检查配置', 'ERROR')
def reserve_port(session, portList, force=True): try: ixnetwork = session.Ixnetwork portMap = session.PortMapAssistant() vport = dict() for index, port in enumerate(portList): portCntInCfg = len(ixnetwork.Vport.find()) if index >= portCntInCfg: break portName = ixnetwork.Vport.find()[index].Name logger_msg(u'连接机框 %s,开始抢占端口%s/%s' % (port[0], port[1], port[2])) portMap.Map(IpAddress=port[0], CardId=port[1], PortId=port[2], Name=portName) forceTakePortOwnership = force portMap.Connect(forceTakePortOwnership) except Exception as err: logger.error(err) logger_msg(u'占用端口失败,请检查配置', 'ERROR')
def modify_vlan(session, portname, vlanid, index='0'): #modify the interface vlanid of input port name, #'index' means the interface offset on that port try: ixnetwork = session.Ixnetwork porthref = ixnetwork.Vport.find(Name=portname).href for topology in ixnetwork.Topology.find(): vports = topology.Ports if porthref in vports: ethernet = topology.DeviceGroup.find()[int( index)].Ethernet.find()[0] if math.isnan(vlanid): ethernet.EnableVlans.Single(False) else: ethernet.EnableVlans.Single(True) ethernet.Vlan.find()[0].VlanId.Increment( start_value=vlanid, step_value=0) break except Exception as err: logger.error(err) logger_msg(u'更改vlan失败,请检查配置', 'ERROR')
def start_protocols(session): """This function starts all the protocols configured on the IxNetwork protocol stack (e.g., IP and Ethernet). Args: session (obj) : IxNetwork session object. Returns: None """ try: ixnetwork = session.Ixnetwork ixnetwork.StartAllProtocols(Arg1='sync') logger_msg(u'等待协议建立完成,检查协议状态和数量是否正确。') protocolSummary = session.StatViewAssistant('Protocols Summary') protocolSummary.CheckCondition('Sessions Not Started', protocolSummary.EQUAL, 0) protocolSummary.CheckCondition('Sessions Down', protocolSummary.EQUAL, 0) #logger.info(protocolSummary) except Exception as e: logger.error(e) logger_msg(u'协议开启失败,请检查配置', 'ERROR')