'Find a router to handle the floating-ip association.
:param internal_port: The port for the fixed-ip.
:param internal_subnet: The subnet for the fixed-ip.
:param external_network_id: The external network for floating-ip.
:raises: ExternalGatewayForFloatingIPNotFound if no suitable router
is found.'

def get_router_for_floatingip(self, context, internal_port, internal_subnet, external_network_id):

    RouterPort = l3_models.RouterPort
    gw_port = orm.aliased(models_v2.Port, name='gw_port')
    routerport_qry = context.session.query(RouterPort.router_id, models_v2.IPAllocation.ip_address).join(models_v2.Port, models_v2.IPAllocation).filter((models_v2.Port.network_id == internal_port['network_id']), RouterPort.port_type.in_(constants.ROUTER_INTERFACE_OWNERS), (models_v2.IPAllocation.subnet_id == internal_subnet['id'])).join(gw_port, (gw_port.device_id == RouterPort.router_id)).filter((gw_port.network_id == external_network_id), (gw_port.device_owner == DEVICE_OWNER_ROUTER_GW)).distinct()
    first_router_id = None
    for (router_id, interface_ip) in routerport_qry:
        if (interface_ip == internal_subnet['gateway_ip']):
            return router_id
        if (not first_router_id):
            first_router_id = router_id
    if first_router_id:
        return first_router_id
    raise l3.ExternalGatewayForFloatingIPNotFound(subnet_id=internal_subnet['id'], external_network_id=external_network_id, port_id=internal_port['id'])
