def construct_attributes(attr_dict, asn4=False): """ construts BGP Update attirubte. :param attr_dict: bgp attribute dictionary :param asn4: support 4 bytes asn or not """ attr_raw_hex = b'' for type_code, value in attr_dict.items(): if type_code == bgp_cons.BGPTYPE_ORIGIN: origin_hex = Origin.construct(value=value) attr_raw_hex += origin_hex elif type_code == bgp_cons.BGPTYPE_AS_PATH: aspath_hex = ASPath.construct(value=value, asn4=asn4) attr_raw_hex += aspath_hex elif type_code == bgp_cons.BGPTYPE_NEXT_HOP: nexthop_hex = NextHop.construct(value=value) attr_raw_hex += nexthop_hex elif type_code == bgp_cons.BGPTYPE_MULTI_EXIT_DISC: med_hex = MED.construct(value=value) attr_raw_hex += med_hex elif type_code == bgp_cons.BGPTYPE_LOCAL_PREF: localpre_hex = LocalPreference.construct(value=value) attr_raw_hex += localpre_hex elif type_code == bgp_cons.BGPTYPE_ATOMIC_AGGREGATE: atomicaggregate_hex = AtomicAggregate.construct(value=value) attr_raw_hex += atomicaggregate_hex elif type_code == bgp_cons.BGPTYPE_AGGREGATOR: aggregator_hex = Aggregator.construct(value=value, asn4=asn4) attr_raw_hex += aggregator_hex elif type_code == bgp_cons.BGPTYPE_COMMUNITIES: community_hex = Community.construct(value=value) attr_raw_hex += community_hex elif type_code == bgp_cons.BGPTYPE_ORIGINATOR_ID: originatorid_hex = OriginatorID.construct(value=value) attr_raw_hex += originatorid_hex elif type_code == bgp_cons.BGPTYPE_CLUSTER_LIST: clusterlist_hex = ClusterList.construct(value=value) attr_raw_hex += clusterlist_hex elif type_code == bgp_cons.BGPTYPE_MP_REACH_NLRI: mpreach_hex = MpReachNLRI().construct(value=value) attr_raw_hex += mpreach_hex elif type_code == bgp_cons.BGPTYPE_MP_UNREACH_NLRI: mpunreach_hex = MpUnReachNLRI.construct(value=value) attr_raw_hex += mpunreach_hex elif type_code == bgp_cons.BGPTYPE_EXTENDED_COMMUNITY: community_ext_hex = ExtCommunity.construct(value=value) attr_raw_hex += community_ext_hex return attr_raw_hex