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 |
| 27 | |
| 28 | vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0]))) |
| 29 | sys.path.insert(0, vlandpath) |
| 30 | |
| 31 | import drivers |
| 32 | from db.db import VlanDB |
| 33 | |
| 34 | version = "0.0.0-DEV" |
| 35 | banner = "Linaro VLANd admin interface, version %s" % version |
| 36 | |
| 37 | #print '%s' % banner |
| 38 | |
| 39 | #print 'Connecting to DB...' |
| 40 | db = VlanDB() |
| 41 | |
| 42 | switches = db.all_switches() |
Steve McIntyre | b1db710 | 2014-11-28 17:56:12 +0000 | [diff] [blame^] | 43 | print 'The DB knows about %d switch(es)' % len(switches) |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 44 | if len(switches) > 0: |
| 45 | print switches |
| 46 | |
| 47 | ports = db.all_ports() |
Steve McIntyre | b1db710 | 2014-11-28 17:56:12 +0000 | [diff] [blame^] | 48 | print 'The DB knows about %d port(s)' % len(ports) |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 49 | if len(ports) > 0: |
| 50 | print ports |
| 51 | |
| 52 | vlans = db.all_vlans() |
Steve McIntyre | b1db710 | 2014-11-28 17:56:12 +0000 | [diff] [blame^] | 53 | print 'The DB knows about %d vlan(s)' % len(vlans) |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 54 | if len(vlans) > 0: |
| 55 | print vlans |
| 56 | |
| 57 | usage = 'Usage: %prog --command [command options]' |
| 58 | commands = ['switch_create', |
| 59 | 'switch_destroy', |
| 60 | 'switch_details'] |
| 61 | parser = optparse.OptionParser(usage=usage, description=banner) |
| 62 | |
| 63 | # Switch commands |
| 64 | switch_group = optparse.OptionGroup(parser, "Switch commands") |
| 65 | switch_group.add_option("--list_all_switches", |
| 66 | dest = "list_all_switches", |
| 67 | action = "store_true", |
| 68 | default = False, |
| 69 | help = "List all the existing switches in the system") |
| 70 | switch_group.add_option("--create_switch", |
| 71 | dest = "create_switch", |
| 72 | action = "store", |
| 73 | type = "string", |
| 74 | help = "Add a new switch to the system", |
| 75 | nargs = 1, |
| 76 | metavar = "<name>") |
| 77 | switch_group.add_option("--remove_switch", |
| 78 | dest = "remove_switch", |
| 79 | action = "store", |
| 80 | type = "int", |
| 81 | help = "Remove an existing switch from the system", |
| 82 | default = -1, |
| 83 | nargs = 1, |
| 84 | metavar = "<switch_id>") |
| 85 | switch_group.add_option("--show_switch", |
| 86 | dest = "show_switch", |
| 87 | action = "store", |
| 88 | type = "int", |
| 89 | help = "Show the details of an existing switch in the system", |
| 90 | default = -1, |
| 91 | nargs = 1, |
| 92 | metavar = "<switch_id>") |
| 93 | switch_group.add_option("--lookup_switch_by_name", |
| 94 | dest = "lookup_switch_by_name", |
| 95 | action = "store", |
| 96 | type = "string", |
| 97 | help = "Lookup a switch ID by name", |
| 98 | nargs = 1, |
| 99 | metavar = "<name>") |
| 100 | switch_group.add_option("--list_switch_ports", |
| 101 | dest = "list_switch_ports", |
| 102 | action = "store", |
| 103 | type = "int", |
| 104 | help = "List the ports of an existing switch in the system", |
| 105 | default = -1, |
| 106 | nargs = 1, |
| 107 | metavar = "<switch_id>") |
| 108 | parser.add_option_group(switch_group) |
| 109 | |
| 110 | # Port commands |
| 111 | port_group = optparse.OptionGroup(parser, "Port commands") |
| 112 | port_group.add_option("--list_all_ports", |
| 113 | dest = "list_all_ports", |
| 114 | action = "store_true", |
| 115 | default = False, |
| 116 | help = "List all the existing ports in the system") |
| 117 | port_group.add_option("--create_port", |
| 118 | dest = "create_port", |
| 119 | action = "store", |
| 120 | type = "string", |
| 121 | help = "Add a new port to the system", |
| 122 | nargs = 2, |
| 123 | metavar = "<switch_id> <name>") |
| 124 | port_group.add_option("--remove_port", |
| 125 | dest = "remove_port", |
| 126 | action = "store", |
| 127 | type = "int", |
| 128 | help = "Remove an existing port from the system", |
| 129 | default = -1, |
| 130 | nargs = 1, |
| 131 | metavar = "<port_id>") |
| 132 | port_group.add_option("--show_port", |
| 133 | dest = "show_port", |
| 134 | action = "store", |
| 135 | type = "int", |
| 136 | help = "Show the details of an existing port in the system", |
| 137 | default = -1, |
| 138 | nargs = 1, |
| 139 | metavar = "<port_id>") |
| 140 | port_group.add_option("--lookup_port_by_switch_and_name", |
| 141 | dest = "lookup_port_by_switch_and_name", |
| 142 | action = "store", |
| 143 | type = "string", |
| 144 | help = "Lookup a port ID by switch and port name", |
| 145 | nargs = 2, |
| 146 | metavar = "<switch_id> <name>") |
| 147 | port_group.add_option("--set_port_type", |
| 148 | dest = "set_port_type", |
| 149 | action = "store", |
| 150 | type = "string", |
| 151 | help = "Set the mode of a port to 'trunk' or 'access'", |
| 152 | nargs = 2, |
| 153 | metavar = "<port_id> <mode>") |
| 154 | port_group.add_option("--lock_port", |
| 155 | dest = "lock_port", |
| 156 | action = "store", |
| 157 | type = "string", |
| 158 | help = "Lock the settings on a port", |
| 159 | nargs = 1, |
| 160 | metavar = "<port_id>") |
| 161 | port_group.add_option("--unlock_port", |
| 162 | dest = "unlock_port", |
| 163 | action = "store", |
| 164 | type = "string", |
| 165 | help = "Unock the settings on a port", |
| 166 | nargs = 1, |
| 167 | metavar = "<port_id>") |
| 168 | port_group.add_option("--set_port_current_vlan", |
| 169 | dest = "set_port_current_vlan", |
| 170 | action = "store", |
| 171 | type = "int", |
| 172 | help = "Set the current VLAN assignment for a port", |
| 173 | nargs = 2, |
| 174 | metavar = "<port_id> <vlan_id>") |
| 175 | port_group.add_option("--get_port_current_vlan", |
| 176 | dest = "get_port_current_vlan", |
| 177 | action = "store", |
| 178 | type = "int", |
| 179 | help = "Get the current VLAN assignment for a port", |
| 180 | nargs = 1, |
| 181 | metavar = "<port_id>") |
| 182 | port_group.add_option("--set_port_base_vlan", |
| 183 | dest = "set_port_base_vlan", |
| 184 | action = "store", |
| 185 | type = "int", |
| 186 | help = "Set the base VLAN assignment for a port", |
| 187 | nargs = 2, |
| 188 | metavar = "<port_id> <vlan_id>") |
| 189 | port_group.add_option("--get_port_base_vlan", |
| 190 | dest = "get_port_base_vlan", |
| 191 | action = "store", |
| 192 | type = "int", |
| 193 | help = "Get the base VLAN assignment for a port", |
| 194 | nargs = 1, |
| 195 | metavar = "<port_id>") |
| 196 | parser.add_option_group(port_group) |
| 197 | |
| 198 | # VLAN commands |
| 199 | vlan_group = optparse.OptionGroup(parser, "VLAN commands") |
| 200 | vlan_group.add_option("--list_all_vlans", |
| 201 | dest = "list_all_vlans", |
| 202 | action = "store_true", |
| 203 | default = False, |
| 204 | help = "List all the existing vlans in the system") |
| 205 | vlan_group.add_option("--create_vlan", |
| 206 | dest = "create_vlan", |
| 207 | action = "store", |
| 208 | type = "string", |
| 209 | help = "Add a new vlan to the system", |
| 210 | nargs = 3, |
Steve McIntyre | 9f0bb60 | 2014-11-28 14:36:39 +0000 | [diff] [blame] | 211 | metavar = "<name> <tag> <is_base_vlan>") |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 212 | vlan_group.add_option("--remove_vlan", |
| 213 | dest = "remove_vlan", |
| 214 | action = "store", |
| 215 | type = "int", |
| 216 | help = "Remove an existing vlan from the system", |
| 217 | default = -1, |
| 218 | nargs = 1, |
| 219 | metavar = "<vlan_id>") |
| 220 | vlan_group.add_option("--show_vlan", |
| 221 | dest = "show_vlan", |
| 222 | action = "store", |
| 223 | type = "int", |
| 224 | help = "Show the details of an existing vlan in the system", |
| 225 | default = -1, |
| 226 | nargs = 1, |
| 227 | metavar = "<vlan_id>") |
| 228 | parser.add_option_group(vlan_group) |
| 229 | |
| 230 | (opts, args) = parser.parse_args() |
| 231 | |
| 232 | print opts |
| 233 | print args |
| 234 | |
| 235 | if opts.list_all_switches: |
| 236 | result = db.all_switches() |
| 237 | count = 0; |
| 238 | for line in result: |
| 239 | print line |
| 240 | count += 1 |
| 241 | print '%d entries' % count |
| 242 | elif opts.create_switch is not None: |
| 243 | switch_id = db.create_switch(opts.create_switch, 'bar', 'baz') |
| 244 | print 'Created switch_id %d' % switch_id |
| 245 | elif opts.create_port is not None: |
| 246 | port_id = db.create_port(opts.create_port[0], |
Steve McIntyre | c21d8d1 | 2014-11-28 14:42:40 +0000 | [diff] [blame] | 247 | opts.create_port[1], |
| 248 | False, False, 1, 1) |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 249 | print 'Created port_id %d' % port_id |
Steve McIntyre | c21d8d1 | 2014-11-28 14:42:40 +0000 | [diff] [blame] | 250 | elif opts.create_vlan is not None: |
| 251 | is_base = False |
| 252 | if opts.create_vlan[2] in ('1', 'y', 'Y'): |
| 253 | is_base = True |
| 254 | vlan_id = db.create_vlan(opts.create_vlan[0], |
| 255 | opts.create_vlan[1], |
| 256 | is_base) |
Steve McIntyre | be61b63 | 2014-11-28 15:14:25 +0000 | [diff] [blame] | 257 | print 'Created vlan_id %d' % vlan_id |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 258 | else: |
| 259 | print 'wuh?' |
| 260 | |