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