blob: 3c19de395b15980174780752436cb56d813fc228 [file] [log] [blame]
Steve McIntyref1c04f92014-12-16 18:23:15 +00001import logging
2import os
Steve McIntyre53c7ad92014-12-16 19:21:13 +00003import time
Steve McIntyref1c04f92014-12-16 18:23:15 +00004from db.db import VlanDB
5from errors import CriticalError, InputError, ConfigError, SocketError
6
7class 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 McIntyre091e2ac2014-12-16 19:20:07 +000018 # Simple helper wrapper for all the read-only database queries
Steve McIntyre2150bc22014-12-17 13:13:56 +000019 def perform_db_query(self, state, command, data):
Steve McIntyre091e2ac2014-12-16 19:20:07 +000020 print 'perform_db_query'
Steve McIntyref1c04f92014-12-16 18:23:15 +000021 print command
22 print data
23 ret = {}
Steve McIntyre2150bc22014-12-17 13:13:56 +000024 db = state.db
Steve McIntyref1c04f92014-12-16 18:23:15 +000025 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +000026 if command == 'db.all_switches':
Steve McIntyref1c04f92014-12-16 18:23:15 +000027 ret = db.all_switches()
28 elif command == 'db.all_ports':
29 ret = db.all_ports()
30 elif command == 'db.all_vlans':
31 ret = db.all_vlans()
32 elif command == 'db.get_switch_by_id':
33 ret = db.get_switch_by_id(data['switch_id'])
34 elif command == 'db.get_switch_id_by_name':
35 ret = db.get_switch_id_by_name(data['name'])
36 elif command == 'db.get_switch_name_by_id':
37 ret = db.get_switch_name_by_id(data['switch_id'])
38 elif command == 'db.get_port_by_id':
39 ret = db.get_port_by_id(data['port_id'])
40 elif command == 'db.get_ports_by_switch':
41 ret = db.get_ports_by_switch(data['switch_id'])
42 elif command == 'db.get_port_by_switch_and_name':
43 ret = db.get_port_by_switch_and_name(data['switch_id'], data['name'])
44 elif command == 'db.get_current_vlan_id_by_port':
45 ret = db.get_current_vlan_id_by_port(data['port_id'])
46 elif command == 'db.get_base_vlan_id_by_port':
47 ret = db.get_base_vlan_id_by_port(data['port_id'])
48 elif command == 'db.get_ports_by_current_vlan':
49 ret = db.get_ports_by_current_vlan(data['vlan_id'])
50 elif command == 'db.get_ports_by_base_vlan':
51 ret = db.get_ports_by_base_vlan(data['vlan_id'])
52 elif command == 'db.get_vlan_by_id':
53 ret = db.get_vlan_by_id(data['vlan_id'])
54 elif command == 'db.get_vlan_id_by_name':
55 ret = db.get_vlan_id_by_name(data['name'])
56 elif command == 'db.get_vlan_id_by_tag':
Steve McIntyre07946c22014-12-17 13:14:15 +000057 ret = db.get_vlan_id_by_tag(data['tag'])
Steve McIntyref1c04f92014-12-16 18:23:15 +000058 elif command == 'db.get_vlan_name_by_id':
59 ret = db.get_vlan_name_by_id(data['vlan_id'])
60 else:
61 raise InputError("Unknown query command \"%s\"" % command)
62
63 except:
64 raise InputError("Invalid input in query")
65
66 return ret
67
Steve McIntyre53c7ad92014-12-16 19:21:13 +000068 # Simple helper wrapper for all the read-only daemon state queries
69 def perform_daemon_query(self, state, command, data):
70 print 'perform_daemon_query'
71 print command
72 print data
73 ret = {}
74 try:
75 if command == 'daemon.status':
76 # data ignored
77 ret['running'] = 'ok'
78 elif command == 'daemon.version':
79 # data ignored
80 ret['version'] = state.version
81 elif command == 'daemon.statistics':
82 ret['uptime'] = time.time() - state.starttime
83 else:
84 raise InputError("Unknown query command \"%s\"" % command)
85
86 except:
87 raise InputError("Invalid input in query")
88
89 return ret
90
91