Skip to content

Commit

Permalink
split, improve action and direction
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-mangin committed May 19, 2022
1 parent dc61bd4 commit daaf6b9
Show file tree
Hide file tree
Showing 34 changed files with 131 additions and 129 deletions.
3 changes: 1 addition & 2 deletions src/exabgp/bgp/message/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions src/exabgp/bgp/message/action.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 0 additions & 12 deletions src/exabgp/bgp/message/direction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 10 additions & 11 deletions src/exabgp/bgp/message/update/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/exabgp/bgp/message/update/attribute/mprnlri.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions src/exabgp/bgp/message/update/attribute/mpurnlri.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/exabgp/bgp/message/update/nlri/bgpls/nlri.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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''

Expand Down
6 changes: 3 additions & 3 deletions src/exabgp/bgp/message/update/nlri/evpn/nlri.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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''

Expand All @@ -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 ''

Expand Down
6 changes: 3 additions & 3 deletions src/exabgp/bgp/message/update/nlri/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 ''

Expand Down
9 changes: 4 additions & 5 deletions src/exabgp/bgp/message/update/nlri/inet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 ''

Expand Down Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions src/exabgp/bgp/message/update/nlri/ipvpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/exabgp/bgp/message/update/nlri/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ''

Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/exabgp/bgp/message/update/nlri/nlri.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions src/exabgp/bgp/message/update/nlri/rtc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
Loading

0 comments on commit daaf6b9

Please sign in to comment.