1+ """
2+ Classes to manage IxExplorer HW objects - chassis, card and resource group.
3+ Port class in in ixe_port module.
4+ """
15import re
26from collections import OrderedDict
3- from typing import Dict
7+ from typing import TYPE_CHECKING , Dict
48
59from trafficgenerator .tgn_tcl import tcl_list_2_py_list
610
711from ixexplorer .api .ixapi import FLAG_RDONLY , IxTclHalError , TclMember , ixe_obj_meta
812from ixexplorer .ixe_object import IxeObject , IxeObjectObj
913from ixexplorer .ixe_port import IxePort
1014
15+ if TYPE_CHECKING :
16+ from ixexplorer .ixe_app import IxeSession
17+
1118
1219class IxeCard (IxeObject , metaclass = ixe_obj_meta ):
1320 __tcl_command__ = "card"
@@ -33,7 +40,7 @@ class IxeCard(IxeObject, metaclass=ixe_obj_meta):
3340 def __init__ (self , parent , uri ):
3441 super ().__init__ (parent = parent , uri = uri .replace ("/" , " " ))
3542
36- def discover (self ):
43+ def discover (self ) -> None :
3744 self .logger .info ("Discover card {}" .format (self .obj_name ()))
3845 for pid in range (1 , self .portCount + 1 ):
3946 IxePort (self , self .uri + "/" + str (pid ))
@@ -60,7 +67,7 @@ def discover(self):
6067 ports = range (1 , 13 )
6168 operationMode = "1000"
6269 IxeResourceGroup (self , 1 , operationMode , - 1 , ports , ports , ports )
63- except Exception as _ :
70+ except Exception :
6471 print ("no resource group support" )
6572
6673 def add_vm_port (self , port_id , nic_id , mac , promiscuous = 0 , mtu = 1500 , speed = 1000 ):
@@ -84,18 +91,15 @@ def get_resource_group(self):
8491
8592 resourceGroup = property (get_resource_group )
8693
87- def write (self ):
94+ def write (self ) -> None :
8895 self .ix_command ("write" )
8996
9097 #
9198 # Properties.
9299 #
93100
94- def get_ports (self ):
95- """
96- :return: dictionary {index: object} of all ports.
97- """
98-
101+ def get_ports (self ) -> Dict [int , IxePort ]:
102+ """Get dictionary {index: object} of all ports."""
99103 return {int (p .index ): p for p in self .get_objects_by_type ("port" )}
100104
101105 ports = property (get_ports )
@@ -205,27 +209,35 @@ class IxeChassis(IxeObject, metaclass=ixe_obj_meta):
205209 OS_WIN2000 = 3
206210 OS_WINXP = 4
207211
208- def __init__ (self , parent , host , chassis_id = 1 ):
212+ def __init__ (self , parent : "IxeSession" , host : str ) -> None :
213+ """Create IxeChassis object with name = url == IP address."""
209214 super ().__init__ (parent = parent , uri = host , name = host )
210- self .chassis_id = chassis_id
215+ self .chassis_id = 0
211216
212217 def connect (self ) -> None :
213- self .add ()
214- self .id = self .chassis_id
218+ """Connect to chassis and get assigned chassis ID.
219+
220+ Note that sometimes, randomly, ixConnectToChassis fails. However, using chassis.add also fails, so it seems there is
221+ no advantage for using one over the other.
222+ """
223+ self .api .call_rc (f"ixConnectToChassis { self .uri } " )
224+ self .chassis_id = self .id
215225
216226 def disconnect (self ) -> None :
217- self .ix_command ("del" )
227+ """Disconnect from chassis."""
228+ self .api .call_rc (f"ixDisconnectFromChassis { self .uri } " )
218229
219230 def add_card (self , cid ):
220- """
231+ """Add card.
232+
221233 There is no config option which cards are used. So we have to iterate over all possible card ids and check if we are
222234 able to get a handle.
223235 """
224236 card = IxeCard (self , str (self .chassis_id ) + "/" + str (cid ))
225237 try :
226238 card .discover ()
227239 except IxTclHalError :
228- self .logger .info (f"slot { cid } is empty" )
240+ self .logger .info (f"Slot { cid } is empty" )
229241 card .del_object_from_parent ()
230242
231243 def discover (self ) -> None :
0 commit comments