3a9559043053ff838813bee6319d1435d89c7bc2
[flowspy] / flowspec / tasks.py
1 from utils import proxy as PR
2 from celery.task import task
3 from celery.task.sets import subtask
4 import logging
5 import json
6
7 from celery.task.http import *
8 from flowspy.utils import beanstalkc
9 from django.conf import settings
10
11 FORMAT = '%(asctime)s %(levelname)s: %(message)s'
12 logging.basicConfig(format=FORMAT)
13 logger = logging.getLogger(__name__)
14 logger.setLevel(logging.DEBUG)
15
16 @task
17 def add(route, callback=None):
18     applier = PR.Applier(route_object=route)
19     commit, response = applier.apply()
20     if commit:
21         status = "ACTIVE"
22     else:
23         status = "ERROR"
24     route.status = status
25     route.response = response
26     subtask(announce).delay("[%s] Route add: %s - Result: %s" %(route.applier, route.name, response), route.applier)
27     route.save()
28
29 @task
30 def edit(route, callback=None):
31     applier = PR.Applier(route_object=route)
32     commit, response = applier.apply(operation="replace")
33     if commit:
34         status = "ACTIVE"
35     else:
36         status = "ERROR"
37     route.status = status
38     route.response = response
39     route.save()
40     subtask(announce).delay("[%s] Route edit: %s - Result: %s"%(route.applier, route.name, response), route.applier)
41
42
43
44 @task
45 def delete(route, **kwargs):
46     applier = PR.Applier(route_object=route)
47     commit, response = applier.apply(operation="delete")
48     reason_text = ''
49     if commit:
50         status = "INACTIVE"
51         if "reason" in kwargs and kwargs['reason']=='EXPIRED':
52             status = 'EXPIRED'
53             reason_text = " Reason: %s " %status
54     else:
55         status = "ERROR"
56     route.status = status
57     route.response = response
58     route.save()
59     subtask(announce).delay("[%s] Route removal: %s%s- Result %s" %(route.applier, route.name, reason_text, response), route.applier)
60
61 # May not work in the first place... proxy is not aware of Route models
62 @task
63 def batch_delete(routes, **kwargs):
64     if routes:
65         applier = PR.Applier(route_objects=routes)
66         conf = applier.delete_routes()
67         commit, response = applier.apply(configuration = conf)
68         reason_text = ''
69         if commit:
70             status = "INACTIVE"
71             if "reason" in kwargs and kwargs['reason']=='EXPIRED':
72                 status = 'EXPIRED'
73                 reason_text = " Reason: %s " %status
74             elif "reason" in kwargs and kwargs['reason']!='EXPIRED':
75                 status = kwargs['reason']
76                 reason_text = " Reason: %s " %status
77         else:
78             status = "ERROR"
79         for route in routes:
80             route.status = status
81             route.response = response
82             route.save()
83             subtask(announce).delay("[%s] Route removal: %s%s- Result %s" %(route.applier, route.name, reason_text, response), route.applier)
84     else:
85         return False
86
87 @task
88 def announce(messg, user):
89     messg = str(messg)
90 #    username = user.username
91     username = user.get_profile().peer.domain_name
92     b = beanstalkc.Connection()
93     b.use(settings.POLLS_TUBE)
94     tube_message = json.dumps({'message': messg, 'username':username})
95     b.put(tube_message)
96     b.close()
97
98 @task
99 def check_sync(route_name=None, selected_routes = []):
100     from flowspy.flowspec.models import Route, MatchPort, MatchDscp, ThenAction
101     if not selected_routes:
102         routes = Route.objects.all()
103     else:
104         routes = selected_routes
105     if route_name:
106         routes = routes.filter(name=route_name)
107     for route in routes:
108         if route.has_expired() and (route.status != 'EXPIRED' or route.status != 'ADMININACTIVE' or route.status != 'INACTIVE'):
109             logger.info('Expiring route %s' %route.name)
110             subtask(delete).delay(route, reason="EXPIRED")
111         elif route.has_expired() and (route.status == 'ADMININACTIVE' or route.status == 'INACTIVE'):
112             route.status = 'EXPIRED'
113             route.response = 'Route Expired'
114             route.save()
115         elif route.status != 'EXPIRED':
116             route.check_sync()
117
118
119 #def delete(route):
120 #    
121 #    applier = PR.Applier(route_object=route)
122 #    commit, response = applier.apply(configuration=applier.delete_routes())
123 #    if commit:
124 #            rows = queryset.update(is_online=False, is_active=False)
125 #            queryset.update(response="Successfully removed route from network")
126 #            self.message_user(request, "Successfully removed %s routes from network" % rows)
127 #        else:
128 #            self.message_user(request, "Could not remove routes from network")
129 #    if commit:
130 #        is_online = False
131 #        is_active = False
132 #        response = "Successfully removed route from network"
133 #    else:
134 #        is_online = False
135 #        is_active = True
136 #    route.is_online = is_online
137 #    route.is_active = is_active
138 #    route.response = response
139 #    route.save()