1 from __future__ import absolute_import
6 from ryu.services.protocols.bgp.operator.command import Command
7 from ryu.services.protocols.bgp.operator.command import CommandsResponse
8 from ryu.services.protocols.bgp.operator.command import STATUS_ERROR
9 from ryu.services.protocols.bgp.operator.command import STATUS_OK
10 from ryu.services.protocols.bgp.operator.commands.responses import \
12 from ryu.services.protocols.bgp.operator.views.conf import ConfDetailView
13 from ryu.services.protocols.bgp.operator.views.conf import ConfDictView
14 from .route_formatter_mixin import RouteFormatterMixin
16 LOG = logging.getLogger('bgpspeaker.operator.commands.show.vrf')
18 SUPPORTED_VRF_RF = ('ipv4', 'ipv6', 'evpn')
21 class Routes(Command, RouteFormatterMixin):
22 help_msg = 'show routes present for vrf'
23 param_help_msg = '<vpn-name> <route-family>%s' % str(SUPPORTED_VRF_RF)
26 def __init__(self, *args, **kwargs):
27 super(Routes, self).__init__(*args, **kwargs)
32 def action(self, params):
34 return WrongParamResp()
37 if vrf_rf not in SUPPORTED_VRF_RF:
38 return WrongParamResp('route-family not one of %s' %
39 str(SUPPORTED_VRF_RF))
41 from ryu.services.protocols.bgp.operator.internal_api import \
45 return CommandsResponse(
47 self.api.get_single_vrf_routes(vrf_name, vrf_rf)
49 except WrongParamError as e:
50 return CommandsResponse(
52 'wrong parameters: %s' % str(e)
56 def cli_resp_formatter(cls, resp):
57 if resp.status == STATUS_ERROR:
58 return super(Routes, cls).cli_resp_formatter(resp)
59 return cls._format_family_header() + cls._format_family(resp.value)
61 class All(Command, RouteFormatterMixin):
62 help_msg = 'show routes for all VRFs'
65 def action(self, params):
67 return WrongParamResp()
68 return CommandsResponse(
70 self.api.get_all_vrf_routes()
74 def cli_resp_formatter(cls, resp):
75 if resp.status == STATUS_ERROR:
76 return Command.cli_resp_formatter(resp)
77 ret = cls._format_family_header()
78 for family, data in resp.value.items():
79 ret += 'VPN: {0}\n'.format(family)
80 ret += cls._format_family(data)
84 class CountRoutesMixin(object):
85 api = None # not assigned yet
87 def _count_routes(self, vrf_name, vrf_rf):
88 return len(self.api.get_single_vrf_routes(vrf_name, vrf_rf))
91 class Summary(Command, CountRoutesMixin):
92 help_msg = 'show configuration and summary of vrf'
93 param_help_msg = '<rd> <route_family>| all'
96 def __init__(self, *args, **kwargs):
97 super(Summary, self).__init__(*args, **kwargs)
102 def action(self, params):
104 return WrongParamResp('Not enough params')
106 vrf_confs = self.api.get_vrfs_conf()
112 vrf_key = params[0], vrf_rf
114 if vrf_key in vrf_confs:
115 view = ConfDetailView(vrf_confs[vrf_key])
116 encoded = view.encode()
117 encoded['routes_count'] = self._count_routes(params[0], vrf_rf)
119 return WrongParamResp('No vrf matched by %s' % str(vrf_key))
121 return CommandsResponse(
127 def cli_resp_formatter(cls, resp):
128 if resp.status == STATUS_ERROR:
129 return Command.cli_resp_formatter(resp)
130 return pprint.pformat(resp.value)
132 class All(Command, CountRoutesMixin):
134 help_msg = 'shows all vrfs configurations and summary'
136 def action(self, params):
137 vrf_confs = self.api.get_vrfs_conf()
138 view = ConfDictView(vrf_confs)
139 encoded = view.encode()
140 for vrf_key, conf in encoded.items():
141 vrf_name, vrf_rf = vrf_key
142 conf['routes_count'] = self._count_routes(
147 encoded = dict([(str(k), v)
148 for k, v in encoded.items()])
149 return CommandsResponse(
154 def _count_routes(self, vrf_name, vrf_rf):
155 return len(self.api.get_single_vrf_routes(vrf_name, vrf_rf))
159 """Main node for vrf related commands. Acts also as Routes node (that's why
160 it inherits from it) for legacy reasons.
162 help_msg = 'vrf related commands subtree'
165 def __init__(self, *args, **kwargs):
166 super(Vrf, self).__init__(*args, **kwargs)
167 self.subcommands.update({