Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 1 | #! /usr/bin/env python |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 2 | # -*- Mode: python; py-indent-offset: 4; tab-width: 8; -*- |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 3 | |
| 4 | """ |
| 5 | onoscli : ONOS-specific Command Line Interface |
| 6 | |
| 7 | Usage: |
| 8 | # Running the CLI in interactive mode: |
| 9 | $ ./onoscli |
| 10 | |
| 11 | # Running multiple CLI commands in batch mode |
| 12 | $ cat commands.txt | ./onoscli |
| 13 | |
| 14 | # Running a single command from the system command-line |
| 15 | $ ./onoscli -c show switch all |
| 16 | |
| 17 | # Run the following command for additional help |
| 18 | $ ./onoscli -h |
| 19 | """ |
| 20 | |
| 21 | # |
| 22 | # INSTALLATION NOTES: MUST install Python cmd2 module. E.g., on Ubuntu: |
| 23 | # sudo apt-get install python-cmd2 |
| 24 | # On older Ubuntu installations (e.g., Ubuntu-12.04), install also: |
| 25 | # sudo apt-get install python-pyparsing |
| 26 | # |
| 27 | |
| 28 | # |
| 29 | # ADDING A NEW CLI COMMAND: |
| 30 | # 1. Add the appropriate Command entry (or entries) inside array |
| 31 | # OnosCli.init_commands. |
| 32 | # See the comments for init_commands for details. |
| 33 | # 2. Add the appropriate callback (inside class OnosCli) for the CLI command |
| 34 | # |
| 35 | # |
| 36 | |
| 37 | import sys |
| 38 | import argparse |
| 39 | import json |
| 40 | from optparse import make_option |
| 41 | import urllib2 |
| 42 | from urllib2 import URLError, HTTPError |
| 43 | import cmd2 |
| 44 | from cmd2 import Cmd |
| 45 | from cmd2 import options |
| 46 | |
| 47 | |
| 48 | class Command(): |
| 49 | "Command node. A hierarchy of nodes are organized in a command tree." |
| 50 | |
| 51 | def __init__(self, name, help, callback=None, add_parser_args=None): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 52 | """name: a string with the full command name |
| 53 | help: the help string for the command |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 54 | callback: the method to be called if the command is executed |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 55 | add_parser_args: the parser arguments to add to the command: a dictionary of argparse arguments""" |
| 56 | # Normalize the name by removing extra spaces |
| 57 | self.split_name = name.split() # List of the words in the name |
| 58 | self.name = ' '.join(self.split_name) # Normalized name |
| 59 | self.last_subname = self.split_name[-1] # Last word in the name |
| 60 | self.parent_name = ' '.join(self.split_name[:-1]) # Name of parent command |
| 61 | self.help = help # THe help string |
| 62 | self.callback = callback # The command callback |
| 63 | self.add_parser_args = add_parser_args # Parser arguments to add |
| 64 | self.parent = None # The parent Command |
| 65 | self.children = [] # The children Command entries |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 66 | |
| 67 | |
| 68 | class OnosCli(Cmd): |
| 69 | "The main ONOS CLI class" |
| 70 | |
| 71 | # Setup generic CLI fields |
| 72 | intro = "Welcome to the ONOS CLI. Type help or ? to list commands.\n" |
| 73 | prompt = "(onos) " |
| 74 | settable = Cmd.settable + "prompt CLI prompt" |
| 75 | |
| 76 | # Setup ONOS-specific fields |
| 77 | onos_ip = "127.0.0.1" |
| 78 | settable = settable + "onos_ip ONOS IP address" |
| 79 | onos_port = 8080 |
| 80 | settable = settable + "onos_port ONOS REST port number" |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 81 | output_format = "json" # Valid values: json, text |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 82 | settable = settable + "output_format The output format: `text` or `json`" |
| 83 | |
| 84 | # Collection of commands sorted by the level in the CLI command hierarchy |
| 85 | commands = [] |
| 86 | # Commands, parsers and subparsers keyed by the command name |
| 87 | commands_dict = {} |
| 88 | parsers_dict = {} |
| 89 | subparsers_dict = {} |
| 90 | |
| 91 | def __init__(self): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 92 | Cmd.__init__(self) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 93 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 94 | # |
| 95 | # An array of the ONOS-specific CLI commands. |
| 96 | # Each entry is a Command instance, and must have at least |
| 97 | # two arguments: |
| 98 | # * Command name as typed on the CLI. E.g.: |
| 99 | # "show intent" |
| 100 | # * Command help description. E.g.: |
| 101 | # "Show intents" |
| 102 | # |
| 103 | # Executable commands should have a third Command argument, which is |
| 104 | # the name of the method to be called when the command is executed. |
| 105 | # The method will be called with the (argparse) parsed arguments |
| 106 | # for that command. |
| 107 | # |
| 108 | # If an executable command takes arguments, those should be described |
| 109 | # in the Command's fourth argument. It is a list of pairs: |
| 110 | # [ |
| 111 | # ("--argument-name1", dict(...)), |
| 112 | # ("--argument-name2", dict(...)), |
| 113 | # ... |
| 114 | # ] |
| 115 | # where the first entry in the pair is the argument name, and the |
| 116 | # second entry in the pair is a dictionary with argparse-specific |
| 117 | # description of the argument. |
| 118 | # |
| 119 | init_commands = [ |
| 120 | Command("delete", "Delete command"), |
| 121 | # |
| 122 | Command("delete intent", |
| 123 | """Delete high-level intents |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 124 | Usage: |
| 125 | delete intent --intent-id INTENT_ID Delete a high-level intent |
| 126 | delete intent --all Delete all high-level intents |
| 127 | Arguments: |
| 128 | --intent-id INTENT_ID The Intent ID (an integer) |
| 129 | --all Delete all high-level intents""", |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 130 | self.delete_intent, |
| 131 | [ |
| 132 | ("--intent-id", dict(required=False, type=int)), |
| 133 | ("--all", dict(required=False, action='store_true')) |
| 134 | ] |
| 135 | ), |
| 136 | # |
| 137 | Command("set", "Set command"), |
| 138 | # |
| 139 | Command("set intent", |
| 140 | """Set a high-level intent |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 141 | Usage: |
| 142 | set intent <ARGS> |
| 143 | Arguments: |
| 144 | --intent-id INTENT_ID The Intent ID (an integer) (REQUIRED) |
| 145 | --src-dpid SRC_DPID Source Switch DPID (REQUIRED) |
| 146 | --src-port SRC_PORT Source Switch Port (REQUIRED) |
| 147 | --dst-dpid DST_DPID Destination Switch DPID (REQUIRED) |
| 148 | --dst-port DST_PORT Destination Switch Port (REQUIRED) |
| 149 | --match-src-mac MATCH_SRC_MAC Matching Source MAC Address (REQUIRED) |
| 150 | --match-dst-mac MATCH_DST_MAC Matching Destination MAC Address (REQUIRED)""", |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 151 | self.set_intent, |
| 152 | [ |
| 153 | ("--intent-id", dict(required=True, type=int)), |
| 154 | ("--src-dpid", dict(required=True)), |
| 155 | ("--src-port", dict(required=True, type=int)), |
| 156 | ("--dst-dpid", dict(required=True)), |
| 157 | ("--dst-port", dict(required=True, type=int)), |
| 158 | ("--match-src-mac", dict(required=True)), |
| 159 | ("--match-dst-mac", dict(required=True)) |
| 160 | ]), |
| 161 | # |
| 162 | Command("show", "Show command"), |
| 163 | # |
| 164 | Command("show device", "Show devices"), |
| 165 | # |
| 166 | Command("show device all", "Show all devices", self.show_device_all), |
| 167 | # |
| 168 | Command("show intent", "Show intents"), |
| 169 | # |
| 170 | Command("show intent high", |
| 171 | """Show all high-level intents |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 172 | show intent high --intent-id INTENT_ID Show a high-level intent""", |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 173 | self.show_intent_high, |
| 174 | [ |
| 175 | ("--intent-id", dict(required=False, type=int)) |
| 176 | ] |
| 177 | ), |
| 178 | # |
| 179 | Command("show intent low", |
| 180 | """Show all low-level intents |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 181 | show intent low --intent-id INTENT_ID Show a low-level intent""", |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 182 | self.show_intent_low, |
| 183 | [ |
| 184 | ("--intent-id", dict(required=False)) |
| 185 | ] |
| 186 | ), |
| 187 | # |
| 188 | Command("show link", "Show links"), |
| 189 | # |
| 190 | Command("show link all", "Show all links", self.show_link_all), |
| 191 | # |
| 192 | Command("show path", "Show a path"), |
| 193 | # |
| 194 | Command("show path shortest", |
| 195 | """Show a shortest path |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 196 | Usage: |
| 197 | show path shortest --src-dpid SRC_DPID --dst-dpid DST_DPID |
| 198 | Arguments: |
| 199 | --src-dpid SRC_DPID Source Switch DPID |
| 200 | --dst-dpid DST_DPID Destination Switch DPID""", |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 201 | self.show_path_shortest, |
| 202 | [ |
| 203 | ("--src-dpid", dict(required=True)), |
| 204 | ("--dst-dpid", dict(required=True)) |
| 205 | ]), |
| 206 | # |
| 207 | Command("show switch", "Show switches"), |
| 208 | # |
| 209 | Command("show switch all", "Show all switches", self.show_switch_all), |
| 210 | # |
| 211 | Command("show topology", "Show network topology"), |
| 212 | # |
| 213 | Command("show topology all", "Show whole network topology", self.show_topology_all) |
| 214 | ] |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 215 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 216 | # Sort the commands by the level in the CLI command hierarchy |
| 217 | self.commands = sorted(init_commands, key = lambda c: len(c.name.split())) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 218 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 219 | # Create a dictionary with all commands: name -> Command |
| 220 | for c in self.commands: |
| 221 | self.commands_dict[c.name] = c |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 222 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 223 | # Create a tree with all commands |
| 224 | for c in self.commands: |
| 225 | if c.parent_name: |
| 226 | pc = self.commands_dict[c.parent_name] |
| 227 | pc.children.append(c) |
| 228 | c.parent = pc |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 229 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 230 | # Create the parsers and the sub-parsers |
| 231 | for c in self.commands: |
| 232 | # Add a parser |
| 233 | parser = None |
| 234 | if c.parent is None: |
| 235 | # Add a top-level parser |
| 236 | parser = argparse.ArgumentParser(description=c.help, |
| 237 | prog=c.name, |
| 238 | add_help=False) |
| 239 | else: |
| 240 | # Add a parser from the parent's subparser |
| 241 | parent_subparser = self.subparsers_dict[c.parent_name] |
| 242 | parser = parent_subparser.add_parser(c.last_subname, |
| 243 | help=c.help, |
| 244 | add_help=False) |
| 245 | self.parsers_dict[c.name] = parser |
| 246 | # Add a sub-parser |
| 247 | if c.children: |
| 248 | subparser = parser.add_subparsers(help=c.help) |
| 249 | self.subparsers_dict[c.name] = subparser |
| 250 | # Setup the callback |
| 251 | if c.callback is not None: |
| 252 | parser.set_defaults(func=c.callback) |
| 253 | # Init the argument parser |
| 254 | if c.add_parser_args is not None: |
| 255 | for a in c.add_parser_args: |
| 256 | (p1, p2) = a |
| 257 | parser.add_argument(p1, **p2) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 258 | |
| 259 | def delete_intent(self, args): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 260 | "CLI command callback: delete intent" |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 261 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 262 | url = "" |
| 263 | if args.all: |
| 264 | # Delete all intents |
| 265 | url = "http://%s:%s/wm/onos/intent/high" % (self.onos_ip, self.onos_port) |
| 266 | else: |
| 267 | if args.intent_id is None: |
| 268 | print "*** Unknown syntax:" |
| 269 | self.help_delete() |
| 270 | return; |
| 271 | # Delete an intent |
| 272 | url = "http://%s:%s/wm/onos/intent/high/%s" % (self.onos_ip, self.onos_port, args.intent_id) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 273 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 274 | result = delete_json(url) |
| 275 | # NOTE: No need to print the response |
| 276 | # if len(result) != 0: |
| 277 | # self.print_json_result(result) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 278 | |
| 279 | def set_intent(self, args): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 280 | "CLI command callback: set intent" |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 281 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 282 | intents = [] |
| 283 | oper = {} |
| 284 | # Create the POST payload |
| 285 | oper['intentId'] = args.intent_id |
| 286 | oper['intentType'] = 'CONSTRAINED_SHORTEST_PATH' # XXX: Hardcoded |
| 287 | oper['staticPath'] = False # XXX: Hardcoded |
| 288 | oper['srcSwitchDpid'] = args.src_dpid |
| 289 | oper['srcSwitchPort'] = args.src_port |
| 290 | oper['dstSwitchDpid'] = args.dst_dpid |
| 291 | oper['dstSwitchPort'] = args.dst_port |
| 292 | oper['matchSrcMac'] = args.match_src_mac |
| 293 | oper['matchDstMac'] = args.match_dst_mac |
| 294 | intents.append(oper) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 295 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 296 | url = "http://%s:%s/wm/onos/intent/high" % (self.onos_ip, self.onos_port) |
| 297 | result = post_json(url, intents) |
| 298 | # NOTE: No need to print the response |
| 299 | # if len(result) != 0: |
| 300 | # self.print_json_result(result) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 301 | |
| 302 | def show_device_all(self, args): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 303 | "CLI command callback: show device all" |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 304 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 305 | url = "http://%s:%s/wm/onos/topology/devices" % (self.onos_ip, self.onos_port) |
| 306 | result = get_json(url) |
| 307 | self.print_json_result(result) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 308 | |
| 309 | def show_intent_high(self, args): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 310 | "CLI command callback: show intent high" |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 311 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 312 | if args.intent_id is None: |
| 313 | # Show all intents |
| 314 | url = "http://%s:%s/wm/onos/intent/high" % (self.onos_ip, self.onos_port) |
| 315 | else: |
| 316 | # Show a single intent |
| 317 | url = "http://%s:%s/wm/onos/intent/high/%s" % (self.onos_ip, self.onos_port, args.intent_id) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 318 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 319 | result = get_json(url) |
| 320 | self.print_json_result(result) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 321 | |
| 322 | def show_intent_low(self, args): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 323 | "CLI command callback: show intent low" |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 324 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 325 | if args.intent_id is None: |
| 326 | # Show all intents |
| 327 | url = "http://%s:%s/wm/onos/intent/low" % (self.onos_ip, self.onos_port) |
| 328 | else: |
| 329 | # Show a single intent |
| 330 | url = "http://%s:%s/wm/onos/intent/low/%s" % (self.onos_ip, self.onos_port, args.intent_id) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 331 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 332 | result = get_json(url) |
| 333 | self.print_json_result(result) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 334 | |
| 335 | def show_link_all(self, args): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 336 | "CLI command callback: show link all" |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 337 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 338 | url = "http://%s:%s/wm/onos/topology/links" % (self.onos_ip, self.onos_port) |
| 339 | result = get_json(url) |
| 340 | # |
| 341 | if (self.output_format == "json"): |
| 342 | self.print_json_result(result) |
| 343 | else: |
| 344 | # NOTE: The code below is for demo purpose only how to |
| 345 | # decode and print the links in text format. It will be |
| 346 | # reimplemented in the future. |
| 347 | links = result |
| 348 | print "# src_dpid src_port -> dst_dpid dst_port" |
| 349 | for v in sorted(links, key=lambda x: x['src-switch']): |
| 350 | if v.has_key('dst-switch'): |
| 351 | dst_dpid = str(v['dst-switch']) |
| 352 | if v.has_key('src-switch'): |
| 353 | src_dpid = str(v['src-switch']) |
| 354 | if v.has_key('src-port'): |
| 355 | src_port = str(v['src-port']) |
| 356 | if v.has_key('dst-port'): |
| 357 | dst_port = str(v['dst-port']) |
| 358 | self.print_result("%s %s -> %s %s" % (src_dpid, src_port, dst_dpid, dst_port)) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 359 | |
| 360 | def show_path_shortest(self, args): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 361 | "CLI command callback: show path shortest" |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 362 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 363 | 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) |
| 364 | result = get_json(url) |
| 365 | # |
| 366 | self.print_json_result(result) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 367 | |
| 368 | def show_switch_all(self, args): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 369 | "CLI command callback: show switch all" |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 370 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 371 | url = "http://%s:%s/wm/onos/topology/switches" % (self.onos_ip, self.onos_port) |
| 372 | result = get_json(url) |
| 373 | # |
| 374 | self.print_json_result(result) |
Pavlin Radoslavov | cf2b553 | 2014-05-23 18:12:23 -0700 | [diff] [blame] | 375 | |
| 376 | def show_topology_all(self, args): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 377 | "CLI command callback: show topology all" |
Pavlin Radoslavov | cf2b553 | 2014-05-23 18:12:23 -0700 | [diff] [blame] | 378 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 379 | url = "http://%s:%s/wm/onos/topology" % (self.onos_ip, self.onos_port) |
| 380 | result = get_json(url) |
| 381 | # |
| 382 | self.print_json_result(result) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 383 | |
| 384 | # |
| 385 | # Implement "delete" top-level command |
| 386 | # |
| 387 | def do_delete(self, arg): |
| 388 | "Top-level 'delete' command" |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 389 | self.impl_do_command('delete', arg) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 390 | def complete_delete(self, text, line, begidx, endidx): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 391 | "Completion of top-level 'delete' command" |
| 392 | return self.impl_complete_command('delete', text, line, begidx, endidx) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 393 | def help_delete(self): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 394 | "Help for top-level 'delete' command" |
| 395 | self.impl_help_command('delete') |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 396 | |
| 397 | # |
| 398 | # Implement "set" top-level command |
| 399 | # |
| 400 | def do_set(self, arg): |
| 401 | "Top-level 'set' command" |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 402 | self.impl_do_command('set', arg) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 403 | def complete_set(self, text, line, begidx, endidx): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 404 | "Completion of top-level 'set' command" |
| 405 | return self.impl_complete_command('set', text, line, begidx, endidx) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 406 | def help_set(self): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 407 | "Help for top-level 'set' command" |
| 408 | self.impl_help_command('set') |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 409 | |
| 410 | # |
| 411 | # Implement "show" top-level command |
| 412 | # |
| 413 | def do_show(self, arg): |
| 414 | "Top-level 'show' command" |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 415 | self.impl_do_command('show', arg) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 416 | def complete_show(self, text, line, begidx, endidx): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 417 | "Completion of top-level 'show' command" |
| 418 | return self.impl_complete_command('show', text, line, begidx, endidx) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 419 | def help_show(self): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 420 | "Help for top-level 'show' command" |
| 421 | self.impl_help_command('show') |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 422 | |
| 423 | # |
| 424 | # Implement the "do_something" top-level command execution |
| 425 | # |
| 426 | def impl_do_command(self, root_name, arg): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 427 | "Implementation of top-level 'do_something' command execution" |
| 428 | parser = self.parsers_dict[root_name] |
| 429 | parsed_args = parser.parse_args(arg.split()) |
| 430 | parsed_args.func(parsed_args) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 431 | |
| 432 | # |
| 433 | # Implement the "complete_something" top-level command completion |
| 434 | # |
| 435 | def impl_complete_command(self, root_name, text, line, begidx, endidx): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 436 | "Implementation of top-level 'complete_something' command completion" |
| 437 | root_command = self.commands_dict[root_name] |
| 438 | subtree_commands = self.collect_subtree_commands(root_command) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 439 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 440 | # |
| 441 | # Loop through the commands and add their portion |
| 442 | # of the sub-name to the list of completions. |
| 443 | # |
| 444 | # NOTE: We add a command only if it has a callback. |
| 445 | # |
| 446 | completions = [] |
| 447 | for c in subtree_commands: |
| 448 | if c.callback is None: |
| 449 | continue |
| 450 | name = c.split_name[len(root_command.split_name):] |
| 451 | completions.append(' '.join(name)) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 452 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 453 | mline = line.partition(" ")[2] |
| 454 | offs = len(mline) - len(text) |
| 455 | return [s[offs:] for s in completions if s.startswith(mline)] |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 456 | |
| 457 | # |
| 458 | # Implement the "help_something" top-level command help |
| 459 | # |
| 460 | def impl_help_command(self, root_name): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 461 | "Implementation of top-level 'help_something' command help" |
| 462 | root_command = self.commands_dict[root_name] |
| 463 | subtree_commands = self.collect_subtree_commands(root_command) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 464 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 465 | # |
| 466 | # Loop through the commands and print the help for each command. |
| 467 | # NOTE: We add a command only if it has a callback. |
| 468 | # |
| 469 | print "Help for the `%s` command:" % (root_name) |
| 470 | for c in subtree_commands: |
| 471 | if c.callback is None: |
| 472 | continue |
| 473 | print " {0:30}{1:30}".format(c.name, c.help) |
| 474 | # if c.init_arg_parser is not None: |
| 475 | # parser = self.parsers_dict[c.name] |
| 476 | # parser.print_help() |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 477 | |
| 478 | # |
| 479 | # Traverse (breadth-first) a subtree and return all nodes except the |
| 480 | # root node. |
| 481 | # |
| 482 | def collect_subtree_commands(self, root_command): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 483 | """Collect a subtree of commands. |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 484 | Traverses (breadth-first) a subtree of commands and returns |
| 485 | all nodes except the root node.""" |
| 486 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 487 | commands = [] |
| 488 | subtree_commands = [] |
| 489 | commands.append(root_command) |
| 490 | # Use breadth-first to traverse the subtree |
| 491 | while commands: |
| 492 | pc = commands.pop(0) |
| 493 | for c in pc.children: |
| 494 | commands.append(c) |
| 495 | subtree_commands.append(c) |
| 496 | return subtree_commands |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 497 | |
| 498 | def log_debug(self, msg): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 499 | """Log debug information. |
| 500 | msg: the message to log |
| 501 | Use the following CLI commands to enable/disable debugging: |
| 502 | paramset debug true |
| 503 | paramset debug false |
| 504 | """ |
| 505 | if self.debug: |
| 506 | print "%s" % (msg) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 507 | |
| 508 | def print_json_result(self, json_result): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 509 | """Print JSON result.""" |
| 510 | if len(json_result) == 0: |
| 511 | return |
| 512 | result = json.dumps(json_result, indent=4) |
| 513 | self.print_result(result) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 514 | |
| 515 | def print_result(self, result): |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 516 | """Print parsed result.""" |
| 517 | print "%s" % (result) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 518 | |
| 519 | # |
| 520 | # Implementation of the "paramshow" CLI command. |
| 521 | # |
| 522 | # NOTE: The do_paramshow implementation below is copied from |
| 523 | # the cmd2.do_show() implementation |
| 524 | # |
| 525 | @options([make_option('-l', '--long', action="store_true", |
| 526 | help="describe function of parameter")]) |
| 527 | def do_paramshow(self, arg, opts): |
| 528 | '''Shows value of a parameter.''' |
| 529 | param = arg.strip().lower() |
| 530 | result = {} |
| 531 | maxlen = 0 |
| 532 | for p in self.settable: |
| 533 | if (not param) or p.startswith(param): |
| 534 | result[p] = '%s: %s' % (p, str(getattr(self, p))) |
| 535 | maxlen = max(maxlen, len(result[p])) |
| 536 | if result: |
| 537 | for p in sorted(result): |
| 538 | if opts.long: |
| 539 | self.poutput('%s # %s' % (result[p].ljust(maxlen), self.settable[p])) |
| 540 | else: |
| 541 | self.poutput(result[p]) |
| 542 | else: |
| 543 | raise NotImplementedError("Parameter '%s' not supported (type 'show' for list of parameters)." % param) |
| 544 | |
| 545 | # |
| 546 | # Implementation of the "paramset" CLI command. |
| 547 | # |
| 548 | # |
| 549 | # NOTE: The do_paramset implementation below is copied from |
| 550 | # the cmd2.do_set() implementation (with minor modifications). |
| 551 | # |
| 552 | def do_paramset(self, arg): |
| 553 | ''' |
| 554 | Sets a cmd2 parameter. Accepts abbreviated parameter names so long |
| 555 | as there is no ambiguity. Call without arguments for a list of |
| 556 | settable parameters with their values.''' |
| 557 | |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 558 | class NotSettableError(Exception): |
| 559 | pass |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 560 | |
| 561 | try: |
| 562 | statement, paramName, val = arg.parsed.raw.split(None, 2) |
| 563 | val = val.strip() |
| 564 | paramName = paramName.strip().lower() |
| 565 | if paramName not in self.settable: |
| 566 | hits = [p for p in self.settable if p.startswith(paramName)] |
| 567 | if len(hits) == 1: |
| 568 | paramName = hits[0] |
| 569 | else: |
| 570 | return self.do_paramshow(paramName) |
| 571 | currentVal = getattr(self, paramName) |
| 572 | if (val[0] == val[-1]) and val[0] in ("'", '"'): |
| 573 | val = val[1:-1] |
| 574 | else: |
| 575 | val = cmd2.cast(currentVal, val) |
| 576 | setattr(self, paramName, val) |
| 577 | self.stdout.write('%s - was: %s\nnow: %s\n' % (paramName, currentVal, val)) |
| 578 | if currentVal != val: |
| 579 | try: |
| 580 | onchange_hook = getattr(self, '_onchange_%s' % paramName) |
| 581 | onchange_hook(old=currentVal, new=val) |
| 582 | except AttributeError: |
| 583 | pass |
| 584 | except (ValueError, AttributeError, NotSettableError) as exc: |
| 585 | self.do_paramshow(arg) |
| 586 | |
| 587 | |
| 588 | def get_json(url): |
| 589 | """Make a REST GET call and return the JSON result |
| 590 | url: the URL to call""" |
| 591 | |
Pavlin Radoslavov | cf2b553 | 2014-05-23 18:12:23 -0700 | [diff] [blame] | 592 | parsed_result = [] |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 593 | try: |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 594 | response = urllib2.urlopen(url) |
| 595 | result = response.read() |
| 596 | response.close() |
| 597 | if len(result) != 0: |
| 598 | parsed_result = json.loads(result) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 599 | except HTTPError as exc: |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 600 | print "ERROR:" |
| 601 | print " REST GET URL: %s" % url |
| 602 | # NOTE: exc.fp contains the object with the response payload |
| 603 | error_payload = json.loads(exc.fp.read()) |
| 604 | print " REST Error Code: %s" % (error_payload['code']) |
| 605 | print " REST Error Summary: %s" % (error_payload['summary']) |
| 606 | print " REST Error Description: %s" % (error_payload['formattedDescription']) |
| 607 | print " HTTP Error Code: %s" % exc.code |
| 608 | print " HTTP Error Reason: %s" % exc.reason |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 609 | except URLError as exc: |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 610 | print "ERROR:" |
| 611 | print " REST GET URL: %s" % url |
| 612 | print " URL Error Reason: %s" % exc.reason |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 613 | return parsed_result |
| 614 | |
| 615 | def post_json(url, data): |
| 616 | """Make a REST POST call and return the JSON result |
| 617 | url: the URL to call |
| 618 | data: the data to POST""" |
| 619 | |
Pavlin Radoslavov | cf2b553 | 2014-05-23 18:12:23 -0700 | [diff] [blame] | 620 | parsed_result = [] |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 621 | data_json = json.dumps(data) |
| 622 | try: |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 623 | request = urllib2.Request(url, data_json) |
| 624 | request.add_header("Content-Type", "application/json") |
| 625 | response = urllib2.urlopen(request) |
| 626 | result = response.read() |
| 627 | response.close() |
| 628 | if len(result) != 0: |
| 629 | parsed_result = json.loads(result) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 630 | except HTTPError as exc: |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 631 | print "ERROR:" |
| 632 | print " REST POST URL: %s" % url |
| 633 | # NOTE: exc.fp contains the object with the response payload |
| 634 | error_payload = json.loads(exc.fp.read()) |
| 635 | print " REST Error Code: %s" % (error_payload['code']) |
| 636 | print " REST Error Summary: %s" % (error_payload['summary']) |
| 637 | print " REST Error Description: %s" % (error_payload['formattedDescription']) |
| 638 | print " HTTP Error Code: %s" % exc.code |
| 639 | print " HTTP Error Reason: %s" % exc.reason |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 640 | except URLError as exc: |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 641 | print "ERROR:" |
| 642 | print " REST POST URL: %s" % url |
| 643 | print " URL Error Reason: %s" % exc.reason |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 644 | return parsed_result |
| 645 | |
| 646 | def delete_json(url): |
| 647 | """Make a REST DELETE call and return the JSON result |
| 648 | url: the URL to call""" |
| 649 | |
Pavlin Radoslavov | cf2b553 | 2014-05-23 18:12:23 -0700 | [diff] [blame] | 650 | parsed_result = [] |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 651 | try: |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 652 | request = urllib2.Request(url) |
| 653 | request.get_method = lambda: 'DELETE' |
| 654 | response = urllib2.urlopen(request) |
| 655 | result = response.read() |
| 656 | response.close() |
| 657 | if len(result) != 0: |
| 658 | parsed_result = json.loads(result) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 659 | except HTTPError as exc: |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 660 | print "ERROR:" |
| 661 | print " REST DELETE URL: %s" % url |
| 662 | # NOTE: exc.fp contains the object with the response payload |
| 663 | error_payload = json.loads(exc.fp.read()) |
| 664 | print " REST Error Code: %s" % (error_payload['code']) |
| 665 | print " REST Error Summary: %s" % (error_payload['summary']) |
| 666 | print " REST Error Description: %s" % (error_payload['formattedDescription']) |
| 667 | print " HTTP Error Code: %s" % exc.code |
| 668 | print " HTTP Error Reason: %s" % exc.reason |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 669 | except URLError as exc: |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 670 | print "ERROR:" |
| 671 | print " REST DELETE URL: %s" % url |
| 672 | print " URL Error Reason: %s" % exc.reason |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 673 | return parsed_result |
| 674 | |
| 675 | if __name__ == '__main__': |
| 676 | onosCli = OnosCli() |
| 677 | |
| 678 | # Setup the parser |
| 679 | parser = argparse.ArgumentParser() |
| 680 | parser.add_argument('-c', '--command', nargs=argparse.REMAINDER, |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 681 | help="Run arguments to the end of the line as a CLI command") |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 682 | parser.add_argument('--onos-ip', |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 683 | help="Set the ONOS IP address (for REST calls)") |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 684 | parser.add_argument('--onos-port', |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 685 | help="Set the ONOS port number (for REST calls)") |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 686 | parser.add_argument('-t', '--test', nargs='+', |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 687 | help="Test against transcript(s) in FILE (wildcards OK)") |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 688 | |
| 689 | # Parse the arguments |
| 690 | parsed_args = parser.parse_args() |
| 691 | if parsed_args.onos_ip: |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 692 | onosCli.onos_ip = parsed_args.onos_ip |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 693 | if parsed_args.onos_port: |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 694 | onosCli.onos_port = parsed_args.onos_port |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 695 | # |
| 696 | # NOTE: We have to reset the command-line options so the Cmd2 parser |
| 697 | # doesn't process them again. |
| 698 | # |
| 699 | sys.argv = [sys.argv[0]] |
| 700 | |
| 701 | # Run the CLI as appropriate |
| 702 | if parsed_args.test: |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 703 | # Run CLI Transcript Tests |
| 704 | onosCli.runTranscriptTests(parsed_args.test) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 705 | elif parsed_args.command: |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 706 | # Run arguments as a CLI command |
| 707 | command_line = ' '.join(parsed_args.command) |
| 708 | onosCli.onecmd(command_line) |
Pavlin Radoslavov | b94817b | 2014-05-22 21:12:47 -0700 | [diff] [blame] | 709 | else: |
Pavlin Radoslavov | 8500fc0 | 2014-05-28 14:04:55 -0700 | [diff] [blame^] | 710 | # Run interactive CLI |
| 711 | onosCli.cmdloop() |