Skip to content
This repository was archived by the owner on Aug 12, 2020. It is now read-only.

Commit 02a539b

Browse files
committed
add decorator for checking peer's state
peer's state must be established before some of the apis can work, so add a decorator to them for checking the peer's state before doing the reall job. Signed-off-by: Peng Xiao <[email protected]>
1 parent 8a4cad7 commit 02a539b

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

bin/yabgpd

100644100755
File mode changed.

yabgp/api/utils.py

+22-19
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from oslo_config import cfg
2121
from flask import request
22+
import flask
2223

2324
from yabgp.common import constants as common_cons
2425

@@ -39,6 +40,20 @@ def decorated_function(*args, **kwargs):
3940
return decorated_function
4041

4142

43+
def makesure_peer_establish(f):
44+
@wraps(f)
45+
def decorator(*args, **kwargs):
46+
value = kwargs['peer_ip']
47+
if _ready_to_send_msg(peer_ip=value):
48+
return f(*args, **kwargs)
49+
else:
50+
return flask.jsonify({
51+
'status': False,
52+
'code': "Please check the peer's state"
53+
})
54+
return decorator
55+
56+
4257
def get_peer_conf_and_state(peer_ip=None):
4358
"""
4459
get peer configuration and state
@@ -105,24 +120,17 @@ def send_route_refresh(peer_ip, afi, safi, res):
105120
:param peer_ip: peer ip address
106121
:return: the sending results
107122
"""
108-
if _ready_to_send_msg(peer_ip):
109-
LOG.debug('peer %s is ready to send route refresh', peer_ip)
110-
try:
111-
if cfg.CONF.bgp.running_config[peer_ip]['factory'].fsm.protocol.send_route_refresh(
112-
afi=afi, safi=safi, res=res):
113-
return {
114-
'status': True
115-
}
116-
except Exception as e:
117-
LOG.error(e)
123+
try:
124+
if cfg.CONF.bgp.running_config[peer_ip]['factory'].fsm.protocol.send_route_refresh(
125+
afi=afi, safi=safi, res=res):
118126
return {
119-
'status': False,
120-
'code': 'failed when send this message out'
127+
'status': True
121128
}
122-
else:
129+
except Exception as e:
130+
LOG.error(e)
123131
return {
124132
'status': False,
125-
'code': "Please check the peer's state"
133+
'code': 'failed when send this message out'
126134
}
127135

128136

@@ -132,11 +140,6 @@ def send_update(peer_ip, attr, nlri, withdraw):
132140
:param peer_ip: peer ip address
133141
:return:
134142
"""
135-
if not _ready_to_send_msg(peer_ip):
136-
return {
137-
'status': False,
138-
'code': "Please check the peer's state"
139-
}
140143
# TODO check RIB out policy
141144
# TODO update RIB out table
142145
if cfg.CONF.bgp.running_config[peer_ip]['factory'].fsm.protocol.send_update({

yabgp/api/v1.py

+2
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ def get_peer_statistic(peer_ip):
210210
@blueprint.route('/peer/<peer_ip>/send/route-refresh', methods=['POST'])
211211
@auth.login_required
212212
@api_utils.log_request
213+
@api_utils.makesure_peer_establish
213214
def send_route_refresh(peer_ip):
214215
"""
215216
Try to send BGP Route Refresh message to a peer
@@ -272,6 +273,7 @@ def send_route_refresh(peer_ip):
272273
@blueprint.route('/peer/<peer_ip>/send/update', methods=['POST'])
273274
@auth.login_required
274275
@api_utils.log_request
276+
@api_utils.makesure_peer_establish
275277
def send_update_message(peer_ip):
276278
"""
277279
Try to send BGP update message to the peer. Both update nlri and withdraw nlri treated as Update.

0 commit comments

Comments
 (0)