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