<s> import logging <EOL> import pprint <EOL> from ryu . services . protocols . bgp . operator . command import Command <EOL> from ryu . services . protocols . bgp . operator . command import CommandsResponse <EOL> from ryu . services . protocols . bgp . operator . command import STATUS_ERROR <EOL> from ryu . services . protocols . bgp . operator . command import STATUS_OK <EOL> from ryu . services . protocols . bgp . operator . commands . responses import WrongParamResp <EOL> from ryu . services . protocols . bgp . operator . views . conf import ConfDetailView <EOL> from ryu . services . protocols . bgp . operator . views . conf import ConfDictView <EOL> from route_formatter_mixin import RouteFormatterMixin <EOL> LOG = logging . getLogger ( '<STR_LIT>' ) <EOL> class Routes ( Command , RouteFormatterMixin ) : <EOL> help_msg = '<STR_LIT>' <EOL> param_help_msg = '<STR_LIT>' <EOL> command = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( Routes , self ) . __init__ ( * args , ** kwargs ) <EOL> self . subcommands = { <EOL> '<STR_LIT:all>' : self . All , <EOL> } <EOL> def action ( self , params ) : <EOL> if len ( params ) != <NUM_LIT:2> : <EOL> return WrongParamResp ( ) <EOL> vrf_name = params [ <NUM_LIT:0> ] <EOL> vrf_rf = params [ <NUM_LIT:1> ] <EOL> if vrf_rf not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return WrongParamResp ( '<STR_LIT>' ) <EOL> from ryu . services . protocols . bgp . operator . internal_api import WrongParamError <EOL> try : <EOL> return CommandsResponse ( <EOL> STATUS_OK , <EOL> self . api . get_single_vrf_routes ( vrf_name , vrf_rf ) <EOL> ) <EOL> except WrongParamError as e : <EOL> return CommandsResponse ( <EOL> STATUS_ERROR , <EOL> '<STR_LIT>' % str ( e ) <EOL> ) <EOL> @ classmethod <EOL> def cli_resp_formatter ( cls , resp ) : <EOL> if resp . status == STATUS_ERROR : <EOL> return super ( Routes , cls ) . cli_resp_formatter ( resp ) <EOL> return cls . _format_family_header ( ) + cls . _format_family ( resp . value ) <EOL> class All ( Command , RouteFormatterMixin ) : <EOL> help_msg = '<STR_LIT>' <EOL> command = '<STR_LIT:all>' <EOL> def action ( self , params ) : <EOL> if len ( params ) != <NUM_LIT:0> : <EOL> return WrongParamResp ( ) <EOL> return CommandsResponse ( <EOL> STATUS_OK , <EOL> self . api . get_all_vrf_routes ( ) <EOL> ) <EOL> @ classmethod <EOL> def cli_resp_formatter ( cls , resp ) : <EOL> if resp . status == STATUS_ERROR : <EOL> return Command . cli_resp_formatter ( resp ) <EOL> ret = cls . _format_family_header ( ) <EOL> for family , data in resp . value . items ( ) : <EOL> ret += '<STR_LIT>' . format ( family ) <EOL> ret += cls . _format_family ( data ) <EOL> return ret <EOL> class CountRoutesMixin ( object ) : <EOL> def _count_routes ( self , vrf_name , vrf_rf ) : <EOL> return len ( self . api . get_single_vrf_routes ( vrf_name , vrf_rf ) ) <EOL> class Summary ( Command , CountRoutesMixin ) : <EOL> help_msg = '<STR_LIT>' <EOL> param_help_msg = '<STR_LIT>' <EOL> command = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( Summary , self ) . __init__ ( * args , ** kwargs ) <EOL> self . subcommands = { <EOL> '<STR_LIT:all>' : self . All <EOL> } <EOL> def action ( self , params ) : <EOL> if len ( params ) == <NUM_LIT:0> : <EOL> return WrongParamResp ( '<STR_LIT>' ) <EOL> vrf_confs = self . api . get_vrfs_conf ( ) <EOL> if len ( params ) < <NUM_LIT:2> : <EOL> vrf_rf = '<STR_LIT>' <EOL> else : <EOL> vrf_rf = params [ <NUM_LIT:1> ] <EOL> vrf_key = params [ <NUM_LIT:0> ] , vrf_rf <EOL> if vrf_key in vrf_confs : <EOL> view = ConfDetailView ( vrf_confs [ vrf_key ] ) <EOL> encoded = view . encode ( ) <EOL> encoded [ '<STR_LIT>' ] = self . _count_routes ( params [ <NUM_LIT:0> ] , vrf_rf ) <EOL> else : <EOL> return WrongParamResp ( '<STR_LIT>' % str ( vrf_key ) ) <EOL> return CommandsResponse ( <EOL> STATUS_OK , <EOL> encoded <EOL> ) <EOL> @ classmethod <EOL> def cli_resp_formatter ( cls , resp ) : <EOL> if resp . status == STATUS_ERROR : <EOL> return Command . cli_resp_formatter ( resp ) <EOL> return pprint . pformat ( resp . value ) <EOL> class All ( Command , CountRoutesMixin ) : <EOL> command = '<STR_LIT:all>' <EOL> help_msg = '<STR_LIT>' <EOL> def action ( self , params ) : <EOL> vrf_confs = self . api . get_vrfs_conf ( ) <EOL> view = ConfDictView ( vrf_confs ) <EOL> encoded = view . encode ( ) <EOL> for vrf_key , conf in encoded . items ( ) : <EOL> vrf_name , vrf_rf = vrf_key <EOL> conf [ '<STR_LIT>' ] = self . _count_routes ( <EOL> vrf_name , <EOL> vrf_rf <EOL> ) <EOL> encoded = dict ( [ ( str ( k ) , v ) <EOL> for k , v in encoded . items ( ) ] ) <EOL> return CommandsResponse ( <EOL> STATUS_OK , <EOL> encoded <EOL> ) <EOL> def _count_routes ( self , vrf_name , vrf_rf ) : <EOL> return len ( self . api . get_single_vrf_routes ( vrf_name , vrf_rf ) ) <EOL> class Vrf ( Routes ) : <EOL> """<STR_LIT>""" <EOL> help_msg = '<STR_LIT>' <EOL> command = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( Vrf , self ) . __init__ ( * args , ** kwargs ) <EOL> self . subcommands . update ( { <EOL> '<STR_LIT>' : Routes , <EOL> '<STR_LIT>' : Summary <EOL> } ) </s>
