-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefix_map.py
148 lines (130 loc) · 4.27 KB
/
prefix_map.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
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
import re
class RouteInfo:
def __init__(self):
self.prefix = None
self.from_ip = None
self.from_as = None
self.as_path_list = []
self.next_hop = None
self.origin = None
self.view = None
self.sequence = None
self.med = None
self.local_pref = None
self.community = None
self.status = None
def display(self):
print("PREFIX:"+self.prefix)
print("from_ip:"+self.from_ip)
print("from_as:"+self.from_as)
print("next_hop:"+self.next_hop)
print("AS PATH LIST:")
print(self.as_path_list)
print("origin: ",self.origin)
print("view: ",self.view)
print("sequence: ",self.sequence)
print("MULTI_EXIT_DISC: ",self.med)
print("local_pref: ",self.local_pref)
print("community: ",self.community)
print("status: ",self.status)
class Prefix:
def __init__(self, rib_file_name = None):
self.prefix_to_route = dict()
if rib_file_name is not None:
self.rib_file = open(rib_file_name, "r")
else:
self.rib_file = None
def display_prefix(self):
for prefix in self.prefix_to_route.keys():
print("\n\n=======PREFIX: "+prefix+"=========\n")
route_info_list = self.prefix_to_route[prefix]
for route_info in route_info_list:
print("\n")
route_info.display()
def build_prefix(self):
route_info = self.build_route_info()
while (route_info is not None):
## Add to the list only if route_info is a valid one
if route_info.prefix is not None and route_info.from_ip is not None:
current_prefix = route_info.prefix
if current_prefix != "":
if current_prefix in self.prefix_to_route.keys():
current_route_info_list = self.prefix_to_route[current_prefix]
current_route_info_list.append(route_info)
else:
new_route_info_list = []
new_route_info_list.append(route_info)
self.prefix_to_route[current_prefix] = new_route_info_list
route_info = self.build_route_info()
def build_route_info(self):
print("In build_route_info")
route_info = None
line = self.rib_file.readline()
while (line):
print("ENTERED WHILE")
if "TIME:" in line:
print("TIME match")
if route_info is None:
print("START")
route_info = RouteInfo()
else:
print("END")
return route_info
if "TYPE:" in line:
print("TYPE match")
if route_info is None:
print("START")
print("RouteInfo created")
route_info = RouteInfo()
if "PREFIX" in line:
print("PREFIX match")
matchObj = re.match( r'PREFIX: (.*)', line, re.M|re.I)
route_info.prefix = matchObj.group(1)
if "FROM" in line:
print("FROM match")
matchObj = re.match( r'FROM: (.*) AS(.*)', line, re.M|re.I)
route_info.from_ip = matchObj.group(1)
route_info.from_as = matchObj.group(2)
if "ASPATH" in line:
print("ASPATH match")
matchObj = re.match( r'ASPATH: (.*)', line, re.M|re.I)
as_path_string = matchObj.group(1)
route_info.as_path_list = as_path_string.split(" ")
if "NEXT_HOP" in line:
print("NEXT_HOP match")
matchObj = re.match( r'NEXT_HOP: (.*)', line, re.M|re.I)
route_info.next_hop = matchObj.group(1)
if "ORIGIN:" in line:
print("ORIGIN match")
matchObj = re.match( r'ORIGIN: (.*)', line, re.M|re.I)
route_info.origin = matchObj.group(1)
if "VIEW" in line:
print("VIEW match")
matchObj = re.match( r'VIEW: (.*)', line, re.M|re.I)
route_info.view = matchObj.group(1)
if "SEQUENCE:" in line:
print("SEQUENCE match")
matchObj = re.match( r'SEQUENCE: (.*)', line, re.M|re.I)
route_info.sequence = matchObj.group(1)
if "MULTI_EXIT_DISC:" in line:
print("MULTI_EXIT_DISC match")
matchObj = re.match( r'MULTI_EXIT_DISC: (.*)', line, re.M|re.I)
route_info.med = matchObj.group(1)
if "LOCAL_PREF:" in line:
print("LOCAL_PREF match")
matchObj = re.match( r'LOCAL_PREF: (.*)', line, re.M|re.I)
route_info.local_pref = matchObj.group(1)
if "COMMUNITY:" in line:
print("COMMUNITY match")
matchObj = re.match( r'COMMUNITY: (.*)', line, re.M|re.I)
route_info.community = matchObj.group(1)
if "STATUS:" in line:
print("STATUS match")
matchObj = re.match( r'STATUS: (.*)', line, re.M|re.I)
route_info.status = matchObj.group(1)
line = self.rib_file.readline()
print("NEW LINE READ:"+line)
return route_info
prefix_obj = Prefix('rib_data')
prefix_obj.build_prefix()
prefix_obj.display_prefix()