#!/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()

[((752, 773), 'networking_mlnx.eswitchd.cli.conn_utils.ConnUtil', 'conn_utils.ConnUtil', ([], {}), '()\n', (771, 773), False, 'from networking_mlnx.eswitchd.cli import conn_utils\n'), ((1098, 1136), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ([], {'prog': '"""ebrctl"""'}), "(prog='ebrctl')\n", (1121, 1136), False, 'import argparse\n'), ((1213, 1252), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ([], {'add_help': '(False)'}), '(add_help=False)\n', (1236, 1252), False, 'import argparse\n'), ((2627, 2648), 'sys.stdout.write', 'sys.stdout.write', (['dev'], {}), '(dev)\n', (2643, 2648), False, 'import sys\n'), ((2653, 2664), 'sys.exit', 'sys.exit', (['(0)'], {}), '(0)\n', (2661, 2664), False, 'import sys\n'), ((2987, 3008), 'sys.stdout.write', 'sys.stdout.write', (['dev'], {}), '(dev)\n', (3003, 3008), False, 'import sys\n'), ((3013, 3024), 'sys.exit', 'sys.exit', (['(0)'], {}), '(0)\n', (3021, 3024), False, 'import sys\n'), ((3269, 3280), 'sys.exit', 'sys.exit', (['(0)'], {}), '(0)\n', (3277, 3280), False, 'import sys\n'), ((3537, 3548), 'sys.exit', 'sys.exit', (['(0)'], {}), '(0)\n', (3545, 3548), False, 'import sys\n'), ((2521, 2566), 'sys.stderr.write', 'sys.stderr.write', (['"""Error in allocate command"""'], {}), "('Error in allocate command')\n", (2537, 2566), False, 'import sys\n'), ((2575, 2602), 'sys.stderr.write', 'sys.stderr.write', (['e.message'], {}), '(e.message)\n', (2591, 2602), False, 'import sys\n'), ((2611, 2622), 'sys.exit', 'sys.exit', (['(1)'], {}), '(1)\n', (2619, 2622), False, 'import sys\n'), ((2881, 2926), 'sys.stderr.write', 'sys.stderr.write', (['"""Error in add-port command"""'], {}), "('Error in add-port command')\n", (2897, 2926), False, 'import sys\n'), ((2935, 2962), 'sys.stderr.write', 'sys.stderr.write', (['e.message'], {}), '(e.message)\n', (2951, 2962), False, 'import sys\n'), ((2971, 2982), 'sys.exit', 'sys.exit', (['(1)'], {}), '(1)\n', (2979, 2982), False, 'import sys\n'), ((3163, 3208), 'sys.stderr.write', 'sys.stderr.write', (['"""Error in del-port command"""'], {}), "('Error in del-port command')\n", (3179, 3208), False, 'import sys\n'), ((3217, 3244), 'sys.stderr.write', 'sys.stderr.write', (['e.message'], {}), '(e.message)\n', (3233, 3244), False, 'import sys\n'), ((3253, 3264), 'sys.exit', 'sys.exit', (['(1)'], {}), '(1)\n', (3261, 3264), False, 'import sys\n'), ((3430, 3476), 'sys.stderr.write', 'sys.stderr.write', (['"""Error in write-sys command"""'], {}), "('Error in write-sys command')\n", (3446, 3476), False, 'import sys\n'), ((3485, 3512), 'sys.stderr.write', 'sys.stderr.write', (['e.message'], {}), '(e.message)\n', (3501, 3512), False, 'import sys\n'), ((3521, 3532), 'sys.exit', 'sys.exit', (['(1)'], {}), '(1)\n', (3529, 3532), False, 'import sys\n')]