#NEWLINE# def new_flow( self , data ) : #NEWLINE# #INDENT# " Insert a new static rule\n            Params: data: dictionary with the following content:\n                priority:     rule priority\n                name:         rule name\n                ingress_port: match input port of the rule\n                dst_mac:      match destination mac address of the rule, missing or None if not apply\n                vlan_id:      match vlan tag of the rule, missing or None if not apply\n                actions:      list of actions, composed by a pair tuples with these posibilities:\n                    ('vlan', None/int): for stripping/setting a vlan tag\n                    ('out', port):      send to this port\n            Return\n                0, None if ok\n                -1, text_error if fails\n        " #NEWLINE# if ( len ( self . pp2ofi ) == 0 ) : #NEWLINE# #INDENT# ( r , c ) = self . obtain_port_correspondence ( ) #NEWLINE# if ( r < 0 ) : #NEWLINE# #INDENT# return ( r , c ) #NEWLINE# #UNINDENT# #UNINDENT# try : #NEWLINE# #INDENT# sdata = dict ( ) #NEWLINE# sdata [ 'flow-node-inventory:flow' ] = list ( ) #NEWLINE# sdata [ 'flow-node-inventory:flow' ] . append ( dict ( ) ) #NEWLINE# flow = sdata [ 'flow-node-inventory:flow' ] [ 0 ] #NEWLINE# flow [ 'id' ] = data [ 'name' ] #NEWLINE# flow [ 'flow-name' ] = data [ 'name' ] #NEWLINE# flow [ 'idle-timeout' ] = 0 #NEWLINE# flow [ 'hard-timeout' ] = 0 #NEWLINE# flow [ 'table_id' ] = 0 #NEWLINE# flow [ 'priority' ] = data . get ( 'priority' ) #NEWLINE# flow [ 'match' ] = dict ( ) #NEWLINE# if ( not ( data [ 'ingress_port' ] in self . pp2ofi ) ) : #NEWLINE# #INDENT# error_text = ( ( 'Error. Port ' + data [ 'ingress_port' ] ) + ' is not present in the switch' ) #NEWLINE# self . logger . warning ( ( 'new_flow ' + error_text ) ) #NEWLINE# return ( ( - 1 ) , error_text ) #NEWLINE# #UNINDENT# flow [ 'match' ] [ 'in-port' ] = self . pp2ofi [ data [ 'ingress_port' ] ] #NEWLINE# if ( 'dst_mac' in data ) : #NEWLINE# #INDENT# flow [ 'match' ] [ 'ethernet-match' ] = dict ( ) #NEWLINE# flow [ 'match' ] [ 'ethernet-match' ] [ 'ethernet-destination' ] = dict ( ) #NEWLINE# flow [ 'match' ] [ 'ethernet-match' ] [ 'ethernet-destination' ] [ 'address' ] = data [ 'dst_mac' ] #NEWLINE# #UNINDENT# if data . get ( 'vlan_id' ) : #NEWLINE# #INDENT# flow [ 'match' ] [ 'vlan-match' ] = dict ( ) #NEWLINE# flow [ 'match' ] [ 'vlan-match' ] [ 'vlan-id' ] = dict ( ) #NEWLINE# flow [ 'match' ] [ 'vlan-match' ] [ 'vlan-id' ] [ 'vlan-id-present' ] = True #NEWLINE# flow [ 'match' ] [ 'vlan-match' ] [ 'vlan-id' ] [ 'vlan-id' ] = int ( r [ 'vlan_id' ] ) #NEWLINE# #UNINDENT# flow [ 'instructions' ] = dict ( ) #NEWLINE# flow [ 'instructions' ] [ 'instruction' ] = list ( ) #NEWLINE# flow [ 'instructions' ] [ 'instruction' ] . append ( dict ( ) ) #NEWLINE# flow [ 'instructions' ] [ 'instruction' ] [ 0 ] [ 'order' ] = 1 #NEWLINE# flow [ 'instructions' ] [ 'instruction' ] [ 0 ] [ 'apply-actions' ] = dict ( ) #NEWLINE# flow [ 'instructions' ] [ 'instruction' ] [ 0 ] [ 'apply-actions' ] [ 'action' ] = list ( ) #NEWLINE# actions = flow [ 'instructions' ] [ 'instruction' ] [ 0 ] [ 'apply-actions' ] [ 'action' ] #NEWLINE# order = 0 #NEWLINE# for action in data [ 'actions' ] : #NEWLINE# #INDENT# new_action = { #NEWLINE# #INDENT# 'order' : order , #NEWLINE# #UNINDENT# } #NEWLINE# if ( action [ 0 ] == 'vlan' ) : #NEWLINE# #INDENT# if ( action [ 1 ] == None ) : #NEWLINE# #INDENT# new_action [ 'strip-vlan-action' ] = dict ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# new_action [ 'set-field' ] = dict ( ) #NEWLINE# new_action [ 'set-field' ] [ 'vlan-match' ] = dict ( ) #NEWLINE# new_action [ 'set-field' ] [ 'vlan-match' ] [ 'vlan-id' ] = dict ( ) #NEWLINE# new_action [ 'set-field' ] [ 'vlan-match' ] [ 'vlan-id' ] [ 'vlan-id-present' ] = True #NEWLINE# new_action [ 'set-field' ] [ 'vlan-match' ] [ 'vlan-id' ] [ 'vlan-id' ] = int ( action [ 1 ] ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( action [ 0 ] == 'out' ) : #NEWLINE# #INDENT# new_action [ 'output-action' ] = dict ( ) #NEWLINE# if ( not ( action [ 1 ] in self . pp2ofi ) ) : #NEWLINE# #INDENT# error_msj = ( ( 'Port ' + action [ 1 ] ) + ' is not present in the switch' ) #NEWLINE# return ( ( - 1 ) , error_msj ) #NEWLINE# #UNINDENT# new_action [ 'output-action' ] [ 'output-node-connector' ] = self . pp2ofi [ action [ 1 ] ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# error_msj = ( "Unknown item '%s' in action list" % action [ 0 ] ) #NEWLINE# self . logger . error ( ( 'new_flow ' + error_msj ) ) #NEWLINE# return ( ( - 1 ) , error_msj ) #NEWLINE# #UNINDENT# #UNINDENT# actions . append ( new_action ) #NEWLINE# order += 1 #NEWLINE# #UNINDENT# of_response = requests . put ( ( ( ( ( self . url + '/restconf/config/opendaylight-inventory:nodes/node/' ) + self . id ) + '/table/0/flow/' ) + data [ 'name' ] ) , headers = self . headers , data = json . dumps ( sdata ) ) #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 ( ( 'new_flow ' + error_text ) ) #NEWLINE# return ( ( - 1 ) , error_text ) #NEWLINE# #UNINDENT# self . logger . debug ( ( 'new_flow OK ' + error_text ) ) #NEWLINE# return ( 0 , None ) #NEWLINE# #UNINDENT# except requests . exceptions . RequestException as e : #NEWLINE# #INDENT# error_text = ( ( type ( e ) . __name__ + ': ' ) + str ( e ) ) #NEWLINE# self . logger . error ( ( 'new_flow ' + error_text ) ) #NEWLINE# return ( ( - 1 ) , error_text )