# SPDX-License-Identifier: LGPL-2.0-or-later
#
# Copyright (c) 2015 Kontron Europe GmbH
#

import logging
import argparse

from . import Ixia
from .helper import obj_match_attribute_value

ALLOWED_COMMANDS = 'take_ownership clear_ownership \
        start_transmit stop_transmit step_transmit pause_transmit \
        start_capture stop_capture reset_statistics'.split()

ALLOWED_STATISTICS = 'bytes_received bytes_sent \
        bits_received bits_sent \
        frames_received frames_sent \
        fcs_errors framer_fcs_errors fragments'.split()


def exec_commands(pg, commands_list):
    for command in commands_list:
        getattr(pg, command)()


def get_statistics(i, ports, stats):
    fmt = '{!s:>8} ' + ' | {:<18}' * len(stats)
    for port in ports:
        print(fmt.format(port, *map(lambda s: getattr(port.stats, s), stats)))


def print_ports(i):
    print('%8s | %6s | %s' % ('Port', 'Link', 'Owner'))
    print('---------+--------+-----------------')
    for card in i.chassis.cards:
        if card is None:
            continue
        for port in card.ports:
            state = obj_match_attribute_value(port, 'LINK_STATE_',
                                              port.link_state)
            print('%8s | %6s | %s' % (port, state.lower(), port.owner))


def main():
    parser = argparse.ArgumentParser(prog="ixcli")
    parser.add_argument('url',
                        help='URL to connect to', metavar='URL')
    parser.add_argument('ports', nargs='*',
                        help='operate on these ports', metavar='PORTS')
    parser.add_argument('-v', action='store_true', dest='verbose',
                        help='be more verbose')
    parser.add_argument('-d', action='store_true', dest='debug',
                        help='enable debug mode')
    parser.add_argument('-u', '--user', dest='user', default='pyixia',
                        help='user login', metavar='OWNER')
    parser.add_argument('-c', '--commands', dest='commands',
                        choices=ALLOWED_COMMANDS, action='append',
                        help='execute commands', metavar='CMD')
    parser.add_argument('-s', '--statistics', dest='statistics',
                        choices=ALLOWED_STATISTICS, action='append',
                        help='show statistics', metavar='STAT')

    args = parser.parse_args()

    logging.basicConfig()
    if args.verbose:
        logging.getLogger('').setLevel(logging.INFO)
    if args.debug:
        logging.getLogger('').setLevel(logging.DEBUG)

    i = Ixia(args.url)
    i.connect()
    i.discover()

    if args.commands or args.statistics:
        i.session.login(args.user)
        pg = i.new_port_group()
        pg.create()
        for port in args.ports:
            pg.add_port(i.get_port(port))

        if args.commands:
            exec_commands(pg, args.commands)

        if args.statistics:
            get_statistics(i, pg.ports, args.statistics)

        pg.destroy()
    else:
        print_ports(i)

    i.disconnect()


if __name__ == '__main__':
    main()

@@ -99,16 +99,27 @@
 logging%0A
+import sys%0A
 import a
@@ -201,22 +201,21 @@
 ALLOWED_
-COMMAN
+PG_CM
 DS = 'ta
@@ -374,17 +374,16 @@
 split()%0A
-%0A
 ALLOWED_
@@ -386,21 +386,16 @@
 WED_STAT
-ISTIC
 S = 'byt
@@ -557,39 +557,28 @@
 def 
-exec_commands(pg, commands_list
+run_pg_cmds(pg, cmds
 ):%0A 
@@ -589,31 +589,18 @@
 or c
-omman
+m
 d in c
-ommands_list
+mds
 :%0A  
@@ -618,21 +618,17 @@
 tr(pg, c
-omman
+m
 d)()%0A%0A%0Ad
@@ -630,29 +630,26 @@
 )%0A%0A%0Adef 
-ge
+prin
 t_stat
-istic
 s(i, por
@@ -713,27 +713,26 @@
 s)%0A    for p
-ort
+id
  in ports:%0A 
@@ -726,24 +726,55 @@
 d in ports:%0A
+        port = i.get_port(pid)%0A
         prin
@@ -1952,24 +1952,26 @@
 -c', '--
+pg-
 command
-s
 ', dest=
@@ -1971,22 +1971,21 @@
 , dest='
-comman
+pg_cm
 ds',%0A   
@@ -2025,14 +2025,13 @@
 WED_
-COMMAN
+PG_CM
 DS, 
@@ -2081,15 +2081,22 @@
 lp='
-execute
+run port group
  com
@@ -2151,29 +2151,24 @@
 -s', '--stat
-istic
 s', dest='st
@@ -2169,21 +2169,16 @@
 st='stat
-istic
 s',%0A    
@@ -2221,13 +2221,8 @@
 STAT
-ISTIC
 S, a
@@ -2576,46 +2576,112 @@
  if 
+not 
 args.
-commands or args.statistics:%0A    
+pg_cmds and not args.stats:%0A        print_ports(i)%0A        i.disconnect()%0A        sys.exit(0)%0A%0A
     
@@ -2706,16 +2706,38 @@
 gs.user)
+%0A%0A    if args.pg_cmds:
 %0A       
@@ -2868,49 +2868,17 @@
     
-if args.commands:%0A            exec_comman
+run_pg_cm
 ds(p
@@ -2885,22 +2885,21 @@
 g, args.
-comman
+pg_cm
 ds)%0A%0A   
@@ -2895,24 +2895,42 @@
 _cmds)%0A%0A    
+    pg.destroy()%0A%0A
     if args.
@@ -2933,21 +2933,16 @@
 rgs.stat
-istic
 s:%0A     
@@ -2948,32 +2948,27 @@
     
-    ge
+prin
 t_stat
-istic
 s(i, 
-pg
+args
 .por
@@ -2984,69 +2984,9 @@
 stat
-istics)%0A%0A        pg.destroy()%0A    else:%0A        print_ports(i
+s
 )%0A%0A 

prepare new port command