Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 1 | import threading |
| 2 | import requests, json |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 3 | |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 4 | from core.models import * |
smbaker | d39abb6 | 2013-08-05 22:21:09 -0700 | [diff] [blame] | 5 | #from openstack.manager import OpenStackManager |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 6 | from planetstack.config import Config |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 7 | |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 8 | import os |
| 9 | import base64 |
Tony Mack | 5c0c455 | 2013-07-03 09:36:51 -0400 | [diff] [blame] | 10 | from fofum import Fofum |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 11 | |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 12 | # decorator that marks dispatachable event methods |
| 13 | def event(func): |
| 14 | setattr(func, 'event', func.__name__) |
| 15 | return func |
| 16 | |
| 17 | class EventHandler: |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 18 | # This code is currently not in use. |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 19 | def __init__(self): |
smbaker | d39abb6 | 2013-08-05 22:21:09 -0700 | [diff] [blame] | 20 | pass #self.manager = OpenStackManager() |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 21 | |
Tony Mack | e9ba12d | 2013-06-24 09:37:49 -0400 | [diff] [blame] | 22 | @staticmethod |
| 23 | def get_events(): |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 24 | events = [] |
Tony Mack | e9ba12d | 2013-06-24 09:37:49 -0400 | [diff] [blame] | 25 | for name in dir(EventHandler): |
| 26 | attribute = getattr(EventHandler, name) |
| 27 | if hasattr(attribute, 'event'): |
| 28 | events.append(getattr(attribute, 'event')) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 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 | |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 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() |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 101 | |
| 102 | class EventListener: |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 103 | def __init__(self,wake_up=None): |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 104 | self.handler = EventHandler() |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 105 | self.wake_up = wake_up |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 106 | |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 107 | def handle_event(self, payload): |
| 108 | payload_dict = json.loads(payload) |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 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) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 116 | |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 117 | if (self.wake_up): |
| 118 | self.wake_up() |
| 119 | |
| 120 | |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 121 | def run(self): |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 122 | # This is our unique client id, to be used when firing and receiving events |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 123 | # It needs to be generated once and placed in the config file |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 124 | |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 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) |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 133 | |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 134 | listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event)) |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 135 | listener_thread.start() |