Soft Patch Panel
 help / color / mirror / Atom feed
From: ogawa.yasufumi@lab.ntt.co.jp
To: spp@dpdk.org, ferruh.yigit@intel.com, ogawa.yasufumi@lab.ntt.co.jp
Subject: [spp] [PATCH 5/9] controller: move pri command to SppPrimary
Date: Thu, 18 Oct 2018 20:25:14 +0900	[thread overview]
Message-ID: <20181018112518.77556-6-ogawa.yasufumi@lab.ntt.co.jp> (raw)
In-Reply-To: <20181018112518.77556-1-ogawa.yasufumi@lab.ntt.co.jp>

From: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>

'spp.py' includes many methods and vals for supporting several commands.
Some of them are global and others are command's local. It would be hard
to maintain if more commands are added to 'spp.py'.

SppPrimary defines 'pri' 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 <ogawa.yasufumi@lab.ntt.co.jp>
---
 src/controller/commands/__init__.py |   0
 src/controller/commands/pri.py      | 123 ++++++++++++++++++++++++++++++++++++
 src/controller/shell.py             | 108 ++-----------------------------
 3 files changed, 129 insertions(+), 102 deletions(-)
 create mode 100644 src/controller/commands/__init__.py
 create mode 100644 src/controller/commands/pri.py

diff --git a/src/controller/commands/__init__.py b/src/controller/commands/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/src/controller/commands/pri.py b/src/controller/commands/pri.py
new file mode 100644
index 0000000..b51138d
--- /dev/null
+++ b/src/controller/commands/pri.py
@@ -0,0 +1,123 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2018 Nippon Telegraph and Telephone Corporation
+
+
+class SppPrimary(object):
+    """Exec SPP primary command.
+
+    SppPrimary class is intended to be used in Shell class as a delegator
+    for running 'pri' command.
+
+    'self.command()' is called from do_pri() and 'self.complete()' is called
+    from complete_pri() of both of which is defined in Shell.
+    """
+
+    # All of primary commands used for validation and completion.
+    PRI_CMDS = ['status', 'exit', 'clear']
+
+    def __init__(self, spp_ctl_cli):
+        self.spp_ctl_cli = spp_ctl_cli
+
+    def run(self, cmd):
+        """Called from do_pri() to Send command to primary process."""
+
+        if not (cmd in self.PRI_CMDS):
+            print("Invalid pri command: '%s'" % cmd)
+            return None
+
+        if cmd == 'status':
+            res = self.spp_ctl_cli.get('primary/status')
+            if res is not None:
+                if res.status_code == 200:
+                    self.print_status(res.json())
+                elif res.status_code in self.rest_common_error_codes:
+                    # Print default error message
+                    pass
+                else:
+                    print('Error: unknown response.')
+
+        elif cmd == 'clear':
+            res = self.spp_ctl_cli.delete('primary/status')
+            if res is not None:
+                if res.status_code == 204:
+                    print('Clear port statistics.')
+                elif res.status_code in self.rest_common_error_codes:
+                    pass
+                else:
+                    print('Error: unknown response.')
+
+        elif cmd == 'exit':
+            print('"pri; exit" is deprecated.')
+
+        else:
+            print('Invalid pri command!')
+
+    def print_status(self, json_obj):
+        """Parse SPP primary's status and print.
+
+        Primary returns the status as JSON format, but it is just a little
+        long.
+
+            {
+                "phy_ports": [
+                    {
+                        "eth": "56:48:4f:12:34:00",
+                        "id": 0,
+                        "rx": 78932932,
+                        "tx": 78932931,
+                        "tx_drop": 1,
+                    }
+                    ...
+                ],
+                "ring_ports": [
+                    {
+                        "id": 0,
+                        "rx": 89283,
+                        "rx_drop": 0,
+                        "tx": 89283,
+                        "tx_drop": 0
+                    },
+                    ...
+                ]
+            }
+
+        It is formatted to be simple and more understandable.
+
+            Physical Ports:
+              ID          rx          tx     tx_drop  mac_addr
+               0    78932932    78932931           1  56:48:4f:53:54:00
+            Ring Ports:
+              ID          rx          tx     rx_drop     rx_drop
+               0       89283       89283           0           0
+               ...
+        """
+
+        if 'phy_ports' in json_obj:
+            print('Physical Ports:')
+            print('  ID          rx          tx     tx_drop  mac_addr')
+            for pports in json_obj['phy_ports']:
+                print('  %2d  %10d  %10d  %10d  %s' % (
+                    pports['id'], pports['rx'],  pports['tx'],
+                    pports['tx_drop'], pports['eth']))
+
+        if 'ring_ports' in json_obj:
+            print('Ring Ports:')
+            print('  ID          rx          tx     rx_drop     rx_drop')
+            for rports in json_obj['ring_ports']:
+                print('  %2d  %10d  %10d  %10d  %10d' % (
+                    rports['id'], rports['rx'],  rports['tx'],
+                    rports['rx_drop'], rports['tx_drop']))
+
+    def complete(self, text, line, begidx, endidx):
+        """Completion for primary process commands.
+
+        Called from complete_pri() to complete primary command.
+        """
+
+        if not text:
+            completions = self.PRI_CMDS[:]
+        else:
+            completions = [p for p in self.PRI_CMDS
+                           if p.startswith(text)
+                           ]
+        return completions
diff --git a/src/controller/shell.py b/src/controller/shell.py
index f1c9ccd..9834df0 100644
--- a/src/controller/shell.py
+++ b/src/controller/shell.py
@@ -1,10 +1,10 @@
-#!/usr/bin/env python
 # SPDX-License-Identifier: BSD-3-Clause
 # Copyright(c) 2015-2016 Intel Corporation
 
 from __future__ import absolute_import
 
 import cmd
