backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / services / protocols / bgp / operator / commands / show / rib.py
1 from __future__ import absolute_import
2
3 from ryu.services.protocols.bgp.base import ActivityException
4 from ryu.services.protocols.bgp.operator.command import Command
5 from ryu.services.protocols.bgp.operator.command import CommandsResponse
6 from ryu.services.protocols.bgp.operator.command import STATUS_ERROR
7 from ryu.services.protocols.bgp.operator.command import STATUS_OK
8 from ryu.services.protocols.bgp.operator.commands.responses import (
9     WrongParamResp)
10 from .route_formatter_mixin import RouteFormatterMixin
11
12
13 class RibBase(Command, RouteFormatterMixin):
14     supported_families = [
15         'ipv4',
16         'ipv6',
17         'vpnv4',
18         'vpnv6',
19         'rtfilter',
20         'evpn',
21         'ipv4fs',
22         'ipv6fs',
23         'vpnv4fs',
24         'vpnv6fs',
25         'l2vpnfs',
26     ]
27
28
29 class Rib(RibBase):
30     help_msg = 'show all routes for address family'
31     param_help_msg = '<address-family>'
32     command = 'rib'
33
34     def __init__(self, *args, **kwargs):
35         super(Rib, self).__init__(*args, **kwargs)
36         self.subcommands = {
37             'all': self.All}
38
39     def action(self, params):
40         if len(params) != 1 or params[0] not in self.supported_families:
41             return WrongParamResp()
42         from ryu.services.protocols.bgp.operator.internal_api \
43             import WrongParamError
44         try:
45             return CommandsResponse(
46                 STATUS_OK,
47                 self.api.get_single_rib_routes(params[0])
48             )
49         except WrongParamError as e:
50             return WrongParamResp(e)
51
52     @classmethod
53     def cli_resp_formatter(cls, resp):
54         if resp.status == STATUS_ERROR:
55             return RibBase.cli_resp_formatter(resp)
56         return cls._format_family_header() + cls._format_family(resp.value)
57
58     class All(RibBase):
59         help_msg = 'show routes for all RIBs'
60         command = 'all'
61
62         def action(self, params):
63             if len(params) != 0:
64                 return WrongParamResp()
65             ret = {}
66             try:
67                 for family in self.supported_families:
68                     ret[family] = self.api.get_single_rib_routes(family)
69                 return CommandsResponse(STATUS_OK, ret)
70             except ActivityException as e:
71                 return CommandsResponse(STATUS_ERROR, e)
72
73         @classmethod
74         def cli_resp_formatter(cls, resp):
75             if resp.status == STATUS_ERROR:
76                 return RibBase.cli_resp_formatter(resp)
77             ret = cls._format_family_header()
78             for family, data in resp.value.items():
79                 ret += 'Family: {0}\n'.format(family)
80                 ret += cls._format_family(data)
81             return ret