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

import datetime
import sys

import pscheduler

logger = pscheduler.Log(prefix='tool-nuttcp', quiet=True)

json = pscheduler.json_load(exit_on_error=True)

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



errors = []

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)]
    })


supported_options = ["schema",
                     "source", "source-node",
                     "dest", "dest-node",
                     "duration",
                     "interval", "parallel", "window-size",
                     "mss", "bandwidth", "udp", "buffer-length",
                     "ip-tos", "ip-version", "client-cpu-affinity",
                     "server-cpu-affinity", "reverse"
                     ]


for option in spec.keys():
    if option not in supported_options:
        logger.debug("nuttcp unsupported option %s" % option)
        errors.append('nuttcp does not support %s option' % option)

logger.debug("can-run succeeded")

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

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

pscheduler.succeed_json(result)
