'Check whether a rule can be applied to a specific port.
This function has the logic to decide whether a rule should
be applied to a port or not, depending on the source of the
policy (network, or port). Eventually rules could override
this method, or we could make it abstract to allow different
rule behaviour.'

def should_apply_to_port(self, port):

    is_port_policy = (self.qos_policy_id == port[qos_consts.QOS_POLICY_ID])
    is_network_policy_only = (port[qos_consts.QOS_POLICY_ID] is None)
    is_network_device_port = any((port['device_owner'].startswith(prefix) for prefix in constants.DEVICE_OWNER_PREFIXES))
    is_router_gw = any((port['device_owner'].startswith(prefix) for prefix in [constants.DEVICE_OWNER_AGENT_GW, constants.DEVICE_OWNER_ROUTER_GW]))
    return (is_port_policy or ((is_router_gw or (not is_network_device_port)) and is_network_policy_only))
