blob: c2dc725adfe5c09e241b79987ac37fad0a6903a0 [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 McIntyrec68a18e2014-12-17 16:29:28 +000018 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 McIntyre091e2ac2014-12-16 19:20:07 +000046 # Simple helper wrapper for all the read-only database queries
Steve McIntyre2150bc22014-12-17 13:13:56 +000047 def perform_db_query(self, state, command, data):
Steve McIntyre091e2ac2014-12-16 19:20:07 +000048 print 'perform_db_query'
Steve McIntyref1c04f92014-12-16 18:23:15 +000049 print command
50 print data
51 ret = {}
Steve McIntyre2150bc22014-12-17 13:13:56 +000052 db = state.db
Steve McIntyref1c04f92014-12-16 18:23:15 +000053 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +000054 if command == 'db.all_switches':
Steve McIntyref1c04f92014-12-16 18:23:15 +000055 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 McIntyre07946c22014-12-17 13:14:15 +000085 ret = db.get_vlan_id_by_tag(data['tag'])
Steve McIntyref1c04f92014-12-16 18:23:15 +000086 elif command == 'db.get_vlan_name_by_id':
87 ret = db.get_vlan_name_by_id(data['vlan_id'])
88 else:
Steve McIntyree749fef2014-12-17 16:35:45 +000089 raise InputError("Unknown db_query command \"%s\"" % command)
Steve McIntyref1c04f92014-12-16 18:23:15 +000090
Steve McIntyre5da37fa2014-12-17 13:14:44 +000091 except InputError:
92 raise
93
Steve McIntyref1c04f92014-12-16 18:23:15 +000094 except:
95 raise InputError("Invalid input in query")
96
97 return ret
98
Steve McIntyre53c7ad92014-12-16 19:21:13 +000099 # 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 McIntyree749fef2014-12-17 16:35:45 +0000115 raise InputError("Unknown daemon_query command \"%s\"" % command)
Steve McIntyre53c7ad92014-12-16 19:21:13 +0000116
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000117 except InputError:
118 raise
119
120 except:
121 raise InputError("Invalid input in query")
122
123 return ret
124
Steve McIntyree749fef2014-12-17 16:35:45 +0000125 # Helper wrapper for API functions modifying database state only
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000126 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 McIntyre53c7ad92014-12-16 19:21:13 +0000153 except:
154 raise InputError("Invalid input in query")
155
156 return ret
157
158