+from .commands import pri
 import os
 import re
 import readline
@@ -27,7 +27,6 @@ class Shell(cmd.Cmd, object):
 
     PORT_TYPES = ['phy', 'ring', 'vhost', 'pcap', 'nullpmd']
 
-    PRI_CMDS = ['status', 'exit', 'clear']
     SEC_CMDS = ['status', 'exit', 'forward', 'stop', 'add', 'patch', 'del']
     SEC_SUBCMDS = ['vhost', 'ring', 'pcap', 'nullpmd']
     BYE_CMDS = ['sec', 'all']
@@ -49,6 +48,7 @@ class Shell(cmd.Cmd, object):
     def __init__(self, spp_ctl_cli):
         cmd.Cmd.__init__(self)
         self.spp_ctl_cli = spp_ctl_cli
+        self.spp_primary = pri.SppPrimary(self.spp_ctl_cli)
 
     def default(self, line):
         """Define defualt behaviour.
@@ -131,62 +131,6 @@ class Shell(cmd.Cmd, object):
             else:
                 print('Error: unknown response.')
 
-    def print_pri_status(self, json_obj):
-        """Parse SPP primary's status and print.
-
-        Primary returns the status as JSON format, but it is just a little
-        long.
-
-            {
-                "phy_ports": [
-                    {
-                        "eth": "56:48:4f:12:34:00",
-                        "id": 0,
-                        "rx": 78932932,
-                        "tx": 78932931,
-                        "tx_drop": 1,
-                    }
-                    ...
-                ],
-                "ring_ports": [
-                    {
-                        "id": 0,
-                        "rx": 89283,
-                        "rx_drop": 0,
-                        "tx": 89283,
-                        "tx_drop": 0
-                    },
-                    ...
-                ]
-            }
-
-        It is formatted to be simple and more understandable.
-
-            Physical Ports:
-              ID          rx          tx     tx_drop  mac_addr
-               0    78932932    78932931           1  56:48:4f:53:54:00
-            Ring Ports:
-              ID          rx          tx     rx_drop     rx_drop
-               0       89283       89283           0           0
-               ...
-        """
-
-        if 'phy_ports' in json_obj:
-            print('Physical Ports:')
-            print('  ID          rx          tx     tx_drop  mac_addr')
-            for pports in json_obj['phy_ports']:
-                print('  %2d  %10d  %10d  %10d  %s' % (
-                    pports['id'], pports['rx'],  pports['tx'],
-                    pports['tx_drop'], pports['eth']))
-
-        if 'ring_ports' in json_obj:
-            print('Ring Ports:')
-            print('  ID          rx          tx     rx_drop     rx_drop')
-            for rports in json_obj['ring_ports']:
-                print('  %2d  %10d  %10d  %10d  %10d' % (
-                    rports['id'], rports['rx'],  rports['tx'],
-                    rports['rx_drop'], rports['tx_drop']))
-
     def print_sec_status(self, json_obj):
         """Parse and print message from SPP secondary.
 
