import pytest
import json

pytestmark = pytest.mark.usefixtures("contrail")

STATUS_FILTER = r'grep -Pv "(==|^$|Disk|unix|support|boot|\*\*|FOR NODE)"'
STATUS_COMMAND = "contrail-status"

def get_contrail_status(salt_client, pillar, command, processor):
    return salt_client.cmd(
        pillar, 'cmd.run',
        ['{} | {}'.format(command, processor)],
        expr_form='pillar'
    )

def test_contrail_compute_status(local_salt_client):
    cs = get_contrail_status(local_salt_client, 'nova:compute',
                             STATUS_COMMAND, STATUS_FILTER)
    broken_services = []

    for node in cs:
        for line in cs[node].split('\n'):
            line = line.strip()
            if len (line.split(None, 1)) == 1:
                err_msg = "{0}: {1}".format(
                    node, line)
                broken_services.append(err_msg)
                continue
            name, status = line.split(None, 1)
            if status not in {'active'}:
                err_msg = "{node}:{service} - {status}".format(
                    node=node, service=name, status=status)
                broken_services.append(err_msg)

    assert not broken_services, 'Broken services: {}'.format(json.dumps(
                                                             broken_services,
                                                             indent=4))


def test_contrail_node_status(local_salt_client):
    command = STATUS_COMMAND

    # TODO: what will be in OpenContrail 5?
    if pytest.contrail == '4':
        command = "doctrail all " + command
    cs = get_contrail_status(local_salt_client,
                             'opencontrail:client:analytics_node',
                             command, STATUS_FILTER)
    cs.update(get_contrail_status(local_salt_client, 'opencontrail:control',
                                  command, STATUS_FILTER)
    )
    broken_services = []
    for node in cs:
        for line in cs[node].split('\n'):
            line = line.strip()
            if 'crashes/core.java.' not in line:
                name, status = line.split(None, 1)
            else:
                name, status = line, 'FATAL'
            if status not in {'active', 'backup'}:
                err_msg = "{node}:{service} - {status}".format(
                    node=node, service=name, status=status)
                broken_services.append(err_msg)

    assert not broken_services, 'Broken services: {}'.format(json.dumps(
                                                             broken_services,
                                                             indent=4))


def test_contrail_vrouter_count(local_salt_client):
    cs = get_contrail_status(local_salt_client, 'nova:compute',
                             STATUS_COMMAND, STATUS_FILTER)

    # TODO: what if compute lacks these service unintentionally?
    if not cs:
        pytest.skip("Contrail services were not found on compute nodes")

    actual_vrouter_count = 0
    for node in cs:
        for line in cs[node].split('\n'):
            if 'contrail-vrouter-nodemgr' in line:
                actual_vrouter_count += 1

    assert actual_vrouter_count == len(cs.keys()),\
        'The length of vRouters {} differs' \
        ' from the length of compute nodes {}'.format(actual_vrouter_count,
                                                      len(cs.keys()))
