<s> from oslo_log import log as logging <EOL> from neutron . _i18n import _LE <EOL> from neutron . agent . linux import ip_lib <EOL> LOG = logging . getLogger ( __name__ ) <EOL> NS_PREFIX = '<STR_LIT>' <EOL> INTERNAL_DEV_PREFIX = '<STR_LIT>' <EOL> EXTERNAL_DEV_PREFIX = '<STR_LIT>' <EOL> ROUTER_2_FIP_DEV_PREFIX = '<STR_LIT>' <EOL> def build_ns_name ( prefix , identifier ) : <EOL> """<STR_LIT>""" <EOL> return prefix + identifier <EOL> def get_prefix_from_ns_name ( ns_name ) : <EOL> """<STR_LIT>""" <EOL> dash_index = ns_name . find ( '<STR_LIT:->' ) <EOL> if <NUM_LIT:0> <= dash_index : <EOL> return ns_name [ : dash_index + <NUM_LIT:1> ] <EOL> def get_id_from_ns_name ( ns_name ) : <EOL> """<STR_LIT>""" <EOL> dash_index = ns_name . find ( '<STR_LIT:->' ) <EOL> if <NUM_LIT:0> <= dash_index : <EOL> return ns_name [ dash_index + <NUM_LIT:1> : ] <EOL> class Namespace ( object ) : <EOL> def __init__ ( self , name , agent_conf , driver , use_ipv6 ) : <EOL> self . name = name <EOL> self . ip_wrapper_root = ip_lib . IPWrapper ( ) <EOL> self . agent_conf = agent_conf <EOL> self . driver = driver <EOL> self . use_ipv6 = use_ipv6 <EOL> def create ( self ) : <EOL> ip_wrapper = self . ip_wrapper_root . ensure_namespace ( self . name ) <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ip_wrapper . netns . execute ( cmd ) <EOL> if self . use_ipv6 : <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ip_wrapper . netns . execute ( cmd ) <EOL> def delete ( self ) : <EOL> try : <EOL> self . ip_wrapper_root . netns . delete ( self . name ) <EOL> except RuntimeError : <EOL> msg = _LE ( '<STR_LIT>' ) <EOL> LOG . exception ( msg , self . name ) <EOL> class RouterNamespace ( Namespace ) : <EOL> def __init__ ( self , router_id , agent_conf , driver , use_ipv6 ) : <EOL> self . router_id = router_id <EOL> name = self . _get_ns_name ( router_id ) <EOL> super ( RouterNamespace , self ) . __init__ ( <EOL> name , agent_conf , driver , use_ipv6 ) <EOL> @ classmethod <EOL> def _get_ns_name ( cls , router_id ) : <EOL> return build_ns_name ( NS_PREFIX , router_id ) <EOL> def delete ( self ) : <EOL> ns_ip = ip_lib . IPWrapper ( namespace = self . name ) <EOL> for d in ns_ip . get_devices ( exclude_loopback = True ) : <EOL> if d . name . startswith ( INTERNAL_DEV_PREFIX ) : <EOL> self . driver . unplug ( d . name , namespace = self . name , <EOL> prefix = INTERNAL_DEV_PREFIX ) <EOL> elif d . name . startswith ( ROUTER_2_FIP_DEV_PREFIX ) : <EOL> ns_ip . del_veth ( d . name ) <EOL> elif d . name . startswith ( EXTERNAL_DEV_PREFIX ) : <EOL> self . driver . unplug ( <EOL> d . name , <EOL> bridge = self . agent_conf . external_network_bridge , <EOL> namespace = self . name , <EOL> prefix = EXTERNAL_DEV_PREFIX ) <EOL> super ( RouterNamespace , self ) . delete ( ) </s>
