]>
git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/lib/bgprib.py
3 # Copyright 2018, LabN Consulting, L.L.C.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; see the file COPYING; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 # {'rd':'10:1', 'p':'5.1.0.0/24', 'n':'1.1.1.1'},
22 # {'rd':'10:1', 'p':'5.1.0.0/24', 'n':'1.1.1.1'},
24 # {'rd':'10:3', 'p':'5.1.0.0/24', 'n':'3.3.3.3'},
27 # ribRequireVpnRoutes('r2','Customer routes',want_rd_routes)
29 # want_unicast_routes = [
30 # {'p':'5.1.0.0/24', 'n':'1.1.1.1'},
33 # ribRequireUnicastRoutes('r1','ipv4','r1-cust1','Customer routes in vrf',want_unicast_routes)
34 # ribRequireUnicastRoutes('r1','ipv4','','Customer routes in default',want_unicast_routes)
37 from lutil
import luCommand
,luResult
41 # gpz: get rib in json form and compare against desired routes
43 def routes_include_wanted(self
,pfxtbl
,want
,debug
):
44 # helper function to RequireVpnRoutes
45 for pfx
in pfxtbl
.iterkeys():
47 print 'trying pfx ' + pfx
50 print 'want pfx=' + want
['p'] + ', not ' + pfx
53 print 'have pfx=' + pfx
57 nexthops
= r
['nexthops']
60 print 'trying nh ' + nh
['ip']
61 if nh
['ip'] == want
['n']:
63 print 'found ' + want
['n']
67 print 'want nh=' + want
['n'] + ', not ' + nh
['ip']
69 print 'missing route: pfx=' + want
['p'] + ', nh=' + want
['n']
72 def RequireVpnRoutes(self
, target
, title
, wantroutes
, debug
=0):
74 logstr
= "RequireVpnRoutes " + str(wantroutes
)
75 #non json form for humans
76 luCommand(target
,'vtysh -c "show bgp ipv4 vpn"','.','None','Get VPN RIB (non-json)')
77 ret
= luCommand(target
,'vtysh -c "show bgp ipv4 vpn json"','.*','None','Get VPN RIB (json)')
78 if re
.search(r
'^\s*$', ret
):
79 # degenerate case: empty json means no routes
80 if len(wantroutes
) > 0:
81 luResult(target
, False, title
, logstr
)
83 luResult(target
, True, title
, logstr
)
85 rds
= rib
['routes']['routeDistinguishers']
86 for want
in wantroutes
:
89 print "want rd " + want
['rd']
90 for rd
in rds
.iterkeys():
94 print "found rd " + rd
96 if self
.routes_include_wanted(table
,want
,debug
):
100 luResult(target
, False, title
, logstr
)
102 luResult(target
, True, title
, logstr
)
104 def RequireUnicastRoutes(self
,target
,afi
,vrf
,title
,wantroutes
,debug
=0):
105 logstr
= "RequireVpnRoutes " + str(wantroutes
)
108 vrfstr
= 'vrf %s' % (vrf
)
110 if (afi
!= 'ipv4') and (afi
!= 'ipv6'):
111 print "ERROR invalid afi";
113 cmdstr
= 'show bgp %s %s unicast' % (vrfstr
, afi
)
114 #non json form for humans
115 cmd
= 'vtysh -c "%s"' % cmdstr
116 luCommand(target
,cmd
,'.','None','Get %s %s RIB (non-json)' % (vrfstr
, afi
))
117 cmd
= 'vtysh -c "%s json"' % cmdstr
118 ret
= luCommand(target
,cmd
,'.*','None','Get %s %s RIB (json)' % (vrfstr
, afi
))
119 if re
.search(r
'^\s*$', ret
):
120 # degenerate case: empty json means no routes
121 if len(wantroutes
) > 0:
122 luResult(target
, False, title
, logstr
)
124 luResult(target
, True, title
, logstr
)
125 rib
= json
.loads(ret
)
126 table
= rib
['routes']
127 for want
in wantroutes
:
128 if not self
.routes_include_wanted(table
,want
,debug
):
129 luResult(target
, False, title
, logstr
)
131 luResult(target
, True, title
, logstr
)
136 def bgpribRequireVpnRoutes(target
, title
, wantroutes
, debug
=0):
137 BgpRib
.RequireVpnRoutes(target
, title
, wantroutes
, debug
)
139 def bgpribRequireUnicastRoutes(target
, afi
, vrf
, title
, wantroutes
, debug
=0):
140 BgpRib
.RequireUnicastRoutes(target
, afi
, vrf
, title
, wantroutes
, debug
)