summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/examples/client_interactive_example.py
blob: d21b2b15e6e8f3f20693f18010ad2ac5a45a71b7 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/router/bin/python-2.7.4

import trex_root_path
from client.trex_client import *
from common.trex_exceptions import *
import cmd
import termstyle
import os
from argparse import ArgumentParser
from pprint import pprint
import json
import time
import socket
import errno

class InteractiveTRexClient(cmd.Cmd):

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

    def __init__(self, trex_host, max_history_size = 100, trex_port = 8090, verbose_mode = False ):
        cmd.Cmd.__init__(self)
        self.verbose = verbose_mode
        self.trex = CTRexClient(trex_host, max_history_size, trex_daemon_port = trex_port, verbose = verbose_mode)
        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)
        self.decoder = json.JSONDecoder()


    def do_push_files (self, filepaths):
        """Pushes a custom file to be stored locally on TRex server.\nPush multiple files by spefiying 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)

    def do_show_default_run_params(self,line):
        """Outputs the default TRex running parameters"""
        pprint(self.DEFAULT_RUN_PARAMS)
        print termstyle.green("*** End of default TRex running parameters ***")

    def do_show_run_params(self,line):
        """Outputs the currently configured TRex running parameters"""
        pprint(self.run_params)
        print termstyle.green("*** End of TRex running parameters ***")

    def do_update_run_params(self, json_str):
        """Updates provided parameters on TRex running configuration. Provide using JSON string"""
        if json_str:
            try:
                upd_params = self.decoder.decode(json_str)
                self.run_params.update(upd_params)
                print termstyle.green("*** End of TRex parameters update ***")
            except ValueError as inst:
                print termstyle.magenta("Provided illegal JSON string. Please try again.\n[", inst,"]")
        else:
            print termstyle.magenta("JSON configuration string is missing. Please try again.")
    
    def do_show_status (self, line):
        """Prompts TRex current status"""
        print self.trex.get_running_status()
        print termstyle.green("*** End of TRex status prompt ***")

    def do_show_trex_files_path (self, line):
        """Prompts the local path in which files are stored when pushed to trex server from client"""
        print self.trex.get_trex_files_path()
        print termstyle.green("*** End of trex_files_path prompt ***")

    def do_show_reservation_status (self, line):
        """Prompts if TRex is currently reserved or not"""
        if self.trex.is_reserved():
            print "TRex is reserved"
        else:
            print "TRex is NOT reserved"
        print termstyle.green("*** End of reservation status prompt ***")

    def do_reserve_trex (self, user):
        """Reserves the usage of TRex to a certain user"""
        try:
            if not user:
                ret = self.trex.reserve_trex()
            else:
                ret = self.trex.reserve_trex(user.split(' ')[0])
            print termstyle.green("*** TRex reserved successfully ***")
        except TRexException as inst:
            print termstyle.red(inst)

    def do_cancel_reservation (self, user):
        """Cancels a current reservation of TRex to a certain user"""
        try:
            if not user:
                ret = self.trex.cancel_reservation()
            else:
                ret = self.trex.cancel_reservation(user.split(' ')[0])
            print termstyle.green("*** TRex reservation canceled successfully ***")
        except TRexException as inst:
            print termstyle.red(inst)

    def do_restore_run_default (self, line):
        """Restores original TRex running configuration"""
        self.run_params = dict(self.DEFAULT_RUN_PARAMS)
        print termstyle.green("*** End of restoring default run parameters ***")

    def do_run_until_finish (self, sample_rate):
        """Starts TRex and sample server until run is done."""
        print termstyle.green("*** Starting TRex run_until_finish scenario ***")

        if not sample_rate: # use default sample rate if not passed
            sample_rate = 5
        try:
            sample_rate = int(sample_rate)
            ret = self.trex.start_trex(**self.run_params)
            self.trex.sample_to_run_finish(sample_rate)
            print termstyle.green("*** End of TRex run ***")
        except ValueError as inst:
            print termstyle.magenta("Provided illegal sample rate value. Please try again.\n[", inst,"]")
        except TRexException as inst:
            print termstyle.red(inst)

    def do_run_and_poll (self, sample_rate):
        """Starts TRex and sample server manually until run is done."""
        print termstyle.green("*** Starting TRex run and manually poll scenario ***")
        if not sample_rate: # use default sample rate if not passed
            sample_rate = 5
        try:
            sample_rate = int(sample_rate)
            ret = self.trex.start_trex(**self.run_params)
            last_res = dict()
            while self.trex.is_running(dump_out = last_res):
                obj = self.trex.get_result_obj()
                if (self.verbose):
                    print obj
                # do WHATEVER here
                time.sleep(sample_rate)
                
            print termstyle.green("*** End of TRex run ***")
        except ValueError as inst:
            print termstyle.magenta("Provided illegal sample rate value. Please try again.\n[", inst,"]")
        except TRexException as inst:
            print termstyle.red(inst)


    def do_run_until_condition (self, sample_rate):
        """Starts TRex and sample server until condition is satisfied."""
        print termstyle.green("*** Starting TRex run until condition is satisfied scenario ***")

        def condition (result_obj):
                return result_obj.get_current_tx_rate()['m_tx_pps'] > 200000

        if not sample_rate: # use default sample rate if not passed
            sample_rate = 5
        try:
            sample_rate = int(sample_rate)
            ret = self.trex.start_trex(**self.run_params)
            ret_val = self.trex.sample_until_condition(condition, sample_rate)
            print ret_val
            print termstyle.green("*** End of TRex run ***")
        except ValueError as inst:
            print termstyle.magenta("Provided illegal sample rate value. Please try again.\n[", inst,"]")
        except TRexException as inst:
            print termstyle.red(inst)

    def do_start_and_return (self, line):
        """Start TRex run and once in 'Running' mode, return to cmd prompt"""
        print termstyle.green("*** Starting TRex run, wait until in 'Running' state ***")
        try:
            ret = self.trex.start_trex(**self.run_params)
            print termstyle.green("*** End of scenario (TRex is probably still running!) ***")
        except TRexException as inst:
            print termstyle.red(inst)

    def do_poll_once (self, line):
        """Performs a single poll of TRex current data dump (if TRex is running) and prompts and short version of latest result_obj"""
        print termstyle.green("*** Trying TRex single poll ***")
        try:
            last_res = dict()
            if self.trex.is_running(dump_out = last_res):
                obj = self.trex.get_result_obj()
                print obj
            else:
                print termstyle.magenta("TRex isn't currently running.")
            print termstyle.green("*** End of scenario (TRex is posssibly still running!) ***")
        except TRexException as inst:
            print termstyle.red(inst)


    def do_stop_trex (self, line):
        """Try to stop TRex run (if TRex is currently running)"""
        print termstyle.green("*** Starting TRex termination ***")
        try:
            ret = self.trex.stop_trex()
            print termstyle.green("*** End of scenario (TRex is not running now) ***")
        except TRexException as inst:
            print termstyle.red(inst)

    def do_kill_indiscriminately (self, line):
        """Force killing of running TRex process (if exists) on the server."""
        print termstyle.green("*** Starting TRex termination ***")
        ret = self.trex.force_kill()
        if ret:
            print termstyle.green("*** End of scenario (TRex is not running now) ***")
        elif ret is None:
            print termstyle.magenta("*** End of scenario (TRex termination aborted) ***")
        else:
            print termstyle.red("*** End of scenario (TRex termination failed) ***")

    def do_exit(self, arg):
        """Quits the application"""
        print termstyle.cyan('Bye Bye!')
        return True


if __name__ == "__main__":
    parser = ArgumentParser(description = termstyle.cyan('Run TRex client 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 = 8090, metavar="PORT", dest="trex_port", 
        help="Select port on which the TRex server listens. Default port is 8090.", 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("--verbose", dest="verbose",
        action="store_true", help="Switch ON verbose option at TRex client. Default is: OFF.",
        default = False )
    args = parser.parse_args()
    
    try:
        InteractiveTRexClient(args.trex_host, args.hist_size, args.trex_port, args.verbose).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.")