fixeria has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/erlang/osmo-s1gw/+/41172?usp=email )


Change subject: [REST] osmo-s1gw-cli.py: implement eNB/E-RAB list sorting
......................................................................

[REST] osmo-s1gw-cli.py: implement eNB/E-RAB list sorting

Change-Id: Iee7a922479fff7c2038f50325b5f6dfe39c16969
Related: SYS#7066
---
M contrib/osmo-s1gw-cli.py
M doc/osmo-s1gw-cli.md
2 files changed, 78 insertions(+), 17 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/erlang/osmo-s1gw 
refs/changes/72/41172/1

diff --git a/contrib/osmo-s1gw-cli.py b/contrib/osmo-s1gw-cli.py
index ac4b1c6..484a650 100755
--- a/contrib/osmo-s1gw-cli.py
+++ b/contrib/osmo-s1gw-cli.py
@@ -244,8 +244,11 @@
             '# E-RABs': item.get('erab_count'),
         }

-    def enb_list_print(self, items: list[dict]) -> None:
-        ''' Print a list of eNBs in tabular form '''
+    def enb_list_print(self, items: list[dict],
+                             sort_by: str = 'handle',
+                             reverse: bool = False) -> None:
+        ''' Print a sorted list of eNBs in tabular form '''
+        items.sort(key=lambda item: item.get(sort_by), reverse=reverse)
         self.poutput(tabulate.tabulate(map(self.enb_list_item, items),
                                        headers='keys', tablefmt=self.tablefmt))

@@ -255,11 +258,31 @@
                                        headers=['Parameter', 'Value'],
                                        tablefmt=self.tablefmt))

+    @staticmethod
+    def add_sort_group(parser,
+                       default: str,
+                       choices: set[str]) -> None:
+        ''' Add argparse group with sorting params '''
+        sort_group = parser.add_argument_group('Sorting options')
+        sort_group.add_argument('-S', '--sort-by',
+                                type=str,
+                                default=default,
+                                choices=choices,
+                                help='Sort by (default: %(default)s)')
+        sort_group.add_argument('--reverse',
+                                action='store_true',
+                                help='Reverse order (default: %(default)s)')
+
+    enb_list_parser = cmd2.Cmd2ArgumentParser()
+    add_sort_group(enb_list_parser, default='handle',
+                   choices=('handle', 'pid', 'state', 'genb_id', 'uptime'))
+
+    @cmd2.with_argparser(enb_list_parser)
     @cmd2.with_category(CAT_ENB)
     def do_enb_list(self, opts) -> None:
         ''' Get a list of eNB connections '''
         data = self.iface.enb_list()
-        self.enb_list_print(data)
+        self.enb_list_print(data, opts.sort_by, opts.reverse)

     @staticmethod
     def gen_enb_id(opts) -> str:
@@ -341,8 +364,12 @@
             'U2A F-TEID': f_teid(item.get('f_teid_u2a')) if 'f_teid_u2a' in 
item else None,
         }

-    def erab_list_print(self, items: list[dict], full: bool) -> None:
+    def erab_list_print(self, items: list[dict],
+                              full: bool = False,
+                              sort_by: str = 'pid',
+                              reverse: bool = False) -> None:
         ''' Print a list of E-RABs in tabular form '''
+        items.sort(key=lambda item: item.get(sort_by), reverse=reverse)
         func = self.erab_list_item_full if full else self.erab_list_item
         self.poutput(tabulate.tabulate(map(func, items),
                                        headers='keys', tablefmt=self.tablefmt))
@@ -357,6 +384,8 @@
     enb_erab_list_parser.add_argument('-f', '--full',
                                       action='store_true',
                                       help='Print full table (more columns)')
+    add_sort_group(enb_erab_list_parser, default='pid',
+                   choices=('pid', 'state', 'mme_ue_id', 'erab_id'))
     add_enb_id_group(enb_erab_list_parser)

     @cmd2.with_argparser(enb_erab_list_parser)
@@ -365,19 +394,21 @@
         ''' Get E-RAB list for a specific eNB '''
         enb_id = self.gen_enb_id(opts)
         data = self.iface.enb_erab_list(enb_id)
-        self.erab_list_print(data, opts.full)
+        self.erab_list_print(data, opts.full, opts.sort_by, opts.reverse)

     erab_list_parser = cmd2.Cmd2ArgumentParser()
     erab_list_parser.add_argument('-f', '--full',
                                   action='store_true',
                                   help='Print full table (more columns)')
+    add_sort_group(erab_list_parser, default='pid',
+                   choices=('pid', 'state', 'mme_ue_id', 'erab_id'))

     @cmd2.with_argparser(erab_list_parser)
     @cmd2.with_category(CAT_ERAB)
     def do_erab_list(self, opts) -> None:
         ''' Get E-RAB list for all eNBs '''
         data = self.iface.erab_list()
