<s> import collections <EOL> import itertools <EOL> import operator <EOL> import time <EOL> import uuid <EOL> from neutron_lib import exceptions <EOL> from oslo_config import cfg <EOL> from oslo_log import log as logging <EOL> from oslo_utils import excutils <EOL> import retrying <EOL> import six <EOL> from neutron . _i18n import _ , _LE , _LI , _LW <EOL> from neutron . agent . common import utils <EOL> from neutron . agent . linux import ip_lib <EOL> from neutron . agent . ovsdb import api as ovsdb <EOL> from neutron . plugins . common import constants as p_const <EOL> from neutron . plugins . ml2 . drivers . openvswitch . agent . common import constants <EOL> UINT64_BITMASK = ( <NUM_LIT:1> << <NUM_LIT:64> ) - <NUM_LIT:1> <EOL> DEFAULT_OVS_VSCTL_TIMEOUT = <NUM_LIT:10> <EOL> INVALID_OFPORT = - <NUM_LIT:1> <EOL> UNASSIGNED_OFPORT = [ ] <EOL> FAILMODE_SECURE = '<STR_LIT>' <EOL> FAILMODE_STANDALONE = '<STR_LIT>' <EOL> OPTS = [ <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = DEFAULT_OVS_VSCTL_TIMEOUT , <EOL> help = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) , <EOL> ] <EOL> cfg . CONF . register_opts ( OPTS ) <EOL> LOG = logging . getLogger ( __name__ ) <EOL> OVS_DEFAULT_CAPS = { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> } <EOL> def _ofport_result_pending ( result ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> int ( result ) <EOL> return False <EOL> except ( ValueError , TypeError ) : <EOL> return True <EOL> def _ofport_retry ( fn ) : <EOL> """<STR_LIT>""" <EOL> @ six . wraps ( fn ) <EOL> def wrapped ( * args , ** kwargs ) : <EOL> self = args [ <NUM_LIT:0> ] <EOL> new_fn = retrying . retry ( <EOL> retry_on_result = _ofport_result_pending , <EOL> stop_max_delay = self . vsctl_timeout * <NUM_LIT:1000> , <EOL> wait_exponential_multiplier = <NUM_LIT:10> , <EOL> wait_exponential_max = <NUM_LIT:1000> , <EOL> retry_on_exception = lambda _ : False ) ( fn ) <EOL> return new_fn ( * args , ** kwargs ) <EOL> return wrapped <EOL> class VifPort ( object ) : <EOL> def __init__ ( self , port_name , ofport , vif_id , vif_mac , switch ) : <EOL> self . port_name = port_name <EOL> self . ofport = ofport <EOL> self . vif_id = vif_id <EOL> self . vif_mac = vif_mac <EOL> self . switch = switch <EOL> def __str__ ( self ) : <EOL> return ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % ( <EOL> self . vif_id , self . vif_mac , <EOL> self . port_name , self . ofport , <EOL> self . switch . br_name ) <EOL> class BaseOVS ( object ) : <EOL> def __init__ ( self ) : <EOL> self . vsctl_timeout = cfg . CONF . ovs_vsctl_timeout <EOL> self . ovsdb = ovsdb . API . get ( self ) <EOL> def add_bridge ( self , bridge_name , <EOL> datapath_type = constants . OVS_DATAPATH_SYSTEM ) : <EOL> self . ovsdb . add_br ( bridge_name , <EOL> datapath_type ) . execute ( ) <EOL> br = OVSBridge ( bridge_name ) <EOL> br . get_port_ofport ( bridge_name ) <EOL> return br <EOL> def delete_bridge ( self , bridge_name ) : <EOL> self . ovsdb . del_br ( bridge_name ) . execute ( ) <EOL> def bridge_exists ( self , bridge_name ) : <EOL> return self . ovsdb . br_exists ( bridge_name ) . execute ( ) <EOL> def port_exists ( self , port_name ) : <EOL> cmd = self . ovsdb . db_get ( '<STR_LIT>' , port_name , '<STR_LIT:name>' ) <EOL> return bool ( cmd . execute ( check_error = False , log_errors = False ) ) <EOL> def get_bridge_for_iface ( self , iface ) : <EOL> return self . ovsdb . iface_to_br ( iface ) . execute ( ) <EOL> def get_bridges ( self ) : <EOL> return self . ovsdb . list_br ( ) . execute ( check_error = True ) <EOL> def get_bridge_external_bridge_id ( self , bridge ) : <EOL> return self . ovsdb . br_get_external_id ( bridge , '<STR_LIT>' ) . execute ( ) <EOL> def set_db_attribute ( self , table_name , record , column , value , <EOL> check_error = False , log_errors = True ) : <EOL> self . ovsdb . db_set ( table_name , record , ( column , value ) ) . execute ( <EOL> check_error = check_error , log_errors = log_errors ) <EOL> def clear_db_attribute ( self , table_name , record , column ) : <EOL> self . ovsdb . db_clear ( table_name , record , column ) . execute ( ) <EOL> def db_get_val ( self , table , record , column , check_error = False , <EOL> log_errors = True ) : <EOL> return self . ovsdb . db_get ( table , record , column ) . execute ( <EOL> check_error = check_error , log_errors = log_errors ) <EOL> @ property <EOL> def config ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . ovsdb . db_list ( "<STR_LIT>" ) . execute ( ) [ <NUM_LIT:0> ] <EOL> @ property <EOL> def capabilities ( self ) : <EOL> _cfg = self . config <EOL> return { k : _cfg . get ( k , OVS_DEFAULT_CAPS [ k ] ) for k in OVS_DEFAULT_CAPS } <EOL> class OVSBridge ( BaseOVS ) : <EOL> def __init__ ( self , br_name , datapath_type = constants . OVS_DATAPATH_SYSTEM ) : <EOL> super ( OVSBridge , self ) . __init__ ( ) <EOL> self . br_name = br_name <EOL> self . datapath_type = datapath_type <EOL> self . _default_cookie = generate_random_cookie ( ) <EOL> @ property <EOL> def default_cookie ( self ) : <EOL> return self . _default_cookie <EOL> def set_agent_uuid_stamp ( self , val ) : <EOL> self . _default_cookie = val <EOL> def set_controller ( self , controllers ) : <EOL> self . ovsdb . set_controller ( self . br_name , <EOL> controllers ) . execute ( check_error = True ) <EOL> def del_controller ( self ) : <EOL> self . ovsdb . del_controller ( self . br_name ) . execute ( check_error = True ) <EOL> def get_controller ( self ) : <EOL> return self . ovsdb . get_controller ( self . br_name ) . execute ( <EOL> check_error = True ) <EOL> def _set_bridge_fail_mode ( self , mode ) : <EOL> self . ovsdb . set_fail_mode ( self . br_name , mode ) . execute ( check_error = True ) <EOL> def set_secure_mode ( self ) : <EOL> self . _set_bridge_fail_mode ( FAILMODE_SECURE ) <EOL> def set_standalone_mode ( self ) : <EOL> self . _set_bridge_fail_mode ( FAILMODE_STANDALONE ) <EOL> def set_protocols ( self , protocols ) : <EOL> self . set_db_attribute ( '<STR_LIT>' , self . br_name , '<STR_LIT>' , protocols , <EOL> check_error = True ) <EOL> def create ( self , secure_mode = False ) : <EOL> with self . ovsdb . transaction ( ) as txn : <EOL> txn . add ( <EOL> self . ovsdb . add_br ( self . br_name , <EOL> datapath_type = self . datapath_type ) ) <EOL> if secure_mode : <EOL> txn . add ( self . ovsdb . set_fail_mode ( self . br_name , <EOL> FAILMODE_SECURE ) ) <EOL> self . get_port_ofport ( self . br_name ) <EOL> def destroy ( self ) : <EOL> self . delete_bridge ( self . br_name ) <EOL> def add_port ( self , port_name , * interface_attr_tuples ) : <EOL> with self . ovsdb . transaction ( ) as txn : <EOL> txn . add ( self . ovsdb . add_port ( self . br_name , port_name ) ) <EOL> if interface_attr_tuples : <EOL> txn . add ( self . ovsdb . db_set ( '<STR_LIT>' , port_name , <EOL> * interface_attr_tuples ) ) <EOL> return self . get_port_ofport ( port_name ) <EOL> def replace_port ( self , port_name , * interface_attr_tuples ) : <EOL> """<STR_LIT>""" <EOL> self . ovsdb . del_port ( port_name ) . execute ( ) <EOL> with self . ovsdb . transaction ( ) as txn : <EOL> txn . add ( self . ovsdb . add_port ( self . br_name , port_name , <EOL> may_exist = False ) ) <EOL> if interface_attr_tuples : <EOL> txn . add ( self . ovsdb . db_set ( '<STR_LIT>' , port_name , <EOL> * interface_attr_tuples ) ) <EOL> self . get_port_ofport ( port_name ) <EOL> def delete_port ( self , port_name ) : <EOL> self . ovsdb . del_port ( port_name , self . br_name ) . execute ( ) <EOL> def run_ofctl ( self , cmd , args , process_input = None ) : <EOL> full_args = [ "<STR_LIT>" , cmd , self . br_name ] + args <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:11> ) : <EOL> try : <EOL> return utils . execute ( full_args , run_as_root = True , <EOL> process_input = process_input ) <EOL> except Exception as e : <EOL> if "<STR_LIT>" in str ( e ) : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , i ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> continue <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : full_args , '<STR_LIT>' : e } ) <EOL> break <EOL> def count_flows ( self ) : <EOL> flow_list = self . run_ofctl ( "<STR_LIT>" , [ ] ) . split ( "<STR_LIT:\n>" ) [ <NUM_LIT:1> : ] <EOL> return len ( flow_list ) - <NUM_LIT:1> <EOL> def remove_all_flows ( self ) : <EOL> self . run_ofctl ( "<STR_LIT>" , [ ] ) <EOL> @ _ofport_retry <EOL> def _get_port_ofport ( self , port_name ) : <EOL> return self . db_get_val ( "<STR_LIT>" , port_name , "<STR_LIT>" ) <EOL> def get_port_ofport ( self , port_name ) : <EOL> """<STR_LIT>""" <EOL> ofport = INVALID_OFPORT <EOL> try : <EOL> ofport = self . _get_port_ofport ( port_name ) <EOL> except retrying . RetryError : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) , <EOL> port_name ) <EOL> return ofport <EOL> def get_datapath_id ( self ) : <EOL> return self . db_get_val ( '<STR_LIT>' , <EOL> self . br_name , '<STR_LIT>' ) <EOL> def do_action_flows ( self , action , kwargs_list ) : <EOL> if action != '<STR_LIT>' : <EOL> for kw in kwargs_list : <EOL> if '<STR_LIT>' not in kw : <EOL> kw [ '<STR_LIT>' ] = self . _default_cookie <EOL> flow_strs = [ _build_flow_expr_str ( kw , action ) for kw in kwargs_list ] <EOL> self . run_ofctl ( '<STR_LIT>' % action , [ '<STR_LIT:->' ] , '<STR_LIT:\n>' . join ( flow_strs ) ) <EOL> def add_flow ( self , ** kwargs ) : <EOL> self . do_action_flows ( '<STR_LIT>' , [ kwargs ] ) <EOL> def mod_flow ( self , ** kwargs ) : <EOL> self . do_action_flows ( '<STR_LIT>' , [ kwargs ] ) <EOL> def delete_flows ( self , ** kwargs ) : <EOL> self . do_action_flows ( '<STR_LIT>' , [ kwargs ] ) <EOL> def dump_flows_for_table ( self , table ) : <EOL> return self . dump_flows_for ( table = table ) <EOL> def dump_flows_for ( self , ** kwargs ) : <EOL> retval = None <EOL> if "<STR_LIT>" in kwargs : <EOL> kwargs [ "<STR_LIT>" ] = check_cookie_mask ( str ( kwargs [ "<STR_LIT>" ] ) ) <EOL> flow_str = "<STR_LIT:U+002C>" . join ( "<STR_LIT:=>" . join ( [ key , str ( val ) ] ) <EOL> for key , val in kwargs . items ( ) ) <EOL> flows = self . run_ofctl ( "<STR_LIT>" , [ flow_str ] ) <EOL> if flows : <EOL> retval = '<STR_LIT:\n>' . join ( item for item in flows . splitlines ( ) <EOL> if '<STR_LIT>' not in item ) <EOL> return retval <EOL> def dump_all_flows ( self ) : <EOL> return [ f for f in self . run_ofctl ( "<STR_LIT>" , [ ] ) . splitlines ( ) <EOL> if '<STR_LIT>' not in f ] <EOL> def deferred ( self , ** kwargs ) : <EOL> return DeferredOVSBridge ( self , ** kwargs ) <EOL> def add_tunnel_port ( self , port_name , remote_ip , local_ip , <EOL> tunnel_type = p_const . TYPE_GRE , <EOL> vxlan_udp_port = p_const . VXLAN_UDP_PORT , <EOL> dont_fragment = True , <EOL> tunnel_csum = False ) : <EOL> attrs = [ ( '<STR_LIT:type>' , tunnel_type ) ] <EOL> options = collections . OrderedDict ( ) <EOL> vxlan_uses_custom_udp_port = ( <EOL> tunnel_type == p_const . TYPE_VXLAN and <EOL> vxlan_udp_port != p_const . VXLAN_UDP_PORT <EOL> ) <EOL> if vxlan_uses_custom_udp_port : <EOL> options [ '<STR_LIT>' ] = vxlan_udp_port <EOL> options [ '<STR_LIT>' ] = str ( dont_fragment ) . lower ( ) <EOL> options [ '<STR_LIT>' ] = remote_ip <EOL> options [ '<STR_LIT>' ] = local_ip <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if tunnel_csum : <EOL> options [ '<STR_LIT>' ] = str ( tunnel_csum ) . lower ( ) <EOL> attrs . append ( ( '<STR_LIT>' , options ) ) <EOL> return self . add_port ( port_name , * attrs ) <EOL> def add_patch_port ( self , local_name , remote_name ) : <EOL> attrs = [ ( '<STR_LIT:type>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , { '<STR_LIT>' : remote_name } ) ] <EOL> return self . add_port ( local_name , * attrs ) <EOL> def get_iface_name_list ( self ) : <EOL> return self . ovsdb . list_ifaces ( self . br_name ) . execute ( check_error = True ) <EOL> def get_port_name_list ( self ) : <EOL> return self . ovsdb . list_ports ( self . br_name ) . execute ( check_error = True ) <EOL> def get_port_stats ( self , port_name ) : <EOL> return self . db_get_val ( "<STR_LIT>" , port_name , "<STR_LIT>" ) <EOL> def get_xapi_iface_id ( self , xs_vif_uuid ) : <EOL> args = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" % xs_vif_uuid ] <EOL> try : <EOL> return utils . execute ( args , run_as_root = True ) . strip ( ) <EOL> except Exception as e : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : args , '<STR_LIT>' : e } ) <EOL> def get_ports_attributes ( self , table , columns = None , ports = None , <EOL> check_error = True , log_errors = True , <EOL> if_exists = False ) : <EOL> port_names = ports or self . get_port_name_list ( ) <EOL> if not port_names : <EOL> return [ ] <EOL> return ( self . ovsdb . db_list ( table , port_names , columns = columns , <EOL> if_exists = if_exists ) . <EOL> execute ( check_error = check_error , log_errors = log_errors ) ) <EOL> def get_vif_ports ( self ) : <EOL> edge_ports = [ ] <EOL> port_info = self . get_ports_attributes ( <EOL> '<STR_LIT>' , columns = [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> if_exists = True ) <EOL> for port in port_info : <EOL> name = port [ '<STR_LIT:name>' ] <EOL> external_ids = port [ '<STR_LIT>' ] <EOL> ofport = port [ '<STR_LIT>' ] <EOL> if "<STR_LIT>" in external_ids and "<STR_LIT>" in external_ids : <EOL> p = VifPort ( name , ofport , external_ids [ "<STR_LIT>" ] , <EOL> external_ids [ "<STR_LIT>" ] , self ) <EOL> edge_ports . append ( p ) <EOL> elif ( "<STR_LIT>" in external_ids and <EOL> "<STR_LIT>" in external_ids ) : <EOL> iface_id = self . get_xapi_iface_id ( external_ids [ "<STR_LIT>" ] ) <EOL> p = VifPort ( name , ofport , iface_id , <EOL> external_ids [ "<STR_LIT>" ] , self ) <EOL> edge_ports . append ( p ) <EOL> return edge_ports <EOL> def get_vif_port_to_ofport_map ( self ) : <EOL> results = self . get_ports_attributes ( <EOL> '<STR_LIT>' , columns = [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> if_exists = True ) <EOL> port_map = { } <EOL> for r in results : <EOL> key = self . portid_from_external_ids ( r [ '<STR_LIT>' ] ) or r [ '<STR_LIT:name>' ] <EOL> try : <EOL> port_map [ key ] = int ( r [ '<STR_LIT>' ] ) <EOL> except TypeError : <EOL> pass <EOL> return port_map <EOL> def get_vif_port_set ( self ) : <EOL> edge_ports = set ( ) <EOL> results = self . get_ports_attributes ( <EOL> '<STR_LIT>' , columns = [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> if_exists = True ) <EOL> for result in results : <EOL> if result [ '<STR_LIT>' ] == UNASSIGNED_OFPORT : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) , <EOL> result [ '<STR_LIT:name>' ] ) <EOL> elif result [ '<STR_LIT>' ] == INVALID_OFPORT : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) , <EOL> result [ '<STR_LIT:name>' ] ) <EOL> elif '<STR_LIT>' in result [ '<STR_LIT>' ] : <EOL> port_id = self . portid_from_external_ids ( result [ '<STR_LIT>' ] ) <EOL> if port_id : <EOL> edge_ports . add ( port_id ) <EOL> return edge_ports <EOL> def portid_from_external_ids ( self , external_ids ) : <EOL> if '<STR_LIT>' in external_ids : <EOL> return external_ids [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in external_ids : <EOL> iface_id = self . get_xapi_iface_id ( <EOL> external_ids [ '<STR_LIT>' ] ) <EOL> return iface_id <EOL> def get_port_tag_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> results = self . get_ports_attributes ( <EOL> '<STR_LIT>' , columns = [ '<STR_LIT:name>' , '<STR_LIT>' ] , if_exists = True ) <EOL> return { p [ '<STR_LIT:name>' ] : p [ '<STR_LIT>' ] for p in results } <EOL> def get_vifs_by_ids ( self , port_ids ) : <EOL> interface_info = self . get_ports_attributes ( <EOL> "<STR_LIT>" , columns = [ "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> if_exists = True ) <EOL> by_id = { x [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) : x for x in interface_info } <EOL> result = { } <EOL> for port_id in port_ids : <EOL> result [ port_id ] = None <EOL> if port_id not in by_id : <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : port_id , '<STR_LIT>' : self . br_name } ) <EOL> continue <EOL> pinfo = by_id [ port_id ] <EOL> if not self . _check_ofport ( port_id , pinfo ) : <EOL> continue <EOL> mac = pinfo [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) <EOL> result [ port_id ] = VifPort ( pinfo [ '<STR_LIT:name>' ] , pinfo [ '<STR_LIT>' ] , <EOL> port_id , mac , self ) <EOL> return result <EOL> @ staticmethod <EOL> def _check_ofport ( port_id , port_info ) : <EOL> if port_info [ '<STR_LIT>' ] in [ UNASSIGNED_OFPORT , INVALID_OFPORT ] : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : port_info [ '<STR_LIT>' ] , '<STR_LIT>' : port_id } ) <EOL> return False <EOL> return True <EOL> def get_vif_port_by_id ( self , port_id ) : <EOL> ports = self . ovsdb . db_find ( <EOL> '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT:=>' , { '<STR_LIT>' : port_id } ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> columns = [ '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' ] ) . execute ( ) <EOL> for port in ports : <EOL> if self . br_name != self . get_bridge_for_iface ( port [ '<STR_LIT:name>' ] ) : <EOL> continue <EOL> if not self . _check_ofport ( port_id , port ) : <EOL> continue <EOL> mac = port [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) <EOL> return VifPort ( port [ '<STR_LIT:name>' ] , port [ '<STR_LIT>' ] , port_id , mac , self ) <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : port_id , '<STR_LIT>' : self . br_name } ) <EOL> def delete_ports ( self , all_ports = False ) : <EOL> if all_ports : <EOL> port_names = self . get_port_name_list ( ) <EOL> else : <EOL> port_names = ( port . port_name for port in self . get_vif_ports ( ) ) <EOL> for port_name in port_names : <EOL> self . delete_port ( port_name ) <EOL> def get_local_port_mac ( self ) : <EOL> """<STR_LIT>""" <EOL> address = ip_lib . IPDevice ( self . br_name ) . link . address <EOL> if address : <EOL> return address <EOL> else : <EOL> msg = _ ( '<STR_LIT>' ) % self . br_name <EOL> raise Exception ( msg ) <EOL> def set_controllers_connection_mode ( self , connection_mode ) : <EOL> """<STR_LIT>""" <EOL> attr = [ ( '<STR_LIT>' , connection_mode ) ] <EOL> controllers = self . db_get_val ( '<STR_LIT>' , self . br_name , '<STR_LIT>' ) <EOL> controllers = [ controllers ] if isinstance ( <EOL> controllers , uuid . UUID ) else controllers <EOL> with self . ovsdb . transaction ( check_error = True ) as txn : <EOL> for controller_uuid in controllers : <EOL> txn . add ( self . ovsdb . db_set ( '<STR_LIT>' , <EOL> controller_uuid , * attr ) ) <EOL> def _set_egress_bw_limit_for_port ( self , port_name , max_kbps , <EOL> max_burst_kbps ) : <EOL> with self . ovsdb . transaction ( check_error = True ) as txn : <EOL> txn . add ( self . ovsdb . db_set ( '<STR_LIT>' , port_name , <EOL> ( '<STR_LIT>' , max_kbps ) ) ) <EOL> txn . add ( self . ovsdb . db_set ( '<STR_LIT>' , port_name , <EOL> ( '<STR_LIT>' , <EOL> max_burst_kbps ) ) ) <EOL> def create_egress_bw_limit_for_port ( self , port_name , max_kbps , <EOL> max_burst_kbps ) : <EOL> self . _set_egress_bw_limit_for_port ( <EOL> port_name , max_kbps , max_burst_kbps ) <EOL> def get_egress_bw_limit_for_port ( self , port_name ) : <EOL> max_kbps = self . db_get_val ( '<STR_LIT>' , port_name , <EOL> '<STR_LIT>' ) <EOL> max_burst_kbps = self . db_get_val ( '<STR_LIT>' , port_name , <EOL> '<STR_LIT>' ) <EOL> max_kbps = max_kbps or None <EOL> max_burst_kbps = max_burst_kbps or None <EOL> return max_kbps , max_burst_kbps <EOL> def delete_egress_bw_limit_for_port ( self , port_name ) : <EOL> self . _set_egress_bw_limit_for_port ( <EOL> port_name , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> def __enter__ ( self ) : <EOL> self . create ( ) <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_value , exc_tb ) : <EOL> self . destroy ( ) <EOL> class DeferredOVSBridge ( object ) : <EOL> '''<STR_LIT>''' <EOL> ALLOWED_PASSTHROUGHS = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> def __init__ ( self , br , full_ordered = False , <EOL> order = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> '''<STR_LIT>''' <EOL> self . br = br <EOL> self . full_ordered = full_ordered <EOL> self . order = order <EOL> if not self . full_ordered : <EOL> self . weights = dict ( ( y , x ) for x , y in enumerate ( self . order ) ) <EOL> self . action_flow_tuples = [ ] <EOL> def __getattr__ ( self , name ) : <EOL> if name in self . ALLOWED_PASSTHROUGHS : <EOL> return getattr ( self . br , name ) <EOL> raise AttributeError ( name ) <EOL> def add_flow ( self , ** kwargs ) : <EOL> self . action_flow_tuples . append ( ( '<STR_LIT>' , kwargs ) ) <EOL> def mod_flow ( self , ** kwargs ) : <EOL> self . action_flow_tuples . append ( ( '<STR_LIT>' , kwargs ) ) <EOL> def delete_flows ( self , ** kwargs ) : <EOL> self . action_flow_tuples . append ( ( '<STR_LIT>' , kwargs ) ) <EOL> def apply_flows ( self ) : <EOL> action_flow_tuples = self . action_flow_tuples <EOL> self . action_flow_tuples = [ ] <EOL> if not action_flow_tuples : <EOL> return <EOL> if not self . full_ordered : <EOL> action_flow_tuples . sort ( key = lambda af : self . weights [ af [ <NUM_LIT:0> ] ] ) <EOL> grouped = itertools . groupby ( action_flow_tuples , <EOL> key = operator . itemgetter ( <NUM_LIT:0> ) ) <EOL> itemgetter_1 = operator . itemgetter ( <NUM_LIT:1> ) <EOL> for action , action_flow_list in grouped : <EOL> flows = list ( map ( itemgetter_1 , action_flow_list ) ) <EOL> self . br . do_action_flows ( action , flows ) <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> if exc_type is None : <EOL> self . apply_flows ( ) <EOL> else : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) , <EOL> self . br . br_name ) <EOL> def _build_flow_expr_str ( flow_dict , cmd ) : <EOL> flow_expr_arr = [ ] <EOL> actions = None <EOL> if cmd == '<STR_LIT>' : <EOL> flow_expr_arr . append ( "<STR_LIT>" % <EOL> flow_dict . pop ( '<STR_LIT>' , '<STR_LIT:0>' ) ) <EOL> flow_expr_arr . append ( "<STR_LIT>" % <EOL> flow_dict . pop ( '<STR_LIT>' , '<STR_LIT:0>' ) ) <EOL> flow_expr_arr . append ( "<STR_LIT>" % <EOL> flow_dict . pop ( '<STR_LIT>' , '<STR_LIT:1>' ) ) <EOL> elif '<STR_LIT>' in flow_dict : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exceptions . InvalidInput ( error_message = msg ) <EOL> if cmd != '<STR_LIT>' : <EOL> if "<STR_LIT>" not in flow_dict : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise exceptions . InvalidInput ( error_message = msg ) <EOL> actions = "<STR_LIT>" % flow_dict . pop ( '<STR_LIT>' ) <EOL> for key , value in six . iteritems ( flow_dict ) : <EOL> if key == '<STR_LIT>' : <EOL> flow_expr_arr . append ( value ) <EOL> else : <EOL> flow_expr_arr . append ( "<STR_LIT>" % ( key , str ( value ) ) ) <EOL> if actions : <EOL> flow_expr_arr . append ( actions ) <EOL> return '<STR_LIT:U+002C>' . join ( flow_expr_arr ) <EOL> def generate_random_cookie ( ) : <EOL> return uuid . uuid4 ( ) . int & UINT64_BITMASK <EOL> def check_cookie_mask ( cookie ) : <EOL> if '<STR_LIT:/>' not in cookie : <EOL> return cookie + '<STR_LIT>' <EOL> else : <EOL> return cookie </s>
