<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def get_port_bindings(binding_key=None):
 """Returns filtered list of port bindings that may be relevant on CVX This query is a little complex as we need all binding levels for any binding that has a single managed physnet, but we need to filter bindings that have no managed physnets. In order to achieve this, we join to the binding_level_model once to filter bindings with no managed levels, then a second time to get all levels for the remaining bindings. The loop at the end is a convenience to associate levels with bindings as a list. This would ideally be done through the use of an orm.relation, but due to some sqlalchemy limitations imposed to make OVO work, we can't add relations to existing models. """

    session = db.get_reader_session()
    with session.begin():
        binding_level_model = ml2_models.PortBindingLevel
        aliased_blm = aliased(ml2_models.PortBindingLevel)
        port_binding_model = ml2_models.PortBinding
        dist_binding_model = ml2_models.DistributedPortBinding
        bindings = (session.query(port_binding_model, aliased_blm)
                    .join(binding_level_model,
                          and_(
                              port_binding_model.port_id ==
                              binding_level_model.port_id,
                              port_binding_model.host ==
                              binding_level_model.host))
                    .filter_unnecessary_ports()
                    .join(aliased_blm,
                          and_(port_binding_model.port_id ==
                               aliased_blm.port_id,
                               port_binding_model.host ==
                               aliased_blm.host)))
        dist_bindings = (session.query(dist_binding_model, aliased_blm)
                         .join(
                             binding_level_model,
                             and_(dist_binding_model.port_id ==
                                  binding_level_model.port_id,
                                  dist_binding_model.host ==
                                  binding_level_model.host))
                         .filter_unnecessary_ports()
                         .filter(dist_binding_model.status ==
                                 n_const.PORT_STATUS_ACTIVE)
                         .join(aliased_blm,
                               and_(dist_binding_model.port_id ==
                                    aliased_blm.port_id,
                                    dist_binding_model.host ==
                                    aliased_blm.host)))
        if binding_key:
            port_id = binding_key[0]
            if type(binding_key[1]) == tuple:
                switch_id = binding_key[1][0]
                switch_port = binding_key[1][1]
                bindings = bindings.filter(and_(
                    port_binding_model.port_id == port_id,
                    port_binding_model.profile.ilike('%%%s%%' % switch_id),
                    port_binding_model.profile.ilike('%%%s%%' % switch_port)))
                dist_bindings = dist_bindings.filter(and_(
                    dist_binding_model.port_id == port_id,
                    dist_binding_model.profile.ilike('%%%s%%' % switch_id),
                    dist_binding_model.profile.ilike('%%%s%%' % switch_port)))
            else:
                host_id = binding_key[1]
                bindings = bindings.filter(and_(
                    port_binding_model.port_id == port_id,
                    port_binding_model.host == host_id))
                dist_bindings = dist_bindings.filter(and_(
                    dist_binding_model.port_id == port_id,
                    dist_binding_model.host == host_id))
    binding_levels = collections.defaultdict(list)
    for binding, level in bindings.all() + dist_bindings.all():
        binding_levels[binding].append(level)
    bindings_with_levels = list()
    for binding, levels in binding_levels.items():
        binding.levels = levels
        bindings_with_levels.append(binding)
    return bindings_with_levels