<s> import base64 <EOL> import logging <EOL> import netaddr <EOL> import six <EOL> from ryu . lib import hub <EOL> LOG = logging . getLogger ( __name__ ) <EOL> DEFAULT_TIMEOUT = <NUM_LIT:1.0> <EOL> OUTPUT = '<STR_LIT>' <EOL> COPY_TTL_OUT = '<STR_LIT>' <EOL> COPY_TTL_IN = '<STR_LIT>' <EOL> SET_MPLS_TTL = '<STR_LIT>' <EOL> DEC_MPLS_TTL = '<STR_LIT>' <EOL> PUSH_VLAN = '<STR_LIT>' <EOL> POP_VLAN = '<STR_LIT>' <EOL> PUSH_MPLS = '<STR_LIT>' <EOL> POP_MPLS = '<STR_LIT>' <EOL> SET_QUEUE = '<STR_LIT>' <EOL> GROUP = '<STR_LIT>' <EOL> SET_NW_TTL = '<STR_LIT>' <EOL> DEC_NW_TTL = '<STR_LIT>' <EOL> SET_FIELD = '<STR_LIT>' <EOL> PUSH_PBB = '<STR_LIT>' <EOL> POP_PBB = '<STR_LIT>' <EOL> EXPERIMENTER = '<STR_LIT>' <EOL> def get_logger ( logger = None ) : <EOL> if logger is not None : <EOL> return logger <EOL> return LOG <EOL> def match_vid_to_str ( value , mask , ofpvid_present ) : <EOL> if mask is not None : <EOL> return '<STR_LIT>' % ( value , mask ) <EOL> if value & ofpvid_present : <EOL> return str ( value & ~ ofpvid_present ) <EOL> return '<STR_LIT>' % value <EOL> def to_action ( dic , ofp , parser , action_type , util ) : <EOL> actions = { COPY_TTL_OUT : parser . OFPActionCopyTtlOut , <EOL> COPY_TTL_IN : parser . OFPActionCopyTtlIn , <EOL> DEC_MPLS_TTL : parser . OFPActionDecMplsTtl , <EOL> POP_VLAN : parser . OFPActionPopVlan , <EOL> DEC_NW_TTL : parser . OFPActionDecNwTtl , <EOL> POP_PBB : parser . OFPActionPopPbb } <EOL> need_ethertype = { PUSH_VLAN : parser . OFPActionPushVlan , <EOL> PUSH_MPLS : parser . OFPActionPushMpls , <EOL> POP_MPLS : parser . OFPActionPopMpls , <EOL> PUSH_PBB : parser . OFPActionPushPbb } <EOL> if action_type in actions : <EOL> return actions [ action_type ] ( ) <EOL> elif action_type in need_ethertype : <EOL> ethertype = int ( dic . get ( '<STR_LIT>' ) ) <EOL> return need_ethertype [ action_type ] ( ethertype ) <EOL> elif action_type == OUTPUT : <EOL> out_port = util . ofp_port_from_user ( dic . get ( '<STR_LIT:port>' , ofp . OFPP_ANY ) ) <EOL> max_len = util . ofp_cml_from_user ( dic . get ( '<STR_LIT>' , ofp . OFPCML_MAX ) ) <EOL> return parser . OFPActionOutput ( out_port , max_len ) <EOL> elif action_type == SET_MPLS_TTL : <EOL> mpls_ttl = int ( dic . get ( '<STR_LIT>' ) ) <EOL> return parser . OFPActionSetMplsTtl ( mpls_ttl ) <EOL> elif action_type == SET_QUEUE : <EOL> queue_id = util . ofp_queue_from_user ( dic . get ( '<STR_LIT>' ) ) <EOL> return parser . OFPActionSetQueue ( queue_id ) <EOL> elif action_type == GROUP : <EOL> group_id = util . ofp_group_from_user ( dic . get ( '<STR_LIT>' ) ) <EOL> return parser . OFPActionGroup ( group_id ) <EOL> elif action_type == SET_NW_TTL : <EOL> nw_ttl = int ( dic . get ( '<STR_LIT>' ) ) <EOL> return parser . OFPActionSetNwTtl ( nw_ttl ) <EOL> elif action_type == SET_FIELD : <EOL> field = dic . get ( '<STR_LIT>' ) <EOL> value = dic . get ( '<STR_LIT:value>' ) <EOL> return parser . OFPActionSetField ( ** { field : value } ) <EOL> elif action_type == EXPERIMENTER : <EOL> experimenter = int ( dic . get ( '<STR_LIT>' ) ) <EOL> data_type = dic . get ( '<STR_LIT>' , '<STR_LIT:ascii>' ) <EOL> if data_type not in ( '<STR_LIT:ascii>' , '<STR_LIT>' ) : <EOL> LOG . error ( '<STR_LIT>' , data_type ) <EOL> return None <EOL> data = dic . get ( '<STR_LIT:data>' , '<STR_LIT>' ) <EOL> if data_type == '<STR_LIT>' : <EOL> data = base64 . b64decode ( data ) <EOL> return parser . OFPActionExperimenterUnknown ( experimenter , data ) <EOL> return None <EOL> def to_match_eth ( value ) : <EOL> if '<STR_LIT:/>' in value : <EOL> value = value . split ( '<STR_LIT:/>' ) <EOL> return value [ <NUM_LIT:0> ] , value [ <NUM_LIT:1> ] <EOL> return value <EOL> def to_match_ip ( value ) : <EOL> if '<STR_LIT:/>' in value : <EOL> ( ip_addr , ip_mask ) = value . split ( '<STR_LIT:/>' ) <EOL> if ip_mask . isdigit ( ) : <EOL> ip = netaddr . ip . IPNetwork ( value ) <EOL> ip_addr = str ( ip . ip ) <EOL> ip_mask = str ( ip . netmask ) <EOL> return ip_addr , ip_mask <EOL> return value <EOL> def to_match_vid ( value , ofpvid_present ) : <EOL> if isinstance ( value , six . integer_types ) : <EOL> return value | ofpvid_present <EOL> else : <EOL> if '<STR_LIT:/>' in value : <EOL> val = value . split ( '<STR_LIT:/>' ) <EOL> return int ( val [ <NUM_LIT:0> ] , <NUM_LIT:0> ) , int ( val [ <NUM_LIT:1> ] , <NUM_LIT:0> ) <EOL> else : <EOL> if value . isdigit ( ) : <EOL> return int ( value , <NUM_LIT:10> ) | ofpvid_present <EOL> return int ( value , <NUM_LIT:0> ) <EOL> def to_match_masked_int ( value ) : <EOL> if isinstance ( value , str ) and '<STR_LIT:/>' in value : <EOL> value = value . split ( '<STR_LIT:/>' ) <EOL> return ( str_to_int ( value [ <NUM_LIT:0> ] ) , str_to_int ( value [ <NUM_LIT:1> ] ) ) <EOL> return str_to_int ( value ) <EOL> def send_experimenter ( dp , exp , logger = None ) : <EOL> experimenter = exp . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> exp_type = exp . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> data_type = exp . get ( '<STR_LIT>' , '<STR_LIT:ascii>' ) <EOL> if data_type not in ( '<STR_LIT:ascii>' , '<STR_LIT>' ) : <EOL> LOG . error ( '<STR_LIT>' , data_type ) <EOL> data = exp . get ( '<STR_LIT:data>' , '<STR_LIT>' ) <EOL> if data_type == '<STR_LIT>' : <EOL> data = base64 . b64decode ( data ) <EOL> expmsg = dp . ofproto_parser . OFPExperimenter ( dp , experimenter , exp_type , <EOL> data ) <EOL> send_msg ( dp , expmsg , logger ) <EOL> def send_msg ( dp , msg , logger = None ) : <EOL> if msg . xid is None : <EOL> dp . set_xid ( msg ) <EOL> get_logger ( logger ) . debug ( '<STR_LIT>' , msg . xid , msg ) <EOL> dp . send_msg ( msg ) <EOL> def send_stats_request ( dp , stats , waiters , msgs , logger = None ) : <EOL> dp . set_xid ( stats ) <EOL> waiters_per_dp = waiters . setdefault ( dp . id , { } ) <EOL> lock = hub . Event ( ) <EOL> previous_msg_len = len ( msgs ) <EOL> waiters_per_dp [ stats . xid ] = ( lock , msgs ) <EOL> send_msg ( dp , stats , logger ) <EOL> lock . wait ( timeout = DEFAULT_TIMEOUT ) <EOL> current_msg_len = len ( msgs ) <EOL> while current_msg_len > previous_msg_len : <EOL> previous_msg_len = current_msg_len <EOL> lock . wait ( timeout = DEFAULT_TIMEOUT ) <EOL> current_msg_len = len ( msgs ) <EOL> if not lock . is_set ( ) : <EOL> del waiters_per_dp [ stats . xid ] <EOL> def str_to_int ( str_num ) : <EOL> return int ( str ( str_num ) , <NUM_LIT:0> ) <EOL> class OFCtlUtil ( object ) : <EOL> def __init__ ( self , ofproto ) : <EOL> self . ofproto = ofproto <EOL> self . deprecated_value = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> def _reserved_num_from_user ( self , num , prefix ) : <EOL> if isinstance ( num , int ) : <EOL> return num <EOL> else : <EOL> if num . startswith ( prefix ) : <EOL> return getattr ( self . ofproto , num ) <EOL> else : <EOL> return getattr ( self . ofproto , prefix + num . upper ( ) ) <EOL> def _reserved_num_to_user ( self , num , prefix ) : <EOL> for k , v in self . ofproto . __dict__ . items ( ) : <EOL> if k not in self . deprecated_value and k . startswith ( prefix ) and v == num : <EOL> return k . replace ( prefix , '<STR_LIT>' ) <EOL> return num <EOL> def ofp_port_features_from_user ( self , act ) : <EOL> return self . _reserved_num_from_user ( act , '<STR_LIT>' ) <EOL> def ofp_port_features_to_user ( self , act ) : <EOL> return self . _reserved_num_to_user ( act , '<STR_LIT>' ) <EOL> def ofp_port_mod_prop_type_from_user ( self , act ) : <EOL> return self . _reserved_num_from_user ( act , '<STR_LIT>' ) <EOL> def ofp_port_mod_prop_type_to_user ( self , act ) : <EOL> return self . _reserved_num_to_user ( act , '<STR_LIT>' ) <EOL> def ofp_port_desc_prop_type_from_user ( self , act ) : <EOL> return self . _reserved_num_from_user ( act , '<STR_LIT>' ) <EOL> def ofp_port_desc_prop_type_to_user ( self , act ) : <EOL> return self . _reserved_num_to_user ( act , '<STR_LIT>' ) <EOL> def ofp_action_type_from_user ( self , act ) : <EOL> return self . _reserved_num_from_user ( act , '<STR_LIT>' ) <EOL> def ofp_action_type_to_user ( self , act ) : <EOL> return self . _reserved_num_to_user ( act , '<STR_LIT>' ) <EOL> def ofp_instruction_type_from_user ( self , act ) : <EOL> return self . _reserved_num_from_user ( act , '<STR_LIT>' ) <EOL> def ofp_instruction_type_to_user ( self , act ) : <EOL> return self . _reserved_num_to_user ( act , '<STR_LIT>' ) <EOL> def ofp_group_type_from_user ( self , act ) : <EOL> return self . _reserved_num_from_user ( act , '<STR_LIT>' ) <EOL> def ofp_group_type_to_user ( self , act ) : <EOL> return self . _reserved_num_to_user ( act , '<STR_LIT>' ) <EOL> def ofp_meter_band_type_from_user ( self , act ) : <EOL> return self . _reserved_num_from_user ( act , '<STR_LIT>' ) <EOL> def ofp_meter_band_type_to_user ( self , act ) : <EOL> return self . _reserved_num_to_user ( act , '<STR_LIT>' ) <EOL> def ofp_table_feature_prop_type_from_user ( self , act ) : <EOL> return self . _reserved_num_from_user ( act , '<STR_LIT>' ) <EOL> def ofp_table_feature_prop_type_to_user ( self , act ) : <EOL> return self . _reserved_num_to_user ( act , '<STR_LIT>' ) <EOL> def ofp_port_stats_prop_type_from_user ( self , act ) : <EOL> return self . _reserved_num_from_user ( act , '<STR_LIT>' ) <EOL> def ofp_port_stats_prop_type_to_user ( self , act ) : <EOL> return self . _reserved_num_to_user ( act , '<STR_LIT>' ) <EOL> def ofp_queue_desc_prop_type_from_user ( self , act ) : <EOL> return self . _reserved_num_from_user ( act , '<STR_LIT>' ) <EOL> def ofp_queue_desc_prop_type_to_user ( self , act ) : <EOL> return self . _reserved_num_to_user ( act , '<STR_LIT>' ) <EOL> def ofp_queue_stats_prop_type_from_user ( self , act ) : <EOL> return self . _reserved_num_from_user ( act , '<STR_LIT>' ) <EOL> def ofp_queue_stats_prop_type_to_user ( self , act ) : <EOL> return self . _reserved_num_to_user ( act , '<STR_LIT>' ) <EOL> def ofp_meter_flags_from_user ( self , act ) : <EOL> return self . _reserved_num_from_user ( act , '<STR_LIT>' ) <EOL> def ofp_meter_flags_to_user ( self , act ) : <EOL> return self . _reserved_num_to_user ( act , '<STR_LIT>' ) <EOL> def ofp_port_from_user ( self , port ) : <EOL> return self . _reserved_num_from_user ( port , '<STR_LIT>' ) <EOL> def ofp_port_to_user ( self , port ) : <EOL> return self . _reserved_num_to_user ( port , '<STR_LIT>' ) <EOL> def ofp_table_from_user ( self , table ) : <EOL> return self . _reserved_num_from_user ( table , '<STR_LIT>' ) <EOL> def ofp_table_to_user ( self , table ) : <EOL> return self . _reserved_num_to_user ( table , '<STR_LIT>' ) <EOL> def ofp_cml_from_user ( self , max_len ) : <EOL> return self . _reserved_num_from_user ( max_len , '<STR_LIT>' ) <EOL> def ofp_cml_to_user ( self , max_len ) : <EOL> return self . _reserved_num_to_user ( max_len , '<STR_LIT>' ) <EOL> def ofp_group_from_user ( self , group ) : <EOL> return self . _reserved_num_from_user ( group , '<STR_LIT>' ) <EOL> def ofp_group_to_user ( self , group ) : <EOL> return self . _reserved_num_to_user ( group , '<STR_LIT>' ) <EOL> def ofp_group_capabilities_from_user ( self , group ) : <EOL> return self . _reserved_num_from_user ( group , '<STR_LIT>' ) <EOL> def ofp_group_capabilities_to_user ( self , group ) : <EOL> return self . _reserved_num_to_user ( group , '<STR_LIT>' ) <EOL> def ofp_buffer_from_user ( self , buffer ) : <EOL> if buffer in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return self . ofproto . OFP_NO_BUFFER <EOL> else : <EOL> return buffer <EOL> def ofp_buffer_to_user ( self , buffer ) : <EOL> if self . ofproto . OFP_NO_BUFFER == buffer : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return buffer <EOL> def ofp_meter_from_user ( self , meter ) : <EOL> return self . _reserved_num_from_user ( meter , '<STR_LIT>' ) <EOL> def ofp_meter_to_user ( self , meter ) : <EOL> return self . _reserved_num_to_user ( meter , '<STR_LIT>' ) <EOL> def ofp_queue_from_user ( self , queue ) : <EOL> return self . _reserved_num_from_user ( queue , '<STR_LIT>' ) <EOL> def ofp_queue_to_user ( self , queue ) : <EOL> return self . _reserved_num_to_user ( queue , '<STR_LIT>' ) </s>
