Printer FriendlyEmail Article Link

Spirent TestCenter: Sample python script to load the config and subscribe the results

Objective/Summary

 Sample python script to load the config and subscribe the results

Environment/Versions
  1.  Spirent TestCenter
  2. Python
  3. API
Procedure

 ##############################################################

#Loading HLTAPI for Python
##############################################################
import sth
 
### Loading stc for python######
from StcPython import StcPython
stc = StcPython()
 
 
stc.log("INFO", "Starting Test")
 
# This line will show the TestCenter commands on stdout
stc.config("automationoptions", logto="stdout", loglevel="INFO")
 
 
 
##############################################################
#config the parameters for the logging
##############################################################
 
test_sta = sth.test_config (
log                                              = '1',
logfile                                          = 'HL_logfile',
vendorlogfile                                    = 'HL_stcExport',
vendorlog                                        = '1',
hltlog                                           = '1',
hltlogfile                                       = 'HL_hltExport',
hlt2stcmappingfile                               = 'HL_hlt2StcMapping',
hlt2stcmapping                                   = '1',
log_level                                        = '7');
 
status = test_sta['status']
if (status == '0') :
print "run sth.test_config failed"
print test_sta
else:
print "***** run sth.test_config successfully"
 
 
##############################################################
#config the parameters for optimization and parsing
##############################################################
 
test_ctrl_sta = sth.test_control (
action                                           = 'enable');
 
status = test_ctrl_sta['status']
if (status == '0') :
print "run sth.test_control failed"
print test_ctrl_sta
else:
print "***** run sth.test_control successfully"
 
 
###  Spirent Chassis and port details
szChassisIp = "10.108.8.20"
port1 = "1/1"
port2 = "1/2"
port_list = ['$port1',' $port2']
 
 
##### Load the saved .xml file. Modify the path as per your requirement.
stc.perform("LoadFromDatabase", DatabaseConnectionString = "test.tcc")
 
stc.apply()
 
# Connect to a chassis
print("Connecting ", szChassisIp)
stc.connect(szChassisIp)
 
## reserve the ports
print("Reserving / and / ".format(szChassisIp, port1, szChassisIp, port2))
stc.reserve(" / / ".format(szChassisIp, port1, szChassisIp, port2))
 
# Create the mapping between the physical ports and their logical 
#   representation in the test configuration.
print("Set up port mappings")
stc.perform("SetupPortMappings")
 
#Issue these commands to get the devices and port handles
hProject = stc.get("system1", "children-project")
print "project", hProject
 
portList = stc.get(hProject, "children-port")
print "ports" , portList
 
deviceList = stc.get(hProject, "children-emulateddevice")
print "devices" , deviceList
 
##Query and list the streamblocks 
sb_list = []
portList = portList.split()
for port in portList :
sb = stc.get(port, 'children-streamblock');
        sb_list.append(sb );
print "the list of streamblocks :", sb_list;
 
 
### start all devices
stc.perform("DevicesStartAll")
 
### start streamblocks
stc.perform("streamblockstartcommand" , StreamBlockList = sb_list)
 
# Subscribe the Streamblock Level Statistics
rds1 = stc.subscribe(Parent=hProject, resultparent=sb_list, ConfigType="StreamBlock", viewAttributeList="StreamId", resulttype="TxStreamResults")
rds2 = stc.subscribe(Parent=hProject, resultparent=sb_list, ConfigType="StreamBlock", viewAttributeList= ['FrameCount', 'StreamIndex', 'DroppedFrameCount'], resulttype="RxStreamSummaryResults")
 
# Refresh the views. Refresh is applied only on the receiving side to get the real time stats.
stc.perform("RefreshResultViewCommand" , ResultDataSet = rds2)
 
print "the value of rds1 is :", rds1;
print "the value of rds2 is :", rds2;
 
# Stop the streamblocks
stc.perform("streamblockstopcommand" , StreamBlockList = sb_list)
 
 
#### get the resultHandle and Pagecount
resultList = stc.get(rds2, "ResultHandleList")
resultList = resultList.split()
 
for result in resultList : 
    print "FrameCount :", stc.get(result, "FrameCount");
    print "StreamIndex :", stc.get(result, "StreamIndex");
    print "DroppedFrameCount :", stc.get(result, "DroppedFrameCount");
 
###Stop all devices
print "Stoping all the Devices ...\n"
stc.perform("DevicesStopAllCommand")
 
print " ......................END OF TEST ......................................\n"
 
##############################################################
#clean up the session, release the ports reserved and cleanup the dbfile
##############################################################
 
cleanup_sta = sth.cleanup_session (
port_handle                                      = portList,
clean_dbfile                                     = '1');
 
status = cleanup_sta['status']
if (status == '0') :
print "run sth.cleanup_session failed"
print cleanup_sta
else:
print "***** run sth.cleanup_session successfully"
 
 
print "**************Finish***************"
 
 
 
 
 
Attachments
Attachment
Attachment Description
Attached is the script for reference

Product : Product,API