[spp] [PATCH 4/4] controller: update for parsing JSON status
ogawa.yasufumi at lab.ntt.co.jp
ogawa.yasufumi at lab.ntt.co.jp
Thu Oct 4 07:59:18 CEST 2018
From: Yasufumi Ogawa <ogawa.yasufumi at lab.ntt.co.jp>
Because the response of status of spp_nfv and spp_vm is changed to
JSON format, update for parsing the JSON format.
Signed-off-by: Yasufumi Ogawa <ogawa.yasufumi at lab.ntt.co.jp>
---
src/controller/shell.py | 50 ++++++++++++++++---------------------------------
1 file changed, 16 insertions(+), 34 deletions(-)
diff --git a/src/controller/shell.py b/src/controller/shell.py
index fb30d5d..89c5c61 100644
--- a/src/controller/shell.py
+++ b/src/controller/shell.py
@@ -154,45 +154,27 @@ class Shell(cmd.Cmd, object):
def print_sec_status(self, msg):
"""Parse and print message from SPP secondary
- The format of sent message is expected as YAML like format as
+ The format of sent message is JSON and ended series of null
+ character "\x00". "ports" is a set of combinations of patches.
- status: idling\nports: 'phy:0-phy:1,phy:1-null'\x00\x00..
+ {"status":"idling","ports":[{"src":"phy:0", "dst": ...,]}'\x00\x00..
- 'ports' is a set of combinations of patches. The value is
- encapsulated with "'" and ended series of null character "\x00".
- If the destination is not defined, null is assigned.
+ If a port is not patched, the "dst" is set to "null".
"""
- msg = msg.replace("\x00", "").replace("'", "") # clean sec's msg
- sec_attr = msg.split("\n")
+ msg = msg.replace("\x00", "") # clean sec's msg
- # Do nothing if returned msg is not valid format.
- if len(sec_attr) < 2:
- return None
-
- status = sec_attr[0]
- ports = sec_attr[1]
-
- # Printed result to which port info is appended.
- res = status
-
- port_list = ports.split(' ')[1].split(',')
- if port_list[0] == '': # port_list is [''] if there are no ports
- res = '%s\nports: "no ports"' % res
- else:
- res = "%s\nports:\n" % res
- tmp_list = []
- for port_ent in port_list:
- if '-' in port_ent:
- p1, p2 = port_ent.split('-')
- if p2 == 'null':
- tmp_list.append(" - '%s'" % p1)
- else:
- tmp_list.append(" - '%s -> %s'" % (p1, p2))
- tmp_list.sort()
- res += "\n".join(tmp_list)
-
- print(res)
+ try:
+ sec_attr = json.loads(msg)
+ print('- status: %s' % sec_attr['status'])
+ print('- ports:')
+ cnt = 1
+ for port in sec_attr['ports']:
+ print(' %d: %s -> %s' % (cnt, port['src'], port['dst']))
+ cnt += 1
+ except ValueError as err:
+ print('Invalid format: {0}.'.format(err))
+ print(" '%s'" % msg)
def command_primary(self, command):
"""Send command to primary process"""
--
2.7.4
More information about the spp
mailing list