summaryrefslogtreecommitdiff
path: root/qpid/extras/dispatch/python/qpid/dispatch/router/path.py
blob: c051dbe7fca4415b4bb1845bf4270ece12c0b154 (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
#
# 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.
#

try:
  from dispatch import *
except ImportError:
  from ..stubs import *

class PathEngine(object):
  """
  This module is responsible for computing the next-hop for every router/area in the domain
  based on the collection of link states that have been gathered.
  """
  def __init__(self, container):
    self.container = container
    self.id = self.container.id
    self.area = self.container.area
    self.recalculate = False
    self.collection = None


  def tick(self, now_unused):
    if self.recalculate:
      self.recalculate = False
      self._calculate_routes()


  def ls_collection_changed(self, collection):
    self.recalculate = True
    self.collection = collection


  def _calculate_tree_from_root(self, root):
    ##
    ## Make a copy of the current collection of link-states that contains
    ## an empty link-state for nodes that are known-peers but are not in the
    ## collection currently.  This is needed to establish routes to those nodes
    ## so we can trade link-state information with them.
    ##
    link_states = {}
    for _id, ls in self.collection.items():
      link_states[_id] = ls.peers
      for p in ls.peers:
        if p not in link_states:
          link_states[p] = []

    ##
    ## Setup Dijkstra's Algorithm
    ##
    cost = {}
    prev = {}
    for _id in link_states:
      cost[_id] = None  # infinite
      prev[_id] = None  # undefined
    cost[root] = 0   # no cost to the root node
    unresolved = NodeSet(cost)

    ##
    ## Process unresolved nodes until lowest cost paths to all reachable nodes have been found.
    ##
    while not unresolved.empty():
      u = unresolved.lowest_cost()
      if cost[u] == None:
        # There are no more reachable nodes in unresolved
        break
      for v in link_states[u]:
        if unresolved.contains(v):
          alt = cost[u] + 1   # TODO - Use link cost instead of 1
          if cost[v] == None or alt < cost[v]:
            cost[v] = alt
            prev[v] = u
            unresolved.set_cost(v, alt)

    ##
    ## Remove unreachable nodes from the map.  Note that this will also remove the
    ## root node (has no previous node) from the map.
    ##
    for u, val in prev.items():
      if not val:
        prev.pop(u)

    ##
    ## Return previous-node map.  This is a map of all reachable, remote nodes to
    ## their predecessor node.
    ##
    return prev


  def _calculate_routes(self):
    ##
    ## Generate the shortest-path tree with the local node as root
    ##
    prev = self._calculate_tree_from_root(self.id)
    nodes = prev.keys()

    ##
    ## Distill the path tree into a map of next hops for each node
    ##
    next_hops = {}
    while len(nodes) > 0:
      u = nodes[0]          # pick any destination
      path = [u]
      nodes.remove(u)
      v = prev[u]
      while v != self.id:   # build a list of nodes in the path back to the root
        if v in nodes:
          path.append(v)
          nodes.remove(v)
        u = v
        v = prev[u]
      for w in path:        # mark each node in the path as reachable via the next hop
        next_hops[w] = u

    ##
    ## TODO - Calculate the tree from each origin, determine the set of origins-per-dest
    ##        for which the path from origin to dest passes through us.  This is the set
    ##        of valid origins for forwarding to the destination.
    ##

    self.container.next_hops_changed(next_hops)


class NodeSet(object):
  """
  This data structure is an ordered list of node IDs, sorted in increasing order by their cost.
  Equal cost nodes are secondarily sorted by their ID in order to provide deterministic and
  repeatable ordering.
  """
  def __init__(self, cost_map):
    self.nodes = []
    for _id, cost in cost_map.items():
      ##
      ## Assume that nodes are either unreachable (cost = None) or local (cost = 0)
      ## during this initialization.
      ##
      if cost == 0:
        self.nodes.insert(0, (_id, cost))
      else:
        ##
        ## There is no need to sort unreachable nodes by ID
        ##
        self.nodes.append((_id, cost))


  def __repr__(self):
    return self.nodes.__repr__()


  def empty(self):
    return len(self.nodes) == 0


  def contains(self, _id):
    for a, b in self.nodes:
      if a == _id:
        return True
    return False


  def lowest_cost(self):
    """
     Remove and return the lowest cost node ID.
    """
    _id, cost = self.nodes.pop(0)
    return _id


  def set_cost(self, _id, new_cost):
    """
    Set the cost for an ID in the NodeSet and re-insert the ID so that the list
    remains sorted in increasing cost order.
    """
    index = 0
    for i, c in self.nodes:
      if i == _id:
        break
      index += 1
    self.nodes.pop(index)

    index = 0
    for i, c in self.nodes:
      if c == None or new_cost < c or (new_cost == c and _id < i):
        break
      index += 1

    self.nodes.insert(index, (_id, new_cost))