Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | # Copyright 2014 Linaro Limited |
| 4 | # |
| 5 | # This program is free software; you can redistribute it and/or modify |
| 6 | # it under the terms of the GNU General Public License as published by |
| 7 | # the Free Software Foundation; either version 2 of the License, or |
| 8 | # (at your option) any later version. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU General Public License |
| 16 | # along with this program; if not, write to the Free Software |
| 17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 18 | # MA 02110-1301, USA. |
| 19 | # |
| 20 | # VLANd admin interface |
| 21 | # |
| 22 | |
Steve McIntyre | 5694f6d | 2014-12-18 16:43:30 +0000 | [diff] [blame] | 23 | import os, sys |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 24 | import optparse |
| 25 | |
| 26 | vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0]))) |
| 27 | sys.path.insert(0, vlandpath) |
| 28 | |
Steve McIntyre | 5694f6d | 2014-12-18 16:43:30 +0000 | [diff] [blame] | 29 | from errors import InputError, SocketError |
Steve McIntyre | 21f0270 | 2014-12-16 18:19:47 +0000 | [diff] [blame] | 30 | from config.config import VlanConfig |
| 31 | from ipc.ipc import VlanIpc |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 32 | |
Steve McIntyre | 77fd71c | 2015-07-31 17:16:01 +0100 | [diff] [blame] | 33 | version = "0.4-DEV" |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 34 | banner = "Linaro VLANd admin interface, version %s" % version |
| 35 | |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 36 | TRUNK_ID_NONE = -1 |
| 37 | |
Steve McIntyre | ae95fd6 | 2014-12-05 16:51:41 +0000 | [diff] [blame] | 38 | def is_positive(text): |
Steve McIntyre | c6895fc | 2014-12-19 15:01:08 +0000 | [diff] [blame] | 39 | if text in ('1', 'y', 'Y', 't', 'T', 'True', 'true'): |
Steve McIntyre | ae95fd6 | 2014-12-05 16:51:41 +0000 | [diff] [blame] | 40 | return True |
Steve McIntyre | c6895fc | 2014-12-19 15:01:08 +0000 | [diff] [blame] | 41 | elif text in ('0', 'n', 'N', 'f', 'F', 'False', 'false'): |
Steve McIntyre | ae95fd6 | 2014-12-05 16:51:41 +0000 | [diff] [blame] | 42 | return False |
| 43 | else: |
| 44 | raise InputError("Cannot parse \"%s\" as True or False" % text) |
| 45 | |
Steve McIntyre | a132c36 | 2014-12-05 15:53:21 +0000 | [diff] [blame] | 46 | def dump_switch(switch): |
Steve McIntyre | 018d30c | 2015-02-09 06:34:57 +0000 | [diff] [blame] | 47 | print "switch_id:%d name:%s" % ( |
| 48 | int(switch[0]), |
| 49 | switch[1]) |
Steve McIntyre | a132c36 | 2014-12-05 15:53:21 +0000 | [diff] [blame] | 50 | |
Steve McIntyre | 11e4cbd | 2014-12-05 16:03:03 +0000 | [diff] [blame] | 51 | def dump_port(port): |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 52 | print "port_id:%d name:%s switch_id:%d locked:%s mode:%s base_vlan_id:%d current_vlan_id:%d number:%d trunk_id:%s" % ( |
Steve McIntyre | 018d30c | 2015-02-09 06:34:57 +0000 | [diff] [blame] | 53 | int(port[0]), |
| 54 | port[1], |
| 55 | int(port[2]), |
| 56 | ("yes" if port[3] is True else "no"), |
| 57 | ("trunk" if port[4] is True else "access"), |
| 58 | int(port[5]), |
Steve McIntyre | 9951b1c | 2015-08-05 13:55:38 +0100 | [diff] [blame] | 59 | int(port[6]), |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 60 | int(port[7]), |
| 61 | 'None' if (TRUNK_ID_NONE == port[8]) else port[8]) |
Steve McIntyre | 11e4cbd | 2014-12-05 16:03:03 +0000 | [diff] [blame] | 62 | |
Steve McIntyre | e41e3f3 | 2014-12-05 18:08:21 +0000 | [diff] [blame] | 63 | def dump_vlan(vlan): |
Steve McIntyre | 018d30c | 2015-02-09 06:34:57 +0000 | [diff] [blame] | 64 | print "vlan_id:%d name:%s tag:%d is_base_vlan:%s, creation_time:%s" % ( |
| 65 | int(vlan[0]), |
| 66 | vlan[1], |
| 67 | int(vlan[2]), |
| 68 | ("yes" if vlan[3] is True else "no"), |
| 69 | vlan[4]) |
Steve McIntyre | e41e3f3 | 2014-12-05 18:08:21 +0000 | [diff] [blame] | 70 | |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 71 | def dump_trunk(trunk): |
| 72 | print "trunk_id:%d creation_time:%s" % ( |
| 73 | int(trunk[0]), |
| 74 | trunk[1]) |
| 75 | |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 76 | def call_vland(msgtype, msg): |
| 77 | ipc = VlanIpc() |
| 78 | ipc.client_connect('localhost', config.vland.port) |
| 79 | msg['client_name'] = 'admin.py' |
| 80 | msg['type'] = msgtype |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 81 | ipc.client_send(msg) |
| 82 | ret = ipc.client_recv_and_close() |
| 83 | if 'response' not in ret: |
| 84 | raise SocketError("Badly-formed response from VLANd server") |
| 85 | if ret['response'] == "ERROR": |
Steve McIntyre | 8a1a1ae | 2015-01-23 17:52:14 +0000 | [diff] [blame] | 86 | print "Input error: VLANd server said \"%s\"" % ret['error'] |
| 87 | sys.exit(1) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 88 | return ret['data'] |
| 89 | |
Steve McIntyre | c12f631 | 2014-12-15 15:00:12 +0000 | [diff] [blame] | 90 | config = VlanConfig(filenames=('./vland.cfg',)) |
Steve McIntyre | c12f631 | 2014-12-15 15:00:12 +0000 | [diff] [blame] | 91 | |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 92 | usage = 'Usage: %prog --command [command options]' |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 93 | parser = optparse.OptionParser(usage=usage, description=banner) |
| 94 | |
Steve McIntyre | 9cd6c3d | 2014-12-05 18:10:35 +0000 | [diff] [blame] | 95 | # System commands |
| 96 | system_group = optparse.OptionGroup(parser, "System commands") |
| 97 | system_group.add_option("--status", |
| 98 | dest="status", |
| 99 | action = "store_true", |
| 100 | default = False, |
| 101 | help = "Describe current system status") |
Steve McIntyre | c00d4cf | 2014-12-16 19:23:39 +0000 | [diff] [blame] | 102 | system_group.add_option("--statistics", |
| 103 | dest="statistics", |
| 104 | action = "store_true", |
| 105 | default = False, |
| 106 | help = "Print some system statistics") |
| 107 | system_group.add_option("--version", |
| 108 | dest="version", |
| 109 | action = "store_true", |
| 110 | default = False, |
| 111 | help = "Describe the version of this admin interface") |
| 112 | system_group.add_option("--vland_version", |
| 113 | dest="vland_version", |
| 114 | action = "store_true", |
| 115 | default = False, |
| 116 | help = "Describe the version of the running VLANd") |
Steve McIntyre | a4bb991 | 2015-01-13 18:40:37 +0000 | [diff] [blame] | 117 | system_group.add_option("--auto_import_switch", |
Steve McIntyre | 6c56260 | 2014-12-22 16:10:54 +0000 | [diff] [blame] | 118 | dest = "auto_import_switch", |
| 119 | action = "store", |
| 120 | type = "string", |
| 121 | help = "Attempt to import a switch's configuration into the VLANd system", |
| 122 | nargs = 1, |
| 123 | metavar = "<switch name>") |
Steve McIntyre | a4bb991 | 2015-01-13 18:40:37 +0000 | [diff] [blame] | 124 | system_group.add_option("--probe_switches", |
Steve McIntyre | deff795 | 2014-12-23 13:45:59 +0000 | [diff] [blame] | 125 | dest = "probe_switches", |
| 126 | action = "store_true", |
| 127 | default = False, |
Steve McIntyre | a4bb991 | 2015-01-13 18:40:37 +0000 | [diff] [blame] | 128 | help = "Probe all the configured switches") |
Steve McIntyre | 06fe642 | 2015-01-23 17:55:43 +0000 | [diff] [blame] | 129 | system_group.add_option("--shutdown", |
| 130 | dest = "shutdown", |
| 131 | action = "store_true", |
| 132 | default = False, |
| 133 | help = "Shut down a running VLANd") |
Steve McIntyre | 9cd6c3d | 2014-12-05 18:10:35 +0000 | [diff] [blame] | 134 | parser.add_option_group(system_group) |
| 135 | # May add shutdown, self-check etc. later |
| 136 | |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 137 | # Switch commands |
| 138 | switch_group = optparse.OptionGroup(parser, "Switch commands") |
| 139 | switch_group.add_option("--list_all_switches", |
| 140 | dest = "list_all_switches", |
| 141 | action = "store_true", |
| 142 | default = False, |
| 143 | help = "List all the existing switches in the system") |
| 144 | switch_group.add_option("--create_switch", |
| 145 | dest = "create_switch", |
| 146 | action = "store", |
| 147 | type = "string", |
| 148 | help = "Add a new switch to the system", |
| 149 | nargs = 1, |
| 150 | metavar = "<name>") |
Steve McIntyre | 99feaee | 2014-12-02 18:22:36 +0000 | [diff] [blame] | 151 | switch_group.add_option("--delete_switch", |
| 152 | dest = "delete_switch", |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 153 | action = "store", |
| 154 | type = "int", |
| 155 | help = "Remove an existing switch from the system", |
Steve McIntyre | 59e0463 | 2014-12-02 18:02:16 +0000 | [diff] [blame] | 156 | default = None, |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 157 | nargs = 1, |
| 158 | metavar = "<switch_id>") |
| 159 | switch_group.add_option("--show_switch", |
| 160 | dest = "show_switch", |
| 161 | action = "store", |
| 162 | type = "int", |
| 163 | help = "Show the details of an existing switch in the system", |
Steve McIntyre | 59e0463 | 2014-12-02 18:02:16 +0000 | [diff] [blame] | 164 | default = None, |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 165 | nargs = 1, |
| 166 | metavar = "<switch_id>") |
| 167 | switch_group.add_option("--lookup_switch_by_name", |
| 168 | dest = "lookup_switch_by_name", |
| 169 | action = "store", |
| 170 | type = "string", |
| 171 | help = "Lookup a switch ID by name", |
| 172 | nargs = 1, |
| 173 | metavar = "<name>") |
| 174 | switch_group.add_option("--list_switch_ports", |
| 175 | dest = "list_switch_ports", |
| 176 | action = "store", |
| 177 | type = "int", |
Steve McIntyre | 11e4cbd | 2014-12-05 16:03:03 +0000 | [diff] [blame] | 178 | help = "List the IDs of the ports on an existing switch in the system", |
Steve McIntyre | 59e0463 | 2014-12-02 18:02:16 +0000 | [diff] [blame] | 179 | default = None, |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 180 | nargs = 1, |
| 181 | metavar = "<switch_id>") |
| 182 | parser.add_option_group(switch_group) |
| 183 | |
| 184 | # Port commands |
| 185 | port_group = optparse.OptionGroup(parser, "Port commands") |
| 186 | port_group.add_option("--list_all_ports", |
| 187 | dest = "list_all_ports", |
| 188 | action = "store_true", |
| 189 | default = False, |
| 190 | help = "List all the existing ports in the system") |
| 191 | port_group.add_option("--create_port", |
| 192 | dest = "create_port", |
| 193 | action = "store", |
| 194 | type = "string", |
| 195 | help = "Add a new port to the system", |
Steve McIntyre | ea75397 | 2015-08-05 13:52:48 +0100 | [diff] [blame] | 196 | nargs = 3, |
| 197 | metavar = "<switch_id> <name> <number>") |
Steve McIntyre | 99feaee | 2014-12-02 18:22:36 +0000 | [diff] [blame] | 198 | port_group.add_option("--delete_port", |
| 199 | dest = "delete_port", |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 200 | action = "store", |
| 201 | type = "int", |
| 202 | help = "Remove an existing port from the system", |
Steve McIntyre | 59e0463 | 2014-12-02 18:02:16 +0000 | [diff] [blame] | 203 | default = None, |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 204 | nargs = 1, |
| 205 | metavar = "<port_id>") |
| 206 | port_group.add_option("--show_port", |
| 207 | dest = "show_port", |
| 208 | action = "store", |
| 209 | type = "int", |
| 210 | help = "Show the details of an existing port in the system", |
Steve McIntyre | 59e0463 | 2014-12-02 18:02:16 +0000 | [diff] [blame] | 211 | default = None, |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 212 | nargs = 1, |
| 213 | metavar = "<port_id>") |
| 214 | port_group.add_option("--lookup_port_by_switch_and_name", |
| 215 | dest = "lookup_port_by_switch_and_name", |
| 216 | action = "store", |
| 217 | type = "string", |
| 218 | help = "Lookup a port ID by switch and port name", |
| 219 | nargs = 2, |
| 220 | metavar = "<switch_id> <name>") |
Steve McIntyre | 45f5501 | 2015-08-05 13:55:15 +0100 | [diff] [blame] | 221 | port_group.add_option("--lookup_port_by_switch_and_number", |
| 222 | dest = "lookup_port_by_switch_and_number", |
| 223 | action = "store", |
| 224 | type = "string", |
| 225 | help = "Lookup a port ID by switch and number", |
| 226 | nargs = 2, |
| 227 | metavar = "<switch_id> <name>") |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 228 | port_group.add_option("--lookup_ports_by_switch", |
| 229 | dest = "lookup_ports_by_switch", |
| 230 | action = "store", |
| 231 | type = "string", |
| 232 | help = "Lookup port ID(s) by switch", |
| 233 | nargs = 1, |
| 234 | metavar = "<switch_id>") |
| 235 | port_group.add_option("--lookup_ports_by_current_vlan", |
| 236 | dest = "lookup_ports_by_current_vlan", |
| 237 | action = "store", |
| 238 | type = "string", |
| 239 | help = "Lookup port ID(s) by current VLAN", |
| 240 | nargs = 1, |
| 241 | metavar = "<vlan_id>") |
| 242 | port_group.add_option("--lookup_ports_by_base_vlan", |
| 243 | dest = "lookup_ports_by_base_vlan", |
| 244 | action = "store", |
| 245 | type = "string", |
| 246 | help = "Lookup port ID(s) by base VLAN", |
| 247 | nargs = 1, |
| 248 | metavar = "<vlan_id>") |
| 249 | port_group.add_option("--lookup_ports_by_trunk", |
| 250 | dest = "lookup_ports_by_trunk", |
| 251 | action = "store", |
| 252 | type = "string", |
| 253 | help = "Lookup port ID(s) by trunk", |
| 254 | nargs = 1, |
| 255 | metavar = "<trunk_id>") |
Steve McIntyre | 71b4102 | 2014-12-05 17:17:44 +0000 | [diff] [blame] | 256 | port_group.add_option("--set_port_mode", |
| 257 | dest = "set_port_mode", |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 258 | action = "store", |
| 259 | type = "string", |
| 260 | help = "Set the mode of a port to 'trunk' or 'access'", |
| 261 | nargs = 2, |
| 262 | metavar = "<port_id> <mode>") |
| 263 | port_group.add_option("--lock_port", |
| 264 | dest = "lock_port", |
| 265 | action = "store", |
| 266 | type = "string", |
| 267 | help = "Lock the settings on a port", |
| 268 | nargs = 1, |
| 269 | metavar = "<port_id>") |
| 270 | port_group.add_option("--unlock_port", |
| 271 | dest = "unlock_port", |
| 272 | action = "store", |
| 273 | type = "string", |
| 274 | help = "Unock the settings on a port", |
| 275 | nargs = 1, |
| 276 | metavar = "<port_id>") |
| 277 | port_group.add_option("--set_port_current_vlan", |
| 278 | dest = "set_port_current_vlan", |
| 279 | action = "store", |
| 280 | type = "int", |
| 281 | help = "Set the current VLAN assignment for a port", |
| 282 | nargs = 2, |
| 283 | metavar = "<port_id> <vlan_id>") |
| 284 | port_group.add_option("--get_port_current_vlan", |
| 285 | dest = "get_port_current_vlan", |
| 286 | action = "store", |
| 287 | type = "int", |
| 288 | help = "Get the current VLAN assignment for a port", |
| 289 | nargs = 1, |
| 290 | metavar = "<port_id>") |
| 291 | port_group.add_option("--set_port_base_vlan", |
| 292 | dest = "set_port_base_vlan", |
| 293 | action = "store", |
| 294 | type = "int", |
| 295 | help = "Set the base VLAN assignment for a port", |
| 296 | nargs = 2, |
| 297 | metavar = "<port_id> <vlan_id>") |
| 298 | port_group.add_option("--get_port_base_vlan", |
| 299 | dest = "get_port_base_vlan", |
| 300 | action = "store", |
| 301 | type = "int", |
| 302 | help = "Get the base VLAN assignment for a port", |
| 303 | nargs = 1, |
| 304 | metavar = "<port_id>") |
Steve McIntyre | a2bbcda | 2014-12-05 17:57:36 +0000 | [diff] [blame] | 305 | port_group.add_option("--restore_port_to_base_vlan", |
| 306 | dest = "restore_port_to_base_vlan", |
| 307 | action = "store", |
| 308 | type = "int", |
| 309 | help = "Reset the port back to its base VLAN", |
| 310 | nargs = 1, |
| 311 | metavar = "<port_id>") |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 312 | parser.add_option_group(port_group) |
| 313 | |
| 314 | # VLAN commands |
| 315 | vlan_group = optparse.OptionGroup(parser, "VLAN commands") |
| 316 | vlan_group.add_option("--list_all_vlans", |
| 317 | dest = "list_all_vlans", |
| 318 | action = "store_true", |
| 319 | default = False, |
| 320 | help = "List all the existing vlans in the system") |
| 321 | vlan_group.add_option("--create_vlan", |
| 322 | dest = "create_vlan", |
| 323 | action = "store", |
| 324 | type = "string", |
| 325 | help = "Add a new vlan to the system", |
| 326 | nargs = 3, |
Steve McIntyre | 9f0bb60 | 2014-11-28 14:36:39 +0000 | [diff] [blame] | 327 | metavar = "<name> <tag> <is_base_vlan>") |
Steve McIntyre | 99feaee | 2014-12-02 18:22:36 +0000 | [diff] [blame] | 328 | vlan_group.add_option("--delete_vlan", |
| 329 | dest = "delete_vlan", |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 330 | action = "store", |
| 331 | type = "int", |
| 332 | help = "Remove an existing vlan from the system", |
Steve McIntyre | 59e0463 | 2014-12-02 18:02:16 +0000 | [diff] [blame] | 333 | default = None, |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 334 | nargs = 1, |
| 335 | metavar = "<vlan_id>") |
| 336 | vlan_group.add_option("--show_vlan", |
| 337 | dest = "show_vlan", |
| 338 | action = "store", |
| 339 | type = "int", |
| 340 | help = "Show the details of an existing vlan in the system", |
Steve McIntyre | 59e0463 | 2014-12-02 18:02:16 +0000 | [diff] [blame] | 341 | default = None, |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 342 | nargs = 1, |
| 343 | metavar = "<vlan_id>") |
Steve McIntyre | 65533d7 | 2015-01-23 18:01:17 +0000 | [diff] [blame] | 344 | vlan_group.add_option("--lookup_vlan_by_tag", |
| 345 | dest = "lookup_vlan_by_tag", |
| 346 | action = "store", |
| 347 | type = "int", |
| 348 | help = "Find the vlan ID of an existing vlan in the system", |
| 349 | default = None, |
| 350 | nargs = 1, |
| 351 | metavar = "<tag>") |
| 352 | vlan_group.add_option("--show_vlan_tag", |
| 353 | dest = "show_vlan_tag", |
| 354 | action = "store", |
| 355 | type = "int", |
| 356 | help = "Print the vlan tag of an existing vlan in the system", |
| 357 | default = None, |
| 358 | nargs = 1, |
| 359 | metavar = "<vlan_id>") |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 360 | parser.add_option_group(vlan_group) |
| 361 | |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 362 | # Port commands |
| 363 | trunk_group = optparse.OptionGroup(parser, "Trunk commands") |
| 364 | trunk_group.add_option("--list_all_trunks", |
| 365 | dest = "list_all_trunks", |
| 366 | action = "store_true", |
| 367 | default = False, |
| 368 | help = "List all the existing trunks in the system") |
| 369 | trunk_group.add_option("--create_trunk", |
| 370 | dest = "create_trunk", |
| 371 | action = "store", |
| 372 | type = "string", |
| 373 | help = "Add a new trunk to the system, linking two ports", |
| 374 | nargs = 2, |
| 375 | metavar = "<port_id1> <port_id2>") |
| 376 | trunk_group.add_option("--delete_trunk", |
| 377 | dest = "delete_trunk", |
| 378 | action = "store", |
| 379 | type = "int", |
| 380 | help = "Remove an existing trunk from the system", |
| 381 | default = None, |
| 382 | nargs = 1, |
| 383 | metavar = "<trunk_id>") |
| 384 | trunk_group.add_option("--show_trunk", |
| 385 | dest = "show_trunk", |
| 386 | action = "store", |
| 387 | type = "int", |
| 388 | help = "Show the details of an existing trunk in the system", |
| 389 | default = None, |
| 390 | nargs = 1, |
| 391 | metavar = "<trunk_id>") |
| 392 | parser.add_option_group(trunk_group) |
| 393 | |
| 394 | |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 395 | (opts, args) = parser.parse_args() |
| 396 | |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 397 | if opts.list_all_switches: |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 398 | result = call_vland('db_query', {'command':'db.all_switches', 'data':None}) |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 399 | for line in result: |
Steve McIntyre | 018d30c | 2015-02-09 06:34:57 +0000 | [diff] [blame] | 400 | dump_switch(line) |
Steve McIntyre | af2d5ed | 2014-12-02 12:42:28 +0000 | [diff] [blame] | 401 | elif opts.list_all_ports: |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 402 | result = call_vland('db_query', {'command':'db.all_ports', 'data':None}) |
Steve McIntyre | af2d5ed | 2014-12-02 12:42:28 +0000 | [diff] [blame] | 403 | for line in result: |
Steve McIntyre | 018d30c | 2015-02-09 06:34:57 +0000 | [diff] [blame] | 404 | dump_port(line) |
Steve McIntyre | af2d5ed | 2014-12-02 12:42:28 +0000 | [diff] [blame] | 405 | elif opts.list_all_vlans: |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 406 | result = call_vland('db_query', {'command':'db.all_vlans', 'data':None}) |
Steve McIntyre | af2d5ed | 2014-12-02 12:42:28 +0000 | [diff] [blame] | 407 | for line in result: |
Steve McIntyre | 018d30c | 2015-02-09 06:34:57 +0000 | [diff] [blame] | 408 | dump_vlan(line) |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 409 | elif opts.list_all_trunks: |
| 410 | result = call_vland('db_query', {'command':'db.all_trunks', 'data':None}) |
| 411 | for line in result: |
| 412 | dump_trunk(line) |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 413 | elif opts.create_switch is not None: |
Steve McIntyre | af2d5ed | 2014-12-02 12:42:28 +0000 | [diff] [blame] | 414 | try: |
Steve McIntyre | 7103de2 | 2014-12-17 13:16:48 +0000 | [diff] [blame] | 415 | switch_id = call_vland('db_update', |
| 416 | {'command':'db.create_switch', |
| 417 | 'data': |
| 418 | {'name':opts.create_switch}}) |
Steve McIntyre | af2d5ed | 2014-12-02 12:42:28 +0000 | [diff] [blame] | 419 | print 'Created switch_id %d' % switch_id |
| 420 | except InputError as inst: |
| 421 | print 'Failed: %s' % inst |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 422 | elif opts.create_port is not None: |
Steve McIntyre | af2d5ed | 2014-12-02 12:42:28 +0000 | [diff] [blame] | 423 | try: |
Steve McIntyre | 7103de2 | 2014-12-17 13:16:48 +0000 | [diff] [blame] | 424 | port_id = call_vland('db_update', |
| 425 | {'command':'db.create_port', |
| 426 | 'data': |
| 427 | {'switch_id': opts.create_port[0], |
Steve McIntyre | ea75397 | 2015-08-05 13:52:48 +0100 | [diff] [blame] | 428 | 'name': opts.create_port[1], |
| 429 | 'number': opts.create_port[2]}}) |
Steve McIntyre | af2d5ed | 2014-12-02 12:42:28 +0000 | [diff] [blame] | 430 | print 'Created port_id %d' % port_id |
| 431 | except InputError as inst: |
| 432 | print 'Failed: %s' % inst |
Steve McIntyre | c21d8d1 | 2014-11-28 14:42:40 +0000 | [diff] [blame] | 433 | elif opts.create_vlan is not None: |
Steve McIntyre | c8aba4c | 2014-12-02 12:48:51 +0000 | [diff] [blame] | 434 | try: |
Steve McIntyre | 42122b7 | 2015-08-03 19:27:35 +0100 | [diff] [blame] | 435 | (vlan_id, vlan_tag) = call_vland('vlan_update', |
| 436 | {'command':'api.create_vlan', |
| 437 | 'data': |
| 438 | {'name': opts.create_vlan[0], |
| 439 | 'tag': opts.create_vlan[1], |
| 440 | 'is_base_vlan': is_positive(opts.create_vlan[2])}}) |
| 441 | print 'Created VLAN tag %d as vlan_id %d' % (vlan_tag, vlan_id) |
Steve McIntyre | c8aba4c | 2014-12-02 12:48:51 +0000 | [diff] [blame] | 442 | except InputError as inst: |
| 443 | print 'Failed: %s' % inst |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 444 | elif opts.create_trunk is not None: |
| 445 | try: |
| 446 | trunk_id = call_vland('db_update', |
| 447 | {'command':'db.create_trunk', |
| 448 | 'data': |
| 449 | {'port_id1': opts.create_trunk[0], |
| 450 | 'port_id2': opts.create_trunk[1]}}) |
| 451 | print 'Created trunk_id %d' % trunk_id |
| 452 | except InputError as inst: |
| 453 | print 'Failed: %s' % inst |
Steve McIntyre | 99feaee | 2014-12-02 18:22:36 +0000 | [diff] [blame] | 454 | elif opts.delete_switch is not None: |
Steve McIntyre | 64e3886 | 2014-12-02 17:19:37 +0000 | [diff] [blame] | 455 | try: |
Steve McIntyre | 7103de2 | 2014-12-17 13:16:48 +0000 | [diff] [blame] | 456 | switch_id = call_vland('db_update', |
| 457 | {'command':'db.delete_switch', |
| 458 | 'data': {'switch_id': opts.delete_switch}}) |
Steve McIntyre | 64e3886 | 2014-12-02 17:19:37 +0000 | [diff] [blame] | 459 | print 'Deleted switch_id %d' % switch_id |
| 460 | except InputError as inst: |
| 461 | print 'Failed: %s' % inst |
Steve McIntyre | 99feaee | 2014-12-02 18:22:36 +0000 | [diff] [blame] | 462 | elif opts.delete_port is not None: |
Steve McIntyre | 6f7ee5c | 2014-12-02 18:02:50 +0000 | [diff] [blame] | 463 | try: |
Steve McIntyre | 7103de2 | 2014-12-17 13:16:48 +0000 | [diff] [blame] | 464 | port_id = call_vland('db_update', |
| 465 | {'command':'db.delete_port', |
| 466 | 'data': {'port_id': opts.delete_port}}) |
Steve McIntyre | 6f7ee5c | 2014-12-02 18:02:50 +0000 | [diff] [blame] | 467 | except InputError as inst: |
| 468 | print 'Failed: %s' % inst |
Steve McIntyre | ed5cbea | 2014-12-02 18:23:00 +0000 | [diff] [blame] | 469 | elif opts.delete_vlan is not None: |
| 470 | try: |
Steve McIntyre | 3f0aceb | 2014-12-17 16:27:13 +0000 | [diff] [blame] | 471 | vlan_id = call_vland('vlan_update', |
| 472 | {'command':'api.delete_vlan', |
| 473 | 'data': {'vlan_id': opts.delete_vlan}}) |
Steve McIntyre | ed5cbea | 2014-12-02 18:23:00 +0000 | [diff] [blame] | 474 | print 'Deleted vlan_id %d' % vlan_id |
| 475 | except InputError as inst: |
| 476 | print 'Failed: %s' % inst |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 477 | elif opts.delete_trunk is not None: |
| 478 | try: |
| 479 | port_id = call_vland('db_update', |
| 480 | {'command':'db.delete_trunk', |
| 481 | 'data': {'trunk_id': opts.delete_trunk}}) |
| 482 | except InputError as inst: |
| 483 | print 'Failed: %s' % inst |
Steve McIntyre | 8e839a6 | 2014-12-05 15:46:05 +0000 | [diff] [blame] | 484 | elif opts.lookup_switch_by_name is not None: |
| 485 | try: |
Steve McIntyre | e3f3eb3 | 2014-12-17 13:23:30 +0000 | [diff] [blame] | 486 | switch_id = call_vland('db_query', |
| 487 | {'command':'db.get_switch_id_by_name', |
| 488 | 'data':{'name':opts.lookup_switch_by_name}}) |
Steve McIntyre | 8e839a6 | 2014-12-05 15:46:05 +0000 | [diff] [blame] | 489 | if switch_id is not None: |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 490 | print '%d' % switch_id |
Steve McIntyre | 8e839a6 | 2014-12-05 15:46:05 +0000 | [diff] [blame] | 491 | else: |
| 492 | print 'No switch found for name %s' % opts.lookup_switch_by_name |
| 493 | except InputError as inst: |
Steve McIntyre | b826fc7 | 2015-07-27 17:57:40 +0100 | [diff] [blame] | 494 | print 'Failed: %s' % inst |
Steve McIntyre | a132c36 | 2014-12-05 15:53:21 +0000 | [diff] [blame] | 495 | elif opts.show_switch is not None: |
| 496 | try: |
Steve McIntyre | 6d84ec1 | 2014-12-18 16:56:56 +0000 | [diff] [blame] | 497 | this_switch = call_vland('db_query', |
| 498 | {'command':'db.get_switch_by_id', |
| 499 | 'data': |
| 500 | {'switch_id': opts.show_switch}}) |
| 501 | if this_switch is not None: |
| 502 | dump_switch(this_switch) |
Steve McIntyre | a132c36 | 2014-12-05 15:53:21 +0000 | [diff] [blame] | 503 | else: |
| 504 | print 'No switch found for switch_id %d' % opts.show_switch |
| 505 | except InputError as inst: |
Steve McIntyre | b826fc7 | 2015-07-27 17:57:40 +0100 | [diff] [blame] | 506 | print 'Failed: %s' % inst |
Steve McIntyre | 11e4cbd | 2014-12-05 16:03:03 +0000 | [diff] [blame] | 507 | elif opts.list_switch_ports is not None: |
| 508 | try: |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 509 | ports = call_vland('db_query', |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 510 | {'command':'db.get_ports_by_switch', |
Steve McIntyre | bfed0fb | 2014-12-18 16:48:28 +0000 | [diff] [blame] | 511 | 'data': |
| 512 | {'switch_id': opts.list_switch_ports}}) |
Steve McIntyre | 11e4cbd | 2014-12-05 16:03:03 +0000 | [diff] [blame] | 513 | if ports is not None: |
Steve McIntyre | 6d84ec1 | 2014-12-18 16:56:56 +0000 | [diff] [blame] | 514 | for p in ports: |
Steve McIntyre | ea15fa7 | 2015-03-26 15:41:34 +0000 | [diff] [blame] | 515 | print p |
Steve McIntyre | 11e4cbd | 2014-12-05 16:03:03 +0000 | [diff] [blame] | 516 | else: |
| 517 | print 'No ports found for switch_id %d' % opts.list_switch_ports |
| 518 | except InputError as inst: |
Steve McIntyre | b826fc7 | 2015-07-27 17:57:40 +0100 | [diff] [blame] | 519 | print 'Failed: %s' % inst |
Steve McIntyre | 08dd839 | 2014-12-05 16:07:20 +0000 | [diff] [blame] | 520 | elif opts.show_port is not None: |
| 521 | try: |
Steve McIntyre | 6d84ec1 | 2014-12-18 16:56:56 +0000 | [diff] [blame] | 522 | this_port = call_vland('db_query', |
| 523 | {'command':'db.get_port_by_id', |
| 524 | 'data': |
| 525 | {'port_id': opts.show_port}}) |
| 526 | if this_port is not None: |
| 527 | dump_port(this_port) |
Steve McIntyre | 08dd839 | 2014-12-05 16:07:20 +0000 | [diff] [blame] | 528 | else: |
| 529 | print 'No port found for port_id %d' % opts.show_port |
| 530 | except InputError as inst: |
Steve McIntyre | b826fc7 | 2015-07-27 17:57:40 +0100 | [diff] [blame] | 531 | print 'Failed: %s' % inst |
Steve McIntyre | 7310657 | 2014-12-05 16:13:36 +0000 | [diff] [blame] | 532 | elif opts.lookup_port_by_switch_and_name is not None: |
| 533 | try: |
Steve McIntyre | 6d84ec1 | 2014-12-18 16:56:56 +0000 | [diff] [blame] | 534 | p = call_vland('db_query', |
| 535 | {'command':'db.get_port_by_switch_and_name', |
| 536 | 'data': |
| 537 | {'switch_id': opts.lookup_port_by_switch_and_name[0], |
| 538 | 'name': opts.lookup_port_by_switch_and_name[1]}}) |
| 539 | if p is not None: |
| 540 | print p |
Steve McIntyre | 7310657 | 2014-12-05 16:13:36 +0000 | [diff] [blame] | 541 | else: |
| 542 | print 'No port found for switch_id %d, name %s' % (int(opts.lookup_port_by_switch_and_name[0]), opts.lookup_port_by_switch_and_name[1]) |
| 543 | except InputError as inst: |
Steve McIntyre | b826fc7 | 2015-07-27 17:57:40 +0100 | [diff] [blame] | 544 | print 'Failed: %s' % inst |
Steve McIntyre | 45f5501 | 2015-08-05 13:55:15 +0100 | [diff] [blame] | 545 | elif opts.lookup_port_by_switch_and_number is not None: |
| 546 | try: |
| 547 | p = call_vland('db_query', |
| 548 | {'command':'db.get_port_by_switch_and_number', |
| 549 | 'data': |
| 550 | {'switch_id': opts.lookup_port_by_switch_and_number[0], |
| 551 | 'number': opts.lookup_port_by_switch_and_number[1]}}) |
| 552 | if p is not None: |
| 553 | print p |
| 554 | else: |
| 555 | print 'No port found for switch_id %d, port number %d' % (int(opts.lookup_port_by_switch_and_number[0]), int(opts.lookup_port_by_switch_and_number[1])) |
| 556 | except InputError as inst: |
| 557 | print 'Failed: %s' % inst |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 558 | elif opts.lookup_ports_by_switch is not None: |
| 559 | try: |
| 560 | p = call_vland('db_query', |
| 561 | {'command':'db.get_ports_by_switch', |
| 562 | 'data': |
| 563 | {'switch_id': opts.lookup_ports_by_switch}}) |
| 564 | if p is not None: |
Steve McIntyre | 913c0e8 | 2015-09-18 14:23:13 +0100 | [diff] [blame] | 565 | for port_id in p: |
| 566 | print port_id |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 567 | else: |
| 568 | print 'No ports found for switch_id %d' % int(opts.lookup_ports_by_switch) |
| 569 | except InputError as inst: |
| 570 | print 'Failed: %s' % inst |
| 571 | elif opts.lookup_ports_by_current_vlan is not None: |
| 572 | try: |
| 573 | p = call_vland('db_query', |
| 574 | {'command':'db.get_ports_by_current_vlan', |
| 575 | 'data': |
| 576 | {'vlan_id': opts.lookup_ports_by_current_vlan}}) |
| 577 | if p is not None: |
Steve McIntyre | 913c0e8 | 2015-09-18 14:23:13 +0100 | [diff] [blame] | 578 | for port_id in p: |
| 579 | print port_id |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 580 | else: |
| 581 | print 'No ports found for current vlan_id %d' % int(opts.lookup_ports_by_current_vlan) |
| 582 | except InputError as inst: |
| 583 | print 'Failed: %s' % inst |
| 584 | elif opts.lookup_ports_by_base_vlan is not None: |
| 585 | try: |
| 586 | p = call_vland('db_query', |
| 587 | {'command':'db.get_ports_by_base_vlan', |
| 588 | 'data': |
| 589 | {'vlan_id': opts.lookup_ports_by_base_vlan}}) |
| 590 | if p is not None: |
Steve McIntyre | 913c0e8 | 2015-09-18 14:23:13 +0100 | [diff] [blame] | 591 | for port_id in p: |
| 592 | print port_id |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 593 | else: |
| 594 | print 'No ports found for base vlan_id %d' % int(opts.lookup_ports_by_base_vlan) |
| 595 | except InputError as inst: |
| 596 | print 'Failed: %s' % inst |
| 597 | elif opts.lookup_ports_by_trunk is not None: |
| 598 | try: |
| 599 | p = call_vland('db_query', |
| 600 | {'command':'db.get_ports_by_trunk', |
| 601 | 'data': |
| 602 | {'trunk_id': opts.lookup_ports_by_trunk}}) |
| 603 | if p is not None: |
Steve McIntyre | 913c0e8 | 2015-09-18 14:23:13 +0100 | [diff] [blame] | 604 | for port_id in p: |
| 605 | print port_id |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 606 | else: |
| 607 | print 'No ports found for trunk_id %d' % int(opts.lookup_ports_by_trunk) |
| 608 | except InputError as inst: |
| 609 | print 'Failed: %s' % inst |
Steve McIntyre | 71b4102 | 2014-12-05 17:17:44 +0000 | [diff] [blame] | 610 | elif opts.set_port_mode is not None: |
| 611 | try: |
Steve McIntyre | 3f0aceb | 2014-12-17 16:27:13 +0000 | [diff] [blame] | 612 | port_id = call_vland('vlan_update', |
| 613 | {'command':'api.set_port_mode', |
| 614 | 'data': |
| 615 | {'port_id': opts.set_port_mode[0], |
| 616 | 'mode': opts.set_port_mode[1]}}) |
Steve McIntyre | 71b4102 | 2014-12-05 17:17:44 +0000 | [diff] [blame] | 617 | print "Updated mode for port_id %d" % port_id |
| 618 | except InputError as inst: |
Steve McIntyre | b826fc7 | 2015-07-27 17:57:40 +0100 | [diff] [blame] | 619 | print 'Failed: %s' % inst |
Steve McIntyre | 75dc4ec | 2014-12-05 17:20:42 +0000 | [diff] [blame] | 620 | elif opts.lock_port is not None: |
| 621 | try: |
Steve McIntyre | 7103de2 | 2014-12-17 13:16:48 +0000 | [diff] [blame] | 622 | port_id = call_vland('db_update', |
| 623 | {'command':'db.set_port_is_locked', |
| 624 | 'data': |
| 625 | {'port_id': opts.lock_port, |
| 626 | 'is_locked': True}}) |
Steve McIntyre | 75dc4ec | 2014-12-05 17:20:42 +0000 | [diff] [blame] | 627 | print "Locked port_id %d" % port_id |
| 628 | except InputError as inst: |
Steve McIntyre | b826fc7 | 2015-07-27 17:57:40 +0100 | [diff] [blame] | 629 | print 'Failed: %s' % inst |
Steve McIntyre | 75dc4ec | 2014-12-05 17:20:42 +0000 | [diff] [blame] | 630 | elif opts.unlock_port is not None: |
| 631 | try: |
Steve McIntyre | 7103de2 | 2014-12-17 13:16:48 +0000 | [diff] [blame] | 632 | port_id = call_vland('db_update', |
| 633 | {'command':'db.set_port_is_locked', |
| 634 | 'data': |
| 635 | {'port_id': opts.unlock_port, |
| 636 | 'is_locked': False}}) |
Steve McIntyre | 75dc4ec | 2014-12-05 17:20:42 +0000 | [diff] [blame] | 637 | print "Unlocked port_id %d" % port_id |
| 638 | except InputError as inst: |
Steve McIntyre | b826fc7 | 2015-07-27 17:57:40 +0100 | [diff] [blame] | 639 | print 'Failed: %s' % inst |
Steve McIntyre | a2bbcda | 2014-12-05 17:57:36 +0000 | [diff] [blame] | 640 | elif opts.set_port_current_vlan is not None: |
| 641 | try: |
Steve McIntyre | 3f0aceb | 2014-12-17 16:27:13 +0000 | [diff] [blame] | 642 | port_id = call_vland('vlan_update', |
| 643 | {'command':'api.set_current_vlan', |
| 644 | 'data': |
Steve McIntyre | e706209 | 2015-02-13 03:02:14 +0000 | [diff] [blame] | 645 | {'port_id': opts.set_port_current_vlan[0], |
| 646 | 'vlan_id': opts.set_port_current_vlan[1]}}) |
Steve McIntyre | a2bbcda | 2014-12-05 17:57:36 +0000 | [diff] [blame] | 647 | print "Set current VLAN on port_id %d" % port_id |
| 648 | except InputError as inst: |
| 649 | print 'Failed: %s' % inst |
| 650 | elif opts.get_port_current_vlan is not None: |
| 651 | try: |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 652 | vlan_id = call_vland('db_query', |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 653 | {'command':'db.get_current_vlan_id_by_port', |
Steve McIntyre | e3f3eb3 | 2014-12-17 13:23:30 +0000 | [diff] [blame] | 654 | 'data': |
| 655 | {'port_id': opts.get_port_current_vlan}}) |
Steve McIntyre | a2bbcda | 2014-12-05 17:57:36 +0000 | [diff] [blame] | 656 | if vlan_id is not None: |
| 657 | print vlan_id |
| 658 | else: |
| 659 | print "No current_vlan_id found for port_id %d" % opts.get_port_current_vlan |
| 660 | except InputError as inst: |
| 661 | print 'Failed: %s' % inst |
| 662 | elif opts.set_port_base_vlan is not None: |
| 663 | try: |
Steve McIntyre | 7103de2 | 2014-12-17 13:16:48 +0000 | [diff] [blame] | 664 | port_id = call_vland('db_update', |
| 665 | {'command':'db.set_base_vlan', |
| 666 | 'data': |
Steve McIntyre | 6ebac38 | 2015-07-28 18:14:20 +0100 | [diff] [blame] | 667 | {'port_id': opts.set_port_base_vlan[0], |
| 668 | 'base_vlan_id': opts.set_port_base_vlan[1]}}) |
Steve McIntyre | a2bbcda | 2014-12-05 17:57:36 +0000 | [diff] [blame] | 669 | print "Set base VLAN on port_id %d" % port_id |
| 670 | except InputError as inst: |
| 671 | print 'Failed: %s' % inst |
| 672 | elif opts.get_port_base_vlan is not None: |
| 673 | try: |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 674 | vlan_id = call_vland('db_query', |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 675 | {'command':'db.get_base_vlan_id_by_port', |
Steve McIntyre | a02ba20 | 2014-12-17 16:27:23 +0000 | [diff] [blame] | 676 | 'data': |
| 677 | {'port_id': opts.get_port_base_vlan}}) |
Steve McIntyre | a2bbcda | 2014-12-05 17:57:36 +0000 | [diff] [blame] | 678 | if vlan_id is not None: |
| 679 | print vlan_id |
| 680 | else: |
| 681 | print "No base_vlan_id found for port_id %d" % port_id |
| 682 | except InputError as inst: |
| 683 | print 'Failed: %s' % inst |
| 684 | elif opts.restore_port_to_base_vlan is not None: |
| 685 | try: |
Steve McIntyre | 3f0aceb | 2014-12-17 16:27:13 +0000 | [diff] [blame] | 686 | port_id = call_vland('vlan_update', |
| 687 | {'command': 'api.restore_base_vlan', |
| 688 | 'data': |
| 689 | {'port_id': opts.restore_port_to_base_vlan}}) |
Steve McIntyre | a2bbcda | 2014-12-05 17:57:36 +0000 | [diff] [blame] | 690 | print "Restored port_id %d back to base VLAN" % port_id |
| 691 | except InputError as inst: |
| 692 | print 'Failed: %s' % inst |
Steve McIntyre | e41e3f3 | 2014-12-05 18:08:21 +0000 | [diff] [blame] | 693 | elif opts.show_vlan is not None: |
| 694 | try: |
Steve McIntyre | 6d84ec1 | 2014-12-18 16:56:56 +0000 | [diff] [blame] | 695 | v = call_vland('db_query', |
| 696 | {'command':'db.get_vlan_by_id', |
| 697 | 'data': |
| 698 | {'vlan_id': opts.show_vlan}}) |
| 699 | if v is not None: |
| 700 | dump_vlan(v) |
Steve McIntyre | e41e3f3 | 2014-12-05 18:08:21 +0000 | [diff] [blame] | 701 | else: |
| 702 | print 'No vlan found for vlan_id %d' % opts.show_vlan |
| 703 | except InputError as inst: |
| 704 | print 'Failed: %s' % inst |
Steve McIntyre | 65533d7 | 2015-01-23 18:01:17 +0000 | [diff] [blame] | 705 | elif opts.lookup_vlan_by_tag is not None: |
| 706 | try: |
| 707 | vlan_id = call_vland('db_query', |
| 708 | {'command':'db.get_vlan_id_by_tag', |
| 709 | 'data': |
| 710 | {'tag': opts.lookup_vlan_by_tag}}) |
| 711 | if vlan_id is not None: |
| 712 | print vlan_id |
| 713 | else: |
| 714 | print 'No vlan found for vlan tag %d' % opts.lookup_vlan_by_tag |
| 715 | except InputError as inst: |
| 716 | print 'Failed: %s' % inst |
| 717 | elif opts.show_vlan_tag is not None: |
| 718 | try: |
| 719 | vlan_tag = call_vland('db_query', |
| 720 | {'command':'db.get_vlan_tag_by_id', |
| 721 | 'data': |
| 722 | {'vlan_id': opts.show_vlan_tag}}) |
| 723 | if vlan_tag is not None: |
| 724 | print vlan_tag |
| 725 | else: |
| 726 | print 'No vlan found for vlan id %d' % opts.show_vlan_tag |
| 727 | except InputError as inst: |
| 728 | print 'Failed: %s' % inst |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 729 | elif opts.show_trunk is not None: |
| 730 | try: |
| 731 | this_trunk = call_vland('db_query', |
| 732 | {'command':'db.get_trunk_by_id', |
| 733 | 'data': |
| 734 | {'trunk_id': opts.show_trunk}}) |
| 735 | if this_trunk is not None: |
| 736 | dump_trunk(this_trunk) |
| 737 | else: |
| 738 | print 'No port found for port_id %d' % opts.show_port |
| 739 | except InputError as inst: |
| 740 | print 'Failed: %s' % inst |
Steve McIntyre | 9cd6c3d | 2014-12-05 18:10:35 +0000 | [diff] [blame] | 741 | elif opts.status: |
Steve McIntyre | bb718cf | 2014-12-15 16:57:25 +0000 | [diff] [blame] | 742 | print 'Config:' |
| 743 | print ' knows about %d switch(es)' % len(config.switches) |
Steve McIntyre | 7103de2 | 2014-12-17 13:16:48 +0000 | [diff] [blame] | 744 | default_vlan_id = call_vland('db_query', |
| 745 | {'command':'db.get_vlan_id_by_tag', |
Steve McIntyre | e3f3eb3 | 2014-12-17 13:23:30 +0000 | [diff] [blame] | 746 | 'data': |
| 747 | {'tag': config.vland.default_vlan_tag}}) |
Steve McIntyre | 0abacac | 2014-12-15 16:51:38 +0000 | [diff] [blame] | 748 | print 'The default vlan tag (%d) is vlan_id %d' % (config.vland.default_vlan_tag, default_vlan_id) |
Steve McIntyre | 6d84ec1 | 2014-12-18 16:56:56 +0000 | [diff] [blame] | 749 | stat = call_vland('daemon_query', {'command':'daemon.status', 'data': None}) |
| 750 | print 'VLANd is running %s' % stat['running'] |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 751 | print 'DB via VLANd:' |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 752 | switches = call_vland('db_query', {'command':'db.all_switches', 'data':None}) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 753 | print ' knows about %d switch(es)' % len(switches) |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 754 | ports = call_vland('db_query', {'command':'db.all_ports', 'data':None}) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 755 | print ' knows about %d port(s)' % len(ports) |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 756 | vlans = call_vland('db_query', {'command':'db.all_vlans', 'data':None}) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 757 | print ' DB knows about %d vlan(s)' % len(vlans) |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 758 | elif opts.vland_version: |
Steve McIntyre | 6d84ec1 | 2014-12-18 16:56:56 +0000 | [diff] [blame] | 759 | ver = call_vland('daemon_query', {'command':'daemon.version', 'data': None}) |
| 760 | print 'VLANd version %s' % ver['version'] |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 761 | elif opts.statistics: |
Steve McIntyre | 6d84ec1 | 2014-12-18 16:56:56 +0000 | [diff] [blame] | 762 | stats = call_vland('daemon_query', {'command':'daemon.statistics', 'data': None}) |
| 763 | print 'VLANd uptime: %d seconds' % stats['uptime'] |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 764 | elif opts.version: |
| 765 | print 'VLANd admin interface version %s' % version |
Steve McIntyre | 6c56260 | 2014-12-22 16:10:54 +0000 | [diff] [blame] | 766 | elif opts.auto_import_switch: |
| 767 | print 'Attempting to import switch %s' % opts.auto_import_switch |
| 768 | if opts.auto_import_switch not in config.switches: |
| 769 | raise InputError("Can't find switch %s in config" % opts.auto_import_switch) |
Steve McIntyre | e26a147 | 2015-04-01 18:03:31 +0100 | [diff] [blame] | 770 | imp = call_vland('vlan_update', |
Steve McIntyre | 6c56260 | 2014-12-22 16:10:54 +0000 | [diff] [blame] | 771 | {'command':'api.auto_import_switch', |
| 772 | 'data': |
| 773 | {'switch': opts.auto_import_switch}}) |
Steve McIntyre | 24a4d57 | 2015-07-09 18:25:17 +0100 | [diff] [blame] | 774 | print 'VLANd imported switch %s successfully: new switch_id %d, %d new ports, %d new VLANs' % (opts.auto_import_switch, imp['switch_id'], imp['num_ports_added'], imp['num_vlans_added']) |
Steve McIntyre | deff795 | 2014-12-23 13:45:59 +0000 | [diff] [blame] | 775 | elif opts.probe_switches: |
| 776 | print 'Asking VLANd to probe all the configured switches' |
Steve McIntyre | e26a147 | 2015-04-01 18:03:31 +0100 | [diff] [blame] | 777 | probe = call_vland('daemon_query', |
| 778 | {'command':'daemon.probe_switches', |
| 779 | 'data': None}) |
| 780 | for field in probe: |
| 781 | print '%s: %s' % (field, probe[field]) |
Steve McIntyre | 06fe642 | 2015-01-23 17:55:43 +0000 | [diff] [blame] | 782 | elif opts.shutdown: |
| 783 | print 'Asking VLANd to shutdown' |
Steve McIntyre | e26a147 | 2015-04-01 18:03:31 +0100 | [diff] [blame] | 784 | shutdown = call_vland('daemon_query', |
| 785 | {'command':'daemon.shutdown', |
| 786 | 'data': None}) |
| 787 | for field in shutdown: |
| 788 | print '%s: %s' % (field, shutdown[field]) |
Steve McIntyre | a0534a5 | 2014-12-05 17:58:40 +0000 | [diff] [blame] | 789 | else: |
Steve McIntyre | 4a80891 | 2014-12-05 15:24:39 +0000 | [diff] [blame] | 790 | print 'No recognised command given. Try -h for help' |