Ejemplo n.º 1
0
def _notify_l3_agent_port_update(resource, event, trigger, **kwargs):
    new_port = kwargs.get('port')
    original_port = kwargs.get('original_port')

    is_fixed_ips_changed = n_utils.port_ip_changed(new_port, original_port)

    if (original_port['device_owner'] in
            [n_const.DEVICE_OWNER_HA_REPLICATED_INT,
             n_const.DEVICE_OWNER_ROUTER_SNAT,
             n_const.DEVICE_OWNER_ROUTER_GW] and
            not is_fixed_ips_changed):
        return

    if new_port and original_port:
        l3plugin = directory.get_plugin(plugin_constants.L3)
        context = kwargs['context']
        is_bound_port_moved = (
            original_port[portbindings.HOST_ID] and
            original_port[portbindings.HOST_ID] !=
            new_port[portbindings.HOST_ID])
        if is_bound_port_moved:
            removed_routers = l3plugin.get_dvr_routers_to_remove(
                context,
                original_port,
                get_related_hosts_info=False)
            if removed_routers:
                removed_router_args = {
                    'context': context,
                    'port': original_port,
                    'removed_routers': removed_routers,
                    'get_related_hosts_info': False,
                }
                _notify_port_delete(
                    event, resource, trigger, **removed_router_args)
            fips = l3plugin._get_floatingips_by_port_id(
                context, port_id=original_port['id'])
            fip = fips[0] if fips else None

            def _should_notify_on_fip_update():
                if not fip:
                    return False
                for info in removed_routers:
                    if info['router_id'] == fip['router_id']:
                        return False
                try:
                    router = l3plugin._get_router(context, fip['router_id'])
                except l3_exc.RouterNotFound:
                    return False
                return l3_dvr_db.is_distributed_router(router)

            if _should_notify_on_fip_update():
                l3plugin.l3_rpc_notifier.routers_updated_on_host(
                    context, [fip['router_id']],
                    original_port[portbindings.HOST_ID])
        is_new_port_binding_changed = (
            new_port[portbindings.HOST_ID] and
            (original_port[portbindings.HOST_ID] !=
                new_port[portbindings.HOST_ID]))
        dest_host = None
        new_port_profile = new_port.get(portbindings.PROFILE)
        if new_port_profile:
            dest_host = new_port_profile.get('migrating_to')
            # This check is required to prevent an arp update
            # of the allowed_address_pair port.
            if new_port_profile.get('original_owner'):
                return
        # If dest_host is set, then the port profile has changed
        # and this port is in migration. The call below will
        # pre-create the router on the new host
        # No need to check for device_owner since we are scheduling
        # the routers without checking for device_owner.
        # If the original_port is None, then it is a migration
        # from unbound to bound.
        if (is_new_port_binding_changed or dest_host):
            if original_port[portbindings.HOST_ID] is None:
                l3plugin.dvr_handle_new_service_port(context, new_port,
                                                     unbound_migrate=True)
            else:
                l3plugin.dvr_handle_new_service_port(context, new_port,
                                                     dest_host=dest_host)
            l3plugin.update_arp_entry_for_dvr_service_port(
                context, new_port)
            return
        # Check for allowed_address_pairs and port state
        new_port_host = new_port.get(portbindings.HOST_ID)
        allowed_address_pairs_list = new_port.get('allowed_address_pairs')
        if allowed_address_pairs_list and new_port_host:
            new_port_state = new_port.get('admin_state_up')
            original_port_state = original_port.get('admin_state_up')
            if new_port_state:
                # Case were we activate the port from inactive state,
                # or the same port has additional address_pairs added.
                for address_pair in allowed_address_pairs_list:
                    _dvr_handle_unbound_allowed_addr_pair_add(
                        l3plugin, context, new_port, address_pair)
                return
            elif original_port_state:
                # Case were we deactivate the port from active state.
                for address_pair in allowed_address_pairs_list:
                    _dvr_handle_unbound_allowed_addr_pair_del(
                        l3plugin, context, original_port, address_pair)
                return

        if kwargs.get('mac_address_updated') or is_fixed_ips_changed:
            l3plugin.update_arp_entry_for_dvr_service_port(
                context, new_port)
