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