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