forked from sdn-ixp/iSDX
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathribh.py
82 lines (67 loc) · 2.44 KB
/
ribh.py
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
#! /usr/bin/python
#
# in memory hash table rib
from collections import namedtuple
# have all the rib implementations return a consistent interface
labels = ('prefix', 'neighbor', 'next_hop', 'origin', 'as_path', 'communities', 'med', 'atomic_aggregate', 'asid')
RibTuple = namedtuple('RibTuple', labels)
class rib(object):
def __init__(self, table_suffix, name):
self.name = name + "_" + str(table_suffix)
self.rib = {}
def __del__(self):
#self.cluster.shutdown()
pass
def __setitem__(self, key, item):
self.add(item)
def add(self, item):
assert(isinstance(item, RibTuple))
entry = {}
for i,v in enumerate(labels):
entry[v] = item[i]
if item.prefix not in self.rib:
self.rib[item.prefix] = {}
self.rib[item.prefix][item.neighbor] = entry
def get(self, **kwargs):
assert 'prefix' in kwargs
if kwargs['prefix'] not in self.rib:
return None
if 'neighbor' in kwargs:
if kwargs['neighbor'] not in self.rib[kwargs['prefix']]:
return None
entry = self.rib[kwargs['prefix']][kwargs['neighbor']]
return RibTuple(*[entry[l] for l in labels])
else:
entries = self.rib[kwargs['prefix']]
for k, v in entries.iteritems():
return RibTuple(*[v[l] for l in labels])
return None
def get_all(self, **kwargs):
assert 'prefix' in kwargs
if kwargs['prefix'] not in self.rib:
return []
entries = self.rib[kwargs['prefix']]
ret = []
for k, v in entries.iteritems():
ret.append(RibTuple(*[v[l] for l in labels]))
return ret
def get_prefixes(self):
output = [prefix for prefix in self.rib.keys()]
return sorted(output)
def update(self, names, item):
assert names == 'prefix'
self.rib[item.prefix] = {}
entry = {}
for i,v in enumerate(labels):
entry[v] = item[i]
self.rib[item.prefix][item.neighbor] = entry
def delete(self, **kwargs):
assert 'prefix' in kwargs
if kwargs['prefix'] not in self.rib:
return
if 'neighbor' in kwargs:
if kwargs['neighbor'] not in self.rib[kwargs['prefix']]:
return
del self.rib[kwargs['prefix']][kwargs['neighbor']]
else:
del self.rib[kwargs['prefix']]