<s> from itertools import chain as iter_chain <EOL> import jinja2 <EOL> import netaddr <EOL> from oslo_config import cfg <EOL> from oslo_log import log as logging <EOL> import six <EOL> from neutron . _i18n import _ <EOL> from neutron . agent . linux import external_process <EOL> from neutron . agent . linux import utils <EOL> from neutron . common import constants <EOL> from neutron . common import utils as common_utils <EOL> RADVD_SERVICE_NAME = '<STR_LIT>' <EOL> RADVD_SERVICE_CMD = '<STR_LIT>' <EOL> MAX_RDNSS_ENTRIES = <NUM_LIT:3> <EOL> LOG = logging . getLogger ( __name__ ) <EOL> OPTS = [ <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = _ ( '<STR_LIT>' ) ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:30> , <EOL> help = _ ( '<STR_LIT>' ) ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:100> , <EOL> help = _ ( '<STR_LIT>' ) ) , <EOL> ] <EOL> CONFIG_TEMPLATE = jinja2 . Template ( """<STR_LIT>""" ) <EOL> class DaemonMonitor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , router_id , router_ns , process_monitor , dev_name_helper , <EOL> agent_conf ) : <EOL> self . _router_id = router_id <EOL> self . _router_ns = router_ns <EOL> self . _process_monitor = process_monitor <EOL> self . _dev_name_helper = dev_name_helper <EOL> self . _agent_conf = agent_conf <EOL> def _generate_radvd_conf ( self , router_ports ) : <EOL> radvd_conf = utils . get_conf_file_name ( self . _agent_conf . ra_confs , <EOL> self . _router_id , <EOL> '<STR_LIT>' , <EOL> True ) <EOL> buf = six . StringIO ( ) <EOL> network_mtu = <NUM_LIT:0> <EOL> for p in router_ports : <EOL> subnets = p . get ( '<STR_LIT>' , [ ] ) <EOL> v6_subnets = [ subnet for subnet in subnets if <EOL> netaddr . IPNetwork ( subnet [ '<STR_LIT>' ] ) . version == <NUM_LIT:6> ] <EOL> if not v6_subnets : <EOL> continue <EOL> ra_modes = { subnet [ '<STR_LIT>' ] for subnet in v6_subnets } <EOL> auto_config_prefixes = [ subnet [ '<STR_LIT>' ] for subnet in v6_subnets if <EOL> subnet [ '<STR_LIT>' ] == constants . IPV6_SLAAC or <EOL> subnet [ '<STR_LIT>' ] == constants . DHCPV6_STATELESS ] <EOL> stateful_config_prefixes = [ subnet [ '<STR_LIT>' ] for subnet in v6_subnets <EOL> if subnet [ '<STR_LIT>' ] == constants . DHCPV6_STATEFUL ] <EOL> interface_name = self . _dev_name_helper ( p [ '<STR_LIT:id>' ] ) <EOL> slaac_subnets = [ subnet for subnet in v6_subnets if <EOL> subnet [ '<STR_LIT>' ] == constants . IPV6_SLAAC ] <EOL> dns_servers = list ( iter_chain ( * [ subnet [ '<STR_LIT>' ] for <EOL> subnet in slaac_subnets if subnet . get ( '<STR_LIT>' ) ] ) ) <EOL> if self . _agent_conf . advertise_mtu : <EOL> network_mtu = p . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> buf . write ( '<STR_LIT:%s>' % CONFIG_TEMPLATE . render ( <EOL> ra_modes = list ( ra_modes ) , <EOL> interface_name = interface_name , <EOL> auto_config_prefixes = auto_config_prefixes , <EOL> stateful_config_prefixes = stateful_config_prefixes , <EOL> dns_servers = dns_servers [ <NUM_LIT:0> : MAX_RDNSS_ENTRIES ] , <EOL> constants = constants , <EOL> min_rtr_adv_interval = self . _agent_conf . min_rtr_adv_interval , <EOL> max_rtr_adv_interval = self . _agent_conf . max_rtr_adv_interval , <EOL> network_mtu = int ( network_mtu ) ) ) <EOL> common_utils . replace_file ( radvd_conf , buf . getvalue ( ) ) <EOL> return radvd_conf <EOL> def _get_radvd_process_manager ( self , callback = None ) : <EOL> return external_process . ProcessManager ( <EOL> uuid = self . _router_id , <EOL> default_cmd_callback = callback , <EOL> namespace = self . _router_ns , <EOL> service = RADVD_SERVICE_NAME , <EOL> conf = self . _agent_conf , <EOL> run_as_root = True ) <EOL> def _spawn_radvd ( self , radvd_conf ) : <EOL> def callback ( pid_file ) : <EOL> radvd_cmd = [ RADVD_SERVICE_CMD , <EOL> '<STR_LIT>' , '<STR_LIT:%s>' % radvd_conf , <EOL> '<STR_LIT>' , '<STR_LIT:%s>' % pid_file , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> return radvd_cmd <EOL> pm = self . _get_radvd_process_manager ( callback ) <EOL> pm . enable ( reload_cfg = True ) <EOL> self . _process_monitor . register ( uuid = self . _router_id , <EOL> service_name = RADVD_SERVICE_NAME , <EOL> monitored_process = pm ) <EOL> LOG . debug ( "<STR_LIT>" , self . _router_id ) <EOL> def enable ( self , router_ports ) : <EOL> for p in router_ports : <EOL> for subnet in p [ '<STR_LIT>' ] : <EOL> if netaddr . IPNetwork ( subnet [ '<STR_LIT>' ] ) . version == <NUM_LIT:6> : <EOL> LOG . debug ( "<STR_LIT>" , self . _router_id ) <EOL> radvd_conf = self . _generate_radvd_conf ( router_ports ) <EOL> self . _spawn_radvd ( radvd_conf ) <EOL> return <EOL> self . disable ( ) <EOL> def disable ( self ) : <EOL> self . _process_monitor . unregister ( uuid = self . _router_id , <EOL> service_name = RADVD_SERVICE_NAME ) <EOL> pm = self . _get_radvd_process_manager ( ) <EOL> pm . disable ( ) <EOL> utils . remove_conf_files ( self . _agent_conf . ra_confs , self . _router_id ) <EOL> LOG . debug ( "<STR_LIT>" , self . _router_id ) <EOL> @ property <EOL> def enabled ( self ) : <EOL> return self . _get_radvd_process_manager ( ) . active </s>
