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