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