Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
Steve McIntyre | d2313b2 | 2016-03-12 11:50:10 +0000 | [diff] [blame] | 3 | # Copyright 2014-2016 Linaro Limited |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 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 | |
Steve McIntyre | 5694f6d | 2014-12-18 16:43:30 +0000 | [diff] [blame] | 23 | import os, sys |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 24 | import argparse |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 25 | import datetime, time |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 26 | |
| 27 | vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0]))) |
| 28 | sys.path.insert(0, vlandpath) |
| 29 | |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 30 | from errors import InputError, SocketError, NotFoundError, Error |
Steve McIntyre | 21f0270 | 2014-12-16 18:19:47 +0000 | [diff] [blame] | 31 | from config.config import VlanConfig |
| 32 | from ipc.ipc import VlanIpc |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 33 | |
Steve McIntyre | d2313b2 | 2016-03-12 11:50:10 +0000 | [diff] [blame] | 34 | prog = "admin" |
Steve McIntyre | c03d68d | 2016-03-24 17:38:34 +0000 | [diff] [blame] | 35 | version = "0.6" |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 36 | banner = "Linaro VLANd admin interface, version %s" % version |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 37 | exitcode = Error.OK |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 38 | TRUNK_ID_NONE = -1 |
| 39 | |
Steve McIntyre | ae95fd6 | 2014-12-05 16:51:41 +0000 | [diff] [blame] | 40 | def is_positive(text): |
Steve McIntyre | c6895fc | 2014-12-19 15:01:08 +0000 | [diff] [blame] | 41 | if text in ('1', 'y', 'Y', 't', 'T', 'True', 'true'): |
Steve McIntyre | ae95fd6 | 2014-12-05 16:51:41 +0000 | [diff] [blame] | 42 | return True |
Steve McIntyre | c6895fc | 2014-12-19 15:01:08 +0000 | [diff] [blame] | 43 | elif text in ('0', 'n', 'N', 'f', 'F', 'False', 'false'): |
Steve McIntyre | ae95fd6 | 2014-12-05 16:51:41 +0000 | [diff] [blame] | 44 | return False |
| 45 | else: |
| 46 | raise InputError("Cannot parse \"%s\" as True or False" % text) |
| 47 | |
Steve McIntyre | a132c36 | 2014-12-05 15:53:21 +0000 | [diff] [blame] | 48 | def dump_switch(switch): |
Steve McIntyre | 018d30c | 2015-02-09 06:34:57 +0000 | [diff] [blame] | 49 | print "switch_id:%d name:%s" % ( |
| 50 | int(switch[0]), |
| 51 | switch[1]) |
Steve McIntyre | a132c36 | 2014-12-05 15:53:21 +0000 | [diff] [blame] | 52 | |
Steve McIntyre | 11e4cbd | 2014-12-05 16:03:03 +0000 | [diff] [blame] | 53 | def dump_port(port): |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 54 | print "port_id:%d name:%s switch_id:%d locked:%s mode:%s base_vlan_id:%d current_vlan_id:%d number:%d trunk_id:%s" % ( |
Steve McIntyre | 018d30c | 2015-02-09 06:34:57 +0000 | [diff] [blame] | 55 | int(port[0]), |
| 56 | port[1], |
| 57 | int(port[2]), |
| 58 | ("yes" if port[3] is True else "no"), |
| 59 | ("trunk" if port[4] is True else "access"), |
| 60 | int(port[5]), |
Steve McIntyre | 9951b1c | 2015-08-05 13:55:38 +0100 | [diff] [blame] | 61 | int(port[6]), |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 62 | int(port[7]), |
| 63 | 'None' if (TRUNK_ID_NONE == port[8]) else port[8]) |
Steve McIntyre | 11e4cbd | 2014-12-05 16:03:03 +0000 | [diff] [blame] | 64 | |
Steve McIntyre | e41e3f3 | 2014-12-05 18:08:21 +0000 | [diff] [blame] | 65 | def dump_vlan(vlan): |
Steve McIntyre | 018d30c | 2015-02-09 06:34:57 +0000 | [diff] [blame] | 66 | print "vlan_id:%d name:%s tag:%d is_base_vlan:%s, creation_time:%s" % ( |
| 67 | int(vlan[0]), |
| 68 | vlan[1], |
| 69 | int(vlan[2]), |
| 70 | ("yes" if vlan[3] is True else "no"), |
| 71 | vlan[4]) |
Steve McIntyre | e41e3f3 | 2014-12-05 18:08:21 +0000 | [diff] [blame] | 72 | |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 73 | def dump_trunk(trunk): |
| 74 | print "trunk_id:%d creation_time:%s" % ( |
| 75 | int(trunk[0]), |
| 76 | trunk[1]) |
| 77 | |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 78 | def call_vland(msgtype, msg): |
| 79 | ipc = VlanIpc() |
| 80 | ipc.client_connect('localhost', config.vland.port) |
| 81 | msg['client_name'] = 'admin.py' |
| 82 | msg['type'] = msgtype |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 83 | ipc.client_send(msg) |
| 84 | ret = ipc.client_recv_and_close() |
| 85 | if 'response' not in ret: |
| 86 | raise SocketError("Badly-formed response from VLANd server") |
| 87 | if ret['response'] == "ERROR": |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 88 | raise InputError("VLANd server said \"%s\"" % ret['error']) |
| 89 | if ret['response'] == "NOTFOUND": |
| 90 | raise NotFoundError("VLANd server said \"%s\"" % ret['error']) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 91 | return ret['data'] |
| 92 | |
Steve McIntyre | c12f631 | 2014-12-15 15:00:12 +0000 | [diff] [blame] | 93 | config = VlanConfig(filenames=('./vland.cfg',)) |
Steve McIntyre | c12f631 | 2014-12-15 15:00:12 +0000 | [diff] [blame] | 94 | |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 95 | parser = argparse.ArgumentParser() |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 96 | |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 97 | #################### |
Steve McIntyre | 9cd6c3d | 2014-12-05 18:10:35 +0000 | [diff] [blame] | 98 | # System commands |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 99 | #################### |
| 100 | sp = parser.add_subparsers(title='Sub-commands', |
| 101 | help="Commands", |
| 102 | dest='subparser_name') |
| 103 | p_status = sp.add_parser("status", |
| 104 | help="Describe current system status") |
| 105 | p_status.set_defaults(which = "status") |
| 106 | p_statistics = sp.add_parser("statistics", |
| 107 | help="Print some system statistics") |
| 108 | p_statistics.set_defaults(which = "statistics") |
| 109 | p_version = sp.add_parser("version", |
| 110 | help="Describe the version of this admin interface") |
| 111 | p_version.set_defaults(which = "version") |
| 112 | p_vland_version = sp.add_parser("vland_version", |
| 113 | help="Describe the version of the running VLANd") |
| 114 | p_vland_version.set_defaults(which = "vland_version") |
| 115 | p_probe_switches = sp.add_parser("probe_switches", |
| 116 | help="Probe all the configured switches") |
| 117 | p_probe_switches.set_defaults(which = "probe_switches") |
| 118 | p_auto_import = sp.add_parser("auto_import_switch", |
| 119 | help="Attempt to import a switch's configuration into the VLANd system") |
| 120 | p_auto_import.add_argument('--name', |
| 121 | required = True, |
| 122 | help="Import the switch named SWITCH_NAME in vland.cfg") |
| 123 | p_auto_import.set_defaults(which = "auto_import_switch") |
| 124 | p_shutdown = sp.add_parser("shutdown", |
| 125 | help="Shut down a running VLANd") |
| 126 | p_shutdown.set_defaults(which = "shutdown") |
Steve McIntyre | 9cd6c3d | 2014-12-05 18:10:35 +0000 | [diff] [blame] | 127 | |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 128 | #################### |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 129 | # Switch commands |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 130 | #################### |
| 131 | p_list_all_switches = sp.add_parser("list_all_switches", |
| 132 | help="List all the existing switches in the system") |
| 133 | p_list_all_switches.set_defaults(which = "list_all_switches") |
| 134 | p_create_switch = sp.add_parser("create_switch", |
| 135 | help = "Add a new switch to the system") |
| 136 | p_create_switch.set_defaults(which = "create_switch") |
| 137 | p_create_switch.add_argument('--name', |
| 138 | required = True, |
| 139 | help="The name of the new switch, as described in vland.cfg") |
| 140 | p_delete_switch = sp.add_parser("delete_switch", |
| 141 | help = "Remove an existing switch from the system") |
| 142 | p_delete_switch.set_defaults(which = "delete_switch") |
| 143 | p_delete_switch.add_argument('--switch_id', |
| 144 | required = True, |
| 145 | help = "The ID of the switch to remove") |
| 146 | p_show_switch = sp.add_parser("show_switch", |
| 147 | help = "Show the details of an existing switch in the system") |
| 148 | p_show_switch.set_defaults(which = "show_switch") |
| 149 | p_show_switch.add_argument('--switch_id', |
| 150 | required = True, |
| 151 | help = "The ID of the switch to show") |
| 152 | p_lookup_switch_by_name = sp.add_parser("lookup_switch_by_name", |
| 153 | help = "Lookup a switch ID by name") |
| 154 | p_lookup_switch_by_name.set_defaults(which = "lookup_switch_by_name") |
| 155 | p_lookup_switch_by_name.add_argument('--name', |
| 156 | required = True, |
| 157 | help = "The switch name to search for") |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 158 | |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 159 | #################### |
| 160 | # Ports commands |
| 161 | #################### |
| 162 | p_list_all_ports = sp.add_parser("list_all_ports", |
| 163 | help="List all the existing ports in the system") |
| 164 | p_list_all_ports.set_defaults(which = "list_all_ports") |
| 165 | p_create_port = sp.add_parser("create_port", |
| 166 | help = "Add a new port to the system") |
| 167 | p_create_port.set_defaults(which = "create_port") |
| 168 | p_create_port.add_argument('--switch_id', |
| 169 | required = True, |
| 170 | help="The ID of the switch containing the new port") |
| 171 | p_create_port.add_argument('--name', |
| 172 | required = True, |
| 173 | help="The name of the new port") |
| 174 | p_create_port.add_argument('--number', |
| 175 | required = True, |
| 176 | help="The human-friendly number for the new port") |
| 177 | p_delete_port = sp.add_parser("delete_port", |
| 178 | help = "Remove an existing port from the system") |
| 179 | p_delete_port.set_defaults(which = "delete_port") |
| 180 | p_delete_port.add_argument('--port_id', |
| 181 | required = True, |
| 182 | help = "The ID of the port to remove") |
| 183 | p_show_port = sp.add_parser("show_port", |
| 184 | help = "Show the details of an existing port in the system") |
| 185 | p_show_port.set_defaults(which = "show_port") |
| 186 | p_show_port.add_argument('--port_id', |
| 187 | required = True, |
| 188 | help = "The ID of the port to show") |
| 189 | p_lookup_port_by_switch_and_name = sp.add_parser("lookup_port_by_switch_and_name", |
| 190 | help = "Lookup a port ID by switch ID and port name") |
| 191 | p_lookup_port_by_switch_and_name.set_defaults(which = "lookup_port_by_switch_and_name") |
| 192 | p_lookup_port_by_switch_and_name.add_argument('--switch_id', |
| 193 | required = True, |
| 194 | help = "The switch ID to search for") |
| 195 | p_lookup_port_by_switch_and_name.add_argument('--name', |
| 196 | required = True, |
| 197 | help = "The port name to search for") |
| 198 | p_lookup_port_by_switch_and_number = sp.add_parser("lookup_port_by_switch_and_number", |
| 199 | help = "Lookup a port ID by switch ID and port number") |
| 200 | p_lookup_port_by_switch_and_number.set_defaults(which = "lookup_port_by_switch_and_number") |
| 201 | p_lookup_port_by_switch_and_number.add_argument('--switch_id', |
| 202 | required = True, |
| 203 | help = "The switch ID to search for") |
| 204 | p_lookup_port_by_switch_and_number.add_argument('--number', |
| 205 | required = True, |
| 206 | help = "The port number to search for") |
| 207 | p_lookup_ports_by_switch = sp.add_parser("lookup_ports_by_switch", |
| 208 | help = "Lookup port ID(s) by switch ID") |
| 209 | p_lookup_ports_by_switch.set_defaults(which = "lookup_ports_by_switch") |
| 210 | p_lookup_ports_by_switch.add_argument('--switch_id', |
| 211 | required = True, |
| 212 | help = "The switch ID to search for") |
| 213 | p_lookup_ports_by_current_vlan = sp.add_parser("lookup_ports_by_current_vlan", |
| 214 | help = "Lookup port ID(s) by current VLAN ID") |
| 215 | p_lookup_ports_by_current_vlan.set_defaults(which = "lookup_ports_by_current_vlan") |
| 216 | p_lookup_ports_by_current_vlan.add_argument('--vlan_id', |
| 217 | required = True, |
| 218 | help = "The VLAN ID to search for") |
| 219 | p_lookup_ports_by_base_vlan = sp.add_parser("lookup_ports_by_base_vlan", |
| 220 | help = "Lookup port ID(s) by base vlan ID") |
| 221 | p_lookup_ports_by_base_vlan.set_defaults(which = "lookup_ports_by_base_vlan") |
| 222 | p_lookup_ports_by_base_vlan.add_argument('--vlan_id', |
| 223 | required = True, |
| 224 | help = "The VLAN ID to search for") |
| 225 | p_lookup_ports_by_trunk = sp.add_parser("lookup_ports_by_trunk", |
| 226 | help = "Lookup port ID(s) by trunk ID") |
| 227 | p_lookup_ports_by_trunk.set_defaults(which = "lookup_ports_by_trunk") |
| 228 | p_lookup_ports_by_trunk.add_argument('--trunk_id', |
| 229 | required = True, |
| 230 | help = "The trunk ID to search for") |
| 231 | p_set_port_mode = sp.add_parser("set_port_mode", |
| 232 | help = "Set the mode of a port to 'trunk' or 'access'") |
| 233 | p_set_port_mode.set_defaults(which = "set_port_mode") |
| 234 | p_set_port_mode.add_argument('--port_id', |
| 235 | required = True, |
| 236 | help = "The ID of the port to modify") |
| 237 | p_set_port_mode.add_argument('--mode', |
| 238 | required = True, |
| 239 | help = "The mode to select ('trunk' or 'access')") |
| 240 | p_get_port_mode = sp.add_parser("get_port_mode", |
| 241 | help = "Get the mode of a port ('trunk' or 'access')") |
| 242 | p_get_port_mode.set_defaults(which = "get_port_mode") |
| 243 | p_get_port_mode.add_argument('--port_id', |
| 244 | required = True, |
| 245 | help = "The ID of the port to query") |
| 246 | p_lock_port = sp.add_parser("lock_port", |
| 247 | help = "Lock the settings on a port") |
| 248 | p_lock_port.set_defaults(which = "lock_port") |
| 249 | p_lock_port.add_argument('--port_id', |
| 250 | required = True, |
| 251 | help = "The ID of the port to lock") |
| 252 | p_unlock_port = sp.add_parser("unlock_port", |
| 253 | help = "Unlock the settings on a port") |
| 254 | p_unlock_port.set_defaults(which = "unlock_port") |
| 255 | p_unlock_port.add_argument('--port_id', |
| 256 | required = True, |
| 257 | help = "The ID of the port to unlock") |
| 258 | p_set_port_current_vlan = sp.add_parser("set_port_current_vlan", |
| 259 | help = "Set the current VLAN assignment for a port") |
| 260 | p_set_port_current_vlan.set_defaults(which = "set_port_current_vlan") |
| 261 | p_set_port_current_vlan.add_argument('--port_id', |
| 262 | required = True, |
| 263 | help = "The ID of the port to modify") |
| 264 | p_set_port_current_vlan.add_argument('--vlan_id', |
| 265 | required = True, |
| 266 | help = "The VLAN ID to be used") |
| 267 | p_get_port_current_vlan = sp.add_parser("get_port_current_vlan", |
| 268 | help = "Get the current VLAN assignment for a port") |
| 269 | p_get_port_current_vlan.set_defaults(which = "get_port_current_vlan") |
| 270 | p_get_port_current_vlan.add_argument('--port_id', |
| 271 | required = True, |
| 272 | help = "The ID of the port to query") |
| 273 | p_set_port_base_vlan = sp.add_parser("set_port_base_vlan", |
| 274 | help = "Set the base VLAN assignment for a port") |
| 275 | p_set_port_base_vlan.set_defaults(which = "set_port_base_vlan") |
| 276 | p_set_port_base_vlan.add_argument('--port_id', |
| 277 | required = True, |
| 278 | help = "The ID of the port to modify") |
| 279 | p_set_port_base_vlan.add_argument('--vlan_id', |
| 280 | required = True, |
| 281 | help = "The VLAN ID to be used") |
| 282 | p_get_port_base_vlan = sp.add_parser("get_port_base_vlan", |
| 283 | help = "Get the base VLAN assignment for a port") |
| 284 | p_get_port_base_vlan.set_defaults(which = "get_port_base_vlan") |
| 285 | p_get_port_base_vlan.add_argument('--port_id', |
| 286 | required = True, |
| 287 | help = "The ID of the port to query") |
| 288 | p_restore_port_to_base_vlan = sp.add_parser("restore_port_to_base_vlan", |
| 289 | help = "Reset a port back to its base VLAN") |
| 290 | p_restore_port_to_base_vlan.set_defaults(which = "restore_port_to_base_vlan") |
| 291 | p_restore_port_to_base_vlan.add_argument('--port_id', |
| 292 | required = True, |
| 293 | help = "The ID of the port to modify") |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 294 | |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 295 | #################### |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 296 | # VLAN commands |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 297 | #################### |
| 298 | p_list_all_vlans = sp.add_parser("list_all_vlans", |
| 299 | help="List all the existing VLANs in the system") |
| 300 | p_list_all_vlans.set_defaults(which = "list_all_vlans") |
| 301 | p_create_vlan = sp.add_parser("create_vlan", |
| 302 | help = "Add a new VLAN to the system") |
| 303 | p_create_vlan.set_defaults(which = "create_vlan") |
| 304 | p_create_vlan.add_argument('--name', |
| 305 | required = True, |
| 306 | help="The name of the new VLAN") |
| 307 | p_create_vlan.add_argument('--tag', |
| 308 | required = True, |
| 309 | help="The tag for the new VLAN (-1 to automatically select)") |
| 310 | p_create_vlan.add_argument('--is_base_vlan', |
| 311 | required = True, |
| 312 | help="Is the new VLAN a base VLAN (true/false)") |
| 313 | p_delete_vlan = sp.add_parser("delete_vlan", |
| 314 | help = "Remove an existing VLAN from the system") |
| 315 | p_delete_vlan.set_defaults(which = "delete_vlan") |
| 316 | p_delete_vlan.add_argument('--vlan_id', |
| 317 | required = True, |
| 318 | help = "The ID of the VLAN to remove") |
| 319 | p_show_vlan = sp.add_parser("show_vlan", |
| 320 | help = "Show the details of an existing VLAN in the system") |
| 321 | p_show_vlan.set_defaults(which = "show_vlan") |
| 322 | p_show_vlan.add_argument('--vlan_id', |
| 323 | required = True, |
| 324 | help = "The ID of the VLAN to show") |
| 325 | p_lookup_vlan_by_tag = sp.add_parser("lookup_vlan_by_tag", |
| 326 | help = "Find the VLAN ID of an existing VLAN in the system") |
| 327 | p_lookup_vlan_by_tag.set_defaults(which = "lookup_vlan_by_tag") |
| 328 | p_lookup_vlan_by_tag.add_argument('--tag', |
| 329 | required = True, |
| 330 | help = "The VLAN tag to search for") |
| 331 | p_show_vlan_tag = sp.add_parser("show_vlan_tag", |
| 332 | help = "Print the VLAN tag of an existing VLAN in the system") |
| 333 | p_show_vlan_tag.set_defaults(which = "show_vlan_tag") |
| 334 | p_show_vlan_tag.add_argument('--vlan_id', |
| 335 | required = True, |
| 336 | help = "The VLAN ID to search for") |
Steve McIntyre | 844bfd4 | 2014-11-27 16:58:31 +0000 | [diff] [blame] | 337 | |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 338 | #################### |
| 339 | # Trunk commands |
| 340 | #################### |
| 341 | p_list_all_trunks = sp.add_parser("list_all_trunks", |
| 342 | help="List all the existing trunks in the system") |
| 343 | p_list_all_trunks.set_defaults(which = "list_all_trunks") |
| 344 | p_create_trunk = sp.add_parser("create_trunk", |
| 345 | help = "Add a new trunk to the system, linking two ports") |
| 346 | p_create_trunk.set_defaults(which = "create_trunk") |
| 347 | p_create_trunk.add_argument('--port_id1', |
| 348 | required = True, |
| 349 | help="The ID of the first port to be used") |
| 350 | p_create_trunk.add_argument('--port_id2', |
| 351 | required = True, |
| 352 | help="The ID of the second port to be used") |
| 353 | p_delete_trunk = sp.add_parser("delete_trunk", |
| 354 | help = "Remove an existing trunk from the system") |
| 355 | p_delete_trunk.set_defaults(which = "delete_trunk") |
| 356 | p_delete_trunk.add_argument('--trunk_id', |
| 357 | required = True, |
| 358 | help = "The ID of the trunk to remove") |
| 359 | p_show_trunk = sp.add_parser("show_trunk", |
| 360 | help = "Show the details of an existing trunk in the system") |
| 361 | p_show_trunk.set_defaults(which = "show_trunk") |
| 362 | p_show_trunk.add_argument('--trunk_id', |
| 363 | required = True, |
| 364 | help = "The ID of the trunk to show") |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 365 | |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 366 | args = parser.parse_args() |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 367 | |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 368 | # Now work out what to do |
| 369 | if args.which == 'status': |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 370 | try: |
| 371 | print 'Config:' |
| 372 | print ' knows about %d switch(es)' % len(config.switches) |
| 373 | default_vlan_id = call_vland('db_query', |
| 374 | {'command':'db.get_vlan_id_by_tag', |
| 375 | 'data': |
| 376 | {'tag': config.vland.default_vlan_tag}}) |
| 377 | print 'The default vlan tag (%d) is vlan ID %d' % (config.vland.default_vlan_tag, default_vlan_id) |
| 378 | stat = call_vland('daemon_query', {'command':'daemon.status', 'data': None}) |
| 379 | print 'VLANd is running %s' % stat['running'] |
| 380 | lastmod = datetime.datetime.strptime(stat['last_modified'], '%Y-%m-%dT%H:%M:%S.%f') |
| 381 | print 'DB Last modified %s' % lastmod.strftime('%Y-%m-%d %H:%M:%S %Z') |
| 382 | print 'DB via VLANd:' |
| 383 | switches = call_vland('db_query', {'command':'db.all_switches', 'data':None}) |
| 384 | print ' knows about %d switch(es)' % len(switches) |
| 385 | ports = call_vland('db_query', {'command':'db.all_ports', 'data':None}) |
| 386 | print ' knows about %d port(s)' % len(ports) |
| 387 | vlans = call_vland('db_query', {'command':'db.all_vlans', 'data':None}) |
| 388 | print ' DB knows about %d vlan(s)' % len(vlans) |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 389 | except (InputError, NotFoundError): |
| 390 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 391 | elif args.which == 'shutdown': |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 392 | try: |
| 393 | print 'Asking VLANd to shutdown' |
| 394 | shutdown = call_vland('daemon_query', |
| 395 | {'command':'daemon.shutdown', |
| 396 | 'data': None}) |
| 397 | for field in shutdown: |
| 398 | print '%s: %s' % (field, shutdown[field]) |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 399 | except (InputError, NotFoundError): |
| 400 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 401 | elif args.which == 'vland_version': |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 402 | try: |
| 403 | ver = call_vland('daemon_query', {'command':'daemon.version', 'data': None}) |
| 404 | print 'VLANd version %s' % ver['version'] |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 405 | except (InputError, NotFoundError): |
| 406 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 407 | elif args.which == 'statistics': |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 408 | try: |
| 409 | stats = call_vland('daemon_query', {'command':'daemon.statistics', 'data': None}) |
| 410 | print 'VLANd uptime: %d seconds' % stats['uptime'] |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 411 | except (InputError, NotFoundError): |
| 412 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 413 | elif args.which == 'version': |
| 414 | print 'VLANd admin interface version %s' % version |
| 415 | elif args.which == 'auto_import_switch': |
| 416 | print 'Attempting to import switch %s' % args.name |
| 417 | if args.name not in config.switches: |
| 418 | raise InputError("Can't find switch %s in config" % args.name) |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 419 | try: |
| 420 | imp = call_vland('vlan_update', |
| 421 | {'command':'api.auto_import_switch', |
| 422 | 'data': |
| 423 | {'switch': args.name}}) |
| 424 | print 'VLANd imported switch %s successfully: new switch_id %d, %d new ports, %d new VLANs' % (args.name, imp['switch_id'], imp['num_ports_added'], imp['num_vlans_added']) |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 425 | except (InputError, NotFoundError): |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 426 | print 'Import failed - see log for details' |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 427 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 428 | elif args.which == 'probe_switches': |
| 429 | print 'Asking VLANd to probe all the configured switches' |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 430 | try: |
| 431 | probe = call_vland('daemon_query', |
| 432 | {'command':'daemon.probe_switches', |
| 433 | 'data': None}) |
| 434 | for field in probe: |
| 435 | print '%s: %s' % (field, probe[field]) |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 436 | except (InputError, NotFoundError): |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 437 | print 'Probe failed - see log for details' |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 438 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 439 | elif args.which == 'list_all_switches': |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 440 | try: |
| 441 | result = call_vland('db_query', {'command':'db.all_switches', 'data':None}) |
| 442 | for line in result: |
| 443 | dump_switch(line) |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 444 | except (InputError, NotFoundError): |
| 445 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 446 | elif args.which == 'list_all_ports': |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 447 | try: |
| 448 | result = call_vland('db_query', {'command':'db.all_ports', 'data':None}) |
| 449 | for line in result: |
| 450 | dump_port(line) |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 451 | except (InputError, NotFoundError): |
| 452 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 453 | elif args.which == 'list_all_vlans': |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 454 | try: |
| 455 | result = call_vland('db_query', {'command':'db.all_vlans', 'data':None}) |
| 456 | for line in result: |
| 457 | dump_vlan(line) |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 458 | except (InputError, NotFoundError): |
| 459 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 460 | elif args.which == 'list_all_trunks': |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 461 | try: |
| 462 | result = call_vland('db_query', {'command':'db.all_trunks', 'data':None}) |
| 463 | for line in result: |
| 464 | dump_trunk(line) |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 465 | except (InputError, NotFoundError): |
| 466 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 467 | elif args.which == 'create_switch': |
| 468 | try: |
| 469 | switch_id = call_vland('db_update', |
| 470 | {'command':'db.create_switch', |
| 471 | 'data': |
| 472 | {'name':args.name}}) |
| 473 | print 'Created switch_id %d' % switch_id |
| 474 | except InputError as inst: |
| 475 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 476 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 477 | elif args.which == 'create_port': |
| 478 | try: |
| 479 | port_id = call_vland('db_update', |
| 480 | {'command':'db.create_port', |
| 481 | 'data': |
| 482 | {'switch_id': args.switch_id, |
| 483 | 'name': args.name, |
| 484 | 'number': args.number}}) |
| 485 | print 'Created port_id %d' % port_id |
| 486 | except InputError as inst: |
| 487 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 488 | exitcode = Error.FAILED |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 489 | except NotFoundError as inst: |
| 490 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 491 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 492 | elif args.which == 'create_vlan': |
| 493 | try: |
| 494 | (vlan_id, vlan_tag) = call_vland('vlan_update', |
| 495 | {'command':'api.create_vlan', |
| 496 | 'data': |
| 497 | {'name': args.name, |
| 498 | 'tag': args.tag, |
| 499 | 'is_base_vlan': is_positive(args.is_base_vlan)}}) |
| 500 | print 'Created VLAN tag %d as vlan_id %d' % (vlan_tag, vlan_id) |
| 501 | except InputError as inst: |
| 502 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 503 | exitcode = Error.FAILED |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 504 | except NotFoundError as inst: |
| 505 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 506 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 507 | elif args.which == 'create_trunk': |
| 508 | try: |
| 509 | trunk_id = call_vland('db_update', |
| 510 | {'command':'db.create_trunk', |
| 511 | 'data': |
| 512 | {'port_id1': args.port_id1, |
| 513 | 'port_id2': args.port_id2}}) |
| 514 | print 'Created trunk_id %d' % trunk_id |
| 515 | except InputError as inst: |
| 516 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 517 | exitcode = Error.FAILED |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 518 | except NotFoundError as inst: |
| 519 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 520 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 521 | elif args.which == 'delete_switch': |
| 522 | try: |
| 523 | switch_id = call_vland('db_update', |
| 524 | {'command':'db.delete_switch', |
| 525 | 'data': {'switch_id': args.switch_id}}) |
| 526 | print 'Deleted switch_id %s' % switch_id |
| 527 | except InputError as inst: |
| 528 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 529 | exitcode = Error.FAILED |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 530 | except NotFoundError as inst: |
| 531 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 532 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 533 | elif args.which == 'delete_port': |
| 534 | try: |
| 535 | port_id = call_vland('db_update', |
| 536 | {'command':'db.delete_port', |
| 537 | 'data': {'port_id': args.port_id}}) |
| 538 | print 'Deleted port_id %s' % port_id |
| 539 | except InputError as inst: |
| 540 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 541 | exitcode = Error.FAILED |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 542 | except NotFoundError as inst: |
| 543 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 544 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 545 | elif args.which == 'delete_vlan': |
| 546 | try: |
| 547 | vlan_id = call_vland('vlan_update', |
| 548 | {'command':'api.delete_vlan', |
| 549 | 'data': {'vlan_id': args.vlan_id}}) |
| 550 | print 'Deleted vlan_id %d' % vlan_id |
| 551 | except InputError as inst: |
| 552 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 553 | exitcode = Error.FAILED |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 554 | except NotFoundError as inst: |
| 555 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 556 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 557 | elif args.which == 'delete_trunk': |
| 558 | try: |
Steve McIntyre | a5bf220 | 2018-01-31 14:15:21 +0000 | [diff] [blame] | 559 | trunk_id = call_vland('db_update', |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 560 | {'command':'db.delete_trunk', |
| 561 | 'data': {'trunk_id': args.trunk_id}}) |
| 562 | print 'Deleted trunk_id %s' % trunk_id |
| 563 | except InputError as inst: |
| 564 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 565 | exitcode = Error.FAILED |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 566 | except NotFoundError as inst: |
| 567 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 568 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 569 | elif args.which == 'lookup_switch_by_name': |
| 570 | try: |
| 571 | switch_id = call_vland('db_query', |
| 572 | {'command':'db.get_switch_id_by_name', |
| 573 | 'data':{'name':args.name}}) |
| 574 | if switch_id is not None: |
| 575 | print '%d' % switch_id |
| 576 | else: |
| 577 | print 'No switch found for name %s' % args.name |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 578 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 579 | except InputError as inst: |
| 580 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 581 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 582 | elif args.which == 'show_switch': |
| 583 | try: |
| 584 | this_switch = call_vland('db_query', |
| 585 | {'command':'db.get_switch_by_id', |
| 586 | 'data': |
| 587 | {'switch_id': args.switch_id}}) |
| 588 | if this_switch is not None: |
| 589 | dump_switch(this_switch) |
| 590 | else: |
| 591 | print 'No switch found for switch_id %s' % args.switch_id |
| 592 | except InputError as inst: |
| 593 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 594 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 595 | elif args.which == 'show_port': |
| 596 | try: |
| 597 | this_port = call_vland('db_query', |
| 598 | {'command':'db.get_port_by_id', |
| 599 | 'data': |
| 600 | {'port_id': args.port_id}}) |
| 601 | if this_port is not None: |
| 602 | dump_port(this_port) |
| 603 | else: |
| 604 | print 'No port found for port_id %s' % args.port_id |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 605 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 606 | except InputError as inst: |
| 607 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 608 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 609 | elif args.which == 'lookup_port_by_switch_and_name': |
| 610 | try: |
| 611 | p = call_vland('db_query', |
| 612 | {'command':'db.get_port_by_switch_and_name', |
| 613 | 'data': |
| 614 | {'switch_id': args.switch_id, |
| 615 | 'name': args.name}}) |
| 616 | if p is not None: |
| 617 | print p |
| 618 | else: |
| 619 | print 'No port found for switch_id %s, name %s' % (args.switch_id, args.name) |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 620 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 621 | except InputError as inst: |
| 622 | print 'Failed: %s' % inst |
| 623 | elif args.which == 'lookup_port_by_switch_and_number': |
| 624 | try: |
| 625 | p = call_vland('db_query', |
| 626 | {'command':'db.get_port_by_switch_and_number', |
| 627 | 'data': |
| 628 | {'switch_id': args.switch_id, |
| 629 | 'number': args.number}}) |
| 630 | if p is not None: |
| 631 | print p |
| 632 | else: |
| 633 | print 'No port found for switch_id %s, port number %s' % (args.switch_id, args.number) |
| 634 | except InputError as inst: |
| 635 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 636 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 637 | elif args.which == 'lookup_ports_by_switch': |
| 638 | try: |
| 639 | p = call_vland('db_query', |
| 640 | {'command':'db.get_ports_by_switch', |
| 641 | 'data': |
| 642 | {'switch_id': args.switch_id}}) |
| 643 | if p is not None: |
| 644 | for port_id in p: |
| 645 | print port_id |
| 646 | else: |
| 647 | print 'No ports found for switch_id %s' % args.switch_id |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 648 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 649 | except InputError as inst: |
| 650 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 651 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 652 | elif args.which == 'lookup_ports_by_current_vlan': |
| 653 | try: |
| 654 | p = call_vland('db_query', |
| 655 | {'command':'db.get_ports_by_current_vlan', |
| 656 | 'data': |
| 657 | {'vlan_id': args.vlan_id}}) |
| 658 | if p is not None: |
| 659 | for port_id in p: |
| 660 | print port_id |
| 661 | else: |
| 662 | print 'No ports found for current vlan_id %s' % args.vlan_id |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 663 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 664 | except InputError as inst: |
| 665 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 666 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 667 | elif args.which == 'lookup_ports_by_base_vlan': |
| 668 | try: |
| 669 | p = call_vland('db_query', |
| 670 | {'command':'db.get_ports_by_base_vlan', |
| 671 | 'data': |
| 672 | {'vlan_id': args.vlan_id}}) |
| 673 | if p is not None: |
| 674 | for port_id in p: |
| 675 | print port_id |
| 676 | else: |
| 677 | print 'No ports found for base vlan_id %s' % args.vlan_id |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 678 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 679 | except InputError as inst: |
| 680 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 681 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 682 | elif args.which == 'lookup_ports_by_trunk': |
| 683 | try: |
| 684 | p = call_vland('db_query', |
| 685 | {'command':'db.get_ports_by_trunk', |
| 686 | 'data': |
| 687 | {'trunk_id': args.trunk_id}}) |
| 688 | if p is not None: |
| 689 | for port_id in p: |
| 690 | print port_id |
| 691 | else: |
| 692 | print 'No ports found for trunk_id %s' % args.trunk_id |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 693 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 694 | except InputError as inst: |
| 695 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 696 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 697 | elif args.which == 'set_port_mode': |
| 698 | try: |
| 699 | port_id = call_vland('vlan_update', |
| 700 | {'command':'api.set_port_mode', |
| 701 | 'data': |
| 702 | {'port_id': args.port_id, |
| 703 | 'mode': args.mode}}) |
| 704 | print "Updated mode for port_id %d" % port_id |
| 705 | except InputError as inst: |
| 706 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 707 | exitcode = Error.FAILED |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 708 | except NotFoundError as inst: |
| 709 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 710 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 711 | elif args.which == 'lock_port': |
| 712 | try: |
| 713 | port_id = call_vland('db_update', |
| 714 | {'command':'db.set_port_is_locked', |
| 715 | 'data': |
| 716 | {'port_id': args.port_id, |
| 717 | 'is_locked': True}}) |
| 718 | print "Locked port_id %d" % port_id |
| 719 | except InputError as inst: |
| 720 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 721 | exitcode = Error.FAILED |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 722 | except NotFoundError as inst: |
| 723 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 724 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 725 | elif args.which == 'unlock_port': |
| 726 | try: |
| 727 | port_id = call_vland('db_update', |
| 728 | {'command':'db.set_port_is_locked', |
| 729 | 'data': |
| 730 | {'port_id': args.port_id, |
| 731 | 'is_locked': False}}) |
| 732 | print "Unlocked port_id %d" % port_id |
| 733 | except InputError as inst: |
| 734 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 735 | exitcode = Error.FAILED |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 736 | except NotFoundError as inst: |
| 737 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 738 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 739 | elif args.which == 'set_port_current_vlan': |
| 740 | try: |
| 741 | port_id = call_vland('vlan_update', |
| 742 | {'command':'api.set_current_vlan', |
| 743 | 'data': |
| 744 | {'port_id': args.port_id, |
| 745 | 'vlan_id': args.vlan_id}}) |
| 746 | print "Set current VLAN on port_id %d" % port_id |
| 747 | except InputError as inst: |
| 748 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 749 | exitcode = Error.FAILED |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 750 | except NotFoundError as inst: |
| 751 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 752 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 753 | elif args.which == 'get_port_current_vlan': |
| 754 | try: |
| 755 | vlan_id = call_vland('db_query', |
| 756 | {'command':'db.get_current_vlan_id_by_port', |
| 757 | 'data': |
| 758 | {'port_id': args.port_id}}) |
| 759 | if vlan_id is not None: |
| 760 | print vlan_id |
| 761 | else: |
| 762 | print "No current_vlan_id found for port_id %s" % args.port_id |
| 763 | except InputError as inst: |
| 764 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 765 | exitcode = Error.FAILED |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 766 | except NotFoundError as inst: |
| 767 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 768 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 769 | elif args.which == 'set_port_base_vlan': |
| 770 | try: |
| 771 | port_id = call_vland('db_update', |
| 772 | {'command':'db.set_base_vlan', |
| 773 | 'data': |
| 774 | {'port_id': args.port_id, |
| 775 | 'base_vlan_id': args.vlan_id}}) |
| 776 | print "Set base VLAN on port_id %d" % port_id |
| 777 | except InputError as inst: |
| 778 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 779 | exitcode = Error.FAILED |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 780 | except NotFoundError as inst: |
| 781 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 782 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 783 | elif args.which == 'get_port_base_vlan': |
| 784 | try: |
| 785 | vlan_id = call_vland('db_query', |
| 786 | {'command':'db.get_base_vlan_id_by_port', |
| 787 | 'data': |
| 788 | {'port_id': args.port_id}}) |
| 789 | if vlan_id is not None: |
| 790 | print vlan_id |
| 791 | else: |
| 792 | print "No base_vlan_id found for port_id %d" % port_id |
| 793 | except InputError as inst: |
| 794 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 795 | exitcode = Error.FAILED |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 796 | except NotFoundError as inst: |
| 797 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 798 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 799 | elif args.which == 'restore_port_to_base_vlan': |
| 800 | try: |
| 801 | port_id = call_vland('vlan_update', |
| 802 | {'command': 'api.restore_base_vlan', |
| 803 | 'data': |
| 804 | {'port_id': args.port_id}}) |
| 805 | print "Restored port_id %d back to base VLAN" % port_id |
| 806 | except InputError as inst: |
| 807 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 808 | exitcode = Error.FAILED |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 809 | except NotFoundError as inst: |
| 810 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 811 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 812 | elif args.which == 'show_vlan': |
| 813 | try: |
| 814 | v = call_vland('db_query', |
| 815 | {'command':'db.get_vlan_by_id', |
| 816 | 'data': |
| 817 | {'vlan_id': args.vlan_id}}) |
| 818 | if v is not None: |
| 819 | dump_vlan(v) |
| 820 | else: |
| 821 | print 'No VLAN found for vlan_id %s' % args.vlan_id |
| 822 | except InputError as inst: |
| 823 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 824 | exitcode = Error.FAILED |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 825 | except NotFoundError as inst: |
| 826 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 827 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 828 | elif args.which == 'lookup_vlan_by_tag': |
| 829 | try: |
| 830 | vlan_id = call_vland('db_query', |
| 831 | {'command':'db.get_vlan_id_by_tag', |
| 832 | 'data': |
| 833 | {'tag': args.tag}}) |
| 834 | if vlan_id is not None: |
| 835 | print vlan_id |
| 836 | else: |
| 837 | print 'No VLAN found for vlan tag %s' % args.tag |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 838 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 839 | except InputError as inst: |
| 840 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 841 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 842 | elif args.which == 'show_vlan_tag': |
| 843 | try: |
| 844 | vlan_tag = call_vland('db_query', |
| 845 | {'command':'db.get_vlan_tag_by_id', |
| 846 | 'data': |
| 847 | {'vlan_id': args.vlan_id}}) |
| 848 | if vlan_tag is not None: |
| 849 | print vlan_tag |
| 850 | else: |
| 851 | print 'No VLAN found for vlan id %s' % args.vlan_id |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 852 | exitcode = Error.NOTFOUND |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 853 | except InputError as inst: |
| 854 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 855 | exitcode = Error.FAILED |
Steve McIntyre | 460358a | 2016-03-12 11:57:53 +0000 | [diff] [blame] | 856 | elif args.which == 'show_trunk': |
| 857 | try: |
| 858 | this_trunk = call_vland('db_query', |
| 859 | {'command':'db.get_trunk_by_id', |
| 860 | 'data': |
| 861 | {'trunk_id': args.trunk_id}}) |
| 862 | if this_trunk is not None: |
| 863 | dump_trunk(this_trunk) |
| 864 | else: |
| 865 | print 'No port found for port_id %s' % args.trunk_id |
| 866 | except InputError as inst: |
| 867 | print 'Failed: %s' % inst |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 868 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 869 | exitcode = Error.FAILED |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 870 | except NotFoundError as inst: |
| 871 | print 'Failed: %s' % inst |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 872 | exitcode = Error.NOTFOUND |
Steve McIntyre | a0534a5 | 2014-12-05 17:58:40 +0000 | [diff] [blame] | 873 | else: |
Steve McIntyre | 4a80891 | 2014-12-05 15:24:39 +0000 | [diff] [blame] | 874 | print 'No recognised command given. Try -h for help' |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 875 | |
Steve McIntyre | f5ef696 | 2016-03-22 17:21:47 +0000 | [diff] [blame] | 876 | sys.exit(exitcode) |