#!/usr/bin/python
# Copyright 2013 Mellanox Technologies, Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import sys

from networking_mlnx.eswitchd.cli import conn_utils
from networking_mlnx.eswitchd.cli import exceptions

client = conn_utils.ConnUtil()


def parse():
    """Main method that manages supported CLI commands.

    The actions that are supported throught the CLI are:
    write-sys, del-port, allocate-port and add-port
    Each action is matched with method that should handle it
    e.g. write-sys action is matched with  write_sys method
    """

    parser = argparse.ArgumentParser(prog='ebrctl')
    parser.add_argument('action', action='store_true')

    parent_parser = argparse.ArgumentParser(add_help=False)
    parent_parser.add_argument('vnic_mac')
    parent_parser.add_argument('device_id')
    parent_parser.add_argument('fabric')
    parent_parser.add_argument('vnic_type')
    subparsers = parser.add_subparsers()

    parser_add_port = subparsers.add_parser('add-port',
                                            parents=[parent_parser])
    parser_add_port.add_argument('dev_name')
    parser_add_port.set_defaults(func=add_port)

    parser_add_port = subparsers.add_parser('allocate-port',
                                            parents=[parent_parser])
    parser_add_port.set_defaults(func=allocate_port)

    parser_del_port = subparsers.add_parser('del-port')
    parser_del_port.set_defaults(func=del_port)
    parser_del_port.add_argument('fabric')
    parser_del_port.add_argument('vnic_mac')

    parser_write_sys = subparsers.add_parser('write-sys')
    parser_write_sys.set_defaults(func=write_sys)
    parser_write_sys.add_argument('path')
    parser_write_sys.add_argument('value')

    args = parser.parse_args()
    args.func(args)


def allocate_port(args):
    try:
        dev = client.allocate_nic(args.vnic_mac, args.device_id,
                                  args.fabric, args.vnic_type)
    except exceptions.MlxException as e:
        sys.stderr.write("Error in allocate command")
        sys.stderr.write(e.message)
        sys.exit(1)
    sys.stdout.write(dev)
    sys.exit(0)


def add_port(args):
    try:
        dev = client.plug_nic(args.vnic_mac, args.device_id, args.fabric,
                              args.vnic_type, args.dev_name)

    except exceptions.MlxException as e:
        sys.stderr.write("Error in add-port command")
        sys.stderr.write(e.message)
        sys.exit(1)
    sys.stdout.write(dev)
    sys.exit(0)


def del_port(args):
    try:
        client.deallocate_nic(args.vnic_mac, args.fabric)
    except exceptions.MlxException as e:
        sys.stderr.write("Error in del-port command")
        sys.stderr.write(e.message)
        sys.exit(1)
    sys.exit(0)


def write_sys(args):
    try:
        fd = open(args.path, 'w')
        fd.write(args.value)
        fd.close()
    except Exception as e:
        sys.stderr.write("Error in write-sys command")
        sys.stderr.write(e.message)
        sys.exit(1)
    sys.exit(0)


def main():
    parse()

[((23, 9, 23, 30), 'networking_mlnx.eswitchd.cli.conn_utils.ConnUtil', 'conn_utils.ConnUtil', ({}, {}), '()', False, 'from networking_mlnx.eswitchd.cli import conn_utils\n'), ((35, 13, 35, 51), 'argparse.ArgumentParser', 'argparse.ArgumentParser', (), '', False, 'import argparse\n'), ((38, 20, 38, 59), 'argparse.ArgumentParser', 'argparse.ArgumentParser', (), '', False, 'import argparse\n'), ((76, 4, 76, 25), 'sys.stdout.write', 'sys.stdout.write', ({(76, 21, 76, 24): 'dev'}, {}), '(dev)', False, 'import sys\n'), ((77, 4, 77, 15), 'sys.exit', 'sys.exit', ({(77, 13, 77, 14): '(0)'}, {}), '(0)', False, 'import sys\n'), ((89, 4, 89, 25), 'sys.stdout.write', 'sys.stdout.write', ({(89, 21, 89, 24): 'dev'}, {}), '(dev)', False, 'import sys\n'), ((90, 4, 90, 15), 'sys.exit', 'sys.exit', ({(90, 13, 90, 14): '(0)'}, {}), '(0)', False, 'import sys\n'), ((100, 4, 100, 15), 'sys.exit', 'sys.exit', ({(100, 13, 100, 14): '(0)'}, {}), '(0)', False, 'import sys\n'), ((112, 4, 112, 15), 'sys.exit', 'sys.exit', ({(112, 13, 112, 14): '(0)'}, {}), '(0)', False, 'import sys\n'), ((73, 8, 73, 53), 'sys.stderr.write', 'sys.stderr.write', ({(73, 25, 73, 52): '"""Error in allocate command"""'}, {}), "('Error in allocate command')", False, 'import sys\n'), ((74, 8, 74, 35), 'sys.stderr.write', 'sys.stderr.write', ({(74, 25, 74, 34): 'e.message'}, {}), '(e.message)', False, 'import sys\n'), ((75, 8, 75, 19), 'sys.exit', 'sys.exit', ({(75, 17, 75, 18): '(1)'}, {}), '(1)', False, 'import sys\n'), ((86, 8, 86, 53), 'sys.stderr.write', 'sys.stderr.write', ({(86, 25, 86, 52): '"""Error in add-port command"""'}, {}), "('Error in add-port command')", False, 'import sys\n'), ((87, 8, 87, 35), 'sys.stderr.write', 'sys.stderr.write', ({(87, 25, 87, 34): 'e.message'}, {}), '(e.message)', False, 'import sys\n'), ((88, 8, 88, 19), 'sys.exit', 'sys.exit', ({(88, 17, 88, 18): '(1)'}, {}), '(1)', False, 'import sys\n'), ((97, 8, 97, 53), 'sys.stderr.write', 'sys.stderr.write', ({(97, 25, 97, 52): '"""Error in del-port command"""'}, {}), "('Error in del-port command')", False, 'import sys\n'), ((98, 8, 98, 35), 'sys.stderr.write', 'sys.stderr.write', ({(98, 25, 98, 34): 'e.message'}, {}), '(e.message)', False, 'import sys\n'), ((99, 8, 99, 19), 'sys.exit', 'sys.exit', ({(99, 17, 99, 18): '(1)'}, {}), '(1)', False, 'import sys\n'), ((109, 8, 109, 54), 'sys.stderr.write', 'sys.stderr.write', ({(109, 25, 109, 53): '"""Error in write-sys command"""'}, {}), "('Error in write-sys command')", False, 'import sys\n'), ((110, 8, 110, 35), 'sys.stderr.write', 'sys.stderr.write', ({(110, 25, 110, 34): 'e.message'}, {}), '(e.message)', False, 'import sys\n'), ((111, 8, 111, 19), 'sys.exit', 'sys.exit', ({(111, 17, 111, 18): '(1)'}, {}), '(1)', False, 'import sys\n')]