]> git.llucax.com Git - software/pymin.git/blob - services/qos/handler.py
Split proxy handler in submodules (refs #2).
[software/pymin.git] / services / qos / handler.py
1 # vim: set encoding=utf-8 et sw=4 sts=4 :
2
3 from os import path
4
5 from pymin.service.util import Restorable, ConfigWriter, \
6                                TransactionalHandler, ExecutionError, \
7                                call, get_network_devices
8
9 from cls import ClassHandler
10 from dev import DeviceHandler, Device
11 from host import HostHandler
12
13 __all__ = ('QoSHandler',)
14
15
16 class QoSHandler(Restorable, ConfigWriter, TransactionalHandler):
17
18     handler_help = u"Manage QoS devices, classes and hosts"
19
20     _persistent_attrs = ('devices')
21
22     _restorable_defaults = dict(
23                             devices=dict((dev, Device(dev, mac))
24                                 for (dev, mac) in get_network_devices().items())
25                             )
26
27     _config_writer_files = ('device', 'class_add', 'class_del', 'host_add')
28
29     _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
30
31     def __init__(self, pickle_dir='.', config_dir='.'):
32         r"Initialize QoSHandler object, see class documentation for details."
33         self._persistent_dir = pickle_dir
34         self._config_writer_cfg_dir = config_dir
35         self._config_build_templates()
36         self._restore()
37         self._write_config()
38         self.dev = DeviceHandler(self)
39         self.cls = ClassHandler(self)
40         self.host = HostHandler(self)
41
42     def _write_config(self):
43         r"_write_config() -> None :: Execute all commands."
44         for device in self.devices.values():
45             try:
46                 call(self._render_config('device', dict(dev=device.name, action='del')), shell=True)
47             except ExecutionError:
48                 pass
49
50             try:
51                 call(self._render_config('device', dict(dev=device.name, action='add')), shell=True)
52             except ExecutionError:
53                 pass
54
55             for cls in device.classes.values():
56                 try:
57                     call(self._render_config('class_add', dict(
58                         dev = device.name,
59                         cid = cls.cid,
60                         rate = cls.rate
61                         )
62                     ), shell=True)
63                 except ExecutionError:
64                     pass
65
66                 for host in cls.hosts.values():
67                     try:
68                         call(self._render_config('host_add', dict(
69                             dev = device.name,
70                             ip = host.ip,
71                             cid = cls.cid
72                             )
73                         ), shell=True)
74                     except ExecutionError:
75                         pass
76
77     def handle_timer(self):
78         self.refresh_devices()
79
80     def refresh_devices(self):
81         devices = get_network_devices()
82         #add not registered devices
83         for k, v in devices.items():
84             if k not in self.devices:
85                 self.devices[k] = Device(k, v)
86         #delete dead devices
87         for k in self.devices.keys():
88             if k not in devices:
89                 del self.devices[k]
90
91
92 if __name__ == '__main__':
93
94     qos = QoSHandler()
95     print '----------------------'
96     qos.commit()
97