summaryrefslogtreecommitdiff
path: root/extras/dispatch/src/py/router/link.py
blob: ce97794b8c6c2c7e3264e80226a9cd8e0a64408a (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
#
# 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 data import MessageRA, MessageLSU, MessageLSR
from time import time

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

class LinkStateEngine(object):
  """
  This module is responsible for running the Link State protocol and maintaining the set
  of link states that are gathered from the domain.  It notifies outbound when changes to
  the link-state-collection are detected.
  """
  def __init__(self, container):
    self.container = container
    self.id = self.container.id
    self.area = self.container.area
    self.ra_interval = self.container.config.ra_interval
    self.remote_ls_max_age = self.container.config.remote_ls_max_age
    self.last_ra_time = 0
    self.collection = {}
    self.collection_changed = False
    self.mobile_seq = 0
    self.needed_lsrs = {}


  def tick(self, now):
    self._expire_ls(now)
    self._send_lsrs()

    if now - self.last_ra_time >= self.ra_interval:
      self.last_ra_time = now
      self._send_ra()

    if self.collection_changed:
      self.collection_changed = False
      self.container.log(LOG_INFO, "New Link-State Collection:")
      for a,b in self.collection.items():
        self.container.log(LOG_INFO, "  %s => %r" % (a, b.peers))
      self.container.ls_collection_changed(self.collection)


  def handle_ra(self, msg, now):
    if msg.id == self.id:
      return
    if msg.id in self.collection:
      ls = self.collection[msg.id]
      ls.last_seen = now
      if ls.ls_seq < msg.ls_seq:
        self.needed_lsrs[(msg.area, msg.id)] = None
    else:
      self.needed_lsrs[(msg.area, msg.id)] = None


  def handle_lsu(self, msg, now):
    if msg.id == self.id:
      return
    if msg.id in self.collection:
      ls = self.collection[msg.id]
      if ls.ls_seq < msg.ls_seq:
        ls = msg.ls
        self.collection[msg.id] = ls
        self.collection_changed = True
      ls.last_seen = now
    else:
      ls = msg.ls
      self.collection[msg.id] = ls
      self.collection_changed = True
      ls.last_seen = now
      self.container.new_node(msg.id)
      self.container.log(LOG_INFO, "Learned link-state from new router: %s" % msg.id)
    # Schedule LSRs for any routers referenced in this LS that we don't know about
    for _id in msg.ls.peers:
      if _id not in self.collection:
        self.needed_lsrs[(msg.area, _id)] = None


  def handle_lsr(self, msg, now):
    if msg.id == self.id:
      return
    if self.id not in self.collection:
      return
    my_ls = self.collection[self.id]
    self.container.send('_topo/%s/%s' % (msg.area, msg.id), MessageLSU(None, self.id, self.area, my_ls.ls_seq, my_ls))


  def new_local_link_state(self, link_state):
    self.collection[self.id] = link_state
    self.collection_changed = True
    self._send_ra()


  def set_mobile_sequence(self, seq):
    self.mobile_seq = seq


  def get_collection(self):
    return self.collection


  def _expire_ls(self, now):
    to_delete = []
    for key, ls in self.collection.items():
      if key != self.id and now - ls.last_seen > self.remote_ls_max_age:
        to_delete.append(key)
    for key in to_delete:
      ls = self.collection.pop(key)
      self.collection_changed = True
      self.container.lost_node(key)
      self.container.log(LOG_INFO, "Expired link-state from router: %s" % key)


  def _send_lsrs(self):
    for (_area, _id) in self.needed_lsrs.keys():
      self.container.send('_topo/%s/%s' % (_area, _id), MessageLSR(None, self.id, self.area))
    self.needed_lsrs = {}


  def _send_ra(self):
    ls_seq = 0
    if self.id in self.collection:
      ls_seq = self.collection[self.id].ls_seq
    self.container.send('_topo/%s/all' % self.area, MessageRA(None, self.id, self.area, ls_seq, self.mobile_seq))