#!/usr/bin/python
# $Id: radbclient 33373 2016-01-22 11:01:15Z schaap $

'''
do requests to the radbservice from the commandline
'''
import sys
import logging
from optparse import OptionParser
from lofar.sas.resourceassignment.resourceassignmentservice.rpc import RARPC
from lofar.sas.resourceassignment.resourceassignmentservice.config import DEFAULT_BUSNAME, DEFAULT_SERVICENAME

logger = logging.getLogger(__name__)

if __name__ == '__main__':
    # Check the invocation arguments
    parser = OptionParser('%prog [options]',
                          description='do requests to the radbservice from the commandline')
    parser.add_option('-q', '--broker', dest='broker', type='string', default=None, help='Address of the qpid broker, default: localhost')
    parser.add_option('-b', '--busname', dest='busname', type='string', default=DEFAULT_BUSNAME, help='Name of the bus exchange on the qpid broker, default: %s' % DEFAULT_BUSNAME)
    parser.add_option('-s', '--servicename', dest='servicename', type='string', default=DEFAULT_SERVICENAME, help='Name for this service, default: %s' % DEFAULT_SERVICENAME)
    parser.add_option('-V', '--verbose', dest='verbose', action='store_true', help='verbose logging')
    parser.add_option('-t', '--task', dest='task', type='int', help='get the task with given id')
    parser.add_option('-T', '--tasks', dest='tasks', action='store_true', help='get all tasks')
    parser.add_option('-c', '--resource_claim', dest='resource_claim', type='int', help='get the resource claim with given id')
    parser.add_option('-C', '--resource_claims', dest='resource_claims', action='store_true', help='get all resource claims')
    (options, args) = parser.parse_args()

    if len(sys.argv) == 1:
        parser.print_help()

    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
                        level=logging.INFO if options.verbose else logging.WARN)

    with RARPC(busname=options.busname, servicename=options.servicename, broker=options.broker) as rpc:
        if options.task:
            print rpc.getTask(options.task)

        if options.tasks:
            for task in rpc.getTasks():
                print task

        if options.resource_claim:
            print rpc.getResourceClaim(options.resource_claim)

        if options.resource_claims:
            for task in rpc.getResourceClaims():
                print task