backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / services / protocols / bgp / operator / commands / show / vrf.py
1 from __future__ import absolute_import
2
3 import logging
4 import pprint
5
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 \
11     WrongParamResp
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
15
16 LOG = logging.getLogger('bgpspeaker.operator.commands.show.vrf')
17
18 SUPPORTED_VRF_RF = ('ipv4', 'ipv6', 'evpn')
19
20
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)
24     command = 'routes'
25
26     def __init__(self, *args, **kwargs):
27         super(Routes, self).__init__(*args, **kwargs)
28         self.subcommands = {
29             'all': self.All,
30         }
31
32     def action(self, params):
33         if len(params) != 2:
34             return WrongParamResp()
35         vrf_name = params[0]
36         vrf_rf = params[1]
37         if vrf_rf not in SUPPORTED_VRF_RF:
38             return WrongParamResp('route-family not one of %s' %
39                                   str(SUPPORTED_VRF_RF))
40
41         from ryu.services.protocols.bgp.operator.internal_api import \
42             WrongParamError
43
44         try:
45             return CommandsResponse(
46                 STATUS_OK,
47                 self.api.get_single_vrf_routes(vrf_name, vrf_rf)
48             )
49         except WrongParamError as e:
50             return CommandsResponse(
51                 STATUS_ERROR,
52                 'wrong parameters: %s' % str(e)
53             )
54
55     @classmethod
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)
60
61     class All(Command, RouteFormatterMixin):
62         help_msg = 'show routes for all VRFs'
63         command = 'all'
64
65         def action(self, params):
66             if len(params) != 0:
67                 return WrongParamResp()
68             return CommandsResponse(
69                 STATUS_OK,
70                 self.api.get_all_vrf_routes()
71             )
72
73         @classmethod
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)
81             return ret
82
83
84 class CountRoutesMixin(object):
85     api = None  # not assigned yet
86
87     def _count_routes(self, vrf_name, vrf_rf):
88         return len(self.api.get_single_vrf_routes(vrf_name, vrf_rf))
89
90
91 class Summary(Command, CountRoutesMixin):
92     help_msg = 'show configuration and summary of vrf'
93     param_help_msg = '<rd> <route_family>| all'
94     command = 'summary'
95
96     def __init__(self, *args, **kwargs):
97         super(Summary, self).__init__(*args, **kwargs)
98         self.subcommands = {
99             'all': self.All
100         }
101
102     def action(self, params):
103         if len(params) == 0:
104             return WrongParamResp('Not enough params')
105
106         vrf_confs = self.api.get_vrfs_conf()
107         if len(params) < 2:
108             vrf_rf = 'ipv4'
109         else:
110             vrf_rf = params[1]
111
112         vrf_key = params[0], vrf_rf
113
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)
118         else:
119             return WrongParamResp('No vrf matched by %s' % str(vrf_key))
120
121         return CommandsResponse(
122             STATUS_OK,
123             encoded
124         )
125
126     @classmethod
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)
131
132     class All(Command, CountRoutesMixin):
133         command = 'all'
134         help_msg = 'shows all vrfs configurations and summary'
135
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(
143                     vrf_name,
144                     vrf_rf
145                 )
146
147             encoded = dict([(str(k), v)
148                             for k, v in encoded.items()])
149             return CommandsResponse(
150                 STATUS_OK,
151                 encoded
152             )
153
154         def _count_routes(self, vrf_name, vrf_rf):
155             return len(self.api.get_single_vrf_routes(vrf_name, vrf_rf))
156
157
158 class Vrf(Routes):
159     """Main node for vrf related commands. Acts also as Routes node (that's why
160     it inherits from it) for legacy reasons.
161     """
162     help_msg = 'vrf related commands subtree'
163     command = 'vrf'
164
165     def __init__(self, *args, **kwargs):
166         super(Vrf, self).__init__(*args, **kwargs)
167         self.subcommands.update({
168             'routes': Routes,
169             'summary': Summary
170         })