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 | 13d6983 | 2015-01-23 17:50:55 +0000 | [diff] [blame] | 33 | version = "0.2-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 | ae95fd6 | 2014-12-05 16:51:41 +0000 | [diff] [blame] | 36 | def is_positive(text): |
Steve McIntyre | c6895fc | 2014-12-19 15:01:08 +0000 | [diff] [blame] | 37 | if text in ('1', 'y', 'Y', 't', 'T', 'True', 'true'): |
Steve McIntyre | ae95fd6 | 2014-12-05 16:51:41 +0000 | [diff] [blame] | 38 | return True |
Steve McIntyre | c6895fc | 2014-12-19 15:01:08 +0000 | [diff] [blame] | 39 | elif text in ('0', 'n', 'N', 'f', 'F', 'False', 'false'): |
Steve McIntyre | ae95fd6 | 2014-12-05 16:51:41 +0000 | [diff] [blame] | 40 | return False |
| 41 | else: |
| 42 | raise InputError("Cannot parse \"%s\" as True or False" % text) |
| 43 | |
Steve McIntyre | a132c36 | 2014-12-05 15:53:21 +0000 | [diff] [blame] | 44 | def dump_switch(switch): |
Steve McIntyre | 018d30c | 2015-02-09 06:34:57 +0000 | [diff] [blame] | 45 | print "switch_id:%d name:%s" % ( |
| 46 | int(switch[0]), |
| 47 | switch[1]) |
Steve McIntyre | a132c36 | 2014-12-05 15:53:21 +0000 | [diff] [blame] | 48 | |
Steve McIntyre | 11e4cbd | 2014-12-05 16:03:03 +0000 | [diff] [blame] | 49 | def dump_port(port): |
Steve McIntyre | 018d30c | 2015-02-09 06:34:57 +0000 | [diff] [blame] | 50 | print "port_id:%d name:%s switch_id:%d locked:%s mode:%s base_vlan_id:%d current_vlan_id:%d" % ( |
| 51 | int(port[0]), |
| 52 | port[1], |
| 53 | int(port[2]), |
| 54 | ("yes" if port[3] is True else "no"), |
| 55 | ("trunk" if port[4] is True else "access"), |
| 56 | int(port[5]), |
| 57 | int(port[6])) |
Steve McIntyre | 11e4cbd | 2014-12-05 16:03:03 +0000 | [diff] [blame] | 58 | |
Steve McIntyre | e41e3f3 | 2014-12-05 18:08:21 +0000 | [diff] [blame] | 59 | def dump_vlan(vlan): |
Steve McIntyre | 018d30c | 2015-02-09 06:34:57 +0000 | [diff] [blame] | 60 | print "vlan_id:%d name:%s tag:%d is_base_vlan:%s, creation_time:%s" % ( |
| 61 | int(vlan[0]), |
| 62 | vlan[1], |
| 63 | int(vlan[2]), |
| 64 | ("yes" if vlan[3] is True else "no"), |
| 65 | vlan[4]) |
Steve McIntyre | e41e3f3 | 2014-12-05 18:08:21 +0000 | [diff] [blame] | 66 | |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 67 | def call_vland(msgtype, msg): |
| 68 | ipc = VlanIpc() |
| 69 | ipc.client_connect('localhost', config.vland.port) |
| 70 | msg['client_name'] = 'admin.py' |
| 71 | msg['type'] = msgtype |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 72 | ipc.client_send(msg) |
| 73 | ret = ipc.client_recv_and_close() |
| 74 | if 'response' not in ret: |
| 75 | raise SocketError("Badly-formed response from VLANd server") |
| 76 | if ret['response'] == "ERROR": |
Steve McIntyre | 8a1a1ae | 2015-01-23 17:52:14 +0000 | [diff] [blame] | 77 | print "Input error: VLANd server said \"%s\"" % ret['error'] |
| 78 | sys.exit(1) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 79 | return ret['data'] |
| 80 | |
Steve McIntyre | c12f631 | 2014-12-15 15:00:12 +0000 | [diff] [blame] | 81 | config = VlanConfig(filenames=('./vland.cfg',)) |
Steve McIntyre | c12f631 | 2014-12-15 15:00:12 +0000 | [diff] [blame] | 82 | |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 83 | usage = 'Usage: %prog --command [command options]' |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 84 | parser = optparse.OptionParser(usage=usage, description=banner) |
| 85 | |
Steve McIntyre | 9cd6c3d | 2014-12-05 18:10:35 +0000 | [diff] [blame] | 86 | # System commands |
| 87 | system_group = optparse.OptionGroup(parser, "System commands") |
| 88 | system_group.add_option("--status", |
| 89 | dest="status", |
| 90 | action = "store_true", |
| 91 | default = False, |
| 92 | help = "Describe current system status") |
Steve McIntyre | c00d4cf | 2014-12-16 19:23:39 +0000 | [diff] [blame] | 93 | system_group.add_option("--statistics", |
| 94 | dest="statistics", |
| 95 | action = "store_true", |
| 96 | default = False, |
| 97 | help = "Print some system statistics") |
| 98 | system_group.add_option("--version", |
| 99 | dest="version", |
| 100 | action = "store_true", |
| 101 | default = False, |
| 102 | help = "Describe the version of this admin interface") |
| 103 | system_group.add_option("--vland_version", |
| 104 | dest="vland_version", |
| 105 | action = "store_true", |
| 106 | default = False, |
| 107 | help = "Describe the version of the running VLANd") |
Steve McIntyre | a4bb991 | 2015-01-13 18:40:37 +0000 | [diff] [blame] | 108 | system_group.add_option("--auto_import_switch", |
Steve McIntyre | 6c56260 | 2014-12-22 16:10:54 +0000 | [diff] [blame] | 109 | dest = "auto_import_switch", |
| 110 | action = "store", |
| 111 | type = "string", |
| 112 | help = "Attempt to import a switch's configuration into the VLANd system", |
| 113 | nargs = 1, |
| 114 | metavar = "<switch name>") |
Steve McIntyre | a4bb991 | 2015-01-13 18:40:37 +0000 | [diff] [blame] | 115 | system_group.add_option("--probe_switches", |
Steve McIntyre | deff795 | 2014-12-23 13:45:59 +0000 | [diff] [blame] | 116 | dest = "probe_switches", |
| 117 | action = "store_true", |
| 118 | default = False, |
Steve McIntyre | a4bb991 | 2015-01-13 18:40:37 +0000 | [diff] [blame] | 119 | help = "Probe all the configured switches") |
Steve McIntyre | 06fe642 | 2015-01-23 17:55:43 +0000 | [diff] [blame] | 120 | system_group.add_option("--shutdown", |
| 121 | dest = "shutdown", |
| 122 | action = "store_true", |
| 123 | default = False, |
| 124 | help = "Shut down a running VLANd") |
Steve McIntyre | 9cd6c3d | 2014-12-05 18:10:35 +0000 | [diff] [blame] | 125 | parser.add_option_group(system_group) |
| 126 | # May add shutdown, self-check etc. later |
| 127 | |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 128 | # Switch commands |
| 129 | switch_group = optparse.OptionGroup(parser, "Switch commands") |
| 130 | switch_group.add_option("--list_all_switches", |
| 131 | dest = "list_all_switches", |
| 132 | action = "store_true", |
| 133 | default = False, |
| 134 | help = "List all the existing switches in the system") |
| 135 | switch_group.add_option("--create_switch", |
| 136 | dest = "create_switch", |
| 137 | action = "store", |
| 138 | type = "string", |
| 139 | help = "Add a new switch to the system", |
| 140 | nargs = 1, |
| 141 | metavar = "<name>") |
Steve McIntyre | 99feaee | 2014-12-02 18:22:36 +0000 | [diff] [blame] | 142 | switch_group.add_option("--delete_switch", |
| 143 | dest = "delete_switch", |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 144 | action = "store", |
| 145 | type = "int", |
| 146 | help = "Remove an existing switch from the system", |
Steve McIntyre | 59e0463 | 2014-12-02 18:02:16 +0000 | [diff] [blame] | 147 | default = None, |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 148 | nargs = 1, |
| 149 | metavar = "<switch_id>") |
| 150 | switch_group.add_option("--show_switch", |
| 151 | dest = "show_switch", |
| 152 | action = "store", |
| 153 | type = "int", |
| 154 | help = "Show the details of an existing switch in the system", |
Steve McIntyre | 59e0463 | 2014-12-02 18:02:16 +0000 | [diff] [blame] | 155 | default = None, |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 156 | nargs = 1, |
| 157 | metavar = "<switch_id>") |
| 158 | switch_group.add_option("--lookup_switch_by_name", |
| 159 | dest = "lookup_switch_by_name", |
| 160 | action = "store", |
| 161 | type = "string", |
| 162 | help = "Lookup a switch ID by name", |
| 163 | nargs = 1, |
| 164 | metavar = "<name>") |
| 165 | switch_group.add_option("--list_switch_ports", |
| 166 | dest = "list_switch_ports", |
| 167 | action = "store", |
| 168 | type = "int", |
Steve McIntyre | 11e4cbd | 2014-12-05 16:03:03 +0000 | [diff] [blame] | 169 | 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] | 170 | default = None, |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 171 | nargs = 1, |
| 172 | metavar = "<switch_id>") |
| 173 | parser.add_option_group(switch_group) |
| 174 | |
| 175 | # Port commands |
| 176 | port_group = optparse.OptionGroup(parser, "Port commands") |
| 177 | port_group.add_option("--list_all_ports", |
| 178 | dest = "list_all_ports", |
| 179 | action = "store_true", |
| 180 | default = False, |
| 181 | help = "List all the existing ports in the system") |
| 182 | port_group.add_option("--create_port", |
| 183 | dest = "create_port", |
| 184 | action = "store", |
| 185 | type = "string", |
| 186 | help = "Add a new port to the system", |
| 187 | nargs = 2, |
| 188 | metavar = "<switch_id> <name>") |
Steve McIntyre | 99feaee | 2014-12-02 18:22:36 +0000 | [diff] [blame] | 189 | port_group.add_option("--delete_port", |
| 190 | dest = "delete_port", |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 191 | action = "store", |
| 192 | type = "int", |
| 193 | help = "Remove an existing port from the system", |
Steve McIntyre | 59e0463 | 2014-12-02 18:02:16 +0000 | [diff] [blame] | 194 | default = None, |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 195 | nargs = 1, |
| 196 | metavar = "<port_id>") |
| 197 | port_group.add_option("--show_port", |
| 198 | dest = "show_port", |
| 199 | action = "store", |
| 200 | type = "int", |
| 201 | help = "Show the details of an existing port in the system", |
Steve McIntyre | 59e0463 | 2014-12-02 18:02:16 +0000 | [diff] [blame] | 202 | default = None, |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 203 | nargs = 1, |
| 204 | metavar = "<port_id>") |
| 205 | port_group.add_option("--lookup_port_by_switch_and_name", |
| 206 | dest = "lookup_port_by_switch_and_name", |
| 207 | action = "store", |
| 208 | type = "string", |
| 209 | help = "Lookup a port ID by switch and port name", |
| 210 | nargs = 2, |
| 211 | metavar = "<switch_id> <name>") |
Steve McIntyre | 71b4102 | 2014-12-05 17:17:44 +0000 | [diff] [blame] | 212 | port_group.add_option("--set_port_mode", |
| 213 | dest = "set_port_mode", |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 214 | action = "store", |
| 215 | type = "string", |
| 216 | help = "Set the mode of a port to 'trunk' or 'access'", |
| 217 | nargs = 2, |
| 218 | metavar = "<port_id> <mode>") |
| 219 | port_group.add_option("--lock_port", |
| 220 | dest = "lock_port", |
| 221 | action = "store", |
| 222 | type = "string", |
| 223 | help = "Lock the settings on a port", |
| 224 | nargs = 1, |
| 225 | metavar = "<port_id>") |
| 226 | port_group.add_option("--unlock_port", |
| 227 | dest = "unlock_port", |
| 228 | action = "store", |
| 229 | type = "string", |
| 230 | help = "Unock the settings on a port", |
| 231 | nargs = 1, |
| 232 | metavar = "<port_id>") |
| 233 | port_group.add_option("--set_port_current_vlan", |
| 234 | dest = "set_port_current_vlan", |
| 235 | action = "store", |
| 236 | type = "int", |
| 237 | help = "Set the current VLAN assignment for a port", |
| 238 | nargs = 2, |
| 239 | metavar = "<port_id> <vlan_id>") |
| 240 | port_group.add_option("--get_port_current_vlan", |
| 241 | dest = "get_port_current_vlan", |
| 242 | action = "store", |
| 243 | type = "int", |
| 244 | help = "Get the current VLAN assignment for a port", |
| 245 | nargs = 1, |
| 246 | metavar = "<port_id>") |
| 247 | port_group.add_option("--set_port_base_vlan", |
| 248 | dest = "set_port_base_vlan", |
| 249 | action = "store", |
| 250 | type = "int", |
| 251 | help = "Set the base VLAN assignment for a port", |
| 252 | nargs = 2, |
| 253 | metavar = "<port_id> <vlan_id>") |
| 254 | port_group.add_option("--get_port_base_vlan", |
| 255 | dest = "get_port_base_vlan", |
| 256 | action = "store", |
| 257 | type = "int", |
| 258 | help = "Get the base VLAN assignment for a port", |
| 259 | nargs = 1, |
| 260 | metavar = "<port_id>") |
Steve McIntyre | a2bbcda | 2014-12-05 17:57:36 +0000 | [diff] [blame] | 261 | port_group.add_option("--restore_port_to_base_vlan", |
| 262 | dest = "restore_port_to_base_vlan", |
| 263 | action = "store", |
| 264 | type = "int", |
| 265 | help = "Reset the port back to its base VLAN", |
| 266 | nargs = 1, |
| 267 | metavar = "<port_id>") |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 268 | parser.add_option_group(port_group) |
| 269 | |
| 270 | # VLAN commands |
| 271 | vlan_group = optparse.OptionGroup(parser, "VLAN commands") |
| 272 | vlan_group.add_option("--list_all_vlans", |
| 273 | dest = "list_all_vlans", |
| 274 | action = "store_true", |
| 275 | default = False, |
| 276 | help = "List all the existing vlans in the system") |
| 277 | vlan_group.add_option("--create_vlan", |
| 278 | dest = "create_vlan", |
| 279 | action = "store", |
| 280 | type = "string", |
| 281 | help = "Add a new vlan to the system", |
| 282 | nargs = 3, |
Steve McIntyre | 9f0bb60 | 2014-11-28 14:36:39 +0000 | [diff] [blame] | 283 | metavar = "<name> <tag> <is_base_vlan>") |
Steve McIntyre | 99feaee | 2014-12-02 18:22:36 +0000 | [diff] [blame] | 284 | vlan_group.add_option("--delete_vlan", |
| 285 | dest = "delete_vlan", |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 286 | action = "store", |
| 287 | type = "int", |
| 288 | help = "Remove an existing vlan from the system", |
Steve McIntyre | 59e0463 | 2014-12-02 18:02:16 +0000 | [diff] [blame] | 289 | default = None, |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 290 | nargs = 1, |
| 291 | metavar = "<vlan_id>") |
| 292 | vlan_group.add_option("--show_vlan", |
| 293 | dest = "show_vlan", |
| 294 | action = "store", |
| 295 | type = "int", |
| 296 | help = "Show the details of an existing vlan in the system", |
Steve McIntyre | 59e0463 | 2014-12-02 18:02:16 +0000 | [diff] [blame] | 297 | default = None, |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 298 | nargs = 1, |
| 299 | metavar = "<vlan_id>") |
Steve McIntyre | 65533d7 | 2015-01-23 18:01:17 +0000 | [diff] [blame] | 300 | vlan_group.add_option("--lookup_vlan_by_tag", |
| 301 | dest = "lookup_vlan_by_tag", |
| 302 | action = "store", |
| 303 | type = "int", |
| 304 | help = "Find the vlan ID of an existing vlan in the system", |
| 305 | default = None, |
| 306 | nargs = 1, |
| 307 | metavar = "<tag>") |
| 308 | vlan_group.add_option("--show_vlan_tag", |
| 309 | dest = "show_vlan_tag", |
| 310 | action = "store", |
| 311 | type = "int", |
| 312 | help = "Print the vlan tag of an existing vlan in the system", |
| 313 | default = None, |
| 314 | nargs = 1, |
| 315 | metavar = "<vlan_id>") |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 316 | parser.add_option_group(vlan_group) |
| 317 | |
| 318 | (opts, args) = parser.parse_args() |
| 319 | |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 320 | if opts.list_all_switches: |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 321 | result = call_vland('db_query', {'command':'db.all_switches', 'data':None}) |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 322 | for line in result: |
Steve McIntyre | 018d30c | 2015-02-09 06:34:57 +0000 | [diff] [blame] | 323 | dump_switch(line) |
Steve McIntyre | af2d5ed | 2014-12-02 12:42:28 +0000 | [diff] [blame] | 324 | elif opts.list_all_ports: |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 325 | result = call_vland('db_query', {'command':'db.all_ports', 'data':None}) |
Steve McIntyre | af2d5ed | 2014-12-02 12:42:28 +0000 | [diff] [blame] | 326 | for line in result: |
Steve McIntyre | 018d30c | 2015-02-09 06:34:57 +0000 | [diff] [blame] | 327 | dump_port(line) |
Steve McIntyre | af2d5ed | 2014-12-02 12:42:28 +0000 | [diff] [blame] | 328 | elif opts.list_all_vlans: |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 329 | result = call_vland('db_query', {'command':'db.all_vlans', 'data':None}) |
Steve McIntyre | af2d5ed | 2014-12-02 12:42:28 +0000 | [diff] [blame] | 330 | for line in result: |
Steve McIntyre | 018d30c | 2015-02-09 06:34:57 +0000 | [diff] [blame] | 331 | dump_vlan(line) |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 332 | elif opts.create_switch is not None: |
Steve McIntyre | af2d5ed | 2014-12-02 12:42:28 +0000 | [diff] [blame] | 333 | try: |
Steve McIntyre | 7103de2 | 2014-12-17 13:16:48 +0000 | [diff] [blame] | 334 | switch_id = call_vland('db_update', |
| 335 | {'command':'db.create_switch', |
| 336 | 'data': |
| 337 | {'name':opts.create_switch}}) |
Steve McIntyre | af2d5ed | 2014-12-02 12:42:28 +0000 | [diff] [blame] | 338 | print 'Created switch_id %d' % switch_id |
| 339 | except InputError as inst: |
| 340 | print 'Failed: %s' % inst |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 341 | elif opts.create_port is not None: |
Steve McIntyre | af2d5ed | 2014-12-02 12:42:28 +0000 | [diff] [blame] | 342 | try: |
Steve McIntyre | 7103de2 | 2014-12-17 13:16:48 +0000 | [diff] [blame] | 343 | port_id = call_vland('db_update', |
| 344 | {'command':'db.create_port', |
| 345 | 'data': |
| 346 | {'switch_id': opts.create_port[0], |
| 347 | 'name': opts.create_port[1]}}) |
Steve McIntyre | af2d5ed | 2014-12-02 12:42:28 +0000 | [diff] [blame] | 348 | print 'Created port_id %d' % port_id |
| 349 | except InputError as inst: |
| 350 | print 'Failed: %s' % inst |
Steve McIntyre | c21d8d1 | 2014-11-28 14:42:40 +0000 | [diff] [blame] | 351 | elif opts.create_vlan is not None: |
Steve McIntyre | c8aba4c | 2014-12-02 12:48:51 +0000 | [diff] [blame] | 352 | try: |
Steve McIntyre | 3f0aceb | 2014-12-17 16:27:13 +0000 | [diff] [blame] | 353 | vlan_id = call_vland('vlan_update', |
| 354 | {'command':'api.create_vlan', |
| 355 | 'data': |
| 356 | {'name': opts.create_vlan[0], |
| 357 | 'tag': opts.create_vlan[1], |
Steve McIntyre | eb2cd20 | 2014-12-18 16:44:49 +0000 | [diff] [blame] | 358 | 'is_base_vlan': is_positive(opts.create_vlan[2])}}) |
Steve McIntyre | c8aba4c | 2014-12-02 12:48:51 +0000 | [diff] [blame] | 359 | print 'Created vlan_id %d' % vlan_id |
| 360 | except InputError as inst: |
| 361 | print 'Failed: %s' % inst |
Steve McIntyre | 99feaee | 2014-12-02 18:22:36 +0000 | [diff] [blame] | 362 | elif opts.delete_switch is not None: |
Steve McIntyre | 64e3886 | 2014-12-02 17:19:37 +0000 | [diff] [blame] | 363 | try: |
Steve McIntyre | 7103de2 | 2014-12-17 13:16:48 +0000 | [diff] [blame] | 364 | switch_id = call_vland('db_update', |
| 365 | {'command':'db.delete_switch', |
| 366 | 'data': {'switch_id': opts.delete_switch}}) |
Steve McIntyre | 64e3886 | 2014-12-02 17:19:37 +0000 | [diff] [blame] | 367 | print 'Deleted switch_id %d' % switch_id |
| 368 | except InputError as inst: |
| 369 | print 'Failed: %s' % inst |
Steve McIntyre | 99feaee | 2014-12-02 18:22:36 +0000 | [diff] [blame] | 370 | elif opts.delete_port is not None: |
Steve McIntyre | 6f7ee5c | 2014-12-02 18:02:50 +0000 | [diff] [blame] | 371 | try: |
Steve McIntyre | 7103de2 | 2014-12-17 13:16:48 +0000 | [diff] [blame] | 372 | port_id = call_vland('db_update', |
| 373 | {'command':'db.delete_port', |
| 374 | 'data': {'port_id': opts.delete_port}}) |
Steve McIntyre | 6f7ee5c | 2014-12-02 18:02:50 +0000 | [diff] [blame] | 375 | except InputError as inst: |
| 376 | print 'Failed: %s' % inst |
Steve McIntyre | ed5cbea | 2014-12-02 18:23:00 +0000 | [diff] [blame] | 377 | elif opts.delete_vlan is not None: |
| 378 | try: |
Steve McIntyre | 3f0aceb | 2014-12-17 16:27:13 +0000 | [diff] [blame] | 379 | vlan_id = call_vland('vlan_update', |
| 380 | {'command':'api.delete_vlan', |
| 381 | 'data': {'vlan_id': opts.delete_vlan}}) |
Steve McIntyre | ed5cbea | 2014-12-02 18:23:00 +0000 | [diff] [blame] | 382 | print 'Deleted vlan_id %d' % vlan_id |
| 383 | except InputError as inst: |
| 384 | print 'Failed: %s' % inst |
Steve McIntyre | 8e839a6 | 2014-12-05 15:46:05 +0000 | [diff] [blame] | 385 | elif opts.lookup_switch_by_name is not None: |
| 386 | try: |
Steve McIntyre | e3f3eb3 | 2014-12-17 13:23:30 +0000 | [diff] [blame] | 387 | switch_id = call_vland('db_query', |
| 388 | {'command':'db.get_switch_id_by_name', |
| 389 | 'data':{'name':opts.lookup_switch_by_name}}) |
Steve McIntyre | 8e839a6 | 2014-12-05 15:46:05 +0000 | [diff] [blame] | 390 | if switch_id is not None: |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 391 | print '%d' % switch_id |
Steve McIntyre | 8e839a6 | 2014-12-05 15:46:05 +0000 | [diff] [blame] | 392 | else: |
| 393 | print 'No switch found for name %s' % opts.lookup_switch_by_name |
| 394 | except InputError as inst: |
| 395 | print 'Failed: %s' % inst |
Steve McIntyre | a132c36 | 2014-12-05 15:53:21 +0000 | [diff] [blame] | 396 | elif opts.show_switch is not None: |
| 397 | try: |
Steve McIntyre | 6d84ec1 | 2014-12-18 16:56:56 +0000 | [diff] [blame] | 398 | this_switch = call_vland('db_query', |
| 399 | {'command':'db.get_switch_by_id', |
| 400 | 'data': |
| 401 | {'switch_id': opts.show_switch}}) |
| 402 | if this_switch is not None: |
| 403 | dump_switch(this_switch) |
Steve McIntyre | a132c36 | 2014-12-05 15:53:21 +0000 | [diff] [blame] | 404 | else: |
| 405 | print 'No switch found for switch_id %d' % opts.show_switch |
| 406 | except InputError as inst: |
| 407 | print 'Failed: %s' % inst |
Steve McIntyre | 11e4cbd | 2014-12-05 16:03:03 +0000 | [diff] [blame] | 408 | elif opts.list_switch_ports is not None: |
| 409 | try: |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 410 | ports = call_vland('db_query', |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 411 | {'command':'db.get_ports_by_switch', |
Steve McIntyre | bfed0fb | 2014-12-18 16:48:28 +0000 | [diff] [blame] | 412 | 'data': |
| 413 | {'switch_id': opts.list_switch_ports}}) |
Steve McIntyre | 11e4cbd | 2014-12-05 16:03:03 +0000 | [diff] [blame] | 414 | if ports is not None: |
Steve McIntyre | 6d84ec1 | 2014-12-18 16:56:56 +0000 | [diff] [blame] | 415 | for p in ports: |
Steve McIntyre | ea15fa7 | 2015-03-26 15:41:34 +0000 | [diff] [blame^] | 416 | print p |
Steve McIntyre | 11e4cbd | 2014-12-05 16:03:03 +0000 | [diff] [blame] | 417 | else: |
| 418 | print 'No ports found for switch_id %d' % opts.list_switch_ports |
| 419 | except InputError as inst: |
| 420 | print 'Failed: %s' % inst |
Steve McIntyre | 08dd839 | 2014-12-05 16:07:20 +0000 | [diff] [blame] | 421 | elif opts.show_port is not None: |
| 422 | try: |
Steve McIntyre | 6d84ec1 | 2014-12-18 16:56:56 +0000 | [diff] [blame] | 423 | this_port = call_vland('db_query', |
| 424 | {'command':'db.get_port_by_id', |
| 425 | 'data': |
| 426 | {'port_id': opts.show_port}}) |
| 427 | if this_port is not None: |
| 428 | dump_port(this_port) |
Steve McIntyre | 08dd839 | 2014-12-05 16:07:20 +0000 | [diff] [blame] | 429 | else: |
| 430 | print 'No port found for port_id %d' % opts.show_port |
| 431 | except InputError as inst: |
| 432 | print 'Failed: %s' % inst |
Steve McIntyre | 7310657 | 2014-12-05 16:13:36 +0000 | [diff] [blame] | 433 | elif opts.lookup_port_by_switch_and_name is not None: |
| 434 | try: |
Steve McIntyre | 6d84ec1 | 2014-12-18 16:56:56 +0000 | [diff] [blame] | 435 | p = call_vland('db_query', |
| 436 | {'command':'db.get_port_by_switch_and_name', |
| 437 | 'data': |
| 438 | {'switch_id': opts.lookup_port_by_switch_and_name[0], |
| 439 | 'name': opts.lookup_port_by_switch_and_name[1]}}) |
| 440 | if p is not None: |
| 441 | print p |
Steve McIntyre | 7310657 | 2014-12-05 16:13:36 +0000 | [diff] [blame] | 442 | else: |
| 443 | 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]) |
| 444 | except InputError as inst: |
| 445 | print 'Failed: %s' % inst |
Steve McIntyre | 71b4102 | 2014-12-05 17:17:44 +0000 | [diff] [blame] | 446 | elif opts.set_port_mode is not None: |
| 447 | try: |
Steve McIntyre | 3f0aceb | 2014-12-17 16:27:13 +0000 | [diff] [blame] | 448 | port_id = call_vland('vlan_update', |
| 449 | {'command':'api.set_port_mode', |
| 450 | 'data': |
| 451 | {'port_id': opts.set_port_mode[0], |
| 452 | 'mode': opts.set_port_mode[1]}}) |
Steve McIntyre | 71b4102 | 2014-12-05 17:17:44 +0000 | [diff] [blame] | 453 | print "Updated mode for port_id %d" % port_id |
| 454 | except InputError as inst: |
| 455 | print 'Failed: %s' % inst |
Steve McIntyre | 75dc4ec | 2014-12-05 17:20:42 +0000 | [diff] [blame] | 456 | elif opts.lock_port is not None: |
| 457 | try: |
Steve McIntyre | 7103de2 | 2014-12-17 13:16:48 +0000 | [diff] [blame] | 458 | port_id = call_vland('db_update', |
| 459 | {'command':'db.set_port_is_locked', |
| 460 | 'data': |
| 461 | {'port_id': opts.lock_port, |
| 462 | 'is_locked': True}}) |
Steve McIntyre | 75dc4ec | 2014-12-05 17:20:42 +0000 | [diff] [blame] | 463 | print "Locked port_id %d" % port_id |
| 464 | except InputError as inst: |
| 465 | print 'Failed: %s' % inst |
| 466 | elif opts.unlock_port is not None: |
| 467 | try: |
Steve McIntyre | 7103de2 | 2014-12-17 13:16:48 +0000 | [diff] [blame] | 468 | port_id = call_vland('db_update', |
| 469 | {'command':'db.set_port_is_locked', |
| 470 | 'data': |
| 471 | {'port_id': opts.unlock_port, |
| 472 | 'is_locked': False}}) |
Steve McIntyre | 75dc4ec | 2014-12-05 17:20:42 +0000 | [diff] [blame] | 473 | print "Unlocked port_id %d" % port_id |
| 474 | except InputError as inst: |
| 475 | print 'Failed: %s' % inst |
Steve McIntyre | a2bbcda | 2014-12-05 17:57:36 +0000 | [diff] [blame] | 476 | elif opts.set_port_current_vlan is not None: |
| 477 | try: |
Steve McIntyre | 3f0aceb | 2014-12-17 16:27:13 +0000 | [diff] [blame] | 478 | port_id = call_vland('vlan_update', |
| 479 | {'command':'api.set_current_vlan', |
| 480 | 'data': |
Steve McIntyre | e706209 | 2015-02-13 03:02:14 +0000 | [diff] [blame] | 481 | {'port_id': opts.set_port_current_vlan[0], |
| 482 | 'vlan_id': opts.set_port_current_vlan[1]}}) |
Steve McIntyre | a2bbcda | 2014-12-05 17:57:36 +0000 | [diff] [blame] | 483 | print "Set current VLAN on port_id %d" % port_id |
| 484 | except InputError as inst: |
| 485 | print 'Failed: %s' % inst |
| 486 | elif opts.get_port_current_vlan is not None: |
| 487 | try: |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 488 | vlan_id = call_vland('db_query', |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 489 | {'command':'db.get_current_vlan_id_by_port', |
Steve McIntyre | e3f3eb3 | 2014-12-17 13:23:30 +0000 | [diff] [blame] | 490 | 'data': |
| 491 | {'port_id': opts.get_port_current_vlan}}) |
Steve McIntyre | a2bbcda | 2014-12-05 17:57:36 +0000 | [diff] [blame] | 492 | if vlan_id is not None: |
| 493 | print vlan_id |
| 494 | else: |
| 495 | print "No current_vlan_id found for port_id %d" % opts.get_port_current_vlan |
| 496 | except InputError as inst: |
| 497 | print 'Failed: %s' % inst |
| 498 | elif opts.set_port_base_vlan is not None: |
| 499 | try: |
Steve McIntyre | 7103de2 | 2014-12-17 13:16:48 +0000 | [diff] [blame] | 500 | port_id = call_vland('db_update', |
| 501 | {'command':'db.set_base_vlan', |
| 502 | 'data': |
| 503 | {'port_id': opts.set_base_vlan[0], |
| 504 | 'base_vlan_id': opts.set_base_vlan[1]}}) |
Steve McIntyre | a2bbcda | 2014-12-05 17:57:36 +0000 | [diff] [blame] | 505 | print "Set base VLAN on port_id %d" % port_id |
| 506 | except InputError as inst: |
| 507 | print 'Failed: %s' % inst |
| 508 | elif opts.get_port_base_vlan is not None: |
| 509 | try: |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 510 | vlan_id = call_vland('db_query', |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 511 | {'command':'db.get_base_vlan_id_by_port', |
Steve McIntyre | a02ba20 | 2014-12-17 16:27:23 +0000 | [diff] [blame] | 512 | 'data': |
| 513 | {'port_id': opts.get_port_base_vlan}}) |
Steve McIntyre | a2bbcda | 2014-12-05 17:57:36 +0000 | [diff] [blame] | 514 | if vlan_id is not None: |
| 515 | print vlan_id |
| 516 | else: |
| 517 | print "No base_vlan_id found for port_id %d" % port_id |
| 518 | except InputError as inst: |
| 519 | print 'Failed: %s' % inst |
| 520 | elif opts.restore_port_to_base_vlan is not None: |
| 521 | try: |
Steve McIntyre | 3f0aceb | 2014-12-17 16:27:13 +0000 | [diff] [blame] | 522 | port_id = call_vland('vlan_update', |
| 523 | {'command': 'api.restore_base_vlan', |
| 524 | 'data': |
| 525 | {'port_id': opts.restore_port_to_base_vlan}}) |
Steve McIntyre | a2bbcda | 2014-12-05 17:57:36 +0000 | [diff] [blame] | 526 | print "Restored port_id %d back to base VLAN" % port_id |
| 527 | except InputError as inst: |
| 528 | print 'Failed: %s' % inst |
Steve McIntyre | e41e3f3 | 2014-12-05 18:08:21 +0000 | [diff] [blame] | 529 | elif opts.show_vlan is not None: |
| 530 | try: |
Steve McIntyre | 6d84ec1 | 2014-12-18 16:56:56 +0000 | [diff] [blame] | 531 | v = call_vland('db_query', |
| 532 | {'command':'db.get_vlan_by_id', |
| 533 | 'data': |
| 534 | {'vlan_id': opts.show_vlan}}) |
| 535 | if v is not None: |
| 536 | dump_vlan(v) |
Steve McIntyre | e41e3f3 | 2014-12-05 18:08:21 +0000 | [diff] [blame] | 537 | else: |
| 538 | print 'No vlan found for vlan_id %d' % opts.show_vlan |
| 539 | except InputError as inst: |
| 540 | print 'Failed: %s' % inst |
Steve McIntyre | 65533d7 | 2015-01-23 18:01:17 +0000 | [diff] [blame] | 541 | elif opts.lookup_vlan_by_tag is not None: |
| 542 | try: |
| 543 | vlan_id = call_vland('db_query', |
| 544 | {'command':'db.get_vlan_id_by_tag', |
| 545 | 'data': |
| 546 | {'tag': opts.lookup_vlan_by_tag}}) |
| 547 | if vlan_id is not None: |
| 548 | print vlan_id |
| 549 | else: |
| 550 | print 'No vlan found for vlan tag %d' % opts.lookup_vlan_by_tag |
| 551 | except InputError as inst: |
| 552 | print 'Failed: %s' % inst |
| 553 | elif opts.show_vlan_tag is not None: |
| 554 | try: |
| 555 | vlan_tag = call_vland('db_query', |
| 556 | {'command':'db.get_vlan_tag_by_id', |
| 557 | 'data': |
| 558 | {'vlan_id': opts.show_vlan_tag}}) |
| 559 | if vlan_tag is not None: |
| 560 | print vlan_tag |
| 561 | else: |
| 562 | print 'No vlan found for vlan id %d' % opts.show_vlan_tag |
| 563 | except InputError as inst: |
| 564 | print 'Failed: %s' % inst |
Steve McIntyre | 9cd6c3d | 2014-12-05 18:10:35 +0000 | [diff] [blame] | 565 | elif opts.status: |
Steve McIntyre | bb718cf | 2014-12-15 16:57:25 +0000 | [diff] [blame] | 566 | print 'Config:' |
| 567 | print ' knows about %d switch(es)' % len(config.switches) |
Steve McIntyre | 7103de2 | 2014-12-17 13:16:48 +0000 | [diff] [blame] | 568 | default_vlan_id = call_vland('db_query', |
| 569 | {'command':'db.get_vlan_id_by_tag', |
Steve McIntyre | e3f3eb3 | 2014-12-17 13:23:30 +0000 | [diff] [blame] | 570 | 'data': |
| 571 | {'tag': config.vland.default_vlan_tag}}) |
Steve McIntyre | 0abacac | 2014-12-15 16:51:38 +0000 | [diff] [blame] | 572 | 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] | 573 | stat = call_vland('daemon_query', {'command':'daemon.status', 'data': None}) |
| 574 | print 'VLANd is running %s' % stat['running'] |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 575 | print 'DB via VLANd:' |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 576 | switches = call_vland('db_query', {'command':'db.all_switches', 'data':None}) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 577 | print ' knows about %d switch(es)' % len(switches) |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 578 | ports = call_vland('db_query', {'command':'db.all_ports', 'data':None}) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 579 | print ' knows about %d port(s)' % len(ports) |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 580 | vlans = call_vland('db_query', {'command':'db.all_vlans', 'data':None}) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 581 | print ' DB knows about %d vlan(s)' % len(vlans) |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 582 | elif opts.vland_version: |
Steve McIntyre | 6d84ec1 | 2014-12-18 16:56:56 +0000 | [diff] [blame] | 583 | ver = call_vland('daemon_query', {'command':'daemon.version', 'data': None}) |
| 584 | print 'VLANd version %s' % ver['version'] |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 585 | elif opts.statistics: |
Steve McIntyre | 6d84ec1 | 2014-12-18 16:56:56 +0000 | [diff] [blame] | 586 | stats = call_vland('daemon_query', {'command':'daemon.statistics', 'data': None}) |
| 587 | print 'VLANd uptime: %d seconds' % stats['uptime'] |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 588 | elif opts.version: |
| 589 | print 'VLANd admin interface version %s' % version |
Steve McIntyre | 6c56260 | 2014-12-22 16:10:54 +0000 | [diff] [blame] | 590 | elif opts.auto_import_switch: |
| 591 | print 'Attempting to import switch %s' % opts.auto_import_switch |
| 592 | if opts.auto_import_switch not in config.switches: |
| 593 | raise InputError("Can't find switch %s in config" % opts.auto_import_switch) |
| 594 | ret = call_vland('vlan_update', |
| 595 | {'command':'api.auto_import_switch', |
| 596 | 'data': |
| 597 | {'switch': opts.auto_import_switch}}) |
Steve McIntyre | 110a525 | 2014-12-24 02:13:13 +0000 | [diff] [blame] | 598 | print 'VLANd imported switch %s successfully: new switch_id %d, %d new ports, %d new VLANs' % (opts.auto_import_switch, ret['switch_id'], ret['num_ports_added'], ret['num_vlans_added']) |
Steve McIntyre | deff795 | 2014-12-23 13:45:59 +0000 | [diff] [blame] | 599 | elif opts.probe_switches: |
| 600 | print 'Asking VLANd to probe all the configured switches' |
| 601 | ret = call_vland('daemon_query',{'command':'daemon.probe_switches', 'data': None}) |
Steve McIntyre | deff795 | 2014-12-23 13:45:59 +0000 | [diff] [blame] | 602 | for field in ret: |
Steve McIntyre | d07c857 | 2014-12-23 16:52:23 +0000 | [diff] [blame] | 603 | print '%s: %s' % (field, ret[field]) |
Steve McIntyre | 06fe642 | 2015-01-23 17:55:43 +0000 | [diff] [blame] | 604 | elif opts.shutdown: |
| 605 | print 'Asking VLANd to shutdown' |
| 606 | ret = call_vland('daemon_query',{'command':'daemon.shutdown', 'data': None}) |
| 607 | for field in ret: |
| 608 | print '%s: %s' % (field, ret[field]) |
Steve McIntyre | a0534a5 | 2014-12-05 17:58:40 +0000 | [diff] [blame] | 609 | else: |
Steve McIntyre | 4a80891 | 2014-12-05 15:24:39 +0000 | [diff] [blame] | 610 | print 'No recognised command given. Try -h for help' |