Skip to content

Commit

Permalink
Update - added "-s" option in "mrt2exabgpy.py".
Browse files Browse the repository at this point in the history
  • Loading branch information
t2mune committed Feb 17, 2017
1 parent b6d9279 commit 287fa26
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
@@ -1,3 +1,7 @@
2017-02-18 Tetsumune KISO <t2mune@gmail.com>
* Fix - can't display properly with "-a" option in "mrt2exabgp.py".
* Update - added "-s" option in "mrt2exabgpy.py". Thank you andydavidson.

2017-02-11 Tetsumune KISO <t2mune@gmail.com>
* Update - deleted all README-ja.rst.
* Update - replaced sample data.
Expand Down
4 changes: 3 additions & 1 deletion examples/README.rst
Expand Up @@ -88,7 +88,7 @@ Usage

usage: mrt2exabgp.py [-h] [-r ROUTER_ID] [-l LOCAL_AS] [-p PEER_AS]
[-L LOCAL_ADDR] [-n NEIGHBOR] [-4 [NEXT_HOP]]
[-6 [NEXT_HOP]] [-a] [-A] [-G [NUM]] [-g [NUM]] [-P]
[-6 [NEXT_HOP]] [-a] [-s] [-A] [-G [NUM]] [-g [NUM]] [-P]
path_to_file

This script converts to ExaBGP format.
Expand All @@ -107,6 +107,8 @@ Usage
-6 [NEXT_HOP] convert IPv6 entries and change IPv6 next-hop if specified
-a convert all entries (default: convert only first entry per
one prefix)
-s convert only entries from a single asn (the peer asn, specify
as -p PEER_ASN)
-A convert to ExaBGP API format
-G [NUM] convert to ExaBGP API format and group updates with the same
attributes for each spceified the number of prefixes using
Expand Down
62 changes: 36 additions & 26 deletions examples/mrt2exabgp.py
Expand Up @@ -29,13 +29,16 @@
'IPv4': 0x01,
'IPv6': 0x02,
'ALL' : 0x04,
'API' : 0x08,
'SINGLE' : 0x50,
'API_GROUP_OLD' : 0x10,
'API_GROUP' : 0x20,
'API_PROG' : 0x40,
'SINGLE' : 0x08,
'API' : 0x10,
'API_GROUP_OLD' : 0x20,
'API_GROUP' : 0x40,
'API_PROG' : 0x80,
}

class NotDisplay(Exception):
pass

def print_conf_header(args):
print('''\
neighbor %s {
Expand Down Expand Up @@ -113,7 +116,8 @@ def parse_args():
(default: convert only first entry per one prefix)')
p.add_argument(
'-s', default=False, action='store_true',
help='convert only entries from a single asn (the peer asn, specify as -p NNN)')
help='convert only entries from a single asn \
(the peer asn, specify as -p PEER_ASN)')
p.add_argument(
'-A', action='store_false',
help='convert to ExaBGP API format')
Expand Down Expand Up @@ -229,18 +233,7 @@ def print_route_td(args, params, m):
if flags & FLAG_T['ALL']:
entry = m.rib.entry
else:
if flags & FLAG_T['SINGLE']:
for thisentry in m.rib.entry:
for attr in thisentry.attr:
if attr.type == BGP_ATTR_T['AS_PATH']:
entry_peer_asn = attr.as_path[0]["val"][0]
if entry_peer_asn == str(args.peer_as):
entry.append(thisentry)
continue
else:
entry.append(m.rib.entry[0])


entry.append(m.rib.entry[0])

elif m.type == MRT_T['TABLE_DUMP']:
if m.subtype == TD_ST['AFI_IPv4']:
Expand All @@ -262,11 +255,16 @@ def print_route_td(args, params, m):
else:
entry.append(m.td)

line = ''
for e in entry:
line = ''
params['next_hop'] = ''
for attr in e.attr:
line += get_bgp_attr(args, params, m, attr)

try:
for attr in e.attr:
line += get_bgp_attr(args, params, m, attr)

except NotDisplay:
continue

if params['flags'] & FLAG_T['API_GROUP']:
params['api_grp'].setdefault('%s next-hop %s' \
Expand Down Expand Up @@ -316,8 +314,13 @@ def print_route_bgp4mp(args, params, m):
msg = m.bgp.msg

attr_line = ''
for attr in msg.attr:
attr_line += get_bgp_attr(args, params, m, attr)

try:
for attr in msg.attr:
attr_line += get_bgp_attr(args, params, m, attr)

except NotDisplay:
return

wd_line = ''
for wd in params['mp_withdrawn']:
Expand Down Expand Up @@ -370,13 +373,20 @@ def get_bgp_attr(args, params, m, attr):
if attr.type == BGP_ATTR_T['ATOMIC_AGGREGATE']:
line += ' atomic-aggregate'

if attr.len == 0:
return line

if attr.type == BGP_ATTR_T['ORIGIN']:
line += ' origin %s' % ORIGIN_T[attr.origin]

elif attr.type == BGP_ATTR_T['AS_PATH']:
if flags & FLAG_T['SINGLE']:
if len(attr.as_path) == 0:
raise NotDisplay()

path_seg = attr.as_path[0]
if path_seg['type'] != AS_PATH_SEG_T['AS_SEQUENCE'] \
or len(path_seg['val']) == 0 \
or path_seg['val'][0] != str(args.peer_as):
raise NotDisplay()

as_path = ''
for path_seg in attr.as_path:
if path_seg['type'] == AS_PATH_SEG_T['AS_SET']:
Expand Down

0 comments on commit 287fa26

Please sign in to comment.