blob: ab50f66ec9577993a9bd9305b30bde367aef3c7e [file] [log] [blame]
Tony Mack79a49c82013-06-15 23:51:57 -04001import threading
2import requests, json
Sapan Bhatia66f4e612013-07-02 12:12:38 -04003
Tony Mack79a49c82013-06-15 23:51:57 -04004from core.models import *
smbakerd39abb62013-08-05 22:21:09 -07005#from openstack.manager import OpenStackManager
Sapan Bhatia66f4e612013-07-02 12:12:38 -04006from planetstack.config import Config
Tony Mack79a49c82013-06-15 23:51:57 -04007
Sapan Bhatia9182b322013-06-25 16:22:14 -04008import os
9import base64
Tony Mack5c0c4552013-07-03 09:36:51 -040010from fofum import Fofum
Sapan Bhatia9182b322013-06-25 16:22:14 -040011
Tony Mack79a49c82013-06-15 23:51:57 -040012# decorator that marks dispatachable event methods
13def event(func):
14 setattr(func, 'event', func.__name__)
15 return func
16
17class EventHandler:
Sapan Bhatia66f4e612013-07-02 12:12:38 -040018 # This code is currently not in use.
Tony Mack79a49c82013-06-15 23:51:57 -040019 def __init__(self):
smbakerd39abb62013-08-05 22:21:09 -070020 pass #self.manager = OpenStackManager()
Tony Mack79a49c82013-06-15 23:51:57 -040021
Tony Macke9ba12d2013-06-24 09:37:49 -040022 @staticmethod
23 def get_events():
Tony Mack79a49c82013-06-15 23:51:57 -040024 events = []
Tony Macke9ba12d2013-06-24 09:37:49 -040025 for name in dir(EventHandler):
26 attribute = getattr(EventHandler, name)
27 if hasattr(attribute, 'event'):
28 events.append(getattr(attribute, 'event'))
Tony Mack79a49c82013-06-15 23:51:57 -040029 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
Sapan Bhatia66f4e612013-07-02 12:12:38 -040087class 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()
Tony Mack79a49c82013-06-15 23:51:57 -0400101
102class EventListener:
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400103 def __init__(self,wake_up=None):
Tony Mack79a49c82013-06-15 23:51:57 -0400104 self.handler = EventHandler()
Sapan Bhatia43c3a772013-07-03 11:19:07 -0400105 self.wake_up = wake_up
Tony Mack79a49c82013-06-15 23:51:57 -0400106
Sapan Bhatia9182b322013-06-25 16:22:14 -0400107 def handle_event(self, payload):
108 payload_dict = json.loads(payload)
Sapan Bhatia43c3a772013-07-03 11:19:07 -0400109
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)
Tony Mack79a49c82013-06-15 23:51:57 -0400116
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400117 if (self.wake_up):
118 self.wake_up()
119
120
Tony Mack79a49c82013-06-15 23:51:57 -0400121 def run(self):
Sapan Bhatia9182b322013-06-25 16:22:14 -0400122 # This is our unique client id, to be used when firing and receiving events
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400123 # It needs to be generated once and placed in the config file
Sapan Bhatia9182b322013-06-25 16:22:14 -0400124
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400125 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)
Sapan Bhatia9182b322013-06-25 16:22:14 -0400133
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400134 listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event))
Sapan Bhatia9182b322013-06-25 16:22:14 -0400135 listener_thread.start()