<s> from __future__ import print_function <EOL> import ryu . contrib <EOL> ryu . contrib . update_module_path ( ) <EOL> from ryu import cfg <EOL> import cmd <EOL> import sys <EOL> import lxml . etree as ET <EOL> from ryu . lib import of_config <EOL> from ryu . lib . of_config import capable_switch <EOL> from ncclient . operations . rpc import RPCError <EOL> import ryu . lib . of_config . classes as ofc <EOL> CONF = cfg . CONF <EOL> CONF . register_cli_opts ( [ <EOL> cfg . ListOpt ( '<STR_LIT>' , default = [ ] , help = '<STR_LIT>' ) <EOL> ] ) <EOL> class Peer ( capable_switch . OFCapableSwitch ) : <EOL> def __init__ ( self , name , host , port , username , password ) : <EOL> self . _name = name <EOL> super ( Peer , self ) . __init__ ( <EOL> host = host , port = port , username = username , password = password , <EOL> unknown_host_cb = lambda host , fingeprint : True ) <EOL> peers = { } <EOL> def add_peer ( name , host , port , username , password ) : <EOL> peers [ name ] = Peer ( name , host , port , username , password ) <EOL> def et_tostring_pp ( tree ) : <EOL> try : <EOL> return ET . tostring ( tree , pretty_print = True ) <EOL> except TypeError : <EOL> return ET . tostring ( tree ) <EOL> def validate ( tree ) : <EOL> schema = ET . XMLSchema ( file = of_config . OF_CONFIG_1_1_1_XSD ) <EOL> if not schema ( tree ) : <EOL> print ( schema . error_log ) <EOL> class Cmd ( cmd . Cmd ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . _in_onecmd = False <EOL> cmd . Cmd . __init__ ( self , * args , ** kwargs ) <EOL> def _request ( self , line , f ) : <EOL> args = line . split ( ) <EOL> try : <EOL> peer = args [ <NUM_LIT:0> ] <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> try : <EOL> p = peers [ peer ] <EOL> except KeyError : <EOL> print ( "<STR_LIT>" % peer ) <EOL> return <EOL> try : <EOL> f ( p , args [ <NUM_LIT:1> : ] ) <EOL> except RPCError as e : <EOL> print ( "<STR_LIT>" % e ) <EOL> except EOFError : <EOL> print ( "<STR_LIT>" ) <EOL> def _complete_peer ( self , text , line , _begidx , _endidx ) : <EOL> if len ( ( line + '<STR_LIT:x>' ) . split ( ) ) >= <NUM_LIT:3> : <EOL> return [ ] <EOL> return [ name for name in peers if name . startswith ( text ) ] <EOL> def do_list_cap ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> for i in p . netconf . server_capabilities : <EOL> print ( i ) <EOL> self . _request ( line , f ) <EOL> def do_raw_get ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> result = p . raw_get ( ) <EOL> tree = ET . fromstring ( result ) <EOL> validate ( tree ) <EOL> print ( et_tostring_pp ( tree ) ) <EOL> self . _request ( line , f ) <EOL> def do_raw_get_config ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> try : <EOL> source = args [ <NUM_LIT:0> ] <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> result = p . raw_get_config ( source ) <EOL> tree = ET . fromstring ( result ) <EOL> validate ( tree ) <EOL> print ( et_tostring_pp ( tree ) ) <EOL> self . _request ( line , f ) <EOL> def do_get ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> print ( p . get ( ) ) <EOL> self . _request ( line , f ) <EOL> def do_commit ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> print ( p . commit ( ) ) <EOL> self . _request ( line , f ) <EOL> def do_discard ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> print ( p . discard_changes ( ) ) <EOL> self . _request ( line , f ) <EOL> def do_get_config ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> try : <EOL> source = args [ <NUM_LIT:0> ] <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> print ( p . get_config ( source ) ) <EOL> self . _request ( line , f ) <EOL> def do_delete_config ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> try : <EOL> source = args [ <NUM_LIT:0> ] <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> print ( p . delete_config ( source ) ) <EOL> self . _request ( line , f ) <EOL> def do_copy_config ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> try : <EOL> source , target = args <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> print ( p . copy_config ( source , target ) ) <EOL> self . _request ( line , f ) <EOL> def do_list_port ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> o = p . get ( ) <EOL> for p in o . resources . port : <EOL> print ( '<STR_LIT>' % ( p . resource_id , p . name , p . number ) ) <EOL> self . _request ( line , f ) <EOL> _port_settings = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def do_get_port_config ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> try : <EOL> source , port = args <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> o = p . get_config ( source ) <EOL> for p in o . resources . port : <EOL> if p . resource_id != port : <EOL> continue <EOL> print ( p . resource_id ) <EOL> conf = p . configuration <EOL> for k in self . _port_settings : <EOL> try : <EOL> v = getattr ( conf , k ) <EOL> except AttributeError : <EOL> continue <EOL> print ( '<STR_LIT>' % ( k , v ) ) <EOL> self . _request ( line , f ) <EOL> def do_set_port_config ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> try : <EOL> target , port , key , value = args <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> print ( args ) <EOL> return <EOL> o = p . get ( ) <EOL> capable_switch_id = o . id <EOL> try : <EOL> capable_switch = ofc . OFCapableSwitchType ( <EOL> id = capable_switch_id , <EOL> resources = ofc . OFCapableSwitchResourcesType ( <EOL> port = [ <EOL> ofc . OFPortType ( <EOL> resource_id = port , <EOL> configuration = ofc . OFPortConfigurationType ( <EOL> ** { key : value } ) ) <EOL> ] <EOL> ) <EOL> ) <EOL> except TypeError : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> try : <EOL> p . edit_config ( target , capable_switch ) <EOL> except Exception as e : <EOL> print ( e ) <EOL> self . _request ( line , f ) <EOL> def do_list_queue ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> o = p . get ( ) <EOL> if o . resources . queue : <EOL> for q in o . resources . queue : <EOL> print ( '<STR_LIT>' % ( q . resource_id , q . port ) ) <EOL> self . _request ( line , f ) <EOL> _queue_settings = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def do_get_queue_config ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> try : <EOL> source , queue = args <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> o = p . get_config ( source ) <EOL> for q in o . resources . queue : <EOL> if q . resource_id != queue : <EOL> continue <EOL> print ( q . resource_id ) <EOL> conf = q . properties <EOL> for k in self . _queue_settings : <EOL> try : <EOL> v = getattr ( conf , k ) <EOL> except AttributeError : <EOL> continue <EOL> print ( '<STR_LIT>' % ( k , v ) ) <EOL> self . _request ( line , f ) <EOL> def do_set_queue_config ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> try : <EOL> target , queue , key , value = args <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> print ( args ) <EOL> return <EOL> o = p . get ( ) <EOL> capable_switch_id = o . id <EOL> try : <EOL> capable_switch = ofc . OFCapableSwitchType ( <EOL> id = capable_switch_id , <EOL> resources = ofc . OFCapableSwitchResourcesType ( <EOL> queue = [ <EOL> ofc . OFQueueType ( <EOL> resource_id = queue , <EOL> properties = ofc . OFQueuePropertiesType ( <EOL> ** { key : value } ) ) , <EOL> ] <EOL> ) <EOL> ) <EOL> except TypeError : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> try : <EOL> p . edit_config ( target , capable_switch ) <EOL> except Exception as e : <EOL> print ( e ) <EOL> self . _request ( line , f ) <EOL> def do_add_queue ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> try : <EOL> target , lsw , queue = args <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> print ( args ) <EOL> return <EOL> o = p . get ( ) <EOL> capable_switch_id = o . id <EOL> try : <EOL> capable_switch = ofc . OFCapableSwitchType ( <EOL> id = capable_switch_id , <EOL> resources = ofc . OFCapableSwitchResourcesType ( <EOL> queue = [ <EOL> ofc . OFQueueType ( resource_id = queue ) <EOL> ] <EOL> ) , <EOL> logical_switches = ofc . OFCapableSwitchLogicalSwitchesType ( <EOL> switch = [ ofc . OFLogicalSwitchType ( <EOL> id = lsw , <EOL> resources = ofc . OFLogicalSwitchResourcesType ( <EOL> queue = [ queue ] ) <EOL> ) ] <EOL> ) <EOL> ) <EOL> except TypeError : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> try : <EOL> p . edit_config ( target , capable_switch ) <EOL> except Exception as e : <EOL> print ( e ) <EOL> self . _request ( line , f ) <EOL> def do_list_logical_switch ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> o = p . get ( ) <EOL> for s in o . logical_switches . switch : <EOL> print ( '<STR_LIT>' % ( s . id , s . datapath_id ) ) <EOL> self . _request ( line , f ) <EOL> def do_show_logical_switch ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> try : <EOL> ( lsw , ) = args <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> o = p . get ( ) <EOL> for s in o . logical_switches . switch : <EOL> if s . id != lsw : <EOL> continue <EOL> print ( s . id ) <EOL> print ( '<STR_LIT>' % s . datapath_id ) <EOL> if s . resources . queue : <EOL> print ( '<STR_LIT>' ) <EOL> for q in s . resources . queue : <EOL> print ( '<STR_LIT>' % q ) <EOL> if s . resources . port : <EOL> print ( '<STR_LIT>' ) <EOL> for p in s . resources . port : <EOL> print ( '<STR_LIT>' % p ) <EOL> self . _request ( line , f ) <EOL> _lsw_settings = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> def do_get_logical_switch_config ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> try : <EOL> source , lsw = args <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> o = p . get_config ( source ) <EOL> for l in o . logical_switches . switch : <EOL> if l . id != lsw : <EOL> continue <EOL> print ( l . id ) <EOL> for k in self . _lsw_settings : <EOL> try : <EOL> v = getattr ( l , k ) <EOL> except AttributeError : <EOL> continue <EOL> print ( '<STR_LIT>' % ( k , v ) ) <EOL> self . _request ( line , f ) <EOL> def do_set_logical_switch_config ( self , line ) : <EOL> """<STR_LIT>""" <EOL> def f ( p , args ) : <EOL> try : <EOL> target , lsw , key , value = args <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> o = p . get_config ( target ) <EOL> capable_switch_id = o . id <EOL> try : <EOL> capable_switch = ofc . OFCapableSwitchType ( <EOL> id = capable_switch_id , <EOL> logical_switches = ofc . OFCapableSwitchLogicalSwitchesType ( <EOL> switch = [ ofc . OFLogicalSwitchType ( <EOL> id = lsw , <EOL> ** { key : value } <EOL> ) ] <EOL> ) <EOL> ) <EOL> except TypeError : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> try : <EOL> p . edit_config ( target , capable_switch ) <EOL> except Exception as e : <EOL> print ( e ) <EOL> self . _request ( line , f ) <EOL> completedefault = _complete_peer <EOL> def complete_EOF ( self , _text , _line , _begidx , _endidx ) : <EOL> return [ ] <EOL> def do_EOF ( self , _line ) : <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> def onecmd ( self , string ) : <EOL> self . _in_onecmd = True <EOL> try : <EOL> return cmd . Cmd . onecmd ( self , string ) <EOL> finally : <EOL> self . _in_onecmd = False <EOL> def main ( args = None , prog = None ) : <EOL> CONF ( args = args , prog = prog , <EOL> project = '<STR_LIT>' , version = '<STR_LIT>' ) <EOL> for p_str in CONF . peers : <EOL> name , addr = p_str . split ( '<STR_LIT:=>' ) <EOL> host , port , username , password = addr . rsplit ( '<STR_LIT::>' , <NUM_LIT:3> ) <EOL> add_peer ( name , host , port , username , password ) <EOL> Cmd ( ) . cmdloop ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
