Statistics
| Branch: | Revision:

iof-tools / BGPpysim / util / routing_table.py @ de0e8e06

History | View | Annotate | Download (2.24 KB)

1
from util.event_log import EventLog
2
from collections import defaultdict
3
from prettytable import PrettyTable
4
import code  # code.interact(local=dict(globals(), **locals()))
5

    
6

    
7
class Route(object):
8

    
9
    def __init__(self, prefix, attr={}):
10
        self.prefix = prefix
11
        self. attr = attr
12

    
13
    def as_path(self):
14
        return self.attr['AS_PATH']
15

    
16

    
17
class RoutingTable(object):
18

    
19
    def __init__(self):
20
        self.rt = {}
21
        self.__flag_change = False
22
        pass
23

    
24
    def __contains__(self, key):
25
        return key in self.rt
26

    
27
    def __getitem__(self, key):
28
        return self.rt[key]
29

    
30
    def __setitem__(self, key, value):
31
        self.rt[key] = value
32

    
33
    def set_neighbours(self, neighs):
34
        self.neighs = neighs
35

    
36
        '''Stiamo sicuramente aggiornando una rotta (appena imparata, o migliorata)
37
        Le cose da fare sono:
38
        - 
39
        '''
40

    
41
    def dumps(self):
42
        s = PrettyTable()
43
        s.field_names = ["PREFIX", "AS_PATH", "NH", "PREFERENCE"]
44
        for p in self.rt:
45
            s.add_row([p, self.rt[p]['AS_PATH'], self.rt[p]
46
                       ['NH'], self.rt[p]['PREFERENCE']])
47
        print(s)
48

    
49
    def install_route(self, route, sender, route_preference, time):
50
        prefix, NH, as_path, preference = route.prefix, sender, route.as_path(), route_preference
51
        new_route = route.prefix not in self.rt
52
        self.rt[prefix] = {'NH': NH, 'AS_PATH': as_path,
53
                           'PREFERENCE': preference}
54
        if new_route:
55

    
56
            self.rt[route.prefix]['MRAIs'] = defaultdict(int)
57
            self.rt[route.prefix]['SHARED_FLAG'] = defaultdict(
58
                bool)  # chiavi nuove nascono con valore False
59
            for neigh in self.neighs:
60
                # virtualmente scaduto! :)
61
                self.rt[route.prefix]['MRAIs'][neigh] = time
62
        else:
63
            # non modifico l'MRAI pre-esistente, aspetto che scada...
64
            # ma setto shared_flag = False per tutti, perchè con nessuno
65
            # ho gia condiviso la nuova rotta appena installata
66
            self.rt[route.prefix]['SHARED_FLAG'] = defaultdict(
67
                bool)  # chiavi nuove nascono con valore False
68
            self.rt[route.prefix]['PREFERENCE'] = preference
69
        #code.interact(local=dict(globals(), **locals()))