def __init__( self, key, node, host, port, cmd ): self.key = _IOUtils.encode_str( as_str( key, name='key' ) ) self.node = as_a( node, instance_of=Node, name="node" ) self.addr = _ShellCommandHandler.get_address( host, port ) self.cmd = as_str( cmd, name='cmd' ) self.sock_timeout = 1 self.line_timeout = 1 self.cmd_proc = None
def _build_forward_rule(src, dest, table=0, priority=1): src = as_str(src) dest = as_str(dest) table = as_int(table, minim=0) priority = as_int(priority, minim=0) rule = 'table={0}'.format(table) rule += ',priority={0}'.format(priority) rule += ',in_port={0}'.format(src) rule += ',actions=output:{0}'.format(dest) return rule
def add_custom_remote_host( self, name, user, server, \ startcmd, stopcmd, morecmds=None, \ **params ): """ Adds a custom remote host to the current topology and returns a CustomRemoteHostConfig object representing it. - name : a textual representation of the host - user : the name of the user on the remote server - server : the IP address of the remote server - startcmd: a function used to start the host when called - stopcmd : a function used to stop the host when called - morecmds: additional optional commands to define in the Mininet node """ if self.built: raise RuntimeError( 'build() method was already called; cannot add any more hosts' ) name = as_str( name ) storage = self.hosts check_duplicate( storage, name ) h = self._new_remote_node( CustomRemoteHostConfig, name, user, server, \ startcmd=startcmd, stopcmd=stopcmd, morecmds=morecmds, \ **params ) storage[ name ] = h info( newline( '=== Added custom remote host', h ) ) return h
def add_remote_varanus_collector( self, name, user, server, varanus_home, cid, **params ): """ Adds a remote VARANUS collector to the current topology and returns a RemoteCollectorConfig object representing it, or a NullNodeConfig if VARANUS usage is disabled. - name : a textual representation of the collector - user : the name of the user on the remote server - server : the IP address of the remote server - varanus_home: home directory of varanus project - cid : the collector identifier """ if self.enable_varanus: if self.built: raise RuntimeError( 'build() method was already called; cannot add any more collectors' ) name = as_str( name ) storage = self.hosts check_duplicate( storage, name ) c = self._new_remote_node( RemoteCollectorConfig, name, user, server, \ varanus_home=varanus_home, cid=cid, \ **params ) storage[ name ] = c info( newline( '=== Added remote VARANUS collector', c ) ) return c else: return self.add_null_host( name )
def add_remote_collector(self, c_name, user, server, varanus_home, cid, **params): c_name = as_str(c_name, name='c_name') cid = as_int(cid, name='cid') self.__add_remote_collector(c_name, user, server, varanus_home, cid, **params)
def register_command(self, name, cmd): """ Registers a new command to be available when command line is instantiated. - name: the name of the command - cmd : the function to be called upon command execution """ self.cmds['do_' + as_str(name)] = as_callable(cmd)
def add_pica8_switch(self, sw_name, user, server, dpid, **params): sw_name = as_str(sw_name, name='sw_name') dpid = as_int(dpid, minim=0, name='dpid') sw = NodeUtils.add_pica8_switch(self.mr, sw_name, user, server, dpid, **params) self.switches[sw_name] = sw
def __init__(self, name, ip=None, port=None, **params): super(ControllerConfig, self).__init__(name, ip=ip, port=port, **params) self.ip = as_str(ip, name='ip') if is_some(ip) else None self.port = as_int(port, minim=1, maxim=65535, name='port') if is_some(port) else None
def _new_node(self, node_builder, name, **params): node_builder = as_callable(node_builder, name='node_builder') num = self._next_node_num() name = as_str( name, name='name') if is_some(name) else self._next_node_name(num) return as_a(node_builder(self.ringnum, num, name, **params), instance_of=NodeConfig, name='created node')
def add_console_command( self, name, cmd ): """ Adds a named command to be available to call when interacting with Mininet. - name: the name of the command - cmd : the function to be called when the command is executed """ name = as_str( name ) info ( newline( '=== Registered console command', '"' + name + '"' ) ) self.cli.register_command( name, cmd )
def add_local_collector(self, c_name, cid, **params): varanus_home = self.local_varanus_home if not is_some(varanus_home): raise RuntimeError( 'VARANUS home is not defined, cannot add collector') c_name = as_str(c_name, name='c_name') cid = as_int(cid, name='cid') self.__add_local_collector(c_name, varanus_home, cid, **params)
def add_phy_collector_link(self, c_name, c_portname, c_portnum, sw_name, sw_portname, sw_portnum): c_name = as_oneof(c_name, self.collectors.keys(), valname='c_name', containername='collector names') sw_name = as_oneof(sw_name, self.switches.keys(), valname='sw_name', containername='switch names') c_portname = as_str(c_portname, name='c_portname') sw_portname = as_str(sw_portname, name='sw_portname') c_portnum = as_int(c_portnum, minim=0, name='c_portnum') sw_portnum = as_int(sw_portnum, minim=0, name='sw_portnum') c_port_cfg = NodeUtils.get_generic_phy_port_cfg(c_portname, c_portnum) sw_port_cfg = NodeUtils.get_switch_to_collector_phy_port_cfg( sw_portname, sw_portnum) self.__add_collector_link(c_name, c_port_cfg, sw_name, sw_port_cfg)
def __init__( self, name, user, server, \ startcmd, stopcmd, morecmds=None, **params ): self.user = as_str(user) self.server = resolve(server) params['user'] = self.user params['server'] = self.server super( CustomRemoteControllerConfig, self ).__init__( name, \ startcmd=startcmd, \ stopcmd=stopcmd, \ morecmds=morecmds, \ **params )
def add_phy_switch_link(self, sw_name1, sw_portname1, sw_portnum1, sw_name2, sw_portname2, sw_portnum2): sw_name1 = as_oneof(sw_name1, self.switches.keys(), valname='sw_name1', containername='switch names') sw_name2 = as_oneof(sw_name2, self.switches.keys(), valname='sw_name2', containername='switch names') sw_portname1 = as_str(sw_portname1, name='sw_portname1') sw_portname2 = as_str(sw_portname2, name='sw_portname2') sw_portnum1 = as_int(sw_portnum1, minim=0, name='sw_portnum1') sw_portnum2 = as_int(sw_portnum2, minim=0, name='sw_portnum2') port_cfg1 = NodeUtils.get_generic_phy_port_cfg(sw_portname1, sw_portnum1) port_cfg2 = NodeUtils.get_generic_phy_port_cfg(sw_portname2, sw_portnum2) self.__add_switch_link(sw_name1, port_cfg1, sw_name2, port_cfg2)
def __init__(self, name, startcmd, stopcmd, morecmds=None, **params): super(CustomMixin, self).__init__(name, **params) self.startcmd = as_callable(startcmd) self.stopcmd = as_callable(stopcmd) if is_some(morecmds): if is_mapping(morecmds): for method_name, cmd in morecmds.iteritems(): cmd = as_callable(cmd, name='cmd') method_name = as_str(method_name, name='method_name') method = lambda *args, **kwargs: cmd(self, *args, **kwargs) setattr(self, method_name, method) elif is_iterable(morecmds): for cmd in morecmds: cmd = as_callable(cmd, name='cmd') method_name = as_str(cmd.__name__, name='cmd.__name__') method = lambda *args, **kwargs: cmd(self, *args, **kwargs) setattr(self, method_name, method) else: raise ValueError('Invalid optional methods') # to prevent repeated calls to start/stop self._is_active = False
def add_phy_host_link(self, h_name, h_portname, h_portnum, h_port_ip, h_port_mac, sw_name, sw_portname, sw_portnum): h_name = as_oneof(h_name, self.hosts.keys(), valname='h_name', containername='host names') sw_name = as_oneof(sw_name, self.switches.keys(), valname='sw_name', containername='switch names') h_portname = as_str(h_portname, name='h_portname') h_portnum = as_int(h_portnum, minim=0, name='h_portnum') h_port_ip = as_str(h_port_ip, name='h_port_ip') h_port_mac = as_str(h_port_mac, name='h_port_mac') if is_some(h_port_mac) else None sw_portname = as_str(sw_portname, name='sw_portname') sw_portnum = as_int(sw_portnum, minim=0, name='sw_portnum') h_port_cfg = NodeUtils.get_host_phy_port_cfg(h_portname, h_portnum, h_port_ip, h_port_mac) sw_port_cfg = NodeUtils.get_generic_phy_port_cfg( sw_portname, sw_portnum) self.__add_host_link(h_name, h_port_cfg, sw_name, sw_port_cfg)
def add_local_host( self, name, **params ): """ Adds a new local host to the current topology and returns a LocalHostConfig object representing it. - name: a textual representation of the host """ if self.built: raise RuntimeError( 'build() method was already called; cannot add any more hosts' ) name = as_str( name ) storage = self.hosts check_duplicate( storage, name ) h = self._new_node( LocalHostConfig, name, **params ) storage[ name ] = h info( newline( '=== Added local host', h ) ) return h
def add_null_controller( self, name, **params ): """ Adds a dummy non-existent controller to the current topology and returns a NullControllerConfig object representing it. - name: a textual representation of the controller """ if self.built: raise RuntimeError( 'build() method was already called; cannot add any more controllers' ) name = as_str( name ) storage = self.controllers check_duplicate( storage, name ) c = self._new_node( NullControllerConfig, name, **params ) storage[ name ] = c info( newline( '=== Added null controller', c ) ) return c
def add_null_switch( self, name, **params ): """ Adds a dummy non-existent switch to the current topology and returns a NullNodeConfig object representing it. - name: a textual representation of the switch """ if self.built: raise RuntimeError( 'build() method was already called; cannot add any more switches' ) name = as_str( name ) storage = self.switches check_duplicate( storage, name ) s = self._new_node( NullNodeConfig, name, **params ) storage[ name ] = s info( newline( '=== Added null switch', s ) ) return s
def add_vir_host_link(self, h_name, h_portnum, h_port_ip, sw_name, sw_portnum): h_name = as_oneof(h_name, self.hosts.keys(), valname='h_name', containername='host names') sw_name = as_oneof(sw_name, self.switches.keys(), valname='sw_name', containername='switch names') h_portnum = as_int(h_portnum, minim=0, name='h_portnum') h_port_ip = as_str(h_port_ip, name='h_port_ip') sw_portnum = as_int(sw_portnum, minim=0, name='sw_portnum') h_port_cfg = NodeUtils.get_host_vir_port_cfg(h_portnum, h_port_ip) sw_port_cfg = NodeUtils.get_generic_vir_port_cfg(sw_portnum) self.__add_host_link(h_name, h_port_cfg, sw_name, sw_port_cfg)
def add_local_ovs_switch( self, name, **params ): """ Adds a new local OVS switch to the current topology and returns a LocalOVSSwitchConfig object representing it. - name: a textual representation of the switch """ if self.built: raise RuntimeError( 'build() method was already called; cannot add any more switches' ) name = as_str( name ) storage = self.switches check_duplicate( storage, name ) s = self._new_node( LocalOVSSwitchConfig, name, \ protocols=self.of_version, **params ) storage[ name ] = s info( newline( '=== Added local OVS switch', s ) ) return s
def __init__( self, num, node, name=None, cls=VirIntf, mac=None, ip=None, \ **params ): """ Constructs a new PortConfig object. - num : the port number - node: the node that owns this port - name: the port name (optional) - cls : the interface class (default is VirIntf) - mac : the MAC address for the interface - ip : the IP address for the interface """ self.num = as_int(num) self.node = some(node) self.name = as_str(fallback(name, '{0}-e{1}'.format(node.name, num))) self.cls = as_a(some(cls), subclass_of=Intf) self.mac = mac self.ip = ip params['ip'] = ip self.params = params self._peer = None # linked PortConfig
def add_remote_host( self, name, user, server, **params ): """ Adds a new remote host to the current topology and returns a RemoteHostConfig object representing it. - name : a textual representation of the host - user : the name of the user on the remote server - server: the IP address of the remote server """ if self.built: raise RuntimeError( 'build() method was already called; cannot add any more hosts' ) name = as_str( name ) storage = self.hosts check_duplicate( storage, name ) h = self._new_remote_node( RemoteHostConfig, name, user, server, **params ) storage[ name ] = h info( newline( '=== Added remote host', h ) ) return h
def add_ring_remote_vir_collector(self, c_name, user, server, varanus_home, cid, ringnum, **params): c_name = as_str(c_name, name='c_name') cid = as_int(cid, name='cid') ringnum = as_oneof(ringnum, self.rings.keys(), valname='ringnum', containername='ring numbers') ring = self.rings[ringnum] self.__add_remote_collector(c_name, user, server, varanus_home, cid, **params) c_port_num = 1 sw_port_cfg = RingPorts.ring_to_collector_vir() for sw in ring.get_nodes(): c_port_cfg = NodeUtils.get_generic_vir_port_cfg(c_port_num) self.__add_collector_link(c_name, c_port_cfg, sw.name, sw_port_cfg) c_port_num += 1
def __init__(self, ringnum, prefix='n', port1_cfg=None, port2_cfg=None, link_builder=None): self.ringnum = as_int(ringnum, minim=0, name='ringnum') self.prefix = as_str(prefix, allow_empty=True, name='prefix') self.port1_cfg = as_a(port1_cfg, instance_of=PortConfig, name='port1_cfg') if is_some(port1_cfg) else None self.port2_cfg = as_a(port2_cfg, instance_of=PortConfig, name='port2_cfg') if is_some(port2_cfg) else None self.link_builder = as_callable( link_builder, name='link_builder') if is_some(link_builder) else None self.nodes = [] self.links = []
def add_pica8_switch( self, name, user, server, **params ): """ Adds a new pica8 switch (remote) to the current topology and returns a Pica8SwitchConfig object representing it. - name : a textual representation of the switch - user : the name of the user on the remote server - server: the IP address of the remote server """ if self.built: raise RuntimeError( 'build() method was already called; cannot add any more switches' ) name = as_str( name ) storage = self.switches check_duplicate( storage, name ) s = self._new_remote_node( Pica8SwitchConfig, name, user, server, \ protocols=self.of_version, \ **params ) storage[ name ] = s info( newline( '=== Added Pica8 switch', s ) ) return s
def add_ring_local_vir_collector(self, c_name, cid, ringnum, **params): varanus_home = self.local_varanus_home if not is_some(varanus_home): raise RuntimeError( 'VARANUS home is not defined, cannot add collector') c_name = as_str(c_name, name='c_name') cid = as_int(cid, name='cid') ringnum = as_oneof(ringnum, self.rings.keys(), valname='ringnum', containername='ring numbers') ring = self.rings[ringnum] self.__add_local_collector(c_name, varanus_home, cid, **params) c_port_num = 1 sw_port_cfg = RingPorts.ring_to_collector_vir() for sw in ring.get_nodes(): c_port_cfg = NodeUtils.get_generic_vir_port_cfg(c_port_num) self.__add_collector_link(c_name, c_port_cfg, sw.name, sw_port_cfg) c_port_num += 1
def add_custom_local_controller( self, name, startcmd, stopcmd, morecmds=None, \ **params ): """ Adds a custom local controller to the current topology and returns a CustomLocalControllerConfig object representing it. - name : a textual representation of the controller - startcmd: a function used to start the controller when called - stopcmd : a function used to stop the controller when called - morecmds: additional optional commands to define in the Mininet node """ if self.built: raise RuntimeError( 'build() method was already called; cannot add any more controllers' ) name = as_str( name ) storage = self.controllers check_duplicate( storage, name ) c = self._new_node( CustomLocalControllerConfig, name, \ startcmd=startcmd, stopcmd=stopcmd, morecmds=morecmds, \ **params ) storage[ name ] = c info( newline( '=== Added custom local controller', c ) ) return c
def add_local_varanus_sdncontroller( self, name, varanus_home, sudo=None, **params ): """ Adds a local VARANUS SDN controller to the current topology and returns a LocalSDNControllerConfig object representing it, or a NullControllerConfig if VARANUS usage is disabled. - name : a textual representation of the controller - varanus_home: home directory of varanus project """ if self.enable_varanus: if self.built: raise RuntimeError( 'build() method was already called; cannot add any more controllers' ) name = as_str( name ) storage = self.controllers check_duplicate( storage, name ) c = self._new_node( LocalSDNControllerConfig, name, \ varanus_home=varanus_home, sudo=sudo, \ **params ) storage[ name ] = c info( newline( '=== Added local VARANUS SDN controller', c ) ) return c else: return self.add_null_controller( name )
def add_ring_remote_vir_host(self, h_name, user, server, h_portnum, h_port_ip, ringnum, sw_name, **params): h_name = some(h_name, name='h_name') h_portnum = as_int(h_portnum, minim=0, name='h_portnum') h_port_ip = as_str(h_port_ip, name='h_port_ip') ringnum = as_oneof(ringnum, self.rings.keys(), valname='ringnum', containername='ring numbers') sw_name = as_oneof(sw_name, self.switches.keys(), valname='sw_name', containername='switch names') ring = self.rings[ringnum] if not any(s.name == sw_name for s in ring.get_nodes()): raise ValueError('no switch named "{}" exists in ring {}'.format( sw_name, ringnum)) self.__add_remote_vir_host(h_name, user, server, **params) h_port_cfg = NodeUtils.get_host_vir_port_cfg(h_portnum, h_port_ip) sw_port_cfg = RingPorts.ring_to_host_vir() self.__add_host_link(h_name, h_port_cfg, sw_name, sw_port_cfg)