#!/usr/bin/python3
#
# Determine if this tool can run a test based on a test spec.
#

import datetime
import sys

import pscheduler

json = pscheduler.json_load(exit_on_error=True);

# TODO: Should be checking the package schema and the task spec schema.
# TODO: Should be validating.

try:
    if json['type'] != 'trace':
        pscheduler.succeed_json({
            "can-run": False,
            "reasons": [ "Unsupported test type" ]
        })
except KeyError:
    pscheduler.succeed_json({
        "can-run": False,
        "reasons": [ "Missing test type" ]
    })


try:
    spec = json["spec"]
    pscheduler.json_check_schema(spec, 1)
except KeyError:
    pscheduler.succeed_json({
        "can-run": False,
        "reasons": ["Missing test specification"]
    })
except ValueError as ex:
    pscheduler.succeed_json({
        "can-run": False,
        "reasons": [str(ex)]
    })


errors = []

try:
    algorithm = spec['algorithm']
    if not algorithm in ['paris-traceroute']:
        errors.append("Algorithm '%s' is not supported" % spec['algorithm'])
except KeyError:
    # No algorithm is just fine.
    pass


# ip-version - Both are supported.

# probe-type - All are supported

if 'length' in spec:
    errors.append("Unable to control packet length")

try:
    fragment = spec['fragment']
    if fragment:
        errors.append("Unable to control fragmentation.")
except KeyError:
    # No fragmentation is just fine.
    pass

try:
    first_ttl = spec['first-ttl']
    max_ttl = spec['max-ttl'] if 'max-ttl' in spec else 30
    if first_ttl < 1 or first_ttl > 255:
        errors.append("First TTL value not supported")
    elif first_ttl > max_ttl:
        errors.append("First TTL must be less than the number of hops.")
except KeyError:
    pass  # None is okay

# source - Not supported

try:
    hops = spec['hops']
    if hops < 1 or hops > 255:
        errors.append("Maximum hop value not supported")
except KeyError:
    pass  # None is okay


# TODO:  Remove this when we can do multiples.
try:
    queries = spec['queries']
    if queries != 1:
        errors.append("Cannot handle more than one query per hop")
except KeyError:
    pass


# hostnames - All are supported

try:
    protocol = spec['probe-type']
    if not protocol in ['udp', 'tcp']:
        errors.append("Cannot support probe type %s" % protocol)
except KeyError:
    pass  # None is okay.

try:
    tos = spec['tos']
    errors.append("TOS is not supported")
except KeyError:
    pass  # None is okay.

# wait - All are supported

# sendwait - All are supported

# as - All are supported

result = {
    "can-run": len(errors) == 0
}

if len(errors) > 0:
    result["reasons"] = errors

pscheduler.succeed_json(result)
