'Event handler on precommit update to validate migration.'

@registry.receives(resources.ROUTER, [events.PRECOMMIT_UPDATE])
def _validate_migration(self, resource, event, trigger, context, router_id, router, router_db, old_router, **kwargs):

    original_ha_state = old_router['ha']
    requested_ha_state = router.get('ha')
    ha_changed = ((requested_ha_state is not None) and (requested_ha_state != original_ha_state))
    if (not ha_changed):
        return
    if router_db.admin_state_up:
        msg = _('Cannot    change    HA    attribute    of    active    routers.    Please    set    router    admin_state_up    to    False    prior    to    upgrade')
        raise n_exc.BadRequest(resource='router', msg=msg)
    if requested_ha_state:
        self.get_number_of_agents_for_scheduling(context)
    else:
        ha_network = self.get_ha_network(context, router_db.tenant_id)
        self._delete_vr_id_allocation(context, ha_network, router_db.extra_attributes.ha_vr_id)
        router_db.extra_attributes.ha_vr_id = None
    self.set_extra_attr_value(context, router_db, 'ha', requested_ha_state)
