Signed-off-by: IWASE Yusuke <iwase.yusu...@gmail.com> --- ryu/services/protocols/bgp/base.py | 32 ++--- ryu/services/protocols/bgp/bgpspeaker.py | 133 ++++++++++++--------- .../protocols/bgp/core_managers/table_manager.py | 3 - .../protocols/bgp/operator/commands/show/rib.py | 10 +- .../protocols/bgp/operator/internal_api.py | 7 +- ryu/services/protocols/bgp/rtconf/base.py | 8 +- ryu/services/protocols/bgp/rtconf/neighbors.py | 3 +- 7 files changed, 107 insertions(+), 89 deletions(-)
diff --git a/ryu/services/protocols/bgp/base.py b/ryu/services/protocols/bgp/base.py index c0c574f..d34a9dc 100644 --- a/ryu/services/protocols/bgp/base.py +++ b/ryu/services/protocols/bgp/base.py @@ -20,13 +20,13 @@ from __future__ import absolute_import import abc from collections import OrderedDict import logging -import six import socket import time import traceback import weakref import netaddr +import six from ryu.lib import hub from ryu.lib import sockopt @@ -106,17 +106,17 @@ def add_bgp_error_metadata(code, sub_code, def_desc='unknown'): raise ValueError('BGPSException with code %d and sub-code %d ' 'already defined.' % (code, sub_code)) - def decorator(klass): + def decorator(subclass): """Sets class constants for exception code and sub-code. If given class is sub-class of BGPSException we sets class constants. """ - if issubclass(klass, BGPSException): - _EXCEPTION_REGISTRY[(code, sub_code)] = klass - klass.CODE = code - klass.SUB_CODE = sub_code - klass.DEF_DESC = def_desc - return klass + if issubclass(subclass, BGPSException): + _EXCEPTION_REGISTRY[(code, sub_code)] = subclass + subclass.CODE = code + subclass.SUB_CODE = sub_code + subclass.DEF_DESC = def_desc + return subclass return decorator @@ -326,11 +326,11 @@ class Activity(object): def get_remotename(self, sock): addr, port = sock.getpeername()[:2] - return (self._canonicalize_ip(addr), str(port)) + return self._canonicalize_ip(addr), str(port) def get_localname(self, sock): addr, port = sock.getsockname()[:2] - return (self._canonicalize_ip(addr), str(port)) + return self._canonicalize_ip(addr), str(port) def _create_listen_socket(self, family, loc_addr): s = socket.socket(family) @@ -358,7 +358,7 @@ class Activity(object): socket.SOCK_STREAM, 0, socket.AI_PASSIVE) listen_sockets = {} for res in info: - af, socktype, proto, cannonname, sa = res + af, socktype, proto, _, sa = res sock = None try: sock = socket.socket(af, socktype, proto) @@ -377,7 +377,7 @@ class Activity(object): count = 0 server = None - for sa in listen_sockets.keys(): + for sa in listen_sockets: name = self.name + '_server@' + str(sa[0]) self._asso_socket_map[name] = listen_sockets[sa] if count == 0: @@ -412,7 +412,7 @@ class Activity(object): if password: sockopt.set_tcp_md5sig(sock, peer_addr[0], password) sock.connect(peer_addr) - # socket.error exception is rasied in cese of timeout and + # socket.error exception is raised in case of timeout and # the following code is executed only when the connection # is established. @@ -450,14 +450,14 @@ class Sink(object): @staticmethod def next_index(): """Increments the sink index and returns the value.""" - Sink.idx = Sink.idx + 1 + Sink.idx += 1 return Sink.idx def __init__(self): # A small integer that represents this sink. self.index = Sink.next_index() - # Event used to signal enqueing. + # Create an event for signal enqueuing. from .utils.evtlet import EventletIOFactory self.outgoing_msg_event = EventletIOFactory.create_custom_event() @@ -487,7 +487,7 @@ class Sink(object): If message list currently has no messages, the calling thread will be put to sleep until we have at-least one message in the list that - can be poped and returned. + can be popped and returned. """ # We pick the first outgoing available and send it. outgoing_msg = self.outgoing_msg_list.pop_first() diff --git a/ryu/services/protocols/bgp/bgpspeaker.py b/ryu/services/protocols/bgp/bgpspeaker.py index 168220b..4608524 100644 --- a/ryu/services/protocols/bgp/bgpspeaker.py +++ b/ryu/services/protocols/bgp/bgpspeaker.py @@ -37,10 +37,8 @@ from ryu.services.protocols.bgp.rtconf.common import LOCAL_AS from ryu.services.protocols.bgp.rtconf.common import ROUTER_ID from ryu.services.protocols.bgp.rtconf.common import BGP_SERVER_PORT from ryu.services.protocols.bgp.rtconf.common import DEFAULT_BGP_SERVER_PORT -from ryu.services.protocols.bgp.rtconf.common \ - import DEFAULT_REFRESH_MAX_EOR_TIME -from ryu.services.protocols.bgp.rtconf.common \ - import DEFAULT_REFRESH_STALEPATH_TIME +from ryu.services.protocols.bgp.rtconf.common import ( + DEFAULT_REFRESH_MAX_EOR_TIME, DEFAULT_REFRESH_STALEPATH_TIME) from ryu.services.protocols.bgp.rtconf.common import DEFAULT_LABEL_RANGE from ryu.services.protocols.bgp.rtconf.common import REFRESH_MAX_EOR_TIME from ryu.services.protocols.bgp.rtconf.common import REFRESH_STALEPATH_TIME @@ -158,13 +156,14 @@ class BGPSpeaker(object): """ super(BGPSpeaker, self).__init__() - settings = {} - settings[LOCAL_AS] = as_number - settings[ROUTER_ID] = router_id - settings[BGP_SERVER_PORT] = bgp_server_port - settings[REFRESH_STALEPATH_TIME] = refresh_stalepath_time - settings[REFRESH_MAX_EOR_TIME] = refresh_max_eor_time - settings[LABEL_RANGE] = label_range + settings = { + LOCAL_AS: as_number, + ROUTER_ID: router_id, + BGP_SERVER_PORT: bgp_server_port, + REFRESH_STALEPATH_TIME: refresh_stalepath_time, + REFRESH_MAX_EOR_TIME: refresh_max_eor_time, + LABEL_RANGE: label_range, + } self._core_start(settings) self._init_signal_listeners() self._best_path_change_handler = best_path_change_handler @@ -329,7 +328,7 @@ class BGPSpeaker(object): CAP_ENHANCED_REFRESH: enable_enhanced_refresh, CAP_FOUR_OCTET_AS_NUMBER: enable_four_octet_as_number, } - # v6 advertizement is available with only v6 peering + # v6 advertisement is available with only v6 peering if netaddr.valid_ipv4(address): bgp_neighbor[CAP_MBGP_IPV4] = enable_ipv4 bgp_neighbor[CAP_MBGP_IPV6] = False @@ -371,8 +370,10 @@ class BGPSpeaker(object): the string representation of an IP address. """ - bgp_neighbor = {} - bgp_neighbor[neighbors.IP_ADDRESS] = address + bgp_neighbor = { + neighbors.IP_ADDRESS: address, + } + call('neighbor.delete', **bgp_neighbor) def neighbor_reset(self, address): @@ -382,8 +383,10 @@ class BGPSpeaker(object): the string representation of an IP address. """ - bgp_neighbor = {} - bgp_neighbor[neighbors.IP_ADDRESS] = address + bgp_neighbor = { + neighbors.IP_ADDRESS: address, + } + call('core.reset_neighbor', **bgp_neighbor) def neighbor_update(self, address, conf_type, conf_value): @@ -408,6 +411,7 @@ class BGPSpeaker(object): param = {neighbors.IP_ADDRESS: address, neighbors.CHANGES: attribute_param} + call(func_name, **param) def neighbor_state_get(self, address=None, format='json'): @@ -418,11 +422,13 @@ class BGPSpeaker(object): state of all the peers return. """ - show = {} - show['params'] = ['neighbor', 'summary'] + show = { + 'params': ['neighbor', 'summary'], + 'format': format, + } if address: show['params'].append(address) - show['format'] = format + return call('operator.show', **show) def prefix_add(self, prefix, next_hop=None, route_dist=None): @@ -441,8 +447,9 @@ class BGPSpeaker(object): """ func_name = 'network.add' - networks = {} - networks[PREFIX] = prefix + networks = { + PREFIX: prefix, + } if next_hop: networks[NEXT_HOP] = next_hop if route_dist: @@ -472,8 +479,9 @@ class BGPSpeaker(object): """ func_name = 'network.del' - networks = {} - networks[PREFIX] = prefix + networks = { + PREFIX: prefix, + } if route_dist: func_name = 'prefix.delete_local' networks[ROUTE_DISTINGUISHER] = route_dist @@ -617,9 +625,11 @@ class BGPSpeaker(object): call('vrf.delete', **vrf) def vrfs_get(self, format='json'): - show = {} - show['params'] = ['vrf', 'routes', 'all'] - show['format'] = format + show = { + 'params': ['vrf', 'routes', 'all'], + 'format': format, + } + return call('operator.show', **show) def rib_get(self, family='ipv4', format='json'): @@ -629,9 +639,11 @@ class BGPSpeaker(object): ``family`` specifies the address family of the RIB. """ - show = {} - show['params'] = ['rib', family] - show['format'] = format + show = { + 'params': ['rib', family], + 'format': format + } + return call('operator.show', **show) def neighbor_get(self, routetype, address, format='json'): @@ -649,12 +661,14 @@ class BGPSpeaker(object): the string representation of an IP address. """ - show = {} + show = { + 'format': format, + } if routetype == 'sent-routes' or routetype == 'received-routes': show['params'] = ['neighbor', routetype, address, 'all'] else: show['params'] = ['neighbor', 'received-routes', address, 'all'] - show['format'] = format + return call('operator.show', **show) def _set_filter(self, filter_type, address, filters): @@ -668,12 +682,14 @@ class BGPSpeaker(object): filters = [] func_name = 'neighbor.' + filter_type + '_filter.set' - param = {} - param[neighbors.IP_ADDRESS] = address + param = { + neighbors.IP_ADDRESS: address, + } if filter_type == 'in': param[neighbors.IN_FILTER] = filters else: param[neighbors.OUT_FILTER] = filters + call(func_name, **param) def out_filter_set(self, address, filters): @@ -714,10 +730,11 @@ class BGPSpeaker(object): """ func_name = 'neighbor.out_filter.get' - param = {} - param[neighbors.IP_ADDRESS] = address - out_filter = call(func_name, **param) - return out_filter + param = { + neighbors.IP_ADDRESS: address, + } + + return call(func_name, **param) def in_filter_set(self, address, filters): """This method sets in-bound filters to a neighbor. @@ -742,10 +759,11 @@ class BGPSpeaker(object): """ func_name = 'neighbor.in_filter.get' - param = {} - param[neighbors.IP_ADDRESS] = address - in_filter = call(func_name, **param) - return in_filter + param = { + neighbors.IP_ADDRESS: address, + } + + return call(func_name, **param) def bmp_server_add(self, address, port): """This method registers a new BMP (BGP monitoring Protocol) @@ -758,9 +776,11 @@ class BGPSpeaker(object): """ func_name = 'bmp.start' - param = {} - param['host'] = address - param['port'] = port + param = { + 'host': address, + 'port': port, + } + call(func_name, **param) def bmp_server_del(self, address, port): @@ -772,9 +792,11 @@ class BGPSpeaker(object): """ func_name = 'bmp.stop' - param = {} - param['host'] = address - param['port'] = port + param = { + 'host': address, + 'port': port, + } + call(func_name, **param) def attribute_map_set(self, address, attribute_maps, @@ -811,12 +833,14 @@ class BGPSpeaker(object): 'route_family must be RF_VPN_V4 or RF_VPN_V6' func_name = 'neighbor.attribute_map.set' - param = {} - param[neighbors.IP_ADDRESS] = address - param[neighbors.ATTRIBUTE_MAP] = attribute_maps + param = { + neighbors.IP_ADDRESS: address, + neighbors.ATTRIBUTE_MAP: attribute_maps, + } if route_dist is not None: param[vrfs.ROUTE_DISTINGUISHER] = route_dist param[vrfs.VRF_RF] = route_family + call(func_name, **param) def attribute_map_get(self, address, route_dist=None, @@ -838,13 +862,14 @@ class BGPSpeaker(object): 'route_family must be RF_VPN_V4 or RF_VPN_V6' func_name = 'neighbor.attribute_map.get' - param = {} - param[neighbors.IP_ADDRESS] = address + param = { + neighbors.IP_ADDRESS: address, + } if route_dist is not None: param[vrfs.ROUTE_DISTINGUISHER] = route_dist param[vrfs.VRF_RF] = route_family - attribute_maps = call(func_name, **param) - return attribute_maps + + return call(func_name, **param) @staticmethod def _check_rf_and_normalize(prefix): diff --git a/ryu/services/protocols/bgp/core_managers/table_manager.py b/ryu/services/protocols/bgp/core_managers/table_manager.py index edb476c..b8285ca 100644 --- a/ryu/services/protocols/bgp/core_managers/table_manager.py +++ b/ryu/services/protocols/bgp/core_managers/table_manager.py @@ -9,15 +9,12 @@ from ryu.services.protocols.bgp.info_base.ipv4 import Ipv4Path from ryu.services.protocols.bgp.info_base.ipv4 import Ipv4Table from ryu.services.protocols.bgp.info_base.ipv6 import Ipv6Path from ryu.services.protocols.bgp.info_base.ipv6 import Ipv6Table -from ryu.services.protocols.bgp.info_base.vpnv4 import Vpnv4Path from ryu.services.protocols.bgp.info_base.vpnv4 import Vpnv4Table -from ryu.services.protocols.bgp.info_base.vpnv6 import Vpnv6Path from ryu.services.protocols.bgp.info_base.vpnv6 import Vpnv6Table from ryu.services.protocols.bgp.info_base.vrf4 import Vrf4Table from ryu.services.protocols.bgp.info_base.vrf6 import Vrf6Table from ryu.services.protocols.bgp.info_base.vrfevpn import VrfEvpnTable from ryu.services.protocols.bgp.info_base.evpn import EvpnTable -from ryu.services.protocols.bgp.rtconf import vrfs from ryu.services.protocols.bgp.rtconf.vrfs import VRF_RF_IPV4 from ryu.services.protocols.bgp.rtconf.vrfs import VRF_RF_IPV6 from ryu.services.protocols.bgp.rtconf.vrfs import VRF_RF_L2_EVPN diff --git a/ryu/services/protocols/bgp/operator/commands/show/rib.py b/ryu/services/protocols/bgp/operator/commands/show/rib.py index 0538045..022e35c 100644 --- a/ryu/services/protocols/bgp/operator/commands/show/rib.py +++ b/ryu/services/protocols/bgp/operator/commands/show/rib.py @@ -1,15 +1,13 @@ from __future__ import absolute_import -from .route_formatter_mixin import RouteFormatterMixin - +from ryu.services.protocols.bgp.base import ActivityException from ryu.services.protocols.bgp.operator.command import Command from ryu.services.protocols.bgp.operator.command import CommandsResponse from ryu.services.protocols.bgp.operator.command import STATUS_ERROR from ryu.services.protocols.bgp.operator.command import STATUS_OK - -from ryu.services.protocols.bgp.base import ActivityException -from ryu.services.protocols.bgp.operator.commands.responses import \ - WrongParamResp +from ryu.services.protocols.bgp.operator.commands.responses import ( + WrongParamResp) +from .route_formatter_mixin import RouteFormatterMixin class RibBase(Command, RouteFormatterMixin): diff --git a/ryu/services/protocols/bgp/operator/internal_api.py b/ryu/services/protocols/bgp/operator/internal_api.py index 7f9449e..e624c82 100644 --- a/ryu/services/protocols/bgp/operator/internal_api.py +++ b/ryu/services/protocols/bgp/operator/internal_api.py @@ -107,7 +107,7 @@ class InternalApi(object): path_seg_list = path.get_pattr(BGP_ATTR_TYPE_AS_PATH).path_seg_list - if type(path_seg_list) == list: + if isinstance(path_seg_list, list): aspath = [] for as_path_seg in path_seg_list: for as_num in as_path_seg: @@ -157,10 +157,7 @@ class InternalApi(object): return ret def check_logging(self): - if self.log_handler and self._has_log_handler(self.log_handler): - return True - else: - return False + return self.log_handler and self._has_log_handler(self.log_handler) def check_logging_level(self): return logging.getLevelName(self.log_handler.level) diff --git a/ryu/services/protocols/bgp/rtconf/base.py b/ryu/services/protocols/bgp/rtconf/base.py index 6c2975b..a02a6a7 100644 --- a/ryu/services/protocols/bgp/rtconf/base.py +++ b/ryu/services/protocols/bgp/rtconf/base.py @@ -21,9 +21,10 @@ from abc import abstractmethod import functools import numbers import logging -import six import uuid +import six + from ryu.services.protocols.bgp.base import add_bgp_error_metadata from ryu.services.protocols.bgp.base import BGPSException from ryu.services.protocols.bgp.base import get_validator @@ -211,8 +212,7 @@ class BaseConf(object): if unknown_attrs: raise RuntimeConfigError(desc=( 'Unknown attributes: %s' % - ', '.join([str(i) for i in unknown_attrs])) - ) + ', '.join([str(i) for i in unknown_attrs]))) missing_req_settings = self._req_settings - given_attrs if missing_req_settings: raise MissingRequiredConf(conf_name=list(missing_req_settings)) @@ -687,7 +687,7 @@ def validate_med(med): def validate_soo_list(soo_list): if not isinstance(soo_list, list): raise ConfigTypeError(conf_name=SITE_OF_ORIGINS, conf_value=soo_list) - if not (len(soo_list) <= MAX_NUM_SOO): + if len(soo_list) > MAX_NUM_SOO: raise ConfigValueError(desc='Max. SOO is limited to %s' % MAX_NUM_SOO) if not all(validation.is_valid_ext_comm_attr(attr) for attr in soo_list): diff --git a/ryu/services/protocols/bgp/rtconf/neighbors.py b/ryu/services/protocols/bgp/rtconf/neighbors.py index c252d5f..2820011 100644 --- a/ryu/services/protocols/bgp/rtconf/neighbors.py +++ b/ryu/services/protocols/bgp/rtconf/neighbors.py @@ -18,9 +18,10 @@ """ from abc import abstractmethod import logging -import netaddr import numbers +import netaddr + from ryu.lib.packet.bgp import RF_IPv4_UC from ryu.lib.packet.bgp import RF_IPv6_UC from ryu.lib.packet.bgp import RF_IPv4_VPN -- 2.7.4 ------------------------------------------------------------------------------ _______________________________________________ Ryu-devel mailing list Ryu-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/ryu-devel