Fixed a typo
[plstackapi.git] / planetstack / openstack / event_manager.py
1 import threading
2 import requests, json
3
4 from core.models import *
5 #from openstack.manager import OpenStackManager
6 from planetstack.config import Config
7
8 import os
9 import base64
10 from fofum import Fofum
11
12 # decorator that marks dispatachable event methods  
13 def event(func):
14     setattr(func, 'event', func.__name__)
15     return func      
16
17 class EventHandler:
18     # This code is currently not in use.
19     def __init__(self):
20         pass #self.manager = OpenStackManager()
21
22     @staticmethod
23     def get_events():
24         events = []
25         for name in dir(EventHandler):
26             attribute = getattr(EventHandler, name)
27             if hasattr(attribute, 'event'):
28                 events.append(getattr(attribute, 'event'))
29         return events
30
31     def dispatch(self, event, *args, **kwds):
32         if hasattr(self, event):
33             return getattr(self, event)(*args, **kwds)
34             
35         
36     @event
37     def save_site(self, id):
38         sites = Site.objects.filter(id=id)
39         if sites:
40             self.manager.save_site(sites[0])
41     
42     @event
43     def delete_site(self, tenant_id):
44         self.manager.driver.delete_tenant(tenant_id)
45
46     @event
47     def save_site_privilege(self, id):
48         site_privileges = SitePrivilege.objects.filter(id=id)
49         if site_privileges:
50             site_priv = self.manager.save_site_privilege(site_privileges[0])
51
52     @event
53     def delete_site_privilege(self, kuser_id, tenant_id, role_type):
54         self.manager.driver.delete_user_role(kuser_id, tenant_id, role_type)
55
56     @event
57     def save_slice(self, id):
58         slices = Slice.objects.filter(id=id)
59         if slices:
60             self.manager.save_slice(slices[0])
61     
62     @event
63     def delete_slice(self, tenant_id, network_id, router_id, subnet_id):
64         self.manager._delete_slice(tenant_id, network_id, router_id, subnet_id)
65
66     @event
67     def save_user(self, id):
68         users = User.objects.filter(id=id)
69         if users:
70             self.manager.save_user(users[0])
71         
72     @event
73     def delete_user(self, kuser_id):
74         self.manager.driver.delete_user(kuser_id)
75     
76     @event
77     def save_sliver(self, id):
78         slivers = Sliver.objects.filter(id=id)
79         if slivers:
80             self.manager.save_sliver(slivers[0])
81
82     @event
83     def delete_sliver(self, instance_id):
84         self.manager.destroy_instance(instance_id)                            
85
86     
87 class EventSender:
88     def __init__(self,user=None,clientid=None):
89         try:
90             clid = Config().feefie_client_id
91             user = Config().feefie_client_user
92         except:
93             clid = 'planetstack_core_team'
94             user = 'pl'
95
96         self.fofum = Fofum(user=user)
97         self.fofum.make(clid)
98
99     def fire(self):
100         self.fofum.fire()
101
102 class EventListener:
103     def __init__(self,wake_up=None):
104         self.handler = EventHandler()
105         self.wake_up = wake_up
106
107     def handle_event(self, payload):
108         payload_dict = json.loads(payload)
109
110         # The code below will come back when we optimize the observer syncs
111         # into 'small' and 'big' syncs.
112
113         #event = payload_dict['event']
114         #ctx = payload_dict['ctx']
115         #self.handler.dispatch(event,**ctx)   
116
117         if (self.wake_up):
118             self.wake_up()
119         
120
121     def run(self):
122         # This is our unique client id, to be used when firing and receiving events
123         # It needs to be generated once and placed in the config file
124
125         try:
126             clid = Config().feefie_client_id
127             user = Config().feefie_client_user
128         except:
129             clid = 'planetstack_core_team'
130             user = 'pl'
131
132         f = Fofum(user=user)
133         
134         listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event))
135         listener_thread.start()