summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/examples/interactive_stateless.py
blob: e64b47558084b92311a1a3eeb2f4ffbe0377caa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.hig
#!/router/bin/python

import trex_root_path
from client.trex_stateless_client import *
from common.trex_exceptions import *
import cmd
from termstyle import termstyle
# import termstyle
import os
from argparse import ArgumentParser
import socket
import errno
import ast
import json


class InteractiveStatelessTRex(cmd.Cmd):

    intro = termstyle.green("\nInteractive shell to play with Cisco's TRex stateless API.\
                             \nType help to view available pre-defined scenarios\n(c) All rights reserved.\n")
    prompt = '> '

    def __init__(self, trex_host, trex_port, virtual, verbose):
        cmd.Cmd.__init__(self)

        self.verbose = verbose
        self.virtual = virtual
        self.trex = CTRexStatelessClient(trex_host, trex_port, self.virtual)
        self.DEFAULT_RUN_PARAMS = dict(m=1.5,
                                       nc=True,
                                       p=True,
                                       d=100,
                                       f='avl/sfr_delay_10_1g.yaml',
                                       l=1000)
        self.run_params = dict(self.DEFAULT_RUN_PARAMS)

    def do_transmit(self, line):
        """Transmits a request over using a given link to server.\
        \nuse: transmit [method_name] [method_params]"""
        if line == "":
            print "\nUsage: [method name] [param dict as string]\n"
            print "Example: rpc test_add {'x': 12, 'y': 17}\n"
            return

        args = line.split(' ', 1)   # args will have max length of 2
        method_name = args[0]
        params = None
        bad_parse = False

        try:
            params = ast.literal_eval(args[1])
            if not isinstance(params, dict):
                bad_parse = True
        except ValueError as e1:
            bad_parse = True
        except SyntaxError as e2:
            bad_parse = True

        if bad_parse:
            print "\nValue should be a valid dict: '{0}'".format(args[1])
            print "\nUsage: [method name] [param dict as string]\n"
            print "Example: rpc test_add {'x': 12, 'y': 17}\n"
            return

        response = self.trex.transmit(method_name, params)
        if not self.virtual:
            # expect response
            rc, msg = response
            if rc:
                print "\nServer Response:\n\n" + json.dumps(msg) + "\n"
            else:
                print "\n*** " + msg + "\n"





    def do_push_files(self, filepaths):
        """Pushes a custom file to be stored locally on TRex server.\
        \nPush multiple files by specifying their path separated by ' ' (space)."""
        try:
            filepaths = filepaths.split(' ')
            print termstyle.green("*** Starting pushing files ({trex_files}) to TRex. ***".format(
                trex_files=', '.join(filepaths))
            )
            ret_val = self.trex.push_files(filepaths)
            if ret_val:
                print termstyle.green("*** End of TRex push_files method (success) ***")
            else:
                print termstyle.magenta("*** End of TRex push_files method (failed) ***")

        except IOError as inst:
            print termstyle.magenta(inst)

if __name__ == "__main__":
    parser = ArgumentParser(description=termstyle.cyan('Run TRex client stateless API demos and scenarios.'),
                            usage="client_interactive_example [options]")

    parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0 \t (C) Cisco Systems Inc.\n')

    parser.add_argument("-t", "--trex-host", required = True, dest="trex_host",
        action="store", help="Specify the hostname or ip to connect with TRex server.",
        metavar="HOST" )
    parser.add_argument("-p", "--trex-port", type=int, default = 5050, metavar="PORT", dest="trex_port",
        help="Select port on which the TRex server listens. Default port is 5050.", action="store")
    # parser.add_argument("-m", "--maxhist", type=int, default = 100, metavar="SIZE", dest="hist_size",
    #     help="Specify maximum history size saved at client side. Default size is 100.", action="store")
    parser.add_argument("--virtual", dest="virtual",
                        action="store_true",
                        help="Switch ON virtual option at TRex client. Default is: OFF.",
                        default=False)
    parser.add_argument("--verbose", dest="verbose",
                        action="store_true",
                        help="Switch ON verbose option at TRex client. Default is: OFF.",
                        default=False)
    args = parser.parse_args()

    try:
        InteractiveStatelessTRex(**vars(args)).cmdloop()

    except KeyboardInterrupt:
        print termstyle.cyan('Bye Bye!')
        exit(-1)
    except socket.error, e:
        if e.errno == errno.ECONNREFUSED:
            raise socket.error(errno.ECONNREFUSED,
                               "Connection from TRex server was terminated. \
                               Please make sure the server is up.")