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