#!/usr/bin/env python

import sys
import socket
import struct
import errno
import argparse

from python_sdk_api.sx_api import *

rc, handle = sx_api_open(None)
print ("sx_api_open handle:0x%x , rc %d " % (handle, rc))
if (rc != SX_STATUS_SUCCESS):
    print "Failed to open api handle.\nPlease check that SDK is running."
    sys.exit(errno.EACCES)

def tele_init():
    " This function init tele. "

    tele_param_p = new_sx_tele_init_params_t_p()

    return sx_api_tele_init_set(handle, tele_param_p)
    #assert SX_STATUS_SUCCESS == rc, "tele init failed rc: %d" % (rc)
    #print "sx_api_tele_init_set rc: %d " % (rc)


def tele_deinit():
    " This function deinit tele. "

    rc = sx_api_tele_deinit_set(handle)
    #assert SX_STATUS_SUCCESS == rc, "tele deinit failed rc %d" % (rc)
    #print "sx_api_tele_deinit_set rc: %d " % (rc)


def tele_threshold_set(cmd, port, tc, threshold_hi, threshold_lo):
    " This function sets a threshold on the port and enables/disables the TC. "

    key = sx_tele_threshold_key_t()
    key.key_type = SX_TELE_THRESHOLD_TYPE_LATENCY_PORT_TC_E

    key_attr = sx_tele_threshold_port_tc_key_t()
    key_attr.log_port = port
    key_attr.tc = tc
    key.key.port_tc = key_attr

    data = sx_tele_threshold_data_t()
    data.threshold_data_type = SX_TELE_THRESHOLD_DATA_TYPE_LATENCY_E
    data.threshold_data.threshold_high = threshold_hi
    data.threshold_data.threshold_low = threshold_lo

    rc = sx_api_tele_threshold_set(handle, cmd, key, data)
    #assert SX_STATUS_SUCCESS == rc, "sx_api_tele_threshold_set rc %d" % (rc)
    print "sx_api_tele_threshold_set rc: %d " % (rc)
    return rc

def tele_threshold_get(cmd, port, tc):
    " This function gets the threshold of the port. "

    key = sx_tele_threshold_key_t()
    key.key_type = SX_TELE_THRESHOLD_TYPE_LATENCY_PORT_TC_E
    key.key.port_tc.log_port = port
    key.key.port_tc.tc = tc

    data_p = new_sx_tele_threshold_data_t_p()

    rc = sx_api_tele_threshold_get(handle, SX_ACCESS_CMD_GET, key, data_p)
    #assert SX_STATUS_SUCCESS == rc, "sx_api_tele_threshold_get rc %d" % (rc)
    print "sx_api_tele_threshold_get rc: %d " % (rc)

    data = sx_tele_threshold_data_t_p_value(data_p)
    print 'Data Type: {}'.format(data.data_type)
    print 'Threshold: port_tc_threshold {}'.format(data.data.port_tc_threshold)
    print 'Threshold: latency threshold high {}'.format(data.threshold_data.threshold_high)
    print 'Threshold: latency threshold low {}'.format(data.threshold_data.threshold_low)

    return data, rc


def setup_arg_parser():
    cfg = argparse.ArgumentParser()
    cfg.add_argument('--init', help='initialize telemetry module', dest='init', default=False, action='store_true')
    cfg.add_argument('--deinit', help='deinitialize telemetry module', dest='deinit', default=False, action='store_true')
    cfg.add_argument('--port', help='lid of the port')
    cfg.add_argument('--clear', help='set or clear', dest='clear', default=False, action='store_true')
    cfg.add_argument('--tc', help='traffic class 0-7', type=int, choices=range(0,8))
    cfg.add_argument('--threshold_hi', help='threshold hi', type=int)
    cfg.add_argument('--threshold_lo', help='threshold lo', type=int)
    return cfg


def main(args):

    parser = setup_arg_parser()
    cfgs = parser.parse_args(args)

    if cfgs.init:
        tele_init()
        sys.exit(0)

    if cfgs.deinit:
        tele_deinit()
        sys.exit(0)

    try:
        port_id = int(cfgs.port, 16)
    except:
        print 'port id invalid'
        sys.exit(1)


    '''
    data, rc = tele_threshold_get(cmd=SX_ACCESS_CMD_GET,
                                  port=port_id,
                                  tc=cfgs.tc)
    '''

    if cfgs.clear:
        cmd = SX_ACCESS_CMD_DESTROY
    else:
        '''
        if rc == SX_STATUS_SUCCESS:
            cmd = SX_ACCESS_CMD_EDIT
        else:
            cmd = SX_ACCESS_CMD_SET
        '''
        cmd = SX_ACCESS_CMD_SET

    rc = tele_threshold_set(cmd=cmd,
                            port=port_id,
                            tc=cfgs.tc,
                            threshold_hi=cfgs.threshold_hi,
                            threshold_lo=cfgs.threshold_lo)
    if rc != SX_STATUS_SUCCESS and cmd == SX_ACCESS_CMD_SET:
        rc = tele_threshold_set(cmd=SX_ACCESS_CMD_EDIT,
                                port=port_id,
                                tc=cfgs.tc,
                                threshold_hi=cfgs.threshold_hi,
                                threshold_lo=cfgs.threshold_lo)


if __name__ == '__main__':
        sys.exit(main(sys.argv[1:]))
