Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 1 | import logging |
| 2 | import os |
Steve McIntyre | 53c7ad9 | 2014-12-16 19:21:13 +0000 | [diff] [blame] | 3 | import time |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 4 | from db.db import VlanDB |
| 5 | from errors import CriticalError, InputError, ConfigError, SocketError |
| 6 | |
| 7 | class VlanUtil: |
| 8 | """VLANd utility functions""" |
| 9 | |
| 10 | def get_switch_driver(self, switch, config): |
| 11 | logging.debug("Trying to find a driver for %s" % switch) |
| 12 | driver = config.switches[switch].driver |
| 13 | logging.debug("Driver: %s" % driver) |
| 14 | module = __import__("drivers.%s" % driver, fromlist=[driver]) |
| 15 | class_ = getattr(module, driver) |
| 16 | return class_(switch) |
| 17 | |
Steve McIntyre | c68a18e | 2014-12-17 16:29:28 +0000 | [diff] [blame] | 18 | def get_all_switches(self, config): |
| 19 | for switch in sorted(config.switches): |
| 20 | print "Found switch %s:" % (switch) |
| 21 | print " Probing:" |
| 22 | |
| 23 | s = util.get_switch_driver(switch, config) |
| 24 | s.switch_connect(config.switches[switch].username, config.switches[switch].password) |
| 25 | print " Found details of switch:" |
| 26 | s._dump_list(s._systemdata) |
| 27 | print " Switch has %d ports:" % len(s.switch_get_port_names()) |
| 28 | for port in s.switch_get_port_names(): |
| 29 | print " %s" % port |
| 30 | if 0 == 1: |
| 31 | mode = s.port_get_mode(port) |
| 32 | if mode == "trunk": |
| 33 | print " port %s is in trunk mode, VLAN(s):" % port |
| 34 | vlans = s.port_get_trunk_vlan_list(port) |
| 35 | for vlan in vlans: |
| 36 | name = s.vlan_get_name(vlan) |
| 37 | print " %d (%s)" % (vlan, name) |
| 38 | else: |
| 39 | vlan = s.port_get_access_vlan(port) |
| 40 | name = s.vlan_get_name(vlan) |
| 41 | print " port %s is in access mode, VLAN %d (%s):" % (port, vlan, name) |
| 42 | |
| 43 | s.switch_disconnect() |
| 44 | del(s) |
| 45 | |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 46 | # Simple helper wrapper for all the read-only database queries |
Steve McIntyre | 2150bc2 | 2014-12-17 13:13:56 +0000 | [diff] [blame] | 47 | def perform_db_query(self, state, command, data): |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 48 | print 'perform_db_query' |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 49 | print command |
| 50 | print data |
| 51 | ret = {} |
Steve McIntyre | 2150bc2 | 2014-12-17 13:13:56 +0000 | [diff] [blame] | 52 | db = state.db |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 53 | try: |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 54 | if command == 'db.all_switches': |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 55 | ret = db.all_switches() |
| 56 | elif command == 'db.all_ports': |
| 57 | ret = db.all_ports() |
| 58 | elif command == 'db.all_vlans': |
| 59 | ret = db.all_vlans() |
| 60 | elif command == 'db.get_switch_by_id': |
| 61 | ret = db.get_switch_by_id(data['switch_id']) |
| 62 | elif command == 'db.get_switch_id_by_name': |
| 63 | ret = db.get_switch_id_by_name(data['name']) |
| 64 | elif command == 'db.get_switch_name_by_id': |
| 65 | ret = db.get_switch_name_by_id(data['switch_id']) |
| 66 | elif command == 'db.get_port_by_id': |
| 67 | ret = db.get_port_by_id(data['port_id']) |
| 68 | elif command == 'db.get_ports_by_switch': |
| 69 | ret = db.get_ports_by_switch(data['switch_id']) |
| 70 | elif command == 'db.get_port_by_switch_and_name': |
| 71 | ret = db.get_port_by_switch_and_name(data['switch_id'], data['name']) |
| 72 | elif command == 'db.get_current_vlan_id_by_port': |
| 73 | ret = db.get_current_vlan_id_by_port(data['port_id']) |
| 74 | elif command == 'db.get_base_vlan_id_by_port': |
| 75 | ret = db.get_base_vlan_id_by_port(data['port_id']) |
| 76 | elif command == 'db.get_ports_by_current_vlan': |
| 77 | ret = db.get_ports_by_current_vlan(data['vlan_id']) |
| 78 | elif command == 'db.get_ports_by_base_vlan': |
| 79 | ret = db.get_ports_by_base_vlan(data['vlan_id']) |
| 80 | elif command == 'db.get_vlan_by_id': |
| 81 | ret = db.get_vlan_by_id(data['vlan_id']) |
| 82 | elif command == 'db.get_vlan_id_by_name': |
| 83 | ret = db.get_vlan_id_by_name(data['name']) |
| 84 | elif command == 'db.get_vlan_id_by_tag': |
Steve McIntyre | 07946c2 | 2014-12-17 13:14:15 +0000 | [diff] [blame] | 85 | ret = db.get_vlan_id_by_tag(data['tag']) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 86 | elif command == 'db.get_vlan_name_by_id': |
| 87 | ret = db.get_vlan_name_by_id(data['vlan_id']) |
| 88 | else: |
Steve McIntyre | e749fef | 2014-12-17 16:35:45 +0000 | [diff] [blame^] | 89 | raise InputError("Unknown db_query command \"%s\"" % command) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 90 | |
Steve McIntyre | 5da37fa | 2014-12-17 13:14:44 +0000 | [diff] [blame] | 91 | except InputError: |
| 92 | raise |
| 93 | |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 94 | except: |
| 95 | raise InputError("Invalid input in query") |
| 96 | |
| 97 | return ret |
| 98 | |
Steve McIntyre | 53c7ad9 | 2014-12-16 19:21:13 +0000 | [diff] [blame] | 99 | # Simple helper wrapper for all the read-only daemon state queries |
| 100 | def perform_daemon_query(self, state, command, data): |
| 101 | print 'perform_daemon_query' |
| 102 | print command |
| 103 | print data |
| 104 | ret = {} |
| 105 | try: |
| 106 | if command == 'daemon.status': |
| 107 | # data ignored |
| 108 | ret['running'] = 'ok' |
| 109 | elif command == 'daemon.version': |
| 110 | # data ignored |
| 111 | ret['version'] = state.version |
| 112 | elif command == 'daemon.statistics': |
| 113 | ret['uptime'] = time.time() - state.starttime |
| 114 | else: |
Steve McIntyre | e749fef | 2014-12-17 16:35:45 +0000 | [diff] [blame^] | 115 | raise InputError("Unknown daemon_query command \"%s\"" % command) |
Steve McIntyre | 53c7ad9 | 2014-12-16 19:21:13 +0000 | [diff] [blame] | 116 | |
Steve McIntyre | a590b5b | 2014-12-17 13:15:14 +0000 | [diff] [blame] | 117 | except InputError: |
| 118 | raise |
| 119 | |
| 120 | except: |
| 121 | raise InputError("Invalid input in query") |
| 122 | |
| 123 | return ret |
| 124 | |
Steve McIntyre | e749fef | 2014-12-17 16:35:45 +0000 | [diff] [blame^] | 125 | # Helper wrapper for API functions modifying database state only |
Steve McIntyre | a590b5b | 2014-12-17 13:15:14 +0000 | [diff] [blame] | 126 | def perform_db_update(self, state, command, data): |
| 127 | print 'perform_db_update' |
| 128 | print command |
| 129 | print data |
| 130 | ret = {} |
| 131 | db = state.db |
| 132 | try: |
| 133 | if command == 'db.create_switch': |
| 134 | ret = db.create_switch(data['name']) |
| 135 | elif command == 'db.create_port': |
| 136 | ret = db.create_port(data['switch_id'], data['name'], |
| 137 | state.config.default_vlan_id, |
| 138 | state.config.default_vlan_id) |
| 139 | elif command == 'db.delete_switch': |
| 140 | ret = db.delete_switch(data['switch_id']) |
| 141 | elif command == 'db.delete_port': |
| 142 | ret = db.delete_port(data['port_id']) |
| 143 | elif command == 'db.set_port_is_locked': |
| 144 | ret = db.set_port_is_locked(data['port_id'], data['is_locked']) |
| 145 | elif command == 'db.set_base_vlan': |
| 146 | ret = db.set_base_vlan(data['port_id'], data['base_vlan_id']) |
| 147 | else: |
| 148 | raise InputError("Unknown query command \"%s\"" % command) |
| 149 | |
| 150 | except InputError: |
| 151 | raise |
| 152 | |
Steve McIntyre | 53c7ad9 | 2014-12-16 19:21:13 +0000 | [diff] [blame] | 153 | except: |
| 154 | raise InputError("Invalid input in query") |
| 155 | |
| 156 | return ret |
| 157 | |
| 158 | |