15
15
16
16
"""BGP Update Message"""
17
17
18
- import struct
19
- import traceback
20
- import logging
21
18
import binascii
22
-
19
+ import logging
23
20
import netaddr
24
-
21
+ import struct
22
+ import traceback
25
23
from yabgp .common import exception as excep
26
24
from yabgp .common import constants as bgp_cons
27
25
from yabgp .message .attribute import AttributeFlag
@@ -191,6 +189,7 @@ def parse(cls, t, msg_hex, asn4=False, add_path_remote=False, add_path_local=Fal
191
189
LOG .error (e )
192
190
results ['sub_error' ] = e .sub_error
193
191
results ['err_data' ] = e .data
192
+ results ['attr' ] = e .sub_results
194
193
except Exception as e :
195
194
LOG .error (e )
196
195
error_str = traceback .format_exc ()
@@ -291,9 +290,9 @@ def parse_attributes(data, asn4=False):
291
290
postfix = data
292
291
bgpls_pro_id = None
293
292
bgpls_attr = None
294
- while len (postfix ) > 0 :
295
293
296
- try :
294
+ try :
295
+ while len (postfix ) > 0 :
297
296
flags , type_code = struct .unpack ('!BB' , postfix [:2 ])
298
297
299
298
if flags & AttributeFlag .EXTENDED_LENGTH :
@@ -307,103 +306,110 @@ def parse_attributes(data, asn4=False):
307
306
attr_len = ord (postfix [2 ])
308
307
attr_value = postfix [3 :3 + attr_len ]
309
308
postfix = postfix [3 + attr_len :] # Next attribute
310
- except Exception as e :
311
- LOG .error (e )
312
- error_str = traceback .format_exc ()
313
- LOG .debug (error_str )
314
- raise excep .UpdateMessageError (
315
- sub_error = bgp_cons .ERR_MSG_UPDATE_MALFORMED_ATTR_LIST ,
316
- data = '' )
317
309
318
- if type_code == bgp_cons .BGPTYPE_ORIGIN :
310
+ if type_code == bgp_cons .BGPTYPE_ORIGIN :
319
311
320
- decode_value = Origin .parse (value = attr_value )
312
+ decode_value = Origin .parse (value = attr_value )
321
313
322
- elif type_code == bgp_cons .BGPTYPE_AS_PATH :
314
+ elif type_code == bgp_cons .BGPTYPE_AS_PATH :
323
315
324
- decode_value = ASPath .parse (value = attr_value , asn4 = asn4 )
316
+ decode_value = ASPath .parse (value = attr_value , asn4 = asn4 )
325
317
326
- elif type_code == bgp_cons .BGPTYPE_NEXT_HOP :
318
+ elif type_code == bgp_cons .BGPTYPE_NEXT_HOP :
327
319
328
- decode_value = NextHop .parse (value = attr_value )
320
+ decode_value = NextHop .parse (value = attr_value )
329
321
330
- elif type_code == bgp_cons .BGPTYPE_MULTI_EXIT_DISC :
322
+ elif type_code == bgp_cons .BGPTYPE_MULTI_EXIT_DISC :
331
323
332
- decode_value = MED .parse (value = attr_value )
324
+ decode_value = MED .parse (value = attr_value )
333
325
334
- elif type_code == bgp_cons .BGPTYPE_LOCAL_PREF :
326
+ elif type_code == bgp_cons .BGPTYPE_LOCAL_PREF :
335
327
336
- decode_value = LocalPreference .parse (value = attr_value )
328
+ decode_value = LocalPreference .parse (value = attr_value )
337
329
338
- elif type_code == bgp_cons .BGPTYPE_ATOMIC_AGGREGATE :
330
+ elif type_code == bgp_cons .BGPTYPE_ATOMIC_AGGREGATE :
339
331
340
- decode_value = AtomicAggregate .parse (value = attr_value )
332
+ decode_value = AtomicAggregate .parse (value = attr_value )
341
333
342
- elif type_code == bgp_cons .BGPTYPE_AGGREGATOR :
334
+ elif type_code == bgp_cons .BGPTYPE_AGGREGATOR :
343
335
344
- decode_value = Aggregator .parse (value = attr_value , asn4 = asn4 )
336
+ decode_value = Aggregator .parse (value = attr_value , asn4 = asn4 )
345
337
346
- elif type_code == bgp_cons .BGPTYPE_COMMUNITIES :
338
+ elif type_code == bgp_cons .BGPTYPE_COMMUNITIES :
347
339
348
- decode_value = Community .parse (value = attr_value )
340
+ decode_value = Community .parse (value = attr_value )
349
341
350
- elif type_code == bgp_cons .BGPTYPE_ORIGINATOR_ID :
342
+ elif type_code == bgp_cons .BGPTYPE_ORIGINATOR_ID :
351
343
352
- decode_value = OriginatorID .parse (value = attr_value )
344
+ decode_value = OriginatorID .parse (value = attr_value )
353
345
354
- elif type_code == bgp_cons .BGPTYPE_CLUSTER_LIST :
346
+ elif type_code == bgp_cons .BGPTYPE_CLUSTER_LIST :
355
347
356
- decode_value = ClusterList .parse (value = attr_value )
348
+ decode_value = ClusterList .parse (value = attr_value )
357
349
358
- elif type_code == bgp_cons .BGPTYPE_NEW_AS_PATH :
350
+ elif type_code == bgp_cons .BGPTYPE_NEW_AS_PATH :
359
351
360
- decode_value = ASPath .parse (value = attr_value , asn4 = True )
352
+ decode_value = ASPath .parse (value = attr_value , asn4 = True )
361
353
362
- elif type_code == bgp_cons .BGPTYPE_NEW_AGGREGATOR :
354
+ elif type_code == bgp_cons .BGPTYPE_NEW_AGGREGATOR :
363
355
364
- decode_value = Aggregator .parse (value = attr_value , asn4 = True )
356
+ decode_value = Aggregator .parse (value = attr_value , asn4 = True )
365
357
366
- elif type_code == bgp_cons .BGPTYPE_LARGE_COMMUNITY :
358
+ elif type_code == bgp_cons .BGPTYPE_LARGE_COMMUNITY :
367
359
368
- decode_value = LargeCommunity .parse (value = attr_value )
360
+ decode_value = LargeCommunity .parse (value = attr_value )
369
361
370
- elif type_code == bgp_cons .BGPTYPE_MP_REACH_NLRI :
371
- decode_value = MpReachNLRI .parse (value = attr_value )
372
- if decode_value ['nlri' ][0 ] and type (decode_value ['nlri' ][0 ]) is dict :
373
- if decode_value ['nlri' ][0 ].get ("protocol_id" ):
374
- bgpls_pro_id = decode_value ['nlri' ][0 ]["protocol_id" ]
362
+ elif type_code == bgp_cons .BGPTYPE_MP_REACH_NLRI :
363
+ decode_value = MpReachNLRI .parse (value = attr_value )
364
+ if decode_value ['nlri' ][0 ] and type (decode_value ['nlri' ][0 ]) is dict :
365
+ if decode_value ['nlri' ][0 ].get ("protocol_id" ):
366
+ bgpls_pro_id = decode_value ['nlri' ][0 ]["protocol_id" ]
375
367
376
- elif type_code == bgp_cons .BGPTYPE_MP_UNREACH_NLRI :
377
- decode_value = MpUnReachNLRI .parse (value = attr_value )
368
+ elif type_code == bgp_cons .BGPTYPE_MP_UNREACH_NLRI :
369
+ decode_value = MpUnReachNLRI .parse (value = attr_value )
378
370
379
- elif type_code == bgp_cons .BGPTYPE_EXTENDED_COMMUNITY :
380
- decode_value = ExtCommunity .parse (value = attr_value )
371
+ elif type_code == bgp_cons .BGPTYPE_EXTENDED_COMMUNITY :
372
+ decode_value = ExtCommunity .parse (value = attr_value )
381
373
382
- elif type_code == bgp_cons .BGPTYPE_PMSI_TUNNEL :
383
- decode_value = PMSITunnel .parse (value = attr_value )
384
- pmsi_hex = attr_value
374
+ elif type_code == bgp_cons .BGPTYPE_PMSI_TUNNEL :
375
+ decode_value = PMSITunnel .parse (value = attr_value )
376
+ pmsi_hex = attr_value
385
377
386
- elif type_code == bgp_cons .BGPTYPE_LINK_STATE :
387
- if bgpls_pro_id :
388
- attributes .update (LinkState .unpack (bgpls_pro_id = bgpls_pro_id , data = attr_value ).dict ())
389
- else :
390
- bgpls_attr = attr_value
391
- continue
378
+ elif type_code == bgp_cons .BGPTYPE_LINK_STATE :
379
+ if bgpls_pro_id :
380
+ attributes .update (LinkState .unpack (bgpls_pro_id = bgpls_pro_id , data = attr_value ).dict ())
381
+ else :
382
+ bgpls_attr = attr_value
383
+ continue
392
384
393
- elif type_code == bgp_cons .BGPTYPE_BGP_PREFIX_SID :
394
- decode_value = BGPPrefixSID .unpack (data = attr_value )
385
+ elif type_code == bgp_cons .BGPTYPE_BGP_PREFIX_SID :
386
+ decode_value = BGPPrefixSID .unpack (data = attr_value )
395
387
396
- else :
397
- decode_value = binascii .b2a_hex (attr_value ).decode ('utf-8' )
398
- attributes [type_code ] = decode_value
388
+ else :
389
+ decode_value = binascii .b2a_hex (attr_value ).decode ('utf-8' )
390
+ attributes [type_code ] = decode_value
399
391
400
- if bgpls_attr :
401
- attributes .update (LinkState .unpack (bgpls_pro_id = bgpls_pro_id , data = bgpls_attr ).dict ())
392
+ if bgpls_attr :
393
+ attributes .update (LinkState .unpack (bgpls_pro_id = bgpls_pro_id , data = bgpls_attr ).dict ())
402
394
403
- evpn_overlay = EVPN .signal_evpn_overlay (attributes )
404
- if evpn_overlay ['evpn' ] and evpn_overlay ['encap_ec' ]:
405
- if bgp_cons .BGPTYPE_PMSI_TUNNEL in attributes :
406
- attributes [bgp_cons .BGPTYPE_PMSI_TUNNEL ] = PMSITunnel .parse (value = pmsi_hex , evpn_overlay = evpn_overlay )
395
+ evpn_overlay = EVPN .signal_evpn_overlay (attributes )
396
+ if evpn_overlay ['evpn' ] and evpn_overlay ['encap_ec' ]:
397
+ if bgp_cons .BGPTYPE_PMSI_TUNNEL in attributes :
398
+ attributes [bgp_cons .BGPTYPE_PMSI_TUNNEL ] = PMSITunnel .parse (value = pmsi_hex ,
399
+ evpn_overlay = evpn_overlay )
400
+ except excep .UpdateMessageError as e :
401
+ raise excep .UpdateMessageError (
402
+ sub_error = e .sub_error ,
403
+ data = e .data ,
404
+ sub_results = attributes )
405
+ except Exception as e :
406
+ LOG .error (e )
407
+ error_str = traceback .format_exc ()
408
+ LOG .debug (error_str )
409
+ raise excep .UpdateMessageError (
410
+ sub_error = bgp_cons .ERR_MSG_UPDATE_MALFORMED_ATTR_LIST ,
411
+ data = '' ,
412
+ sub_results = attributes )
407
413
return attributes
408
414
409
415
@staticmethod
0 commit comments