def get_candidates(self, plugin, context, r_hd_binding_db):
<EOL>router = r_hd_binding_db.router<EOL>tenant_id = router['<STR_LIT>']<EOL>router_type_db = r_hd_binding_db.router_type<EOL>template_id = router_type_db['<STR_LIT>']<EOL>template_db = router_type_db.template<EOL>slot_threshold = template_db.slot_capacity - router_type_db.slot_need<EOL>query = context.session.query(<EOL>hd_models.HostingDevice.id, hd_models.HostingDevice.created_at,<EOL>func.sum(hd_models.SlotAllocation.num_allocated))<EOL>query = query.outerjoin(<EOL>hd_models.SlotAllocation,<EOL>hd_models.HostingDevice.id ==<EOL>hd_models.SlotAllocation.hosting_device_id)<EOL>query = query.filter(<EOL>hd_models.HostingDevice.template_id == template_id,<EOL>hd_models.HostingDevice.admin_state_up == expr.true(),<EOL>hd_models.HostingDevice.status == cisco_constants.HD_ACTIVE)<EOL>if r_hd_binding_db.share_hosting_device:<EOL><INDENT>query = query.filter(<EOL>expr.or_(hd_models.HostingDevice.tenant_bound == expr.null(),<EOL>hd_models.HostingDevice.tenant_bound == tenant_id))<EOL><DEDENT>else:<EOL><INDENT>query = query.filter(<EOL>expr.or_(<EOL>hd_models.SlotAllocation.tenant_bound == tenant_id,<EOL>expr.and_(<EOL>hd_models.SlotAllocation.tenant_bound == expr.null(),<EOL>hd_models.SlotAllocation.logical_resource_owner ==<EOL>tenant_id),<EOL>hd_models.HostingDevice.tenant_bound == tenant_id,<EOL>expr.and_(<EOL>hd_models.HostingDevice.tenant_bound == expr.null(),<EOL>hd_models.SlotAllocation.hosting_device_id ==<EOL>expr.null())))<EOL><DEDENT>query = query.group_by(hd_models.HostingDevice.id)<EOL>query = query.having(<EOL>expr.or_(func.sum(<EOL>hd_models.SlotAllocation.num_allocated) <= slot_threshold,<EOL>func.sum(hd_models.SlotAllocation.num_allocated ==<EOL>expr.null())))<EOL>query = query.order_by(hd_models.HostingDevice.created_at)<EOL>return query.all()<EOL>
Selection criteria: Hosting devices that...
               ... are based on the template required by router's type
               AND
               ... are administratively up
               AND
               ... are active (i.e., has status HD_ACTIVE)
               AND
               ... are bound to tenant owning router OR is unbound
               AND
               ... are enough slots available to host the router

            Among hosting devices meeting these criteria the device with
            less allocated slots is preferred.