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