summaryrefslogtreecommitdiff
path: root/qpid/extras/dispatch/python/qpid/dispatch/router/router_engine.py
blob: 18b48379c50d52e29625a9644e3f50718be27ec7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

from time import time
from uuid import uuid4

from configuration import Configuration
from data import *
from neighbor import NeighborEngine
from link import LinkStateEngine
from path import PathEngine
from mobile import MobileAddressEngine
from routing import RoutingTableEngine
from binding import BindingEngine
from adapter import AdapterEngine
from node import NodeTracker

##
## Import the Dispatch adapters from the environment.  If they are not found
## (i.e. we are in a test bench, etc.), load the stub versions.
##
try:
  from dispatch import *
except ImportError:
  from ..stubs import *


class RouterEngine:
  """
  """

  def __init__(self, router_adapter, router_id=None, area='area', config_override={}):
    """
    Initialize an instance of a router for a domain.
    """
    ##
    ## Record important information about this router instance
    ##
    self.domain         = "domain"
    self.router_adapter = router_adapter
    self.log_adapter    = LogAdapter("dispatch.router")
    self.io_adapter     = IoAdapter(self, "qdxrouter")

    if router_id:
      self.id = router_id
    else:
      self.id = str(uuid4())
    self.area = area
    self.log(LOG_INFO, "Router Engine Instantiated: area=%s id=%s" % (self.area, self.id))

    ##
    ## Setup configuration
    ##
    self.config = Configuration(config_override)
    self.log(LOG_INFO, "Config: %r" % self.config)

    ##
    ## Launch the sub-module engines
    ##
    self.neighbor_engine       = NeighborEngine(self)
    self.link_state_engine     = LinkStateEngine(self)
    self.path_engine           = PathEngine(self)
    self.mobile_address_engine = MobileAddressEngine(self)
    self.routing_table_engine  = RoutingTableEngine(self)
    self.binding_engine        = BindingEngine(self)
    self.adapter_engine        = AdapterEngine(self)
    self.node_tracker          = NodeTracker(self)



  ##========================================================================================
  ## Adapter Entry Points - invoked from the adapter
  ##========================================================================================
  def getId(self):
    """
    Return the router's ID
    """
    return self.id


  def addLocalAddress(self, key):
    """
    """
    try:
      if key.find('_topo') == 0 or key.find('_local') == 0:
        return
      self.mobile_address_engine.add_local_address(key)
    except Exception, e:
      self.log(LOG_ERROR, "Exception in new-address processing: exception=%r" % e)

  def delLocalAddress(self, key):
    """
    """
    try:
      if key.find('_topo') == 0 or key.find('_local') == 0:
        return
      self.mobile_address_engine.del_local_address(key)
    except Exception, e:
      self.log(LOG_ERROR, "Exception in del-address processing: exception=%r" % e)


  def handleTimerTick(self):
    """
    """
    try:
      now = time()
      self.neighbor_engine.tick(now)
      self.link_state_engine.tick(now)
      self.path_engine.tick(now)
      self.mobile_address_engine.tick(now)
      self.routing_table_engine.tick(now)
      self.binding_engine.tick(now)
      self.adapter_engine.tick(now)
      self.node_tracker.tick(now)
    except Exception, e:
      self.log(LOG_ERROR, "Exception in timer processing: exception=%r" % e)


  def handleControlMessage(self, opcode, body):
    """
    """
    try:
      now = time()
      if   opcode == 'HELLO':
        msg = MessageHELLO(body)
        self.log(LOG_TRACE, "RCVD: %r" % msg)
        self.neighbor_engine.handle_hello(msg, now)

      elif opcode == 'RA':
        msg = MessageRA(body)
        self.log(LOG_TRACE, "RCVD: %r" % msg)
        self.link_state_engine.handle_ra(msg, now)
        self.mobile_address_engine.handle_ra(msg, now)

      elif opcode == 'LSU':
        msg = MessageLSU(body)
        self.log(LOG_TRACE, "RCVD: %r" % msg)
        self.link_state_engine.handle_lsu(msg, now)

      elif opcode == 'LSR':
        msg = MessageLSR(body)
        self.log(LOG_TRACE, "RCVD: %r" % msg)
        self.link_state_engine.handle_lsr(msg, now)

      elif opcode == 'MAU':
        msg = MessageMAU(body)
        self.log(LOG_TRACE, "RCVD: %r" % msg)
        self.mobile_address_engine.handle_mau(msg, now)

      elif opcode == 'MAR':
        msg = MessageMAR(body)
        self.log(LOG_TRACE, "RCVD: %r" % msg)
        self.mobile_address_engine.handle_mar(msg, now)

    except Exception, e:
      self.log(LOG_ERROR, "Exception in message processing: opcode=%s body=%r exception=%r" % (opcode, body, e))


  def receive(self, message_properties, body):
    """
    This is the IoAdapter message-receive handler
    """
    try:
      self.handleControlMessage(message_properties['opcode'], body)
    except Exception, e:
      self.log(LOG_ERROR, "Exception in raw message processing: properties=%r body=%r exception=%r" %
               (message_properties, body, e))

  def getRouterData(self, kind):
    """
    """
    if kind == 'help':
      return { 'help'           : "Get list of supported values for kind",
               'link-state'     : "This router's link state",
               'link-state-set' : "The set of link states from known routers",
               'next-hops'      : "Next hops to each known router",
               'topo-table'     : "Topological routing table",
               'mobile-table'   : "Mobile key routing table"
             }
    if kind == 'link-state'     : return self.neighbor_engine.link_state.to_dict()
    if kind == 'next-hops'      : return self.routing_table_engine.next_hops
    if kind == 'topo-table'     : return {'table': self.adapter_engine.key_classes['topological']}
    if kind == 'mobile-table'   : return {'table': self.adapter_engine.key_classes['mobile-key']}
    if kind == 'link-state-set' :
      copy = {}
      for _id,_ls in self.link_state_engine.collection.items():
        copy[_id] = _ls.to_dict()
      return copy

    return {'notice':'Use kind="help" to get a list of possibilities'}


  ##========================================================================================
  ## Adapter Calls - outbound calls to Dispatch
  ##========================================================================================
  def log(self, level, text):
    """
    Emit a log message to the host's event log
    """
    self.log_adapter.log(level, text)


  def send(self, dest, msg):
    """
    Send a control message to another router.
    """
    app_props = {'opcode' : msg.get_opcode() }
    self.io_adapter.send(dest, app_props, msg.to_dict())
    self.log(LOG_TRACE, "SENT: %r dest=%s" % (msg, dest))


  def node_updated(self, addr, reachable, neighbor):
    """
    """
    self.router_adapter(addr, reachable, neighbor)


  ##========================================================================================
  ## Interconnect between the Sub-Modules
  ##========================================================================================
  def local_link_state_changed(self, link_state):
    self.log(LOG_DEBUG, "Event: local_link_state_changed: %r" % link_state)
    self.link_state_engine.new_local_link_state(link_state)

  def ls_collection_changed(self, collection):
    self.log(LOG_DEBUG, "Event: ls_collection_changed: %r" % collection)
    self.path_engine.ls_collection_changed(collection)

  def next_hops_changed(self, next_hop_table):
    self.log(LOG_DEBUG, "Event: next_hops_changed: %r" % next_hop_table)
    self.routing_table_engine.next_hops_changed(next_hop_table)
    self.binding_engine.next_hops_changed()

  def mobile_sequence_changed(self, mobile_seq):
    self.log(LOG_DEBUG, "Event: mobile_sequence_changed: %d" % mobile_seq)
    self.link_state_engine.set_mobile_sequence(mobile_seq)

  def mobile_keys_changed(self, keys):
    self.log(LOG_DEBUG, "Event: mobile_keys_changed: %r" % keys)
    self.binding_engine.mobile_keys_changed(keys)

  def get_next_hops(self):
    return self.routing_table_engine.get_next_hops()

  def remote_routes_changed(self, key_class, routes):
    self.log(LOG_DEBUG, "Event: remote_routes_changed: class=%s routes=%r" % (key_class, routes))
    self.adapter_engine.remote_routes_changed(key_class, routes)

  def new_neighbor(self, rid):
    self.log(LOG_DEBUG, "Event: new_neighbor: id=%s" % rid)
    self.node_tracker.new_neighbor(rid)

  def lost_neighbor(self, rid):
    self.log(LOG_DEBUG, "Event: lost_neighbor: id=%s" % rid)
    self.node_tracker.lost_neighbor(rid)

  def new_node(self, rid):
    self.log(LOG_DEBUG, "Event: new_node: id=%s" % rid)
    self.node_tracker.new_node(rid)

  def lost_node(self, rid):
    self.log(LOG_DEBUG, "Event: lost_node: id=%s" % rid)
    self.node_tracker.lost_node(rid)