#!/usr/bin/python
# Copyright 2017, Cumulus Networks, Inc.  All rights reserved.
#
# chassis-info:
#   Display information about a chassis
#
#

try:
    import argparse
    import cumulus.chassis
    import json
    import sys
except ImportError, e:
    raise ImportError (str(e) + "- required module not found")

def ParseCmdLine():
    """
    Parse the command line options using the standard argparse library.
    """
    # The default values for the arguments

    parser = argparse.ArgumentParser(description="Display information about a chassis.")
    parser.add_argument("--all", "-a", action='store_true', help="Display all information.")
    parser.add_argument("--json", "-j", action='store_true', help="Output in json format.")
    parser.add_argument("--slot", "-s", action='store_true', help="Display linear slot number.")
    parser.add_argument("--mgmtAddr", "-m", action='store_true', help="Display IPv6 link local management address.")
    parser.add_argument("--v4MgmtAddr", "-4", action='store_true', help="Display IPv4 link local management address.")
    parser.add_argument("--mgmtIf", "-i", action='store_true', help="Display name of management interface.")
    parser.add_argument("--fabIntfs", "-f", action='store_true', help="Display the number of fabric interfaces.")
    parser.add_argument("--isFabric", action='store_true', help="Display if this is a fabric card.")
    parser.add_argument("--moduleName", action='store_true', help="Display name of this card.")
    parser.add_argument("--chassisName", "-n", action='store_true', help="Display name of the chassis.")
    parser.add_argument("--v4Hosts", "-b", action='store_true', help="Display all IPv4 link local management addresses.")
    parser.add_argument("--v6Hosts", "-c", action='store_true', help="Display all IPv6 link local management addresses.")
    args = parser.parse_args()
    return(args)

def DisplayInfo(args):
    """
    Display the items which were requested
    """
    keys=[]
    values=[]
    ret = 0
    try:
        chassis = cumulus.chassis.probe()
        if args.all or args.slot:
            keys   += ["slot"]
            values += [chassis.GetSlotNum()]
        if args.all or args.mgmtAddr:
            keys   += ["mgmtAddr"]
            values += [chassis.GetMgmtAddr()]
        if args.all or args.v4MgmtAddr:
            keys   += ["v4MgmtAddr"]
            values += [chassis.GetV4MgmtAddr()]
        if args.all or args.mgmtIf:
            keys   += ["mgmtIf"]
            values += [chassis.GetMgmtIf()]
        if args.all or args.fabIntfs:
            keys   += ["fabIntfs"]
            values += [chassis.GetNumFabIntfs()]
        if args.all or args.isFabric:
            keys   += ["isFabric"]
            values += [chassis.isFabricCard()]
        if args.all or args.moduleName:
            keys   += ["moduleName"]
            values += [chassis.GetModuleName()]
        if args.all or args.chassisName:
            keys   += ["chassisName"]
            values += [chassis.GetChassisName()]
        if args.all or args.v4Hosts:
            keys   += ["v4Hosts"]
            values += [chassis.GetV4Hosts()]
        if args.all or args.v6Hosts:
            keys   += ["v6Hosts"]
            values += [chassis.GetV6Hosts()]
    except RuntimeError as e:
        keys   += ["error"]
        values += [str(e)]
        ret     = 1

    if args.json:
        print json.dumps(dict(zip(keys, values)), indent=4)
    else:
        print " ".join(str(v) for v in values)
    return(ret)

if __name__ == '__main__':
    args = ParseCmdLine()
    ret = DisplayInfo(args)
    sys.exit(ret)