@@ -218,35 +162,6 @@ class Shell(cmd.Cmd, object):
             else:
                 print('  - %s -> %s' % (port, dst))
 
-    def command_primary(self, command):
-        """Send command to primary process"""
-
-        if command == 'status':
-            res = self.spp_ctl_cli.get('primary/status')
-            if res is not None:
-                if res.status_code == 200:
-                    self.print_pri_status(res.json())
-                elif res.status_code in self.rest_common_error_codes:
-                    pass
-                else:
-                    print('Error: unknown response.')
-
-        elif command == 'clear':
-            res = self.spp_ctl_cli.delete('primary/status')
-            if res is not None:
-                if res.status_code == 204:
-                    print('Clear port statistics.')
-                elif res.status_code in self.rest_common_error_codes:
-                    pass
-                else:
-                    print('Error: unknown response.')
-
-        elif command == 'exit':
-            print('"pri; exit" is deprecated.')
-
-        else:
-            print('Invalid pri command!')
-
     def command_secondary(self, sec_id, command):
         """Send command to secondary process."""
 
@@ -442,23 +357,12 @@ class Shell(cmd.Cmd, object):
         if logger is not None:
             logger.info("Receive pri command: '%s'" % command)
 
-        if command and (command in self.PRI_CMDS):
-            self.command_primary(command)
-        else:
-            message = "Invalid pri command: '%s'" % command
-            print(message)
+        self.spp_primary.run(command)
 
     def complete_pri(self, text, line, begidx, endidx):
-        """Completion for primary process commands"""
+        """Completion for primary process commands."""
 
-        if not text:
-            completions = self.PRI_CMDS[:]
-        else:
-            completions = [p
-                           for p in self.PRI_CMDS
-                           if p.startswith(text)
-                           ]
-        return completions
+        return self.spp_primary.complete(text, line, begidx, endidx)
 
     def do_sec(self, arg):
         """Send a command to secondary process specified with ID.
@@ -666,7 +570,7 @@ class Shell(cmd.Cmd, object):
             print('Closing secondary ...')
             self.close_all_secondary()
             print('Closing primary ...')
-            self.command_primary('exit')
+            self.spp_primary.run('exit')
         elif cmds[0] == '':
             print('Thank you for using Soft Patch Panel')
             self.close()
-- 
2.13.1

  parent reply	other threads:[~2018-10-18 11:25 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-18 11:25 [spp] [PATCH 0/9] Change SPP controller to use spp-ctl ogawa.yasufumi
2018-10-18 11:25 ` [spp] [PATCH 1/9] controller: add spp-ctl client for SPP controller ogawa.yasufumi
2018-10-18 11:25 ` [spp] [PATCH 2/9] controller: change controller to use spp-ctl ogawa.yasufumi
2018-10-18 11:25 ` [spp] [PATCH 3/9] spp-ctl: add IP address binding ogawa.yasufumi
2018-10-18 11:25 ` [spp] [PATCH 4/9] controller: add IP address and port binding ogawa.yasufumi
2018-10-18 11:25 ` ogawa.yasufumi [this message]
2018-10-18 11:25 ` [spp] [PATCH 6/9] controller: move sec command to SppSecondary ogawa.yasufumi
2018-10-18 11:25 ` [spp] [PATCH 7/9] controller: move topo command to SppTopo ogawa.yasufumi
2018-10-18 11:25 ` [spp] [PATCH 8/9] controller: move topo_resize " ogawa.yasufumi
2018-10-18 11:25 ` [spp] [PATCH 9/9] controller: move bye command to SppBye ogawa.yasufumi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181018112518.77556-6-ogawa.yasufumi@lab.ntt.co.jp \
    --to=ogawa.yasufumi@lab.ntt.co.jp \
    --cc=ferruh.yigit@intel.com \
    --cc=spp@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).