#NEWLINE# def get_of_switches( self ) : #NEWLINE# #INDENT# ' Obtain a a list of switches or DPID detected by this controller\n            Return\n                >=0, list:      list length, and a list where each element a tuple pair (DPID, IP address)\n                <0, text_error: if fails\n        ' #NEWLINE# try : #NEWLINE# #INDENT# of_response = requests . get ( ( self . url + '/restconf/operational/opendaylight-inventory:nodes' ) , headers = self . headers ) #NEWLINE# error_text = ( 'Openflow response %d: %s' % ( of_response . status_code , of_response . text ) ) #NEWLINE# if ( of_response . status_code != 200 ) : #NEWLINE# #INDENT# self . logger . warning ( ( 'get_of_switches ' + error_text ) ) #NEWLINE# return ( ( - 1 ) , error_text ) #NEWLINE# #UNINDENT# self . logger . debug ( ( 'get_of_switches ' + error_text ) ) #NEWLINE# info = of_response . json ( ) #NEWLINE# if ( type ( info ) != dict ) : #NEWLINE# #INDENT# self . logger . error ( 'get_of_switches. Unexpected response, not a dict: %s' , str ( info ) ) #NEWLINE# return ( ( - 1 ) , 'Unexpected response, not a dict. Wrong version?' ) #NEWLINE# #UNINDENT# nodes = info . get ( 'nodes' ) #NEWLINE# if ( type ( nodes ) is not dict ) : #NEWLINE# #INDENT# self . logger . error ( "get_of_switches. Unexpected response at 'nodes', not found or not a dict: %s" , str ( type ( info ) ) ) #NEWLINE# return ( ( - 1 ) , "Unexpected response at 'nodes', not found or not a dict. Wrong version?" ) #NEWLINE# #UNINDENT# node_list = nodes . get ( 'node' ) #NEWLINE# if ( type ( node_list ) is not list ) : #NEWLINE# #INDENT# self . logger . error ( "get_of_switches. Unexpected response, at 'nodes':'node', not found or not a list: %s" , str ( type ( node_list ) ) ) #NEWLINE# return ( ( - 1 ) , "Unexpected response, at 'nodes':'node', not found or not a list. Wrong version?" ) #NEWLINE# #UNINDENT# switch_list = [ ] #NEWLINE# for node in node_list : #NEWLINE# #INDENT# node_id = node . get ( 'id' ) #NEWLINE# if ( node_id is None ) : #NEWLINE# #INDENT# self . logger . error ( "get_of_switches. Unexpected response at 'nodes':'node'[]:'id', not found: %s" , str ( node ) ) #NEWLINE# return ( ( - 1 ) , "Unexpected response at 'nodes':'node'[]:'id', not found . Wrong version?" ) #NEWLINE# #UNINDENT# if ( node_id == 'controller-config' ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# node_ip_address = node . get ( 'flow-node-inventory:ip-address' ) #NEWLINE# if ( node_ip_address is None ) : #NEWLINE# #INDENT# self . logger . error ( "get_of_switches. Unexpected response at 'nodes':'node'[]:'flow-node-inventory:ip-address', not found: %s" , str ( node ) ) #NEWLINE# return ( ( - 1 ) , "Unexpected response at 'nodes':'node'[]:'flow-node-inventory:ip-address', not found. Wrong version?" ) #NEWLINE# #UNINDENT# node_id_hex = hex ( int ( node_id . split ( ':' ) [ 1 ] ) ) . split ( 'x' ) [ 1 ] . zfill ( 16 ) #NEWLINE# switch_list . append ( ( ':' . join ( ( ( a + b ) for ( a , b ) in zip ( node_id_hex [ : : 2 ] , node_id_hex [ 1 : : 2 ] ) ) ) , node_ip_address ) ) #NEWLINE# #UNINDENT# return ( len ( switch_list ) , switch_list ) #NEWLINE# #UNINDENT# except ( requests . exceptions . RequestException , ValueError ) as e : #NEWLINE# #INDENT# error_text = ( ( type ( e ) . __name__ + ': ' ) + str ( e ) ) #NEWLINE# self . logger . error ( ( 'get_of_switches ' + error_text ) ) #NEWLINE# return ( ( - 1 ) , error_text )