Debian Codegraph @

networking_bagpipe/bagpipe_bgp/engine/worker.py

raw
1 # Copyright 2014 Orange
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import queue
16 import threading
17
18 from oslo_log import log as logging
19
20 from networking_bagpipe.bagpipe_bgp.common import looking_glass as lg
21 from networking_bagpipe.bagpipe_bgp import engine
22
23
24 LOG = logging.getLogger(__name__)
25
26 STOP_EVENT = "STOP_EVENT"
27
28
29 class Worker(engine.EventSource, lg.LookingGlassMixin):
30
31 """Base class for objects that interact with the route table manager
32
33 These objects will:
34 * use _subscribe(...) and _unsubscribe(...) to subscribe to routing events
35 * will specialize _on_event(event) to react to received events
36
37 They also inherit from EventSource to publish events
38 """
39
40 def __init__(self, bgp_manager, worker_name):
41 self.bgp_manager = bgp_manager
42 self.rtm = bgp_manager.rtm
43 self._queue = queue.Queue()
44 self._please_stop = threading.Event()
45
46 self.name = worker_name
47 assert self.name is not None
48
49 engine.EventSource.__init__(self, self.rtm)
50
51 # private data for RouteTableManager
52 self._rtm_matches = set()
53
54 self._was_stopped = False
55
56 LOG.debug("Instantiated %s worker", self.name)
57
58 def stop(self):
59 """Stop this worker.
60
61 Set the _please_stop internal event to stop the event processor loop
62 and indicate to the route table manager that this worker is stopped.
63 Then call _stopped() to let a subclass implement any further work.
64 """
65 if self._was_stopped:
66 LOG.debug("not running, nothing to do to stop")
67 return
68 LOG.info("Stop worker %s", self)
69 self.stop_event_loop()
70 self._cleanup()
71 self._stopped()
72 self._was_stopped = True
73
74 def stop_event_loop(self):
75 self._please_stop.set()
76 self.enqueue(STOP_EVENT)
77
78 def _cleanup(self):
79 self.rtm.enqueue(engine.WorkerCleanupEvent(self))
80
81 def _stopped(self):
82 """Hook for subclasses to react when Worker is stopped
83
84 (NoOp in base Worker class)
85 """
86
87 def _event_queue_processor_loop(self):
88 """Main loop where the worker consumes events."""
89
90 while not self._please_stop.isSet():
91 event = self._dequeue()
92
93 if event == STOP_EVENT:
94 LOG.debug("Stop event, breaking queue processor loop")
95 self._please_stop.set()
96 break
97
98 try:
99 self._on_event(event)
100 except Exception:
101 LOG.exception("Exception raised on subclass._on_event: %s",
102 event)
103
104 def run(self):
105 self._event_queue_processor_loop()
106
107 def _on_event(self, event):
108 """Method implemented by subclasses to react to routing events."""
109
110 LOG.debug("Worker %s _on_event: %s", self.name, event)
111 raise NotImplementedError
112
113 def _dequeue(self):
114 return self._queue.get()
115
116 def enqueue(self, event):
117 # TODO(tmmorin): replace Queue by a PriorityQueue and use a higher
118 # priority for ReInit event
119 self._queue.put(event)
120
121 def _subscribe(self, afi, safi, rt=None):
122 try:
123 subobj = engine.Subscription(afi, safi, rt, self)
124 LOG.info("Subscribe: %s ", subobj)
125 self.rtm.enqueue(subobj)
126 except engine.UnsupportedRT as e:
127 LOG.debug("unsupported RT, ignoring (%s)", e.rt)
128
129 def _unsubscribe(self, afi, safi, rt=None):
130 try:
131 subobj = engine.Unsubscription(afi, safi, rt, self)
132 LOG.info("Unsubscribe: %s ", subobj)
133 self.rtm.enqueue(subobj)
134 except engine.UnsupportedRT as e:
135 LOG.debug("unsupported RT, ignoring (%s)", e.rt)
136
137 def get_subscriptions(self):
138 return sorted(self._rtm_matches)
139
140 def __repr__(self):
141 return "%s" % (self.name)
142
143 # Looking glass ###
144
145 def get_lg_local_info(self, path_prefix):
146 return {
147 "name": self.name,
148 "internals": {
149 "event queue length": self._queue.qsize(),
150 "subscriptions":
151 [repr(sub) for sub in self.get_subscriptions()],
152 }
153 }