Ejemplo n.º 2
0
def _notify_l3_agent_port_update(resource, event, trigger, **kwargs):
    new_port = kwargs.get('port')
    original_port = kwargs.get('original_port')

    is_fixed_ips_changed = n_utils.port_ip_changed(new_port, original_port)

    if (original_port['device_owner'] in
            [n_const.DEVICE_OWNER_HA_REPLICATED_INT,
             n_const.DEVICE_OWNER_ROUTER_SNAT,
             n_const.DEVICE_OWNER_ROUTER_GW] and
            not is_fixed_ips_changed):
        return

    if new_port and original_port:
        l3plugin = directory.get_plugin(plugin_constants.L3)
        context = kwargs['context']
        is_bound_port_moved = (
            original_port[portbindings.HOST_ID] and
            original_port[portbindings.HOST_ID] !=
            new_port[portbindings.HOST_ID])
        if is_bound_port_moved:
            removed_routers = l3plugin.get_dvr_routers_to_remove(
                context,
                original_port,
                get_related_hosts_info=False)
            if removed_routers:
                removed_router_args = {
                    'context': context,
                    'port': original_port,
                    'removed_routers': removed_routers,
                    'get_related_hosts_info': False,
                }
                _notify_port_delete(
                    event, resource, trigger, **removed_router_args)
            fips = l3plugin._get_floatingips_by_port_id(
                context, port_id=original_port['id'])
            fip = fips[0] if fips else None

            def _should_notify_on_fip_update():
                if not fip:
                    return False
                for info in removed_routers:
                    if info['router_id'] == fip['router_id']:
                        return False
                try:
                    router = l3plugin._get_router(context, fip['router_id'])
                except l3_exc.RouterNotFound:
                    return False
                return l3_dvr_db.is_distributed_router(router)

            if _should_notify_on_fip_update():
                l3plugin.l3_rpc_notifier.routers_updated_on_host(
                    context, [fip['router_id']],
                    original_port[portbindings.HOST_ID])
        is_new_port_binding_changed = (
            new_port[portbindings.HOST_ID] and
            (original_port[portbindings.HOST_ID] !=
                new_port[portbindings.HOST_ID]))
        dest_host = None
        new_port_profile = new_port.get(portbindings.PROFILE)
        if new_port_profile:
            dest_host = new_port_profile.get('migrating_to')
            # This check is required to prevent an arp update
            # of the allowed_address_pair port.
            if new_port_profile.get('original_owner'):
                return
        # If dest_host is set, then the port profile has changed
        # and this port is in migration. The call below will
        # pre-create the router on the new host
        # If the original_port is None, then it is a migration
        # from unbound to bound.
        if (is_new_port_binding_changed or dest_host):
            if (not original_port[portbindings.HOST_ID] and
                    not original_port['device_owner']):
                l3plugin.dvr_handle_new_service_port(context, new_port,
                                                     unbound_migrate=True)
            else:
                l3plugin.dvr_handle_new_service_port(context, new_port,
                                                     dest_host=dest_host)
            l3plugin.update_arp_entry_for_dvr_service_port(
                context, new_port)
            return
        # Check for allowed_address_pairs and port state
        new_port_host = new_port.get(portbindings.HOST_ID)
        allowed_address_pairs_list = new_port.get('allowed_address_pairs')
        if allowed_address_pairs_list and new_port_host:
            new_port_state = new_port.get('admin_state_up')
            original_port_state = original_port.get('admin_state_up')
            if new_port_state:
                # Case were we activate the port from inactive state,
                # or the same port has additional address_pairs added.
                for address_pair in allowed_address_pairs_list:
                    _dvr_handle_unbound_allowed_addr_pair_add(
                        l3plugin, context, new_port, address_pair)
                return
            elif original_port_state:
                # Case were we deactivate the port from active state.
                for address_pair in allowed_address_pairs_list:
                    _dvr_handle_unbound_allowed_addr_pair_del(
                        l3plugin, context, original_port, address_pair)
                return

        if kwargs.get('mac_address_updated') or is_fixed_ips_changed:
            l3plugin.update_arp_entry_for_dvr_service_port(
                context, new_port)
Ejemplo n.º 3
0
def _notify_l3_agent_port_update(resource, event, trigger, **kwargs):
    new_port = kwargs.get('port')
    original_port = kwargs.get('original_port')

    is_fixed_ips_changed = n_utils.port_ip_changed(new_port, original_port)

    if (original_port['device_owner'] in [
            n_const.DEVICE_OWNER_HA_REPLICATED_INT,
            n_const.DEVICE_OWNER_ROUTER_SNAT, n_const.DEVICE_OWNER_ROUTER_GW
    ] and not is_fixed_ips_changed):
        return

    if new_port and original_port:
        l3plugin = directory.get_plugin(plugin_constants.L3)
        context = kwargs['context']
        new_port_host = new_port.get(portbindings.HOST_ID)
        original_port_host = original_port.get(portbindings.HOST_ID)
        is_new_port_binding_changed = (new_port_host
                                       and new_port_host != original_port_host)
        is_bound_port_moved = (original_port_host
                               and original_port_host != new_port_host)
        fip_router_id = None
        dest_host = None
        new_port_profile = new_port.get(portbindings.PROFILE)
        if new_port_profile:
            dest_host = new_port_profile.get('migrating_to')
        if is_new_port_binding_changed or is_bound_port_moved or dest_host:
            fips = l3plugin._get_floatingips_by_port_id(
                context, port_id=original_port['id'])
            fip = fips[0] if fips else None
            if fip:
                fip_router_id = fip['router_id']
        if is_bound_port_moved:
            removed_routers = l3plugin.get_dvr_routers_to_remove(
                context, original_port, get_related_hosts_info=False)
            if removed_routers:
                removed_router_args = {
                    'context': context,
                    'port': original_port,
                    'removed_routers': removed_routers,
                    'get_related_hosts_info': False,
                }
                _notify_port_delete(event, resource, trigger,
                                    **removed_router_args)

            def _should_notify_on_fip_update():
                if not fip_router_id:
                    return False
                for info in removed_routers:
                    if info['router_id'] == fip_router_id:
                        return False
                try:
                    router = l3plugin._get_router(context, fip_router_id)
                except l3_exc.RouterNotFound:
                    return False
                return l3_dvr_db.is_distributed_router(router)

            if _should_notify_on_fip_update():
                l3plugin.l3_rpc_notifier.routers_updated_on_host(
                    context, [fip_router_id],
                    original_port[portbindings.HOST_ID])
        # If dest_host is set, then the port profile has changed
        # and this port is in migration. The call below will
        # pre-create the router on the new host
        # If the original_port is None, then it is a migration
        # from unbound to bound.
        if (is_new_port_binding_changed or dest_host):
            if (not original_port[portbindings.HOST_ID]
                    and not original_port['device_owner']):
                l3plugin.dvr_handle_new_service_port(context,
                                                     new_port,
                                                     unbound_migrate=True)
            else:
                l3plugin.dvr_handle_new_service_port(context,
                                                     new_port,
                                                     dest_host=dest_host,
                                                     router_id=fip_router_id)
            return