diff options
author | imarom <imarom@cisco.com> | 2016-04-13 11:22:26 +0300 |
---|---|---|
committer | imarom <imarom@cisco.com> | 2016-04-13 11:23:07 +0300 |
commit | 0b39ec305e80999c7dbe36d4b0d3850b04709571 (patch) | |
tree | 3c372cfaf77dcd3d4db4cf60e896e8deb4676937 | |
parent | 071ef655638f6d6b93930404914bb70004d65c36 (diff) |
support for partial port acquire on console
--readonly, -a [ports], -f
4 files changed, 125 insertions, 23 deletions
diff --git a/scripts/automation/trex_control_plane/stl/console/trex_console.py b/scripts/automation/trex_control_plane/stl/console/trex_console.py index cb266505..a095541e 100755 --- a/scripts/automation/trex_control_plane/stl/console/trex_console.py +++ b/scripts/automation/trex_control_plane/stl/console/trex_console.py @@ -47,7 +47,7 @@ except: from functools import wraps -__version__ = "1.1" +__version__ = "2.0" # console custom logger class ConsoleLogger(LoggerApi): @@ -267,17 +267,19 @@ class TRexConsole(TRexGeneralCmd): if not self.stateless_client.is_connected(): self.prompt = "trex(offline)>" self.supported_rpc = None - return stop - if self.stateless_client.is_all_ports_acquired(): + elif not self.stateless_client.get_acquired_ports(): self.prompt = "trex(read-only)>" - return stop + elif self.stateless_client.is_all_ports_acquired(): + self.prompt = "trex>" - self.prompt = "trex>" + else: + self.prompt = "trex {0}>".format(self.stateless_client.get_acquired_ports()) return stop + def default(self, line): print("'{0}' is an unrecognized command. type 'help' or '?' for a list\n".format(line)) @@ -416,17 +418,35 @@ class TRexConsole(TRexGeneralCmd): ############### connect def do_connect (self, line): - '''Connects to the server\n''' + '''Connects to the server and acquire ports\n''' self.stateless_client.connect_line(line) + def help_connect (self): + self.do_connect("-h") def do_disconnect (self, line): '''Disconnect from the server\n''' self.stateless_client.disconnect_line(line) + + def do_acquire (self, line): + '''Acquire ports\n''' + + self.stateless_client.acquire_line(line) + def help_acquire (self): + self.do_acquire("-h") + + def do_release (self, line): + '''Release ports\n''' + + self.stateless_client.release_line(line) + + def help_release (self): + self.do_release("-h") + ############### start def complete_start(self, text, line, begidx, endidx): @@ -730,9 +750,24 @@ def setParserOptions(): default = False) - parser.add_argument("--no_acquire", dest="acquire", - action="store_false", help="Acquire all ports on connect. Default is: ON.", - default = True) + group = parser.add_mutually_exclusive_group() + + group.add_argument("-a", "--acquire", dest="acquire", + nargs = '+', + type = int, + help="Acquire ports on connect. default is all available ports", + default = None) + + group.add_argument("-r", "--readonly", dest="readonly", + action="store_true", + help="Starts console in a read only mode", + default = False) + + + parser.add_argument("-f", "--force", dest="force", + action="store_true", + help="Force acquire the requested ports", + default = False) parser.add_argument("--batch", dest="batch", nargs = 1, @@ -795,15 +830,19 @@ def main(): logger.log("Log:\n" + format_text(e.brief() + "\n", 'bold')) return - if not options.tui and options.acquire: + if not options.tui and not options.readonly: try: # acquire all ports - stateless_client.acquire() + stateless_client.acquire(options.acquire, force = options.force) except STLError as e: logger.log("Log:\n" + format_text(e.brief() + "\n", 'bold')) - logger.log(format_text("\nSwitching to read only mode - only few commands will be available", 'bold')) + + logger.log("\n*** Failed to acquire all required ports ***\n") + return + + if options.readonly: + logger.log(format_text("\nRead only mode - only few commands will be available", 'bold')) - # a script mode if options.batch: cont = run_script_file(options.batch[0], stateless_client) diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_client.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_client.py index 120f4d10..98f3fe3a 100755 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_client.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_client.py @@ -277,7 +277,7 @@ class EventsHandler(object): ev = "Port {0} was forcely taken by '{1}'".format(port_id, who) # call the handler - self.__async_event_port_forced_acquired(port_id) + self.__async_event_port_forced_acquired(port_id, who) show_event = True # server stopped @@ -316,8 +316,8 @@ class EventsHandler(object): self.client.ports[port_id].async_event_port_resumed() - def __async_event_port_forced_acquired (self, port_id): - self.client.ports[port_id].async_event_forced_acquired() + def __async_event_port_forced_acquired (self, port_id, who): + self.client.ports[port_id].async_event_forced_acquired(who) def __async_event_server_stopped (self): @@ -1000,7 +1000,8 @@ class STLClient(object): """ - return not (self.get_all_ports() == self.get_acquired_ports()) + return (self.get_all_ports() == self.get_acquired_ports()) + # is the client connected ? def is_connected (self): @@ -2023,11 +2024,11 @@ class STLClient(object): @__console def connect_line (self, line): - '''Connects to the TRex server''' - # define a parser + '''Connects to the TRex server and acquire ports''' parser = parsing_opts.gen_parser(self, "connect", self.connect_line.__doc__, + parsing_opts.PORT_LIST_WITH_ALL, parsing_opts.FORCE) opts = parser.parse_args(line.split()) @@ -2035,9 +2036,62 @@ class STLClient(object): if opts is None: return - # call the API self.connect() - self.acquire(force = opts.force) + self.acquire(ports = opts.ports, force = opts.force) + + # true means print time + return True + + @__console + def acquire_line (self, line): + '''Acquire ports\n''' + + # define a parser + parser = parsing_opts.gen_parser(self, + "acquire", + self.acquire_line.__doc__, + parsing_opts.PORT_LIST_WITH_ALL, + parsing_opts.FORCE) + + opts = parser.parse_args(line.split()) + + if opts is None: + return + + # call the API + ports = [x for x in opts.ports if x not in self.get_acquired_ports()] + if not ports: + self.logger.log("Port(s) {0} are already acquired\n".format(opts.ports)) + return + + self.acquire(ports = ports, force = opts.force) + + # true means print time + return True + + + # + @__console + def release_line (self, line): + '''Release ports\n''' + + parser = parsing_opts.gen_parser(self, + "release", + self.release_line.__doc__, + parsing_opts.PORT_LIST_WITH_ALL) + + opts = parser.parse_args(line.split()) + + if opts is None: + return + + # call the API + ports = [x for x in opts.ports if x in self.get_acquired_ports()] + if not ports: + self.logger.log("Port(s) {0} are not owned by you\n".format(opts.ports)) + return + + self.release(ports = ports) # true means print time return True diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_port.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_port.py index 87f7b437..6f6f50b1 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_port.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_port.py @@ -64,6 +64,8 @@ class Port(object): self.tx_stopped_ts = None self.has_rx_streams = False + self.owner = '' + def err(self, msg): return RC_ERR("port {0} : {1}\n".format(self.port_id, msg)) @@ -113,6 +115,11 @@ class Port(object): def is_paused (self): return (self.state == self.STATE_PAUSE) + def get_owner (self): + if self.is_acquired(): + return self.user + else: + return self.owner def sync(self): params = {"port_id": self.port_id} @@ -137,6 +144,7 @@ class Port(object): else: raise Exception("port {0}: bad state received from server '{1}'".format(self.port_id, port_state)) + self.owner = rc.data()['owner'] self.next_available_id = int(rc.data()['max_stream_id']) + 1 @@ -671,6 +679,7 @@ class Port(object): if not self.is_acquired(): self.state = self.STATE_TX - def async_event_forced_acquired (self): + def async_event_forced_acquired (self, who): self.handler = None + self.owner = who diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_stats.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_stats.py index dea7c6d5..f0ac5c33 100644 --- a/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_stats.py +++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/trex_stl_stats.py @@ -747,7 +747,7 @@ class CPortStats(CTRexStats): state = format_text(state, 'bold') - return {"owner": self._port_obj.user if self._port_obj else "", + return {"owner": self._port_obj.get_owner() if self._port_obj else "", "state": "{0}".format(state), "--": " ", |