class Fib(): class NextHop(): def __init__(self, next_hop=''): self._next_hop = [] matches = re.findall('\[([\s\d]+)\]', next_hop) for match in matches: self._next_hop.append([int(s) for s in match.split()]) def __str__(self): return str(self._next_hop) def get_next_hop(self): return self._next_hop def get_next_hop_list(self): port_list = [p for intf in self._next_hop for p in intf] return port_list # Initialize FIB with FIB file def __init__(self, file_path): self._ipv4_lpm_dict = LpmDict() for ip in EXCLUDE_IPV4_PREFIXES: self._ipv4_lpm_dict[ip] = self.NextHop() self._ipv6_lpm_dict = LpmDict(ipv4=False) for ip in EXCLUDE_IPV6_PREFIXES: self._ipv6_lpm_dict[ip] = self.NextHop() # filter out empty lines and lines starting with '#' pattern = re.compile("^#.*$|^[ \t]*$") with open(file_path, 'r') as f: for line in f.readlines(): if pattern.match(line): continue entry = line.split(' ', 1) prefix = ip_network(unicode(entry[0])) next_hop = self.NextHop(entry[1]) if prefix.version is 4: self._ipv4_lpm_dict[str(prefix)] = next_hop elif prefix.version is 6: self._ipv6_lpm_dict[str(prefix)] = next_hop def __getitem__(self, ip): ip = ip_address(unicode(ip)) if ip.version is 4: return self._ipv4_lpm_dict[str(ip)] elif ip.version is 6: return self._ipv6_lpm_dict[str(ip)] def ipv4_ranges(self): return self._ipv4_lpm_dict.ranges() def ipv6_ranges(self): return self._ipv6_lpm_dict.ranges()
def __init__(self, file_path): self._ipv4_lpm_dict = LpmDict() for ip in EXCLUDE_IPV4_PREFIXES: self._ipv4_lpm_dict[ip] = self.NextHop() self._ipv6_lpm_dict = LpmDict(ipv4=False) for ip in EXCLUDE_IPV6_PREFIXES: self._ipv6_lpm_dict[ip] = self.NextHop() with open(file_path, 'r') as f: for line in f.readlines(): if line[0] == '#': continue entry = line.split(' ', 1) prefix = ip_network(unicode(entry[0])) next_hop = self.NextHop(entry[1]) if prefix.version is 4: self._ipv4_lpm_dict[str(prefix)] = next_hop elif prefix.version is 6: self._ipv6_lpm_dict[str(prefix)] = next_hop
def __init__(self, file_path): self._ipv4_lpm_dict = LpmDict() for ip in EXCLUDE_IPV4_PREFIXES: self._ipv4_lpm_dict[ip] = self.NextHop() self._ipv6_lpm_dict = LpmDict(ipv4=False) for ip in EXCLUDE_IPV6_PREFIXES: self._ipv6_lpm_dict[ip] = self.NextHop() # filter out empty lines and lines starting with '#' pattern = re.compile("^#.*$|^[ \t]*$") with open(file_path, 'r') as f: for line in f.readlines(): if pattern.match(line): continue entry = line.split(' ', 1) prefix = ip_network(six.text_type(entry[0])) next_hop = self.NextHop(entry[1]) if prefix.version is 4: self._ipv4_lpm_dict[str(prefix)] = next_hop elif prefix.version is 6: self._ipv6_lpm_dict[str(prefix)] = next_hop