Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 1 | import threading |
| 2 | import requests, json |
| 3 | from core.models import * |
| 4 | from openstack.manager import OpenStackManager |
| 5 | |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 6 | import os |
| 7 | import base64 |
| 8 | import fofum |
| 9 | |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 10 | # decorator that marks dispatachable event methods |
| 11 | def event(func): |
| 12 | setattr(func, 'event', func.__name__) |
| 13 | return func |
| 14 | |
| 15 | class EventHandler: |
| 16 | |
| 17 | def __init__(self): |
| 18 | self.manager = OpenStackManager() |
| 19 | |
Tony Mack | e9ba12d | 2013-06-24 09:37:49 -0400 | [diff] [blame] | 20 | @staticmethod |
| 21 | def get_events(): |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 22 | events = [] |
Tony Mack | e9ba12d | 2013-06-24 09:37:49 -0400 | [diff] [blame] | 23 | for name in dir(EventHandler): |
| 24 | attribute = getattr(EventHandler, name) |
| 25 | if hasattr(attribute, 'event'): |
| 26 | events.append(getattr(attribute, 'event')) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 27 | return events |
| 28 | |
| 29 | def dispatch(self, event, *args, **kwds): |
| 30 | if hasattr(self, event): |
| 31 | return getattr(self, event)(*args, **kwds) |
| 32 | |
| 33 | |
| 34 | @event |
| 35 | def save_site(self, id): |
| 36 | sites = Site.objects.filter(id=id) |
| 37 | if sites: |
| 38 | self.manager.save_site(sites[0]) |
| 39 | |
| 40 | @event |
| 41 | def delete_site(self, tenant_id): |
| 42 | self.manager.driver.delete_tenant(tenant_id) |
| 43 | |
| 44 | @event |
| 45 | def save_site_privilege(self, id): |
| 46 | site_privileges = SitePrivilege.objects.filter(id=id) |
| 47 | if site_privileges: |
| 48 | site_priv = self.manager.save_site_privilege(site_privileges[0]) |
| 49 | |
| 50 | @event |
| 51 | def delete_site_privilege(self, kuser_id, tenant_id, role_type): |
| 52 | self.manager.driver.delete_user_role(kuser_id, tenant_id, role_type) |
| 53 | |
| 54 | @event |
| 55 | def save_slice(self, id): |
| 56 | slices = Slice.objects.filter(id=id) |
| 57 | if slices: |
| 58 | self.manager.save_slice(slices[0]) |
| 59 | |
| 60 | @event |
| 61 | def delete_slice(self, tenant_id, network_id, router_id, subnet_id): |
| 62 | self.manager._delete_slice(tenant_id, network_id, router_id, subnet_id) |
| 63 | |
| 64 | @event |
| 65 | def save_user(self, id): |
| 66 | users = User.objects.filter(id=id) |
| 67 | if users: |
| 68 | self.manager.save_user(users[0]) |
| 69 | |
| 70 | @event |
| 71 | def delete_user(self, kuser_id): |
| 72 | self.manager.driver.delete_user(kuser_id) |
| 73 | |
| 74 | @event |
| 75 | def save_sliver(self, id): |
| 76 | slivers = Sliver.objects.filter(id=id) |
| 77 | if slivers: |
| 78 | self.manager.save_sliver(slivers[0]) |
| 79 | |
| 80 | @event |
| 81 | def delete_sliver(self, instance_id): |
| 82 | self.manager.destroy_instance(instance_id) |
| 83 | |
| 84 | |
| 85 | |
| 86 | class EventListener: |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 87 | def __init__(self): |
| 88 | self.handler = EventHandler() |
| 89 | |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 90 | def handle_event(self, payload): |
| 91 | payload_dict = json.loads(payload) |
| 92 | event = payload_dict['event'] |
| 93 | ctx = payload_dict['ctx'] |
| 94 | self.handler.dispatch(event,**ctx) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 95 | |
| 96 | def run(self): |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 97 | # This is our unique client id, to be used when firing and receiving events |
| 98 | clid = base64.urlsafe_b64encode(os.urandom(12)) |
| 99 | |
| 100 | f = Fofum() |
| 101 | |
| 102 | listener_thread = threading.Thread(target=f.listen_on_event,args=(clid,self.handle_event)) |
| 103 | listener_thread.start() |