diff --git a/src/exabgp/bgp/message/__init__.py b/src/exabgp/bgp/message/__init__.py index 79576e459..4b51761c0 100644 --- a/src/exabgp/bgp/message/__init__.py +++ b/src/exabgp/bgp/message/__init__.py @@ -10,8 +10,7 @@ # Every Message should be imported from this file # as it makes sure that all the registering decorator are run -from exabgp.bgp.message.direction import OUT -from exabgp.bgp.message.direction import IN +from exabgp.bgp.message.action import Action from exabgp.bgp.message.message import Message from exabgp.bgp.message.nop import NOP diff --git a/src/exabgp/bgp/message/action.py b/src/exabgp/bgp/message/action.py new file mode 100644 index 000000000..c80087bf2 --- /dev/null +++ b/src/exabgp/bgp/message/action.py @@ -0,0 +1,19 @@ +# encoding: utf-8 +""" +action.py + +Created by Thomas Mangin on 2022-05-19. +Copyright (c) 2009-2017 Exa Networks. All rights reserved. +License: 3-clause BSD. (See the COPYRIGHT file) +""" + +# =================================================================== Direction +# + +from enum import IntEnum + + +class Action(IntEnum): + UNSET = 0x00 + ANNOUNCE = 0x01 + WITHDRAW = 0x02 diff --git a/src/exabgp/bgp/message/direction.py b/src/exabgp/bgp/message/direction.py index 200365606..29bb4538e 100644 --- a/src/exabgp/bgp/message/direction.py +++ b/src/exabgp/bgp/message/direction.py @@ -13,18 +13,6 @@ from enum import Enum -class OUT(object): - UNSET = 0x00 - ANNOUNCE = 0x01 - WITHDRAW = 0x02 - - -class IN(object): - UNSET = 0x00 - ANNOUNCED = 0x01 - WITHDRAWN = 0x02 - - class Direction(Enum): IN = 1 OUT = 2 diff --git a/src/exabgp/bgp/message/update/__init__.py b/src/exabgp/bgp/message/update/__init__.py index 625ac0057..68883ede2 100644 --- a/src/exabgp/bgp/message/update/__init__.py +++ b/src/exabgp/bgp/message/update/__init__.py @@ -14,8 +14,7 @@ from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI -from exabgp.bgp.message.direction import IN -from exabgp.bgp.message.direction import OUT +from exabgp.bgp.message.action import Action from exabgp.bgp.message.direction import Direction from exabgp.bgp.message.message import Message @@ -119,13 +118,13 @@ def messages(self, negotiated, include_withdraw=True): add_v4 = nlri.afi == AFI.ipv4 add_v4 = add_v4 and nlri.safi in [SAFI.unicast, SAFI.multicast] - del_v4 = add_v4 and nlri.action == OUT.WITHDRAW + del_v4 = add_v4 and nlri.action == Action.WITHDRAW if del_v4: nlris.append(nlri) continue - add_v4 = add_v4 and nlri.action == OUT.ANNOUNCE + add_v4 = add_v4 and nlri.action == Action.ANNOUNCE add_v4 = add_v4 and nlri.nexthop.afi == AFI.ipv4 if add_v4: @@ -158,7 +157,7 @@ def messages(self, negotiated, include_withdraw=True): afi, safi = family if safi not in (SAFI.unicast, SAFI.multicast): break - if set(actions.keys()) != {OUT.WITHDRAW}: + if set(actions.keys()) != {Action.WITHDRAW}: break # no break else: @@ -184,7 +183,7 @@ def messages(self, negotiated, include_withdraw=True): for nlri in nlris: packed = nlri.pack(negotiated) if len(announced + withdraws + packed) <= msg_size: - if nlri.action == OUT.ANNOUNCE: + if nlri.action == Action.ANNOUNCE: announced += packed elif include_withdraw: withdraws += packed @@ -200,7 +199,7 @@ def messages(self, negotiated, include_withdraw=True): else: yield self._message(Update.prefix(withdraws) + Update.prefix(b'') + announced) - if nlri.action == OUT.ANNOUNCE: + if nlri.action == Action.ANNOUNCE: announced = packed withdraws = b'' elif include_withdraw: @@ -220,8 +219,8 @@ def messages(self, negotiated, include_withdraw=True): afi, safi = family mp_reach = b'' mp_unreach = b'' - mp_announce = MPRNLRI(afi, safi, mp_nlris[family].get(OUT.ANNOUNCE, [])) - mp_withdraw = MPURNLRI(afi, safi, mp_nlris[family].get(OUT.WITHDRAW, [])) + mp_announce = MPRNLRI(afi, safi, mp_nlris[family].get(Action.ANNOUNCE, [])) + mp_withdraw = MPURNLRI(afi, safi, mp_nlris[family].get(Action.WITHDRAW, [])) for mprnlri in mp_announce.packed_attributes(negotiated, msg_size - len(withdraws + announced)): if mp_reach: @@ -286,13 +285,13 @@ def unpack_message(cls, data, direction, negotiated): nlris = [] while withdrawn: - nlri, left = NLRI.unpack_nlri(AFI.ipv4, SAFI.unicast, withdrawn, IN.WITHDRAWN, addpath) + nlri, left = NLRI.unpack_nlri(AFI.ipv4, SAFI.unicast, withdrawn, Action.WITHDRAW, addpath) log.debug('withdrawn NLRI %s' % nlri, 'routes') withdrawn = left nlris.append(nlri) while announced: - nlri, left = NLRI.unpack_nlri(AFI.ipv4, SAFI.unicast, announced, IN.ANNOUNCED, addpath) + nlri, left = NLRI.unpack_nlri(AFI.ipv4, SAFI.unicast, announced, Action.ANNOUNCE, addpath) nlri.nexthop = nexthop log.debug('announced NLRI %s' % nlri, 'routes') announced = left diff --git a/src/exabgp/bgp/message/update/attribute/mprnlri.py b/src/exabgp/bgp/message/update/attribute/mprnlri.py index 5d9e90c41..b4ee87f4f 100644 --- a/src/exabgp/bgp/message/update/attribute/mprnlri.py +++ b/src/exabgp/bgp/message/update/attribute/mprnlri.py @@ -14,7 +14,7 @@ from exabgp.protocol.family import SAFI from exabgp.protocol.family import Family -from exabgp.bgp.message.direction import IN +from exabgp.bgp.message.action import Action from exabgp.bgp.message.direction import Direction # from exabgp.bgp.message.update.attribute.attribute import Attribute @@ -182,13 +182,13 @@ def unpack(cls, data, direction, negotiated): while data: if nexthops: for nexthop in nexthops: - nlri, left = NLRI.unpack_nlri(afi, safi, data, IN.ANNOUNCED, addpath) + nlri, left = NLRI.unpack_nlri(afi, safi, data, Action.ANNOUNCE, addpath) # allow unpack_nlri to return none for "treat as withdraw" controlled by NLRI.unpack_nlri if nlri: nlri.nexthop = NextHop.unpack(nexthop) nlris.append(nlri) else: - nlri, left = NLRI.unpack_nlri(afi, safi, data, IN.ANNOUNCED, addpath) + nlri, left = NLRI.unpack_nlri(afi, safi, data, Action.ANNOUNCE, addpath) # allow unpack_nlri to return none for "treat as withdraw" controlled by NLRI.unpack_nlri if nlri: nlris.append(nlri) diff --git a/src/exabgp/bgp/message/update/attribute/mpurnlri.py b/src/exabgp/bgp/message/update/attribute/mpurnlri.py index fffa13b6d..86937af0b 100644 --- a/src/exabgp/bgp/message/update/attribute/mpurnlri.py +++ b/src/exabgp/bgp/message/update/attribute/mpurnlri.py @@ -13,7 +13,6 @@ from exabgp.protocol.family import SAFI from exabgp.protocol.family import Family -from exabgp.bgp.message.direction import IN from exabgp.bgp.message.direction import Direction from exabgp.bgp.message.update.attribute.attribute import Attribute from exabgp.bgp.message.update.nlri import NLRI @@ -94,7 +93,7 @@ def unpack(cls, data, direction, negotiated): addpath = negotiated.addpath.send(afi, safi) while data: - nlri, data = NLRI.unpack_nlri(afi, safi, data, IN.WITHDRAWN, addpath) + nlri, data = NLRI.unpack_nlri(afi, safi, data, Action.WITHDRAW, addpath) # allow unpack_nlri to return none for "treat as withdraw" controlled by NLRI.unpack_nlri if nlri: nlris.append(nlri) diff --git a/src/exabgp/bgp/message/update/nlri/bgpls/nlri.py b/src/exabgp/bgp/message/update/nlri/bgpls/nlri.py index 4aa155a28..88954c506 100644 --- a/src/exabgp/bgp/message/update/nlri/bgpls/nlri.py +++ b/src/exabgp/bgp/message/update/nlri/bgpls/nlri.py @@ -12,7 +12,7 @@ from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.bgp.message.update.nlri import NLRI from exabgp.bgp.message.update.nlri.qualifier import RouteDistinguisher @@ -85,7 +85,7 @@ class BGPLS(NLRI): NAME = 'Unknown' SHORT_NAME = 'unknown' - def __init__(self, action=OUT.UNSET, addpath=None): + def __init__(self, action=Action.UNSET, addpath=None): NLRI.__init__(self, AFI.bgpls, SAFI.bgp_ls, action) self._packed = b'' diff --git a/src/exabgp/bgp/message/update/nlri/evpn/nlri.py b/src/exabgp/bgp/message/update/nlri/evpn/nlri.py index 5565a6679..79972801c 100644 --- a/src/exabgp/bgp/message/update/nlri/evpn/nlri.py +++ b/src/exabgp/bgp/message/update/nlri/evpn/nlri.py @@ -11,7 +11,7 @@ from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.bgp.message.update.nlri import NLRI @@ -37,7 +37,7 @@ class EVPN(NLRI): NAME = 'Unknown' SHORT_NAME = 'unknown' - def __init__(self, action=OUT.UNSET, addpath=None): + def __init__(self, action=Action.UNSET, addpath=None): NLRI.__init__(self, AFI.l2vpn, SAFI.evpn, action) self._packed = b'' @@ -60,7 +60,7 @@ def __repr__(self): return str(self) def feedback(self, action): - # if self.nexthop is None and action == OUT.ANNOUNCE: + # if self.nexthop is None and action == Action.ANNOUNCE: # return 'evpn nlri next-hop is missing' return '' diff --git a/src/exabgp/bgp/message/update/nlri/flow.py b/src/exabgp/bgp/message/update/nlri/flow.py index 08b1c3625..9205c5d72 100644 --- a/src/exabgp/bgp/message/update/nlri/flow.py +++ b/src/exabgp/bgp/message/update/nlri/flow.py @@ -13,7 +13,7 @@ from exabgp.protocol.ip.port import Port from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI -from exabgp.bgp.message.direction import OUT +from exabgp.bgp.message.action import Action from exabgp.bgp.message.notification import Notify from exabgp.bgp.message.update.nlri.cidr import CIDR @@ -527,14 +527,14 @@ class FlowFlowLabel(IOperationByteShortLong, NumericString, IPv6): @NLRI.register(AFI.ipv4, SAFI.flow_vpn) @NLRI.register(AFI.ipv6, SAFI.flow_vpn) class Flow(NLRI): - def __init__(self, afi=AFI.ipv4, safi=SAFI.flow_ip, action=OUT.UNSET): + def __init__(self, afi=AFI.ipv4, safi=SAFI.flow_ip, action=Action.UNSET): NLRI.__init__(self, afi, safi, action) self.rules = {} self.nexthop = NoNextHop self.rd = RouteDistinguisher.NORD def feedback(self, action): - if self.nexthop is None and action == OUT.ANNOUNCE: + if self.nexthop is None and action == Action.ANNOUNCE: return 'flow nlri next-hop missing' return '' diff --git a/src/exabgp/bgp/message/update/nlri/inet.py b/src/exabgp/bgp/message/update/nlri/inet.py index 90497ea9d..6abda6459 100644 --- a/src/exabgp/bgp/message/update/nlri/inet.py +++ b/src/exabgp/bgp/message/update/nlri/inet.py @@ -14,8 +14,7 @@ from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI from exabgp.protocol.family import Family -from exabgp.bgp.message import IN -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.bgp.message.update.nlri.nlri import NLRI from exabgp.bgp.message.update.nlri.cidr import CIDR from exabgp.bgp.message.update.nlri.qualifier import Labels @@ -29,7 +28,7 @@ @NLRI.register(AFI.ipv4, SAFI.multicast) @NLRI.register(AFI.ipv6, SAFI.multicast) class INET(NLRI): - def __init__(self, afi, safi, action=OUT.UNSET): + def __init__(self, afi, safi, action=Action.UNSET): NLRI.__init__(self, afi, safi, action) self.path_info = PathInfo.NOPATH self.cidr = CIDR.NOCIDR @@ -45,7 +44,7 @@ def __repr__(self): return self.extensive() def feedback(self, action): - if self.nexthop is None and action == OUT.ANNOUNCE: + if self.nexthop is None and action == Action.ANNOUNCE: return 'inet nlri next-hop missing' return '' @@ -115,7 +114,7 @@ def unpack_nlri(cls, afi, safi, bgp, action, addpath): # The last bit is set for the last label labels.append(label >> 4) # This is a route withdrawal - if label == 0x800000 and action == IN.WITHDRAWN: + if label == 0x800000 and action == Action.WITHDRAW: break # This is a next-hop if label == 0x000000: diff --git a/src/exabgp/bgp/message/update/nlri/ipvpn.py b/src/exabgp/bgp/message/update/nlri/ipvpn.py index bb0af5398..62e9dbb74 100644 --- a/src/exabgp/bgp/message/update/nlri/ipvpn.py +++ b/src/exabgp/bgp/message/update/nlri/ipvpn.py @@ -10,7 +10,7 @@ from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.bgp.message.update.nlri.nlri import NLRI from exabgp.bgp.message.update.nlri.cidr import CIDR @@ -30,17 +30,17 @@ @NLRI.register(AFI.ipv4, SAFI.mpls_vpn) @NLRI.register(AFI.ipv6, SAFI.mpls_vpn) class IPVPN(Label): - def __init__(self, afi, safi, action=OUT.UNSET): + def __init__(self, afi, safi, action=Action.UNSET): Label.__init__(self, afi, safi, action) self.rd = RouteDistinguisher.NORD def feedback(self, action): - if self.nexthop is None and action == OUT.ANNOUNCE: + if self.nexthop is None and action == Action.ANNOUNCE: return 'ip-vpn nlri next-hop missing' return '' @classmethod - def new(cls, afi, safi, packed, mask, labels, rd, nexthop=None, action=OUT.UNSET): + def new(cls, afi, safi, packed, mask, labels, rd, nexthop=None, action=Action.UNSET): instance = cls(afi, safi, action) instance.cidr = CIDR(packed, mask) instance.labels = labels diff --git a/src/exabgp/bgp/message/update/nlri/label.py b/src/exabgp/bgp/message/update/nlri/label.py index 5bcd2dc1b..02b5c36a4 100644 --- a/src/exabgp/bgp/message/update/nlri/label.py +++ b/src/exabgp/bgp/message/update/nlri/label.py @@ -11,7 +11,7 @@ from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI from exabgp.protocol.family import Family -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.bgp.message.update.nlri.nlri import NLRI from exabgp.bgp.message.update.nlri.inet import INET from exabgp.bgp.message.update.nlri.qualifier import PathInfo @@ -30,7 +30,7 @@ def __init__(self, afi, safi, action): self.labels = Labels.NOLABEL def feedback(self, action): - if self.nexthop is None and action == OUT.ANNOUNCE: + if self.nexthop is None and action == Action.ANNOUNCE: return 'labelled nlri next-hop missing' return '' @@ -84,7 +84,7 @@ def _internal(self, announced=True): # # The last bit is set for the last label # labels.append(label >> 4) # # This is a route withdrawal - # if label == 0x800000 and action == IN.WITHDRAWN: + # if label == 0x800000 and action == Action.WITHDRAW: # break # # This is a next-hop # if label == 0x000000: diff --git a/src/exabgp/bgp/message/update/nlri/nlri.py b/src/exabgp/bgp/message/update/nlri/nlri.py index 2f6f2a150..6f007fab5 100644 --- a/src/exabgp/bgp/message/update/nlri/nlri.py +++ b/src/exabgp/bgp/message/update/nlri/nlri.py @@ -10,7 +10,7 @@ from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI from exabgp.protocol.family import Family -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.bgp.message.notification import Notify from exabgp.logger import log @@ -24,7 +24,7 @@ class NLRI(Family): registered_nlri = dict() registered_families = [(AFI.ipv4, SAFI.multicast)] - def __init__(self, afi, safi, action=OUT.UNSET): + def __init__(self, afi, safi, action=Action.UNSET): Family.__init__(self, afi, safi) self.action = action diff --git a/src/exabgp/bgp/message/update/nlri/rtc.py b/src/exabgp/bgp/message/update/nlri/rtc.py index dcb55dd6e..7193f87ae 100644 --- a/src/exabgp/bgp/message/update/nlri/rtc.py +++ b/src/exabgp/bgp/message/update/nlri/rtc.py @@ -19,7 +19,7 @@ from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.bgp.message.update.nlri.nlri import NLRI @@ -36,12 +36,12 @@ def __init__(self, afi, safi, action, origin, rt): self.nexthop = NoNextHop def feedback(self, action): - if self.nexthop is None and action == OUT.ANNOUNCE: + if self.nexthop is None and action == Action.ANNOUNCE: return 'rtc nlri next-hop missing' return '' @classmethod - def new(cls, afi, safi, origin, rt, nexthop=NoNextHop, action=OUT.UNSET): + def new(cls, afi, safi, origin, rt, nexthop=NoNextHop, action=Action.UNSET): instance = cls(afi, safi, action, origin, rt) instance.origin = origin instance.rt = rt diff --git a/src/exabgp/bgp/message/update/nlri/vpls.py b/src/exabgp/bgp/message/update/nlri/vpls.py index d9579ce8b..af263223f 100644 --- a/src/exabgp/bgp/message/update/nlri/vpls.py +++ b/src/exabgp/bgp/message/update/nlri/vpls.py @@ -12,7 +12,7 @@ from struct import pack from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI -from exabgp.bgp.message.direction import OUT +from exabgp.bgp.message.action import Action from exabgp.bgp.message.notification import Notify from exabgp.bgp.message.update.nlri.nlri import NLRI from exabgp.bgp.message.update.nlri.qualifier import RouteDistinguisher @@ -34,7 +34,7 @@ class VPLS(NLRI): # XXX: Should take AFI, SAFI and OUT.direction as parameter to match other NLRI def __init__(self, rd, endpoint, base, offset, size): NLRI.__init__(self, AFI.l2vpn, SAFI.vpls) - self.action = OUT.ANNOUNCE + self.action = Action.ANNOUNCE self.nexthop = None self.rd = rd self.base = base @@ -44,7 +44,7 @@ def __init__(self, rd, endpoint, base, offset, size): self.unique = next(unique) def feedback(self, action): - if self.nexthop is None and action == OUT.ANNOUNCE: + if self.nexthop is None and action == Action.ANNOUNCE: return 'vpls nlri next-hop missing' if self.endpoint is None: return 'vpls nlri endpoint missing' diff --git a/src/exabgp/configuration/announce/__init__.py b/src/exabgp/configuration/announce/__init__.py index f1f3771b7..6993c4549 100644 --- a/src/exabgp/configuration/announce/__init__.py +++ b/src/exabgp/configuration/announce/__init__.py @@ -8,7 +8,7 @@ from exabgp.protocol.ip import IP -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.rib.change import Change from exabgp.protocol.family import AFI @@ -73,7 +73,7 @@ def split(last): # generate the new routes for _ in range(number): # update ip to the next route, this recalculate the "ip" field of the Inet class - nlri = klass(afi, safi, OUT.ANNOUNCE) + nlri = klass(afi, safi, Action.ANNOUNCE) nlri.cidr = CIDR(pack_int(afi, ip), cut) nlri.nexthop = nexthop # nexthop can be NextHopSelf nlri.path_info = path_info diff --git a/src/exabgp/configuration/announce/flow.py b/src/exabgp/configuration/announce/flow.py index 0f17afde9..de1a2e0ae 100644 --- a/src/exabgp/configuration/announce/flow.py +++ b/src/exabgp/configuration/announce/flow.py @@ -9,7 +9,7 @@ from exabgp.rib.change import Change -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI @@ -181,7 +181,7 @@ def check(self): def flow(tokeniser, afi, safi): - change = Change(Flow(afi, safi, OUT.ANNOUNCE), Attributes()) + change = Change(Flow(afi, safi, Action.ANNOUNCE), Attributes()) while True: command = tokeniser() diff --git a/src/exabgp/configuration/announce/ip.py b/src/exabgp/configuration/announce/ip.py index 21ef8f326..d898caba5 100644 --- a/src/exabgp/configuration/announce/ip.py +++ b/src/exabgp/configuration/announce/ip.py @@ -11,7 +11,7 @@ from exabgp.rib.change import Change -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI @@ -132,7 +132,7 @@ def post(self): @staticmethod def check(change, afi): if ( - change.nlri.action == OUT.ANNOUNCE + change.nlri.action == Action.ANNOUNCE and change.nlri.nexthop is NoNextHop and change.nlri.afi == afi and change.nlri.safi in (SAFI.unicast, SAFI.multicast) @@ -143,7 +143,7 @@ def check(change, afi): def ip(tokeniser, afi, safi): - action = OUT.ANNOUNCE if tokeniser.announce else OUT.WITHDRAW + action = Action.ANNOUNCE if tokeniser.announce else Action.WITHDRAW ipmask = prefix(tokeniser) nlri = INET(afi, safi, action) @@ -177,7 +177,7 @@ def ip(tokeniser, afi, safi): def ip_multicast(tokeniser, afi, safi): - action = OUT.ANNOUNCE if tokeniser.announce else OUT.WITHDRAW + action = Action.ANNOUNCE if tokeniser.announce else Action.WITHDRAW ipmask = prefix(tokeniser) nlri = INET(afi, safi, action) diff --git a/src/exabgp/configuration/announce/label.py b/src/exabgp/configuration/announce/label.py index 5d01dfe5d..bcdb2a915 100644 --- a/src/exabgp/configuration/announce/label.py +++ b/src/exabgp/configuration/announce/label.py @@ -11,7 +11,7 @@ from exabgp.rib.change import Change -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI @@ -71,14 +71,14 @@ def check(change, afi): if not AnnouncePath.check(change, afi): return False - if change.nlri.action == OUT.ANNOUNCE and change.nlri.has_label() and change.nlri.labels is Labels.NOLABEL: + if change.nlri.action == Action.ANNOUNCE and change.nlri.has_label() and change.nlri.labels is Labels.NOLABEL: return False return True def ip_label(tokeniser, afi, safi): - action = OUT.ANNOUNCE if tokeniser.announce else OUT.WITHDRAW + action = Action.ANNOUNCE if tokeniser.announce else Action.WITHDRAW ipmask = prefix(tokeniser) nlri = Label(afi, safi, action) diff --git a/src/exabgp/configuration/announce/path.py b/src/exabgp/configuration/announce/path.py index ed466d0a3..0a17d8b3f 100644 --- a/src/exabgp/configuration/announce/path.py +++ b/src/exabgp/configuration/announce/path.py @@ -11,7 +11,7 @@ from exabgp.rib.change import Change -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI @@ -74,7 +74,7 @@ def check(change, afi): def ip_unicast(tokeniser, afi, safi): - action = OUT.ANNOUNCE if tokeniser.announce else OUT.WITHDRAW + action = Action.ANNOUNCE if tokeniser.announce else Action.WITHDRAW ipmask = prefix(tokeniser) nlri = INET(afi, safi, action) diff --git a/src/exabgp/configuration/announce/vpls.py b/src/exabgp/configuration/announce/vpls.py index 464b66087..8db709509 100644 --- a/src/exabgp/configuration/announce/vpls.py +++ b/src/exabgp/configuration/announce/vpls.py @@ -9,7 +9,7 @@ from exabgp.rib.change import Change -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI diff --git a/src/exabgp/configuration/announce/vpn.py b/src/exabgp/configuration/announce/vpn.py index 0fd52f93a..75ed525c4 100644 --- a/src/exabgp/configuration/announce/vpn.py +++ b/src/exabgp/configuration/announce/vpn.py @@ -11,7 +11,7 @@ from exabgp.rib.change import Change -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI @@ -71,14 +71,14 @@ def check(change, afi): if not AnnounceLabel.check(change, afi): return False - if change.nlri.action == OUT.ANNOUNCE and change.nlri.has_rd() and change.nlri.rd is RouteDistinguisher.NORD: + if change.nlri.action == Action.ANNOUNCE and change.nlri.has_rd() and change.nlri.rd is RouteDistinguisher.NORD: return False return True def ip_vpn(tokeniser, afi, safi): - action = OUT.ANNOUNCE if tokeniser.announce else OUT.WITHDRAW + action = Action.ANNOUNCE if tokeniser.announce else Action.WITHDRAW ipmask = prefix(tokeniser) nlri = IPVPN(afi, safi, action) diff --git a/src/exabgp/configuration/check.py b/src/exabgp/configuration/check.py index 39b4cbd76..044058826 100644 --- a/src/exabgp/configuration/check.py +++ b/src/exabgp/configuration/check.py @@ -28,7 +28,7 @@ from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI -from exabgp.bgp.message.direction import IN +from exabgp.bgp.message.action import Action from exabgp.bgp.message.direction import Direction from exabgp.logger import log @@ -227,7 +227,7 @@ def check_nlri(neighbor, routes): try: while announced: log.debug('parsing NLRI %s' % announced, 'parser') - nlri, announced = NLRI.unpack_nlri(afi, safi, announced, IN.ANNOUNCED, addpath) + nlri, announced = NLRI.unpack_nlri(afi, safi, announced, Action.ANNOUNCE, addpath) nlris.append(nlri) except Exception as exc: log.error('could not parse the nlri', 'parser') diff --git a/src/exabgp/configuration/neighbor/__init__.py b/src/exabgp/configuration/neighbor/__init__.py index 5e4817069..1eaa8f614 100644 --- a/src/exabgp/configuration/neighbor/__init__.py +++ b/src/exabgp/configuration/neighbor/__init__.py @@ -18,7 +18,7 @@ from exabgp.bgp.neighbor import Neighbor -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action # from exabgp.bgp.message.open.asn import ASN from exabgp.bgp.message.open.holdtime import HoldTime @@ -226,12 +226,12 @@ def post(self): for section in ('static', 'l2vpn', 'flow'): routes = local.get(section, {}).get('routes', []) for route in routes: - route.nlri.action = OUT.ANNOUNCE + route.nlri.action = Action.ANNOUNCE neighbor.changes.extend(routes) routes = local.get('routes', []) for route in routes: - route.nlri.action = OUT.ANNOUNCE + route.nlri.action = Action.ANNOUNCE neighbor.changes.extend(routes) messages = local.get('operational', {}).get('routes', []) diff --git a/src/exabgp/configuration/static/__init__.py b/src/exabgp/configuration/static/__init__.py index cd46460dc..c008684d3 100644 --- a/src/exabgp/configuration/static/__init__.py +++ b/src/exabgp/configuration/static/__init__.py @@ -17,7 +17,7 @@ from exabgp.protocol.ip import IP from exabgp.protocol.family import SAFI -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.bgp.message.update.nlri import CIDR from exabgp.bgp.message.update.nlri import INET from exabgp.bgp.message.update.nlri import Label @@ -53,7 +53,7 @@ def post(self): @ParseStatic.register('route', 'append-route') def route(tokeniser): - action = OUT.ANNOUNCE if tokeniser.announce else OUT.WITHDRAW + action = Action.ANNOUNCE if tokeniser.announce else Action.WITHDRAW ipmask = prefix(tokeniser) check = lambda change, afi: True @@ -106,7 +106,7 @@ def route(tokeniser): @ParseStatic.register('attributes', 'append-route') def attributes(tokeniser): - action = OUT.ANNOUNCE if tokeniser.announce else OUT.WITHDRAW + action = Action.ANNOUNCE if tokeniser.announce else Action.WITHDRAW ipmask = prefix(lambda: tokeniser.tokens[-1]) tokeniser.afi = ipmask.afi @@ -160,7 +160,7 @@ def attributes(tokeniser): break ipmask = prefix(tokeniser) - new = Change(nlri.__class__(nlri.afi, nlri.safi, OUT.UNSET), attr) + new = Change(nlri.__class__(nlri.afi, nlri.safi, Action.UNSET), attr) new.nlri.cidr = CIDR(ipmask.pack(), ipmask.mask) if labels: new.nlri.labels = labels diff --git a/src/exabgp/configuration/static/parser.py b/src/exabgp/configuration/static/parser.py index 8f4ffc694..bef5c19c3 100644 --- a/src/exabgp/configuration/static/parser.py +++ b/src/exabgp/configuration/static/parser.py @@ -17,7 +17,7 @@ # from exabgp.protocol.family import SAFI -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.bgp.message.update.nlri import CIDR from exabgp.bgp.message.update.nlri import INET from exabgp.bgp.message.update.nlri import IPVPN @@ -94,25 +94,25 @@ def next_hop(tokeniser): return ip, NextHop(ip.top()) -# XXX: using OUT.UNSET should we use the following ? -# action = OUT.ANNOUNCE if tokeniser.announce else OUT.WITHDRAW +# XXX: using Action.UNSET should we use the following ? +# action = Action.ANNOUNCE if tokeniser.announce else Action.WITHDRAW def inet(tokeniser): ipmask = prefix(tokeniser) - inet = INET(afi=IP.toafi(ipmask.top()), safi=IP.tosafi(ipmask.top()), action=OUT.UNSET) + inet = INET(afi=IP.toafi(ipmask.top()), safi=IP.tosafi(ipmask.top()), action=Action.UNSET) inet.cidr = CIDR(ipmask.ton(), ipmask.mask) return Change(inet, Attributes()) -# XXX: using OUT.ANNOUNCE should we use the following ? -# action = OUT.ANNOUNCE if tokeniser.announce else OUT.WITHDRAW +# XXX: using Action.ANNOUNCE should we use the following ? +# action = Action.ANNOUNCE if tokeniser.announce else Action.WITHDRAW def mpls(tokeniser): ipmask = prefix(tokeniser) - mpls = IPVPN(afi=IP.toafi(ipmask.top()), safi=IP.tosafi(ipmask.top()), action=OUT.ANNOUNCE) + mpls = IPVPN(afi=IP.toafi(ipmask.top()), safi=IP.tosafi(ipmask.top()), action=Action.ANNOUNCE) mpls.cidr = CIDR(ipmask.ton(), ipmask.mask) return Change(mpls, Attributes()) diff --git a/src/exabgp/configuration/static/route.py b/src/exabgp/configuration/static/route.py index 649e00b6e..d9045b187 100644 --- a/src/exabgp/configuration/static/route.py +++ b/src/exabgp/configuration/static/route.py @@ -12,7 +12,7 @@ from exabgp.protocol.ip import IP from exabgp.protocol.ip import NoNextHop -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI @@ -181,7 +181,7 @@ def _check(self): def check(change): if ( change.nlri.nexthop is NoNextHop - and change.nlri.action == OUT.ANNOUNCE + and change.nlri.action == Action.ANNOUNCE and change.nlri.afi == AFI.ipv4 and change.nlri.safi in (SAFI.unicast, SAFI.multicast) ): @@ -232,7 +232,7 @@ def split(last): # generate the new routes for _ in range(number): # update ip to the next route, this recalculate the "ip" field of the Inet class - nlri = klass(afi, safi, OUT.ANNOUNCE) + nlri = klass(afi, safi, Action.ANNOUNCE) nlri.cidr = CIDR(pack_int(afi, ip), cut) nlri.nexthop = nexthop # nexthop can be NextHopSelf if safi.has_path(): diff --git a/src/exabgp/reactor/api/command/announce.py b/src/exabgp/reactor/api/command/announce.py index 3c9091d2d..05f6ebc93 100644 --- a/src/exabgp/reactor/api/command/announce.py +++ b/src/exabgp/reactor/api/command/announce.py @@ -12,7 +12,7 @@ from exabgp.reactor.api.command.limit import extract_neighbors from exabgp.protocol.ip import NoNextHop -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.bgp.message.update.attribute import NextHop from exabgp.configuration.static import ParseStaticRoute @@ -47,7 +47,7 @@ def callback(): 'invalid route for %s : %s' % (', '.join(peers) if peers else 'all peers', change.extensive()) ) continue - change.nlri.action = OUT.ANNOUNCE + change.nlri.action = Action.ANNOUNCE reactor.configuration.inject_change(peers, change) self.log_message( 'route added to %s : %s' % (', '.join(peers) if peers else 'all peers', change.extensive()) @@ -89,7 +89,7 @@ def callback(): for change in changes: # Change the action to withdraw before checking the route - change.nlri.action = OUT.WITHDRAW + change.nlri.action = Action.WITHDRAW # NextHop is a mandatory field (but we do not require in) if change.nlri.nexthop is NoNextHop: change.nlri.nexthop = NextHop('0.0.0.0') @@ -144,7 +144,7 @@ def callback(): return for change in changes: - change.nlri.action = OUT.ANNOUNCE + change.nlri.action = Action.ANNOUNCE reactor.configuration.inject_change(peers, change) self.log_message( 'vpls added to %s : %s' % (', '.join(peers) if peers else 'all peers', change.extensive()) @@ -186,7 +186,7 @@ def callback(): return for change in changes: - change.nlri.action = OUT.WITHDRAW + change.nlri.action = Action.WITHDRAW if reactor.configuration.inject_change(peers, change): self.log_message( 'vpls removed from %s : %s' % (', '.join(peers) if peers else 'all peers', change.extensive()) @@ -233,7 +233,7 @@ def callback(): return for change in changes: - change.nlri.action = OUT.ANNOUNCE + change.nlri.action = Action.ANNOUNCE reactor.configuration.inject_change(peers, change) self.log_message( 'route added to %s : %s' % (', '.join(peers) if peers else 'all peers', change.extensive()) @@ -274,7 +274,7 @@ def callback(): return for change in changes: - change.nlri.action = OUT.WITHDRAW + change.nlri.action = Action.WITHDRAW if reactor.configuration.inject_change(peers, change): self.log_message( 'route removed from %s : %s' % (', '.join(peers) if peers else 'all peers', change.extensive()) @@ -320,7 +320,7 @@ def callback(): return for change in changes: - change.nlri.action = OUT.ANNOUNCE + change.nlri.action = Action.ANNOUNCE reactor.configuration.inject_change(peers, change) self.log_message( 'flow added to %s : %s' % (', '.join(peers) if peers else 'all peers', change.extensive()) @@ -362,7 +362,7 @@ def callback(): return for change in changes: - change.nlri.action = OUT.WITHDRAW + change.nlri.action = Action.WITHDRAW if reactor.configuration.inject_change(peers, change): self.log_message( 'flow removed from %s : %s' % (', '.join(peers) if peers else 'all peers', change.extensive()) diff --git a/src/exabgp/reactor/api/response/json.py b/src/exabgp/reactor/api/response/json.py index a11d98737..898e1b596 100755 --- a/src/exabgp/reactor/api/response/json.py +++ b/src/exabgp/reactor/api/response/json.py @@ -15,7 +15,7 @@ from exabgp.util import hexstring from exabgp.bgp.message import Message -from exabgp.bgp.message import IN +from exabgp.bgp.message import Action from exabgp.environment import getenv from exabgp.bgp.message.open.capability.refresh import REFRESH @@ -322,9 +322,9 @@ def _update(self, update): for nlri in update.nlris: nexthop = str(nlri.nexthop) if nlri.nexthop else 'null' - if nlri.action == IN.ANNOUNCED: # pylint: disable=E1101 + if nlri.action == Action.ANNOUNCE: # pylint: disable=E1101 plus.setdefault(nlri.family(), {}).setdefault(nexthop, []).append(nlri) - if nlri.action == IN.WITHDRAWN: # pylint: disable=E1101 + if nlri.action == Action.WITHDRAW: # pylint: disable=E1101 minus.setdefault(nlri.family(), []).append(nlri) add = [] diff --git a/src/exabgp/reactor/api/response/text.py b/src/exabgp/reactor/api/response/text.py index 04f998795..53e23dcce 100755 --- a/src/exabgp/reactor/api/response/text.py +++ b/src/exabgp/reactor/api/response/text.py @@ -11,7 +11,7 @@ import os from exabgp.util import hexstring -from exabgp.bgp.message import IN +from exabgp.bgp.message import Action class Text(object): @@ -95,7 +95,7 @@ def update(self, neighbor, direction, update, negotiated, header, body): for nlri in update.nlris: if nlri.EOR: r += '%s route %s\n' % (prefix, nlri.extensive()) - elif nlri.action == IN.ANNOUNCED: # pylint: disable=E1101 + elif nlri.action == Action.ANNOUNCE: # pylint: disable=E1101 if nlri.nexthop: r += '%s announced %s%s\n' % (prefix, nlri.extensive(), attributes) else: diff --git a/src/exabgp/reactor/protocol.py b/src/exabgp/reactor/protocol.py index 4cdb85828..73a623e4c 100644 --- a/src/exabgp/reactor/protocol.py +++ b/src/exabgp/reactor/protocol.py @@ -34,7 +34,7 @@ from exabgp.bgp.message import Notify from exabgp.bgp.message import Operational -from exabgp.bgp.message.direction import IN +from exabgp.bgp.message.action import Action from exabgp.bgp.message.direction import Direction from exabgp.bgp.message.update.attribute import Attribute @@ -235,7 +235,7 @@ def read_message(self): if message.TYPE == Update.TYPE: if Attribute.CODE.INTERNAL_TREAT_AS_WITHDRAW in message.attributes: for nlri in message.nlris: - nlri.action = IN.WITHDRAWN + nlri.action = Action.WITHDRAW if for_api: negotiated = self.negotiated if self.neighbor.api.get('negotiated', False) else None diff --git a/src/exabgp/rib/cache.py b/src/exabgp/rib/cache.py index eb0ba60f1..ddddf481e 100644 --- a/src/exabgp/rib/cache.py +++ b/src/exabgp/rib/cache.py @@ -7,8 +7,7 @@ License: 3-clause BSD. (See the COPYRIGHT file) """ -from exabgp.bgp.message import IN -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action class Cache(object): @@ -29,7 +28,7 @@ def delete_cached_family(self, families): if family not in families: del self._seen[family] - def cached_changes(self, families=None, actions=[OUT.ANNOUNCE]): + def cached_changes(self, families=None, actions=[Action.ANNOUNCE]): # families can be None or [] requested_families = self.families if families is None else set(families).intersection(self.families) @@ -50,7 +49,7 @@ def in_cache(self, change): if not self.cache: return False - if change.nlri.action == OUT.WITHDRAW: + if change.nlri.action == Action.WITHDRAW: return False cached = self._seen.get(change.nlri.family(), {}).get(change.index(), None) @@ -71,7 +70,7 @@ def update_cache(self, change): return family = change.nlri.family() index = change.index() - if change.nlri.action == OUT.ANNOUNCE: + if change.nlri.action == Action.ANNOUNCE: self._seen.setdefault(family, {})[index] = change elif family in self._seen: self._seen[family].pop(index, None) diff --git a/src/exabgp/rib/outgoing.py b/src/exabgp/rib/outgoing.py index a876fa661..6295ce67e 100644 --- a/src/exabgp/rib/outgoing.py +++ b/src/exabgp/rib/outgoing.py @@ -12,7 +12,7 @@ from exabgp.protocol.family import AFI from exabgp.protocol.family import SAFI -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action from exabgp.bgp.message import Update from exabgp.bgp.message.refresh import RouteRefresh from exabgp.bgp.message.update.attribute import Attributes @@ -90,7 +90,7 @@ def withdraw(self, families, enhanced_refresh): if family not in self._enhanced_refresh_start: self._enhanced_refresh_start.append(family) - changes = list(self.cached_changes(requested_families, [OUT.ANNOUNCE, OUT.WITHDRAW])) + changes = list(self.cached_changes(requested_families, [Action.ANNOUNCE, Action.WITHDRAW])) for change in changes: self.del_from_rib(change) @@ -119,7 +119,7 @@ def add_to_rib_watchdog(self, change): def announce_watchdog(self, watchdog): if watchdog in self._watchdog: for change in list(self._watchdog[watchdog].get('-', {}).values()): - change.nlri.action = OUT.ANNOUNCE # pylint: disable=E1101 + change.nlri.action = Action.ANNOUNCE # pylint: disable=E1101 self.add_to_rib(change) self._watchdog[watchdog].setdefault('+', {})[change.index()] = change self._watchdog[watchdog]['-'].pop(change.index()) @@ -127,7 +127,7 @@ def announce_watchdog(self, watchdog): def withdraw_watchdog(self, watchdog): if watchdog in self._watchdog: for change in list(self._watchdog[watchdog].get('+', {}).values()): - change.nlri.action = OUT.WITHDRAW + change.nlri.action = Action.WITHDRAW self.add_to_rib(change) self._watchdog[watchdog].setdefault('-', {})[change.index()] = change self._watchdog[watchdog]['+'].pop(change.index()) @@ -135,7 +135,7 @@ def withdraw_watchdog(self, watchdog): def del_from_rib(self, change): log.debug('remove %s' % change, 'rib') change = deepcopy(change) - change.nlri.action = OUT.WITHDRAW + change.nlri.action = Action.WITHDRAW return self._add_to_rib(change, force=True) def add_to_rib(self, change, force=False): @@ -149,7 +149,7 @@ def _add_to_rib(self, change, force): # import traceback # traceback.print_stack() # print("\n\n\n") - # print("%s %s" % ('inserting' if change.nlri.action == OUT.ANNOUNCE else 'withdrawing', change.extensive())) + # print("%s %s" % ('inserting' if change.nlri.action == Action.ANNOUNCE else 'withdrawing', change.extensive())) # print("\n\n\n") if not force and self._enhanced_refresh_start: @@ -178,7 +178,7 @@ def _add_to_rib(self, change, force): # And the yield makes it very cpu/memory intensive .. # always remove previous announcement if cancelled or replaced before being sent - if change.nlri.action == OUT.WITHDRAW: + if change.nlri.action == Action.WITHDRAW: prev_change = new_nlri.get(change_index, None) if prev_change: prev_change_index = prev_change.index() diff --git a/tests/nlri_tests.py b/tests/nlri_tests.py index f9222dce0..cbdb3c59f 100755 --- a/tests/nlri_tests.py +++ b/tests/nlri_tests.py @@ -37,7 +37,7 @@ from exabgp.protocol.ip import IP -from exabgp.bgp.message import OUT +from exabgp.bgp.message import Action class TestNLRIs(unittest.TestCase): @@ -56,7 +56,7 @@ def test200_IPVPNCreatePackUnpack(self): ) packed = nlri.pack() - unpacked, leftover = IPVPN.unpack_nlri(AFI.ipv4, SAFI.mpls_vpn, packed, OUT.UNSET, None) + unpacked, leftover = IPVPN.unpack_nlri(AFI.ipv4, SAFI.mpls_vpn, packed, Action.UNSET, None) self.assertEqual(0, len(leftover)) @@ -86,7 +86,7 @@ def test99_EVPNMACCreatePackUnpack(self): packed = nlri.pack() - unpacked, leftover = EVPN.unpack_nlri(AFI.l2vpn, SAFI.evpn, packed, OUT.UNSET, None) + unpacked, leftover = EVPN.unpack_nlri(AFI.l2vpn, SAFI.evpn, packed, Action.UNSET, None) self.assertEqual(0, len(leftover)) @@ -112,7 +112,7 @@ def test99_EVPNMulticastCreatePackUnpack(self): packed = nlri.pack() - unpacked, leftover = EVPN.unpack_nlri(AFI.l2vpn, SAFI.evpn, packed, OUT.UNSET, None) + unpacked, leftover = EVPN.unpack_nlri(AFI.l2vpn, SAFI.evpn, packed, Action.UNSET, None) self.assertEqual(0, len(leftover)) @@ -140,7 +140,7 @@ def test99_EVPNPrefixCreatePackUnpack(self): packed = nlri.pack() - unpacked, leftover = EVPN.unpack_nlri(AFI.l2vpn, SAFI.evpn, packed, OUT.UNSET, None) + unpacked, leftover = EVPN.unpack_nlri(AFI.l2vpn, SAFI.evpn, packed, Action.UNSET, None) self.assertEqual(0, len(leftover)) @@ -284,7 +284,7 @@ def test99_RTCCreatePackUnpack(self): nlri = RTC.new(AFI.ipv4, SAFI.rtc, 64512, RouteTarget(64577, 123)) packed = nlri.pack() - unpacked, leftover = RTC.unpack_nlri(AFI.ipv4, SAFI.mpls_vpn, packed, OUT.UNSET, None) + unpacked, leftover = RTC.unpack_nlri(AFI.ipv4, SAFI.mpls_vpn, packed, Action.UNSET, None) self.assertEqual(0, len(leftover)) @@ -305,7 +305,7 @@ def test98_RTCWildcardPackUnpack(self): nlri = RTC.new(AFI.ipv4, SAFI.rtc, 0, None) packed = nlri.pack() - unpacked, leftover = RTC.unpack_nlri(AFI.ipv4, SAFI.mpls_vpn, packed, OUT.UNSET, None) + unpacked, leftover = RTC.unpack_nlri(AFI.ipv4, SAFI.mpls_vpn, packed, Action.UNSET, None) self.assertEqual(0, len(leftover))