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