From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from tama500.ecl.ntt.co.jp (tama500.ecl.ntt.co.jp [129.60.39.148]) by dpdk.org (Postfix) with ESMTP id 4999F5B36 for ; Thu, 18 Oct 2018 13:25:39 +0200 (CEST) Received: from vc2.ecl.ntt.co.jp (vc2.ecl.ntt.co.jp [129.60.86.154]) by tama500.ecl.ntt.co.jp (8.13.8/8.13.8) with ESMTP id w9IBPckP009292; Thu, 18 Oct 2018 20:25:38 +0900 Received: from vc2.ecl.ntt.co.jp (localhost [127.0.0.1]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id 2C7FA63947C; Thu, 18 Oct 2018 20:25:38 +0900 (JST) Received: from localhost.localdomain (unknown [129.60.13.51]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id 1346663948B; Thu, 18 Oct 2018 20:25:38 +0900 (JST) From: ogawa.yasufumi@lab.ntt.co.jp To: spp@dpdk.org, ferruh.yigit@intel.com, ogawa.yasufumi@lab.ntt.co.jp Date: Thu, 18 Oct 2018 20:25:18 +0900 Message-Id: <20181018112518.77556-10-ogawa.yasufumi@lab.ntt.co.jp> X-Mailer: git-send-email 2.13.1 In-Reply-To: <20181018112518.77556-1-ogawa.yasufumi@lab.ntt.co.jp> References: <20181018112518.77556-1-ogawa.yasufumi@lab.ntt.co.jp> X-TM-AS-MML: disable Subject: [spp] [PATCH 9/9] controller: move bye command to SppBye X-BeenThere: spp@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Soft Patch Panel List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 18 Oct 2018 11:25:39 -0000 From: Yasufumi Ogawa SppBye defines 'bye' command and its completion as in a separated module. It is intended to be used from Shell, which is derived from 'cmd.Cmd'. Signed-off-by: Yasufumi Ogawa --- src/controller/commands/bye.py | 48 ++++++++++++++++++++++++++++++++++++++++++ src/controller/shell.py | 44 ++++++++++---------------------------- 2 files changed, 59 insertions(+), 33 deletions(-) create mode 100644 src/controller/commands/bye.py diff --git a/src/controller/commands/bye.py b/src/controller/commands/bye.py new file mode 100644 index 0000000..e5ca3bb --- /dev/null +++ b/src/controller/commands/bye.py @@ -0,0 +1,48 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2018 Nippon Telegraph and Telephone Corporation + + +class SppBye(object): + """Exec SPP bye command. + + SppBye class is intended to be used in Shell class as a delegator + for running 'bye' command. + + 'self.command()' is called from do_bye() and 'self.complete()' is called + from complete_bye() of both of which is defined in Shell. + """ + + BYE_CMDS = ['sec', 'all'] + + def __init__(self, spp_ctl_cli, spp_primary, spp_secondary): + self.spp_ctl_cli = spp_ctl_cli + self.spp_primary = spp_primary + self.spp_secondary = spp_secondary + + def run(self, args, sec_ids): + + cmds = args.split(' ') + if cmds[0] == 'sec': + self.close_all_secondary(sec_ids) + elif cmds[0] == 'all': + print('Closing secondary ...') + self.close_all_secondary(sec_ids) + print('Closing primary ...') + self.spp_primary.run('exit') + + def complete(self, text, line, begidx, endidx): + + if not text: + completions = self.BYE_CMDS[:] + else: + completions = [p + for p in self.BYE_CMDS + if p.startswith(text) + ] + return completions + + def close_all_secondary(self, sec_ids): + """Terminate all secondary processes.""" + + for i in sec_ids: + self.spp_secondary.run(i, 'exit') diff --git a/src/controller/shell.py b/src/controller/shell.py index bdc41fe..ec5f481 100644 --- a/src/controller/shell.py +++ b/src/controller/shell.py @@ -4,6 +4,7 @@ from __future__ import absolute_import import cmd +from .commands import bye from .commands import pri from .commands import sec from .commands import topo @@ -11,7 +12,6 @@ import os import re import readline from .shell_lib import common -from . import spp_common from .spp_common import logger import subprocess @@ -28,8 +28,6 @@ class Shell(cmd.Cmd, object): PORT_TYPES = ['phy', 'ring', 'vhost', 'pcap', 'nullpmd'] - BYE_CMDS = ['sec', 'all'] - HIST_EXCEPT = ['bye', 'exit', 'history', 'redo'] PLUGIN_DIR = 'command' @@ -49,6 +47,8 @@ class Shell(cmd.Cmd, object): self.spp_topo = topo.SppTopo(self.spp_ctl_cli, self.get_sec_ids('nfv'), {}, self.topo_size) + self.spp_bye = bye.SppBye(self.spp_ctl_cli, self.spp_primary, + self.spp_secondary) def default(self, line): """Define defualt behaviour. @@ -104,15 +104,6 @@ class Shell(cmd.Cmd, object): except IOError: print('Error: Cannot open history file "%s"' % self.hist_file) - def close_all_secondary(self): - """Terminate all secondary processes.""" - - tmp_list = [] - for i in spp_common.SECONDARY_LIST: - tmp_list.append(i) - for i in tmp_list: - self.command_secondary(i, 'exit') - def print_status(self): """Display information about connected clients.""" @@ -403,7 +394,7 @@ class Shell(cmd.Cmd, object): def complete_mkdir(self, text, line, begidx, endidx): return common.compl_common(text, line) - def do_bye(self, arg): + def do_bye(self, args): """Terminate SPP processes and controller. There are three usages for terminating processes. @@ -419,30 +410,17 @@ class Shell(cmd.Cmd, object): spp > bye """ - cmds = arg.split(' ') - if cmds[0] == 'sec': - self.close_all_secondary() - elif cmds[0] == 'all': - print('Closing secondary ...') - self.close_all_secondary() - print('Closing primary ...') - self.spp_primary.run('exit') - elif cmds[0] == '': - print('Thank you for using Soft Patch Panel') - self.close() + cmds = args.split(' ') + if cmds[0] == '': + self.do_exit('') return True + else: # 'all' or 'sec' + self.spp_bye.run(args, self.get_sec_ids('nfv')) def complete_bye(self, text, line, begidx, endidx): """Completion for bye commands""" - if not text: - completions = self.BYE_CMDS[:] - else: - completions = [p - for p in self.BYE_CMDS - if p.startswith(text) - ] - return completions + return self.spp_bye.complete(text, line, begidx, endidx) def do_cat(self, arg): """View contents of a file. @@ -592,7 +570,7 @@ class Shell(cmd.Cmd, object): label: vm1 subgraph: "vhost:1;vhost:2" """ - #logger.info("Topo initialized with sec IDs %s" % sec_ids) + # logger.info("Topo initialized with sec IDs %s" % sec_ids) # delimiter of node in dot file delim_node = '_' -- 2.13.1