Uptabify onoscli by replacing <TAB> by 8 spaces.
In Emacs (over the whole file): M-x untabify
Also, remove "indent-tabs-mode: t;" from the Emacs-config header, so it
defaults to untabify mode.
No functional changes.
Change-Id: I9944ce3354f822c474e3e7e99dfeaf2c57f1194c
diff --git a/onoscli b/onoscli
index f5fc0e1..20f9bc7 100755
--- a/onoscli
+++ b/onoscli
@@ -1,5 +1,5 @@
#! /usr/bin/env python
-# -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*-
+# -*- Mode: python; py-indent-offset: 4; tab-width: 8; -*-
"""
onoscli : ONOS-specific Command Line Interface
@@ -49,20 +49,20 @@
"Command node. A hierarchy of nodes are organized in a command tree."
def __init__(self, name, help, callback=None, add_parser_args=None):
- """name: a string with the full command name
- help: the help string for the command
+ """name: a string with the full command name
+ help: the help string for the command
callback: the method to be called if the command is executed
- add_parser_args: the parser arguments to add to the command: a dictionary of argparse arguments"""
- # Normalize the name by removing extra spaces
- self.split_name = name.split() # List of the words in the name
- self.name = ' '.join(self.split_name) # Normalized name
- self.last_subname = self.split_name[-1] # Last word in the name
- self.parent_name = ' '.join(self.split_name[:-1]) # Name of parent command
- self.help = help # THe help string
- self.callback = callback # The command callback
- self.add_parser_args = add_parser_args # Parser arguments to add
- self.parent = None # The parent Command
- self.children = [] # The children Command entries
+ add_parser_args: the parser arguments to add to the command: a dictionary of argparse arguments"""
+ # Normalize the name by removing extra spaces
+ self.split_name = name.split() # List of the words in the name
+ self.name = ' '.join(self.split_name) # Normalized name
+ self.last_subname = self.split_name[-1] # Last word in the name
+ self.parent_name = ' '.join(self.split_name[:-1]) # Name of parent command
+ self.help = help # THe help string
+ self.callback = callback # The command callback
+ self.add_parser_args = add_parser_args # Parser arguments to add
+ self.parent = None # The parent Command
+ self.children = [] # The children Command entries
class OnosCli(Cmd):
@@ -78,7 +78,7 @@
settable = settable + "onos_ip ONOS IP address"
onos_port = 8080
settable = settable + "onos_port ONOS REST port number"
- output_format = "json" # Valid values: json, text
+ output_format = "json" # Valid values: json, text
settable = settable + "output_format The output format: `text` or `json`"
# Collection of commands sorted by the level in the CLI command hierarchy
@@ -89,55 +89,55 @@
subparsers_dict = {}
def __init__(self):
- Cmd.__init__(self)
+ Cmd.__init__(self)
- #
- # An array of the ONOS-specific CLI commands.
- # Each entry is a Command instance, and must have at least
- # two arguments:
- # * Command name as typed on the CLI. E.g.:
- # "show intent"
- # * Command help description. E.g.:
- # "Show intents"
- #
- # Executable commands should have a third Command argument, which is
- # the name of the method to be called when the command is executed.
- # The method will be called with the (argparse) parsed arguments
- # for that command.
- #
- # If an executable command takes arguments, those should be described
- # in the Command's fourth argument. It is a list of pairs:
- # [
- # ("--argument-name1", dict(...)),
- # ("--argument-name2", dict(...)),
- # ...
- # ]
- # where the first entry in the pair is the argument name, and the
- # second entry in the pair is a dictionary with argparse-specific
- # description of the argument.
- #
- init_commands = [
- Command("delete", "Delete command"),
- #
- Command("delete intent",
- """Delete high-level intents
+ #
+ # An array of the ONOS-specific CLI commands.
+ # Each entry is a Command instance, and must have at least
+ # two arguments:
+ # * Command name as typed on the CLI. E.g.:
+ # "show intent"
+ # * Command help description. E.g.:
+ # "Show intents"
+ #
+ # Executable commands should have a third Command argument, which is
+ # the name of the method to be called when the command is executed.
+ # The method will be called with the (argparse) parsed arguments
+ # for that command.
+ #
+ # If an executable command takes arguments, those should be described
+ # in the Command's fourth argument. It is a list of pairs:
+ # [
+ # ("--argument-name1", dict(...)),
+ # ("--argument-name2", dict(...)),
+ # ...
+ # ]
+ # where the first entry in the pair is the argument name, and the
+ # second entry in the pair is a dictionary with argparse-specific
+ # description of the argument.
+ #
+ init_commands = [
+ Command("delete", "Delete command"),
+ #
+ Command("delete intent",
+ """Delete high-level intents
Usage:
delete intent --intent-id INTENT_ID Delete a high-level intent
delete intent --all Delete all high-level intents
Arguments:
--intent-id INTENT_ID The Intent ID (an integer)
--all Delete all high-level intents""",
- self.delete_intent,
- [
- ("--intent-id", dict(required=False, type=int)),
- ("--all", dict(required=False, action='store_true'))
- ]
- ),
- #
- Command("set", "Set command"),
- #
- Command("set intent",
- """Set a high-level intent
+ self.delete_intent,
+ [
+ ("--intent-id", dict(required=False, type=int)),
+ ("--all", dict(required=False, action='store_true'))
+ ]
+ ),
+ #
+ Command("set", "Set command"),
+ #
+ Command("set intent",
+ """Set a high-level intent
Usage:
set intent <ARGS>
Arguments:
@@ -148,373 +148,373 @@
--dst-port DST_PORT Destination Switch Port (REQUIRED)
--match-src-mac MATCH_SRC_MAC Matching Source MAC Address (REQUIRED)
--match-dst-mac MATCH_DST_MAC Matching Destination MAC Address (REQUIRED)""",
- self.set_intent,
- [
- ("--intent-id", dict(required=True, type=int)),
- ("--src-dpid", dict(required=True)),
- ("--src-port", dict(required=True, type=int)),
- ("--dst-dpid", dict(required=True)),
- ("--dst-port", dict(required=True, type=int)),
- ("--match-src-mac", dict(required=True)),
- ("--match-dst-mac", dict(required=True))
- ]),
- #
- Command("show", "Show command"),
- #
- Command("show device", "Show devices"),
- #
- Command("show device all", "Show all devices", self.show_device_all),
- #
- Command("show intent", "Show intents"),
- #
- Command("show intent high",
- """Show all high-level intents
+ self.set_intent,
+ [
+ ("--intent-id", dict(required=True, type=int)),
+ ("--src-dpid", dict(required=True)),
+ ("--src-port", dict(required=True, type=int)),
+ ("--dst-dpid", dict(required=True)),
+ ("--dst-port", dict(required=True, type=int)),
+ ("--match-src-mac", dict(required=True)),
+ ("--match-dst-mac", dict(required=True))
+ ]),
+ #
+ Command("show", "Show command"),
+ #
+ Command("show device", "Show devices"),
+ #
+ Command("show device all", "Show all devices", self.show_device_all),
+ #
+ Command("show intent", "Show intents"),
+ #
+ Command("show intent high",
+ """Show all high-level intents
show intent high --intent-id INTENT_ID Show a high-level intent""",
- self.show_intent_high,
- [
- ("--intent-id", dict(required=False, type=int))
- ]
- ),
- #
- Command("show intent low",
- """Show all low-level intents
+ self.show_intent_high,
+ [
+ ("--intent-id", dict(required=False, type=int))
+ ]
+ ),
+ #
+ Command("show intent low",
+ """Show all low-level intents
show intent low --intent-id INTENT_ID Show a low-level intent""",
- self.show_intent_low,
- [
- ("--intent-id", dict(required=False))
- ]
- ),
- #
- Command("show link", "Show links"),
- #
- Command("show link all", "Show all links", self.show_link_all),
- #
- Command("show path", "Show a path"),
- #
- Command("show path shortest",
- """Show a shortest path
+ self.show_intent_low,
+ [
+ ("--intent-id", dict(required=False))
+ ]
+ ),
+ #
+ Command("show link", "Show links"),
+ #
+ Command("show link all", "Show all links", self.show_link_all),
+ #
+ Command("show path", "Show a path"),
+ #
+ Command("show path shortest",
+ """Show a shortest path
Usage:
show path shortest --src-dpid SRC_DPID --dst-dpid DST_DPID
Arguments:
--src-dpid SRC_DPID Source Switch DPID
--dst-dpid DST_DPID Destination Switch DPID""",
- self.show_path_shortest,
- [
- ("--src-dpid", dict(required=True)),
- ("--dst-dpid", dict(required=True))
- ]),
- #
- Command("show switch", "Show switches"),
- #
- Command("show switch all", "Show all switches", self.show_switch_all),
- #
- Command("show topology", "Show network topology"),
- #
- Command("show topology all", "Show whole network topology", self.show_topology_all)
- ]
+ self.show_path_shortest,
+ [
+ ("--src-dpid", dict(required=True)),
+ ("--dst-dpid", dict(required=True))
+ ]),
+ #
+ Command("show switch", "Show switches"),
+ #
+ Command("show switch all", "Show all switches", self.show_switch_all),
+ #
+ Command("show topology", "Show network topology"),
+ #
+ Command("show topology all", "Show whole network topology", self.show_topology_all)
+ ]
- # Sort the commands by the level in the CLI command hierarchy
- self.commands = sorted(init_commands, key = lambda c: len(c.name.split()))
+ # Sort the commands by the level in the CLI command hierarchy
+ self.commands = sorted(init_commands, key = lambda c: len(c.name.split()))
- # Create a dictionary with all commands: name -> Command
- for c in self.commands:
- self.commands_dict[c.name] = c
+ # Create a dictionary with all commands: name -> Command
+ for c in self.commands:
+ self.commands_dict[c.name] = c
- # Create a tree with all commands
- for c in self.commands:
- if c.parent_name:
- pc = self.commands_dict[c.parent_name]
- pc.children.append(c)
- c.parent = pc
+ # Create a tree with all commands
+ for c in self.commands:
+ if c.parent_name:
+ pc = self.commands_dict[c.parent_name]
+ pc.children.append(c)
+ c.parent = pc
- # Create the parsers and the sub-parsers
- for c in self.commands:
- # Add a parser
- parser = None
- if c.parent is None:
- # Add a top-level parser
- parser = argparse.ArgumentParser(description=c.help,
- prog=c.name,
- add_help=False)
- else:
- # Add a parser from the parent's subparser
- parent_subparser = self.subparsers_dict[c.parent_name]
- parser = parent_subparser.add_parser(c.last_subname,
- help=c.help,
- add_help=False)
- self.parsers_dict[c.name] = parser
- # Add a sub-parser
- if c.children:
- subparser = parser.add_subparsers(help=c.help)
- self.subparsers_dict[c.name] = subparser
- # Setup the callback
- if c.callback is not None:
- parser.set_defaults(func=c.callback)
- # Init the argument parser
- if c.add_parser_args is not None:
- for a in c.add_parser_args:
- (p1, p2) = a
- parser.add_argument(p1, **p2)
+ # Create the parsers and the sub-parsers
+ for c in self.commands:
+ # Add a parser
+ parser = None
+ if c.parent is None:
+ # Add a top-level parser
+ parser = argparse.ArgumentParser(description=c.help,
+ prog=c.name,
+ add_help=False)
+ else:
+ # Add a parser from the parent's subparser
+ parent_subparser = self.subparsers_dict[c.parent_name]
+ parser = parent_subparser.add_parser(c.last_subname,
+ help=c.help,
+ add_help=False)
+ self.parsers_dict[c.name] = parser
+ # Add a sub-parser
+ if c.children:
+ subparser = parser.add_subparsers(help=c.help)
+ self.subparsers_dict[c.name] = subparser
+ # Setup the callback
+ if c.callback is not None:
+ parser.set_defaults(func=c.callback)
+ # Init the argument parser
+ if c.add_parser_args is not None:
+ for a in c.add_parser_args:
+ (p1, p2) = a
+ parser.add_argument(p1, **p2)
def delete_intent(self, args):
- "CLI command callback: delete intent"
+ "CLI command callback: delete intent"
- url = ""
- if args.all:
- # Delete all intents
- url = "http://%s:%s/wm/onos/intent/high" % (self.onos_ip, self.onos_port)
- else:
- if args.intent_id is None:
- print "*** Unknown syntax:"
- self.help_delete()
- return;
- # Delete an intent
- url = "http://%s:%s/wm/onos/intent/high/%s" % (self.onos_ip, self.onos_port, args.intent_id)
+ url = ""
+ if args.all:
+ # Delete all intents
+ url = "http://%s:%s/wm/onos/intent/high" % (self.onos_ip, self.onos_port)
+ else:
+ if args.intent_id is None:
+ print "*** Unknown syntax:"
+ self.help_delete()
+ return;
+ # Delete an intent
+ url = "http://%s:%s/wm/onos/intent/high/%s" % (self.onos_ip, self.onos_port, args.intent_id)
- result = delete_json(url)
- # NOTE: No need to print the response
- # if len(result) != 0:
- # self.print_json_result(result)
+ result = delete_json(url)
+ # NOTE: No need to print the response
+ # if len(result) != 0:
+ # self.print_json_result(result)
def set_intent(self, args):
- "CLI command callback: set intent"
+ "CLI command callback: set intent"
- intents = []
- oper = {}
- # Create the POST payload
- oper['intentId'] = args.intent_id
- oper['intentType'] = 'CONSTRAINED_SHORTEST_PATH' # XXX: Hardcoded
- oper['staticPath'] = False # XXX: Hardcoded
- oper['srcSwitchDpid'] = args.src_dpid
- oper['srcSwitchPort'] = args.src_port
- oper['dstSwitchDpid'] = args.dst_dpid
- oper['dstSwitchPort'] = args.dst_port
- oper['matchSrcMac'] = args.match_src_mac
- oper['matchDstMac'] = args.match_dst_mac
- intents.append(oper)
+ intents = []
+ oper = {}
+ # Create the POST payload
+ oper['intentId'] = args.intent_id
+ oper['intentType'] = 'CONSTRAINED_SHORTEST_PATH' # XXX: Hardcoded
+ oper['staticPath'] = False # XXX: Hardcoded
+ oper['srcSwitchDpid'] = args.src_dpid
+ oper['srcSwitchPort'] = args.src_port
+ oper['dstSwitchDpid'] = args.dst_dpid
+ oper['dstSwitchPort'] = args.dst_port
+ oper['matchSrcMac'] = args.match_src_mac
+ oper['matchDstMac'] = args.match_dst_mac
+ intents.append(oper)
- url = "http://%s:%s/wm/onos/intent/high" % (self.onos_ip, self.onos_port)
- result = post_json(url, intents)
- # NOTE: No need to print the response
- # if len(result) != 0:
- # self.print_json_result(result)
+ url = "http://%s:%s/wm/onos/intent/high" % (self.onos_ip, self.onos_port)
+ result = post_json(url, intents)
+ # NOTE: No need to print the response
+ # if len(result) != 0:
+ # self.print_json_result(result)
def show_device_all(self, args):
- "CLI command callback: show device all"
+ "CLI command callback: show device all"
- url = "http://%s:%s/wm/onos/topology/devices" % (self.onos_ip, self.onos_port)
- result = get_json(url)
- self.print_json_result(result)
+ url = "http://%s:%s/wm/onos/topology/devices" % (self.onos_ip, self.onos_port)
+ result = get_json(url)
+ self.print_json_result(result)
def show_intent_high(self, args):
- "CLI command callback: show intent high"
+ "CLI command callback: show intent high"
- if args.intent_id is None:
- # Show all intents
- url = "http://%s:%s/wm/onos/intent/high" % (self.onos_ip, self.onos_port)
- else:
- # Show a single intent
- url = "http://%s:%s/wm/onos/intent/high/%s" % (self.onos_ip, self.onos_port, args.intent_id)
+ if args.intent_id is None:
+ # Show all intents
+ url = "http://%s:%s/wm/onos/intent/high" % (self.onos_ip, self.onos_port)
+ else:
+ # Show a single intent
+ url = "http://%s:%s/wm/onos/intent/high/%s" % (self.onos_ip, self.onos_port, args.intent_id)
- result = get_json(url)
- self.print_json_result(result)
+ result = get_json(url)
+ self.print_json_result(result)
def show_intent_low(self, args):
- "CLI command callback: show intent low"
+ "CLI command callback: show intent low"
- if args.intent_id is None:
- # Show all intents
- url = "http://%s:%s/wm/onos/intent/low" % (self.onos_ip, self.onos_port)
- else:
- # Show a single intent
- url = "http://%s:%s/wm/onos/intent/low/%s" % (self.onos_ip, self.onos_port, args.intent_id)
+ if args.intent_id is None:
+ # Show all intents
+ url = "http://%s:%s/wm/onos/intent/low" % (self.onos_ip, self.onos_port)
+ else:
+ # Show a single intent
+ url = "http://%s:%s/wm/onos/intent/low/%s" % (self.onos_ip, self.onos_port, args.intent_id)
- result = get_json(url)
- self.print_json_result(result)
+ result = get_json(url)
+ self.print_json_result(result)
def show_link_all(self, args):
- "CLI command callback: show link all"
+ "CLI command callback: show link all"
- url = "http://%s:%s/wm/onos/topology/links" % (self.onos_ip, self.onos_port)
- result = get_json(url)
- #
- if (self.output_format == "json"):
- self.print_json_result(result)
- else:
- # NOTE: The code below is for demo purpose only how to
- # decode and print the links in text format. It will be
- # reimplemented in the future.
- links = result
- print "# src_dpid src_port -> dst_dpid dst_port"
- for v in sorted(links, key=lambda x: x['src-switch']):
- if v.has_key('dst-switch'):
- dst_dpid = str(v['dst-switch'])
- if v.has_key('src-switch'):
- src_dpid = str(v['src-switch'])
- if v.has_key('src-port'):
- src_port = str(v['src-port'])
- if v.has_key('dst-port'):
- dst_port = str(v['dst-port'])
- self.print_result("%s %s -> %s %s" % (src_dpid, src_port, dst_dpid, dst_port))
+ url = "http://%s:%s/wm/onos/topology/links" % (self.onos_ip, self.onos_port)
+ result = get_json(url)
+ #
+ if (self.output_format == "json"):
+ self.print_json_result(result)
+ else:
+ # NOTE: The code below is for demo purpose only how to
+ # decode and print the links in text format. It will be
+ # reimplemented in the future.
+ links = result
+ print "# src_dpid src_port -> dst_dpid dst_port"
+ for v in sorted(links, key=lambda x: x['src-switch']):
+ if v.has_key('dst-switch'):
+ dst_dpid = str(v['dst-switch'])
+ if v.has_key('src-switch'):
+ src_dpid = str(v['src-switch'])
+ if v.has_key('src-port'):
+ src_port = str(v['src-port'])
+ if v.has_key('dst-port'):
+ dst_port = str(v['dst-port'])
+ self.print_result("%s %s -> %s %s" % (src_dpid, src_port, dst_dpid, dst_port))
def show_path_shortest(self, args):
- "CLI command callback: show path shortest"
+ "CLI command callback: show path shortest"
- url = "http://%s:%s/wm/onos/intent/path/switch/%s/shortest-path/%s" % (self.onos_ip, self.onos_port, args.src_dpid, args.dst_dpid)
- result = get_json(url)
- #
- self.print_json_result(result)
+ url = "http://%s:%s/wm/onos/intent/path/switch/%s/shortest-path/%s" % (self.onos_ip, self.onos_port, args.src_dpid, args.dst_dpid)
+ result = get_json(url)
+ #
+ self.print_json_result(result)
def show_switch_all(self, args):
- "CLI command callback: show switch all"
+ "CLI command callback: show switch all"
- url = "http://%s:%s/wm/onos/topology/switches" % (self.onos_ip, self.onos_port)
- result = get_json(url)
- #
- self.print_json_result(result)
+ url = "http://%s:%s/wm/onos/topology/switches" % (self.onos_ip, self.onos_port)
+ result = get_json(url)
+ #
+ self.print_json_result(result)
def show_topology_all(self, args):
- "CLI command callback: show topology all"
+ "CLI command callback: show topology all"
- url = "http://%s:%s/wm/onos/topology" % (self.onos_ip, self.onos_port)
- result = get_json(url)
- #
- self.print_json_result(result)
+ url = "http://%s:%s/wm/onos/topology" % (self.onos_ip, self.onos_port)
+ result = get_json(url)
+ #
+ self.print_json_result(result)
#
# Implement "delete" top-level command
#
def do_delete(self, arg):
"Top-level 'delete' command"
- self.impl_do_command('delete', arg)
+ self.impl_do_command('delete', arg)
def complete_delete(self, text, line, begidx, endidx):
- "Completion of top-level 'delete' command"
- return self.impl_complete_command('delete', text, line, begidx, endidx)
+ "Completion of top-level 'delete' command"
+ return self.impl_complete_command('delete', text, line, begidx, endidx)
def help_delete(self):
- "Help for top-level 'delete' command"
- self.impl_help_command('delete')
+ "Help for top-level 'delete' command"
+ self.impl_help_command('delete')
#
# Implement "set" top-level command
#
def do_set(self, arg):
"Top-level 'set' command"
- self.impl_do_command('set', arg)
+ self.impl_do_command('set', arg)
def complete_set(self, text, line, begidx, endidx):
- "Completion of top-level 'set' command"
- return self.impl_complete_command('set', text, line, begidx, endidx)
+ "Completion of top-level 'set' command"
+ return self.impl_complete_command('set', text, line, begidx, endidx)
def help_set(self):
- "Help for top-level 'set' command"
- self.impl_help_command('set')
+ "Help for top-level 'set' command"
+ self.impl_help_command('set')
#
# Implement "show" top-level command
#
def do_show(self, arg):
"Top-level 'show' command"
- self.impl_do_command('show', arg)
+ self.impl_do_command('show', arg)
def complete_show(self, text, line, begidx, endidx):
- "Completion of top-level 'show' command"
- return self.impl_complete_command('show', text, line, begidx, endidx)
+ "Completion of top-level 'show' command"
+ return self.impl_complete_command('show', text, line, begidx, endidx)
def help_show(self):
- "Help for top-level 'show' command"
- self.impl_help_command('show')
+ "Help for top-level 'show' command"
+ self.impl_help_command('show')
#
# Implement the "do_something" top-level command execution
#
def impl_do_command(self, root_name, arg):
- "Implementation of top-level 'do_something' command execution"
- parser = self.parsers_dict[root_name]
- parsed_args = parser.parse_args(arg.split())
- parsed_args.func(parsed_args)
+ "Implementation of top-level 'do_something' command execution"
+ parser = self.parsers_dict[root_name]
+ parsed_args = parser.parse_args(arg.split())
+ parsed_args.func(parsed_args)
#
# Implement the "complete_something" top-level command completion
#
def impl_complete_command(self, root_name, text, line, begidx, endidx):
- "Implementation of top-level 'complete_something' command completion"
- root_command = self.commands_dict[root_name]
- subtree_commands = self.collect_subtree_commands(root_command)
+ "Implementation of top-level 'complete_something' command completion"
+ root_command = self.commands_dict[root_name]
+ subtree_commands = self.collect_subtree_commands(root_command)
- #
- # Loop through the commands and add their portion
- # of the sub-name to the list of completions.
- #
- # NOTE: We add a command only if it has a callback.
- #
- completions = []
- for c in subtree_commands:
- if c.callback is None:
- continue
- name = c.split_name[len(root_command.split_name):]
- completions.append(' '.join(name))
+ #
+ # Loop through the commands and add their portion
+ # of the sub-name to the list of completions.
+ #
+ # NOTE: We add a command only if it has a callback.
+ #
+ completions = []
+ for c in subtree_commands:
+ if c.callback is None:
+ continue
+ name = c.split_name[len(root_command.split_name):]
+ completions.append(' '.join(name))
- mline = line.partition(" ")[2]
- offs = len(mline) - len(text)
- return [s[offs:] for s in completions if s.startswith(mline)]
+ mline = line.partition(" ")[2]
+ offs = len(mline) - len(text)
+ return [s[offs:] for s in completions if s.startswith(mline)]
#
# Implement the "help_something" top-level command help
#
def impl_help_command(self, root_name):
- "Implementation of top-level 'help_something' command help"
- root_command = self.commands_dict[root_name]
- subtree_commands = self.collect_subtree_commands(root_command)
+ "Implementation of top-level 'help_something' command help"
+ root_command = self.commands_dict[root_name]
+ subtree_commands = self.collect_subtree_commands(root_command)
- #
- # Loop through the commands and print the help for each command.
- # NOTE: We add a command only if it has a callback.
- #
- print "Help for the `%s` command:" % (root_name)
- for c in subtree_commands:
- if c.callback is None:
- continue
- print " {0:30}{1:30}".format(c.name, c.help)
- # if c.init_arg_parser is not None:
- # parser = self.parsers_dict[c.name]
- # parser.print_help()
+ #
+ # Loop through the commands and print the help for each command.
+ # NOTE: We add a command only if it has a callback.
+ #
+ print "Help for the `%s` command:" % (root_name)
+ for c in subtree_commands:
+ if c.callback is None:
+ continue
+ print " {0:30}{1:30}".format(c.name, c.help)
+ # if c.init_arg_parser is not None:
+ # parser = self.parsers_dict[c.name]
+ # parser.print_help()
#
# Traverse (breadth-first) a subtree and return all nodes except the
# root node.
#
def collect_subtree_commands(self, root_command):
- """Collect a subtree of commands.
+ """Collect a subtree of commands.
Traverses (breadth-first) a subtree of commands and returns
all nodes except the root node."""
- commands = []
- subtree_commands = []
- commands.append(root_command)
- # Use breadth-first to traverse the subtree
- while commands:
- pc = commands.pop(0)
- for c in pc.children:
- commands.append(c)
- subtree_commands.append(c)
- return subtree_commands
+ commands = []
+ subtree_commands = []
+ commands.append(root_command)
+ # Use breadth-first to traverse the subtree
+ while commands:
+ pc = commands.pop(0)
+ for c in pc.children:
+ commands.append(c)
+ subtree_commands.append(c)
+ return subtree_commands
def log_debug(self, msg):
- """Log debug information.
- msg: the message to log
- Use the following CLI commands to enable/disable debugging:
- paramset debug true
- paramset debug false
- """
- if self.debug:
- print "%s" % (msg)
+ """Log debug information.
+ msg: the message to log
+ Use the following CLI commands to enable/disable debugging:
+ paramset debug true
+ paramset debug false
+ """
+ if self.debug:
+ print "%s" % (msg)
def print_json_result(self, json_result):
- """Print JSON result."""
- if len(json_result) == 0:
- return
- result = json.dumps(json_result, indent=4)
- self.print_result(result)
+ """Print JSON result."""
+ if len(json_result) == 0:
+ return
+ result = json.dumps(json_result, indent=4)
+ self.print_result(result)
def print_result(self, result):
- """Print parsed result."""
- print "%s" % (result)
+ """Print parsed result."""
+ print "%s" % (result)
#
# Implementation of the "paramshow" CLI command.
@@ -555,8 +555,8 @@
as there is no ambiguity. Call without arguments for a list of
settable parameters with their values.'''
- class NotSettableError(Exception):
- pass
+ class NotSettableError(Exception):
+ pass
try:
statement, paramName, val = arg.parsed.raw.split(None, 2)
@@ -591,25 +591,25 @@
parsed_result = []
try:
- response = urllib2.urlopen(url)
- result = response.read()
- response.close()
- if len(result) != 0:
- parsed_result = json.loads(result)
+ response = urllib2.urlopen(url)
+ result = response.read()
+ response.close()
+ if len(result) != 0:
+ parsed_result = json.loads(result)
except HTTPError as exc:
- print "ERROR:"
- print " REST GET URL: %s" % url
- # NOTE: exc.fp contains the object with the response payload
- error_payload = json.loads(exc.fp.read())
- print " REST Error Code: %s" % (error_payload['code'])
- print " REST Error Summary: %s" % (error_payload['summary'])
- print " REST Error Description: %s" % (error_payload['formattedDescription'])
- print " HTTP Error Code: %s" % exc.code
- print " HTTP Error Reason: %s" % exc.reason
+ print "ERROR:"
+ print " REST GET URL: %s" % url
+ # NOTE: exc.fp contains the object with the response payload
+ error_payload = json.loads(exc.fp.read())
+ print " REST Error Code: %s" % (error_payload['code'])
+ print " REST Error Summary: %s" % (error_payload['summary'])
+ print " REST Error Description: %s" % (error_payload['formattedDescription'])
+ print " HTTP Error Code: %s" % exc.code
+ print " HTTP Error Reason: %s" % exc.reason
except URLError as exc:
- print "ERROR:"
- print " REST GET URL: %s" % url
- print " URL Error Reason: %s" % exc.reason
+ print "ERROR:"
+ print " REST GET URL: %s" % url
+ print " URL Error Reason: %s" % exc.reason
return parsed_result
def post_json(url, data):
@@ -620,27 +620,27 @@
parsed_result = []
data_json = json.dumps(data)
try:
- request = urllib2.Request(url, data_json)
- request.add_header("Content-Type", "application/json")
- response = urllib2.urlopen(request)
- result = response.read()
- response.close()
- if len(result) != 0:
- parsed_result = json.loads(result)
+ request = urllib2.Request(url, data_json)
+ request.add_header("Content-Type", "application/json")
+ response = urllib2.urlopen(request)
+ result = response.read()
+ response.close()
+ if len(result) != 0:
+ parsed_result = json.loads(result)
except HTTPError as exc:
- print "ERROR:"
- print " REST POST URL: %s" % url
- # NOTE: exc.fp contains the object with the response payload
- error_payload = json.loads(exc.fp.read())
- print " REST Error Code: %s" % (error_payload['code'])
- print " REST Error Summary: %s" % (error_payload['summary'])
- print " REST Error Description: %s" % (error_payload['formattedDescription'])
- print " HTTP Error Code: %s" % exc.code
- print " HTTP Error Reason: %s" % exc.reason
+ print "ERROR:"
+ print " REST POST URL: %s" % url
+ # NOTE: exc.fp contains the object with the response payload
+ error_payload = json.loads(exc.fp.read())
+ print " REST Error Code: %s" % (error_payload['code'])
+ print " REST Error Summary: %s" % (error_payload['summary'])
+ print " REST Error Description: %s" % (error_payload['formattedDescription'])
+ print " HTTP Error Code: %s" % exc.code
+ print " HTTP Error Reason: %s" % exc.reason
except URLError as exc:
- print "ERROR:"
- print " REST POST URL: %s" % url
- print " URL Error Reason: %s" % exc.reason
+ print "ERROR:"
+ print " REST POST URL: %s" % url
+ print " URL Error Reason: %s" % exc.reason
return parsed_result
def delete_json(url):
@@ -649,27 +649,27 @@
parsed_result = []
try:
- request = urllib2.Request(url)
- request.get_method = lambda: 'DELETE'
- response = urllib2.urlopen(request)
- result = response.read()
- response.close()
- if len(result) != 0:
- parsed_result = json.loads(result)
+ request = urllib2.Request(url)
+ request.get_method = lambda: 'DELETE'
+ response = urllib2.urlopen(request)
+ result = response.read()
+ response.close()
+ if len(result) != 0:
+ parsed_result = json.loads(result)
except HTTPError as exc:
- print "ERROR:"
- print " REST DELETE URL: %s" % url
- # NOTE: exc.fp contains the object with the response payload
- error_payload = json.loads(exc.fp.read())
- print " REST Error Code: %s" % (error_payload['code'])
- print " REST Error Summary: %s" % (error_payload['summary'])
- print " REST Error Description: %s" % (error_payload['formattedDescription'])
- print " HTTP Error Code: %s" % exc.code
- print " HTTP Error Reason: %s" % exc.reason
+ print "ERROR:"
+ print " REST DELETE URL: %s" % url
+ # NOTE: exc.fp contains the object with the response payload
+ error_payload = json.loads(exc.fp.read())
+ print " REST Error Code: %s" % (error_payload['code'])
+ print " REST Error Summary: %s" % (error_payload['summary'])
+ print " REST Error Description: %s" % (error_payload['formattedDescription'])
+ print " HTTP Error Code: %s" % exc.code
+ print " HTTP Error Reason: %s" % exc.reason
except URLError as exc:
- print "ERROR:"
- print " REST DELETE URL: %s" % url
- print " URL Error Reason: %s" % exc.reason
+ print "ERROR:"
+ print " REST DELETE URL: %s" % url
+ print " URL Error Reason: %s" % exc.reason
return parsed_result
if __name__ == '__main__':
@@ -678,20 +678,20 @@
# Setup the parser
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--command', nargs=argparse.REMAINDER,
- help="Run arguments to the end of the line as a CLI command")
+ help="Run arguments to the end of the line as a CLI command")
parser.add_argument('--onos-ip',
- help="Set the ONOS IP address (for REST calls)")
+ help="Set the ONOS IP address (for REST calls)")
parser.add_argument('--onos-port',
- help="Set the ONOS port number (for REST calls)")
+ help="Set the ONOS port number (for REST calls)")
parser.add_argument('-t', '--test', nargs='+',
- help="Test against transcript(s) in FILE (wildcards OK)")
+ help="Test against transcript(s) in FILE (wildcards OK)")
# Parse the arguments
parsed_args = parser.parse_args()
if parsed_args.onos_ip:
- onosCli.onos_ip = parsed_args.onos_ip
+ onosCli.onos_ip = parsed_args.onos_ip
if parsed_args.onos_port:
- onosCli.onos_port = parsed_args.onos_port
+ onosCli.onos_port = parsed_args.onos_port
#
# NOTE: We have to reset the command-line options so the Cmd2 parser
# doesn't process them again.
@@ -700,12 +700,12 @@
# Run the CLI as appropriate
if parsed_args.test:
- # Run CLI Transcript Tests
- onosCli.runTranscriptTests(parsed_args.test)
+ # Run CLI Transcript Tests
+ onosCli.runTranscriptTests(parsed_args.test)
elif parsed_args.command:
- # Run arguments as a CLI command
- command_line = ' '.join(parsed_args.command)
- onosCli.onecmd(command_line)
+ # Run arguments as a CLI command
+ command_line = ' '.join(parsed_args.command)
+ onosCli.onecmd(command_line)
else:
- # Run interactive CLI
- onosCli.cmdloop()
+ # Run interactive CLI
+ onosCli.cmdloop()