def test_straightenrv_example(self): # from straightenRV code comment path = [ '0', '4006', '209', '15254', '15251', '15254', '15254', '15254', '15254' ] norm_path = [0, 4006, 209, 15254] self.assertEqual(norm_path, aspath.normalize_as_path(path))
def test_composed_cases(self): path = ['1', '1', '{1}', '2', '3', '3', '2', '3', '{2}', '{2,3}'] norm_path = [1, 2, -1] self.assertEqual(norm_path, aspath.normalize_as_path(path)) path = ['1', '1', '{1}', '2', '3', '3', '2', '3', '{3}', '{2,3}'] norm_path = None self.assertEqual(norm_path, aspath.normalize_as_path(path)) path = ['{2,3}', '{3,4}', '{4,5}', '{5,6}'] norm_path = [-1] self.assertEqual(norm_path, aspath.normalize_as_path(path)) # confed_set & confed_seq path = ['1', '1', '{1}', '2', '3', '3', '(65000', '65001)', '2'] norm_path = [1, 2] self.assertEqual(norm_path, aspath.normalize_as_path(path)) path = ['1', '1', '{1}', '2', '3', '3', '(6500', '6501)', '2'] norm_path = None self.assertRaises(ValueError, aspath.normalize_as_path, path)
def test_composed_cases(self): path = ['1','1','{1}', '2', '3', '3', '2', '3', '{2}', '{2,3}'] norm_path = [1, 2, -1] self.assertEqual(norm_path, aspath.normalize_as_path(path)) path = ['1','1','{1}', '2', '3', '3', '2', '3', '{3}', '{2,3}'] norm_path = None self.assertEqual(norm_path, aspath.normalize_as_path(path)) path = ['{2,3}', '{3,4}', '{4,5}', '{5,6}'] norm_path = [-1] self.assertEqual(norm_path, aspath.normalize_as_path(path)) # confed_set & confed_seq path = ['1','1','{1}', '2', '3', '3', '(65000', '65001)', '2'] norm_path = [1, 2] self.assertEqual(norm_path, aspath.normalize_as_path(path)) path = ['1','1','{1}', '2', '3', '3', '(6500', '6501)', '2'] norm_path = None self.assertRaises(ValueError, aspath.normalize_as_path, path)
def test_private_asns(self): path = ['1', '65535', '2', '{65400}', '65000', '{3}'] norm_path = [1, 2, 3] self.assertEqual(norm_path, aspath.normalize_as_path(path))
def postprocess_rib(rib_filename, norm_filename, peers_filename, ppapp_filename, include_peer_ip): # ppapp and ppp are keyed on peer_ip, or peer asn if there is no peer_ip ppapp = {} # prefixes per as, per peer ppp = {} # prefixes per peer peer_asns = {} null_peer_as_cache = {} null_origin_as_cache = {} f = open(rib_filename, 'r') outfile = open(norm_filename, 'w') for line in f: try: components = line.split() [prefix, prefix_len] = components[0].split('/') if include_peer_ip: peer_ip = components[1] as_start_index = 2 else: peer_ip = None as_start_index = 1 raw_as_path = components[as_start_index:] if raw_as_path[0] == '-': # null AS -- find observer and peer ASNs if components[0] not in null_origin_as_cache: output = subprocess.Popen( "grep -P -m 1 '{0} (\d+\.)+\d+ \d+' {1}".format( components[0], rib_filename), shell=True, stdout=subprocess.PIPE).communicate()[0] null_origin_as_cache[components[0]] = output.split()[-1] raw_as_path = [null_origin_as_cache[components[0]]] if include_peer_ip: if peer_ip not in null_peer_as_cache: output = subprocess.Popen( "grep -P -m 1 '{0} \d+' {1}".format( peer_ip, rib_filename), shell=True, stdout=subprocess.PIPE).communicate()[0] null_peer_as_cache[peer_ip] = output.split()[2] raw_as_path.insert(0, null_peer_as_cache[peer_ip]) print("NULL AS_PATH: replacing '{0}' with {1}".format( line.strip(), raw_as_path)) norm_path = aspath.normalize_as_path(raw_as_path) norm_path.reverse() except StandardError as e: norm_path = None if norm_path: if include_peer_ip: outfile.write("{0:<18} {1:<15} {2}\n".format( '/'.join([prefix, prefix_len]), peer_ip, aspath.path_to_string(norm_path))) try: ppapp.setdefault(peer_ip, {})[norm_path[0]] += 1 ppp[peer_ip] += 1 except KeyError: ppapp.setdefault(peer_ip, {}).setdefault(norm_path[0], 1) ppp.setdefault(peer_ip, 1) if peer_ip not in peer_asns: peer_asns[peer_ip] = norm_path[-1] else: outfile.write("{0:<18} {1}\n".format( '/'.join([prefix, prefix_len]), aspath.path_to_string(norm_path))) try: ppapp.setdefault(norm_path[-1], {})[norm_path[0]] += 1 ppp[norm_path[-1]] += 1 except KeyError: ppapp.setdefault(norm_path[-1], {}).setdefault( norm_path[0], 1) ppp.setdefault(norm_path[-1], 1) else: print("INVALID AS_PATH: dropping '{0}'".format(line.strip())) outfile.close() f.close() ppapp_file = open(ppapp_filename,'w') peers_file = open(peers_filename,'w') if include_peer_ip: # ppapp_file.write("# prefix_count origin_as peer_as peer_ip\n") # peers_file.write("# prefix_count peer_as peer_ip\n") for peer_ip in ppapp: for asn in ppapp[peer_ip]: ppapp_file.write('{0:>8} {1:>10} {2:>5} {3:<15}\n'.format( ppapp[peer_ip][asn], asn, peer_asns[peer_ip], peer_ip)) for peer_ip in ppp: peers_file.write('{0:>8} {1:>5} {2:<15}\n'.format( ppp[peer_ip], peer_asns[peer_ip], peer_ip)) else: #ppapp_file.write("# prefix_count origin_as peer_as\n") #peers_file.write("# prefix_count peer_as\n") for peer_asn in ppapp: for asn in ppapp[peer_asn]: ppapp_file.write('{0:>8} {1:>10} {2:>10}\n'.format( ppapp[peer_asn][asn], asn, peer_asn)) for peer_asn in ppp: peers_file.write('{0:>8} {1:>5}\n'.format( ppp[peer_asn], peer_asn)) ppapp_file.close() peers_file.close()
def test_straightenrv_example(self): # from straightenRV code comment path = ['0', '4006', '209', '15254', '15251', '15254', '15254', '15254', '15254'] norm_path = [0, 4006, 209, 15254] self.assertEqual(norm_path, aspath.normalize_as_path(path))