2727
2828from jnpr .junos import Device
2929from jnpr .junos .utils .config import Config
30+ from jnpr .junos .exception import RpcError
3031from jnpr .junos .exception import ConfigLoadError
3132from jnpr .junos .exception import RpcTimeoutError
3233from jnpr .junos .exception import ConnectTimeoutError
@@ -966,13 +967,8 @@ def get_route_to(self, destination='', protocol=''):
966967 if not isinstance (destination , py23_compat .string_types ):
967968 raise TypeError ('Please specify a valid destination!' )
968969
969- if protocol and (not isinstance (protocol , py23_compat .string_types ) or
970- protocol .lower () not in ('static' , 'bgp' , 'isis' , 'connected' , 'direct' )):
971- raise TypeError ("Protocol not supported: {protocol}." .format (
972- protocol = protocol
973- ))
974-
975- protocol = protocol .lower ()
970+ if protocol and isinstance (destination , py23_compat .string_types ):
971+ protocol = protocol .lower ()
976972
977973 if protocol == 'connected' :
978974 protocol = 'direct' # this is how is called on JunOS
@@ -1022,7 +1018,7 @@ def get_route_to(self, destination='', protocol=''):
10221018 rt_kargs = {
10231019 'destination' : destination
10241020 }
1025- if protocol :
1021+ if protocol and isinstance ( destination , py23_compat . string_types ) :
10261022 rt_kargs ['protocol' ] = protocol
10271023
10281024 try :
@@ -1033,8 +1029,13 @@ def get_route_to(self, destination='', protocol=''):
10331029 # will take very very long to determine all routes and
10341030 # moreover will return a huge list
10351031 raise CommandTimeoutException (
1036- 'Too many routes returned! Please try with a longer prefix!'
1032+ 'Too many routes returned! Please try with a longer prefix or a specific protocol !'
10371033 )
1034+ except RpcError as rpce :
1035+ if len (rpce .errs ) > 0 and 'bad_element' in rpce .errs [0 ]:
1036+ raise CommandErrorException (
1037+ 'Unknown protocol: {proto}' .format (proto = rpce .errs [0 ]['bad_element' ]))
1038+ raise CommandErrorException (rpce )
10381039 except Exception as err :
10391040 raise CommandErrorException ('Cannot retrieve routes! Reason: {err}' .format (err = err ))
10401041
@@ -1089,39 +1090,35 @@ def get_snmp_information(self):
10891090 """Return the SNMP configuration."""
10901091 snmp_information = {}
10911092
1092- _AUTHORIZATION_MODE_MAP_ = {
1093- 'read-only' : u'ro' ,
1094- 'read-write' : u'rw'
1095- }
1096-
10971093 snmp_config = junos_views .junos_snmp_config_table (self .device )
10981094 snmp_config .get ()
10991095 snmp_items = snmp_config .items ()
11001096
11011097 if not snmp_items :
11021098 return snmp_information
11031099
1104- communities = []
1105- for snmp_config_out in snmp_items :
1106- community_details = snmp_config_out [1 ]
1107- communities .append ({
1108- c [0 ]: c [1 ] for c in community_details
1109- })
1110-
11111100 snmp_information = {
1112- 'contact' : napalm_base .helpers .convert (unicode , communities [0 ].get ('contact' )),
1113- 'location' : napalm_base .helpers .convert (unicode , communities [0 ].get ('location' )),
1114- 'chassis_id' : napalm_base .helpers .convert (unicode , communities [0 ].get ('chassis' )),
1115- 'community' : {}
1101+ py23_compat .text_type (ele [0 ]): ele [1 ] if ele [1 ] else ''
1102+ for ele in snmp_items [0 ][1 ]
11161103 }
11171104
1118- for snmp_entry in communities :
1119- name = napalm_base .helpers .convert (unicode , snmp_entry .get ('name' ))
1120- authorization = napalm_base .helpers .convert (unicode , snmp_entry .get ('authorization' ))
1121- snmp_information ['community' ][name ] = {
1122- 'mode' : _AUTHORIZATION_MODE_MAP_ .get (authorization , u'' ),
1123- 'acl' : u''
1105+ snmp_information ['community' ] = {}
1106+ communities_table = snmp_information .pop ('communities_table' )
1107+ if not communities_table :
1108+ return snmp_information
1109+
1110+ for community in communities_table .items ():
1111+ community_name = py23_compat .text_type (community [0 ])
1112+ community_details = {
1113+ 'acl' : ''
11241114 }
1115+ community_details .update ({
1116+ py23_compat .text_type (ele [0 ]): py23_compat .text_type (
1117+ ele [1 ] if ele [0 ] != 'mode'
1118+ else C .SNMP_AUTHORIZATION_MODE_MAP .get (ele [1 ]))
1119+ for ele in community [1 ]
1120+ })
1121+ snmp_information ['community' ][community_name ] = community_details
11251122
11261123 return snmp_information
11271124
0 commit comments