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