Check for the existence of a rule in the table and chain

    This function accepts a rule in a standard nftables command format,
        starting with the chain. Trying to force users to adapt to a new
        method of creating rules would be irritating at best, and we
        already have a parser that can handle it.

    CLI Example:

    .. code-block:: bash

        salt '*' nftables.check filter input \\
            rule='tcp dport 22 log accept'

        IPv6:
        salt '*' nftables.check filter input \\
            rule='tcp dport 22 log accept' \\
            family=ipv6

def check(table='filter', chain=None, rule=None, family='ipv4'):
    '''
    Check for the existence of a rule in the table and chain

    This function accepts a rule in a standard nftables command format,
        starting with the chain. Trying to force users to adapt to a new
        method of creating rules would be irritating at best, and we
        already have a parser that can handle it.

    CLI Example:

    .. code-block:: bash

        salt '*' nftables.check filter input \\
            rule='tcp dport 22 log accept'

        IPv6:
        salt '*' nftables.check filter input \\
            rule='tcp dport 22 log accept' \\
            family=ipv6
    '''
    ret = {'comment': '',
           'result': False}

    if not chain:
        ret['comment'] = 'Chain needs to be specified'
        return ret

    if not rule:
        ret['comment'] = 'Rule needs to be specified'
        return ret

    res = check_table(table, family=family)
    if not res['result']:
        return res

    res = check_chain(table, chain, family=family)
    if not res['result']:
        return res

    nft_family = _NFTABLES_FAMILIES[family]
    cmd = '{0} --handle --numeric --numeric --numeric list chain {1} {2} {3}'.\
          format(_nftables_cmd(), nft_family, table, chain)
    search_rule = '{0} #'.format(rule)
    out = __salt__['cmd.run'](cmd, python_shell=False).find(search_rule)

    if out == -1:
        ret['comment'] = 'Rule {0} in chain {1} in table {2} in family {3} does not exist'.\
                         format(rule, chain, table, family)
    else:
        ret['comment'] = 'Rule {0} in chain {1} in table {2} in family {3} exists'.\
                         format(rule, chain, table, family)
        ret['result'] = True
    return ret