-        self.erab_list_print(data, opts.full)
+        self.erab_list_print(data, opts.full, opts.sort_by, opts.reverse)
 
     erab_info_parser = cmd2.Cmd2ArgumentParser()
     erab_info_parser.add_argument('-P', '--pid',
diff --git a/doc/osmo-s1gw-cli.md b/doc/osmo-s1gw-cli.md
index b801b25..d407019 100644
--- a/doc/osmo-s1gw-cli.md
+++ b/doc/osmo-s1gw-cli.md
@@ -156,6 +156,23 @@
 Get a list of eNB connections.

 ```
+Usage: enb_list [-h]
+                [-S {handle, pid, state, genb_id, uptime}] [--reverse]
+
+Get a list of eNB connections
+
+optional arguments:
+  -h, --help            show this help message and exit
+
+Sorting options:
+  -S, --sort-by {handle, pid, state, genb_id, uptime}
+                        Sort by (default: handle)
+  --reverse             Reverse order (default: False)
+```
+
+Example: getting a list of eNBs (by default, sorted by handle).
+
+```
 OsmoS1GW# enb_list
 |   eNB handle | PID       | Global-eNB-ID   | State   | eNB addr:port (aid)   
  | MME addr:port (aid)     |   Uptime (s) |   # E-RABs |
 
|--------------|-----------|-----------------|---------|-------------------------|-------------------------|--------------|------------|
@@ -242,20 +259,27 @@
 Get E-RAB list for a specific eNB.

 ```
-Usage: enb_erab_list [-h] [-f] (-H HANDLE | -P PID | -G GENBID | 
--enb-sctp-aid AID | --mme-sctp-aid AID)
+Usage: enb_erab_list [-h] [-f]
+                     [-S {pid, state, mme_ue_id, erab_id}] [--reverse]
+                     (-H HANDLE | -P PID | -G GENBID | --enb-sctp-aid AID | 
--mme-sctp-aid AID)

 Get E-RAB list for a specific eNB

 optional arguments:
-  -h, --help           show this help message and exit
-  -f, --full           Print full table (more columns)
+  -h, --help            show this help message and exit
+  -f, --full            Print full table (more columns)
+
+Sorting options:
+  -S, --sort-by {pid, state, mme_ue_id, erab_id}
+                        Sort by (default: pid)
+  --reverse             Reverse order (default: False)

 eNB ID:
-  -H, --handle HANDLE  eNB handle (example: 0)
-  -P, --pid PID        eNB process ID (example: 0.33.1)
-  -G, --genbid GENBID  Global-eNB-ID (example: 262-42-1337)
-  --enb-sctp-aid AID   eNB association identifier (example: 42)
-  --mme-sctp-aid AID   MME association identifier (example: 42)
+  -H, --handle HANDLE   eNB handle (example: 0)
+  -P, --pid PID         eNB process ID (example: 0.33.1)
+  -G, --genbid GENBID   Global-eNB-ID (example: 262-42-1337)
+  --enb-sctp-aid AID    eNB association identifier (example: 42)
+  --mme-sctp-aid AID    MME association identifier (example: 42)
 ```

 Example: Obtaining E-RAB list for an eNB with the given Global-eNB-ID.
@@ -264,8 +288,8 @@
 OsmoS1GW# enb_erab_list -G 001-01-0
 | PID       |   MME-UE-S1AP-ID |   E-RAB-ID | State      |
 |-----------|------------------|------------|------------|
-| <0.708.0> |             4242 |          1 | erab_setup |
 | <0.707.0> |             4242 |          0 | erab_setup |
+| <0.708.0> |             4242 |          1 | erab_setup |
 | <0.709.0> |             4242 |          2 | erab_setup |
 ```

@@ -275,12 +299,18 @@

 ```
 Usage: erab_list [-h] [-f]
+                 [-S {pid, state, mme_ue_id, erab_id}] [--reverse]

 Get E-RAB list for all eNBs

 optional arguments:
-  -h, --help  show this help message and exit
-  -f, --full  Print full table (more columns)
+  -h, --help            show this help message and exit
+  -f, --full            Print full table (more columns)
+
+Sorting options:
+  -S, --sort-by {pid, state, mme_ue_id, erab_id}
+                        Sort by (default: pid)
+  --reverse             Reverse order (default: False)
 ```

 The output format is the same as produced by the `enb_erab_list` command.

--
To view, visit https://gerrit.osmocom.org/c/erlang/osmo-s1gw/+/41172?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: erlang/osmo-s1gw
Gerrit-Branch: master
Gerrit-Change-Id: Iee7a922479fff7c2038f50325b5f6dfe39c16969
Gerrit-Change-Number: 41172
Gerrit-PatchSet: 1
Gerrit-Owner: fixeria <[email protected]>

Reply via email to