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