Skip to content

Commit

Permalink
Merge d3263a9 into 034eeee
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaopeng163 committed Aug 16, 2018
2 parents 034eeee + d3263a9 commit 0e40c99
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 92 deletions.
28 changes: 28 additions & 0 deletions run_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2015-2018 Cisco Systems, Inc.
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import unittest


def test():
"test load and run"
tests = unittest.TestLoader().discover('.', pattern='test*.py')
result = unittest.TextTestRunner(verbosity=2).run(tests)
if not result.wasSuccessful():
raise ValueError('unittest failed')


if __name__ == '__main__':
test()
52 changes: 7 additions & 45 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[tox]
# List the environment that will be run by default
minversion = 1.6
envlist = py27,py34,py35,docs,pep8
envlist = py27,py36,docs,pep8
skipsdist = True

[testenv]
Expand All @@ -17,52 +17,14 @@ setenv = VIRTUAL_ENV={envdir}
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt

[testenv:py27]
commands = coverage erase
python setup.py testr --testr-args='{posargs}' --coverage --omit='yabgp/tests/*'
coverage run run_test.py
coverage report -m
[testenv:py36]
commands = coverage erase
coverage run run_test.py
coverage report -m
[testenv:py34]
commands = python -m testtools.run \
yabgp.tests.unit.api.test_app \
yabgp.tests.unit.api.test_v1 \
yabgp.tests.unit.message.test_keepalive \
yabgp.tests.unit.message.test_notification \
yabgp.tests.unit.message.test_open \
yabgp.tests.unit.message.test_route_refresh \
yabgp.tests.unit.message.test_update \
yabgp.tests.unit.message.attribute.test_origin \
yabgp.tests.unit.message.attribute.test_aspath \
yabgp.tests.unit.message.attribute.test_nexthop \
yabgp.tests.unit.message.attribute.test_med \
yabgp.tests.unit.message.attribute.test_localpref \
yabgp.tests.unit.message.attribute.test_atomicaggregate \
yabgp.tests.unit.message.attribute.test_aggregator \
yabgp.tests.unit.message.attribute.test_community \
yabgp.tests.unit.message.attribute.test_originatorid \
yabgp.tests.unit.message.attribute.test_clusterlist \
yabgp.tests.unit.message.attribute.test_extcommunity \
yabgp.tests.unit.message.attribute.nlri.test_ipv4_mpls_vpn

[testenv:py35]
commands = python -m testtools.run \
yabgp.tests.unit.api.test_app \
yabgp.tests.unit.api.test_v1 \
yabgp.tests.unit.message.test_keepalive \
yabgp.tests.unit.message.test_notification \
yabgp.tests.unit.message.test_open \
yabgp.tests.unit.message.test_route_refresh \
yabgp.tests.unit.message.test_update \
yabgp.tests.unit.message.attribute.test_origin \
yabgp.tests.unit.message.attribute.test_aspath \
yabgp.tests.unit.message.attribute.test_nexthop \
yabgp.tests.unit.message.attribute.test_med \
yabgp.tests.unit.message.attribute.test_localpref \
yabgp.tests.unit.message.attribute.test_atomicaggregate \
yabgp.tests.unit.message.attribute.test_aggregator \
yabgp.tests.unit.message.attribute.test_community \
yabgp.tests.unit.message.attribute.test_originatorid \
yabgp.tests.unit.message.attribute.test_clusterlist \
yabgp.tests.unit.message.attribute.test_extcommunity \
yabgp.tests.unit.message.attribute.nlri.test_ipv4_mpls_vpn

[testenv:docs]
deps=
Expand Down
2 changes: 1 addition & 1 deletion yabgp/handler/default_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
default=True,
help='Whether the BGP message is written to disk'),
cfg.StrOpt('write_dir',
default=os.path.join(os.environ['HOME'], 'data/bgp/'),
default=os.path.join(os.environ.get('HOME') or '.', 'data/bgp/'),
help='The BGP messages storage path'),
cfg.IntOpt('write_msg_max_size',
default=500,
Expand Down
4 changes: 2 additions & 2 deletions yabgp/message/attribute/mpreachnlri.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def parse(cls, value):
# if nlri length is greater than 240 bytes, it is encoded over 2 bytes
nlri_list = []
while nlri_bin:
length = ord(nlri_bin[0])
length = ord(nlri_bin[0:1])
if length >> 4 == 0xf and len(nlri_bin) > 2:
length = struct.unpack('!H', nlri_bin[:2])[0]
nlri_tmp = nlri_bin[2: length + 2]
Expand Down Expand Up @@ -236,7 +236,7 @@ def construct(cls, value):
try:
nexthop = netaddr.IPAddress(value['nexthop']).packed
except netaddr.core.AddrFormatError:
nexthop = ''
nexthop = b''
nlri_hex = b''
nlri_hex += IPv4FlowSpec.construct(value=value['nlri'])
if nlri_hex:
Expand Down
2 changes: 1 addition & 1 deletion yabgp/message/attribute/mpunreachnlri.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def parse(cls, value):
# if nlri length is greater than 240 bytes, it is encoded over 2 bytes
withdraw_list = []
while nlri_bin:
length = ord(nlri_bin[0])
length = ord(nlri_bin[0:1])
if length >> 4 == 0xf and len(nlri_bin) > 2:
length = struct.unpack('!H', nlri_bin[:2])[0]
nlri_tmp = nlri_bin[2: length + 2]
Expand Down
3 changes: 2 additions & 1 deletion yabgp/message/attribute/nlri/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import division
import struct
import binascii

Expand Down Expand Up @@ -45,7 +46,7 @@ def construct_prefix_v4(masklen, prefix_str):
def construct_prefix_v6(prefix):
mask = int(prefix.split('/')[1])
prefix_hex = binascii.unhexlify(hex(netaddr.IPNetwork(prefix).ip)[2:])
offset = mask / 8
offset = mask // 8
offset_re = mask % 8
if offset == 0:
return prefix_hex[0: 1]
Expand Down
3 changes: 2 additions & 1 deletion yabgp/message/attribute/nlri/evpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import division
import struct
import binascii

Expand Down Expand Up @@ -284,7 +285,7 @@ def parse(cls, value, iswithdraw=False):
offset += 1
# ip address
if ip_addr_len != 0:
route['ip'] = str(netaddr.IPAddress(int(binascii.b2a_hex(value[offset: offset+ip_addr_len / 8]), 16)))
route['ip'] = str(netaddr.IPAddress(int(binascii.b2a_hex(value[offset: offset+ip_addr_len // 8]), 16)))
return route

@classmethod
Expand Down
15 changes: 10 additions & 5 deletions yabgp/message/attribute/nlri/ipv4_flowspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

"""IPv4 Flowspec NLRI
"""
from __future__ import division
from builtins import range
import binascii
import math
import struct
Expand Down Expand Up @@ -105,9 +107,12 @@ def parse_prefix(data):
Encoding: <prefix-length (1 octet), prefix>
"""
prefix_len = ord(data[0:1])
octet_len = int(math.ceil(prefix_len / 8.0))
octet_len = int(math.ceil(prefix_len // 8))
tmp = data[1:octet_len + 1]
prefix_data = [ord(i) for i in tmp]
if isinstance(tmp[0], int):
prefix_data = [i for i in tmp]
else:
prefix_data = [ord(i) for i in tmp]
prefix_data = prefix_data + list(str(0)) * 4
prefix = "%s.%s.%s.%s" % (tuple(prefix_data[0:4])) + '/' + str(prefix_len)
return prefix, octet_len + 1
Expand Down Expand Up @@ -136,6 +141,7 @@ def parse_operators(cls, data):
parse_operator_list = []
while data:
operator = cls.parse_operator_flag(ord(data[0:1]))
print(operator)
offset += 1
operator_value = int(binascii.b2a_hex(data[1:1 + operator['LEN']]), 16)
offset += operator['LEN']
Expand All @@ -156,7 +162,7 @@ def parse_operator_flag(data):
+---+---+---+---+---+---+---+---+
"""
bit_list = []
for i in xrange(8):
for i in range(8):
bit_list.append((data >> i) & 1)
bit_list.reverse()
result = {
Expand Down Expand Up @@ -268,12 +274,11 @@ def construct_operators(cls, data):
flag_dict['GT'] = 1
elif '<' in data:
off_set = 1
# value_hex = str(bytearray.fromhex(hex(int(data[2:]))[2:]))
flag_dict['LT'] = 1
hex_str = hex(int(data[off_set:]))[2:]
if len(hex_str) % 2 == 1:
hex_str = '0' + hex_str
value_hex = str(bytearray.fromhex(hex_str))
value_hex = bytearray.fromhex(hex_str)
flag_dict['LEN'] = len(value_hex)
opt_flag_bin = cls.construct_operator_flag(flag_dict)
data_bin += struct.pack('!B', opt_flag_bin)
Expand Down
14 changes: 7 additions & 7 deletions yabgp/message/attribute/nlri/labeled_unicast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
# License for the specific language governing permissions and limitations
# under the License.

from __future__ import division
import struct
import binascii


import netaddr

from yabgp.message.attribute.nlri import NLRI
Expand Down Expand Up @@ -43,23 +45,21 @@ class LabeledUnicast(NLRI):
def parse(cls, nlri_data):
nlri_list = []
while nlri_data:
nlri_bit_len = ord(nlri_data[0])
nlri_bit_len = ord(nlri_data[0:1])
# get the byte lenght of label stack + prefix
if nlri_bit_len % 8 == 0:
nlri_byte_len = nlri_bit_len / 8
nlri_byte_len = nlri_bit_len // 8
else:
nlri_byte_len = nlri_bit_len / 8 + 1
nlri_byte_len = nlri_bit_len // 8 + 1

offset = nlri_byte_len + 1
label = cls.parse_mpls_label_stack(nlri_data[1:])
label_byte_len = len(label) * 3
prefix_byte_len = nlri_byte_len - label_byte_len
prefix_mask = nlri_bit_len - label_byte_len * 8
if cls.AFI == AFNUM_INET:
prefix_hex = nlri_data[offset - prefix_byte_len: offset] + (4 - prefix_byte_len) * '\x00'
prefix_data = [ord(i) for i in prefix_hex]
prefix_data = prefix_data
prefix = "%s.%s.%s.%s" % (tuple(prefix_data[0:4])) + '/' + str(prefix_mask)
prefix_hex = nlri_data[offset - prefix_byte_len: offset] + (4 - prefix_byte_len) * b'\x00'
prefix = str(netaddr.IPAddress(int(binascii.b2a_hex(prefix_hex), 16))) + '/' + str(prefix_mask)
elif cls.AFI == AFNUM_INET6: # ipv6
prefix_hex = nlri_data[offset - prefix_byte_len: offset]
prefix = str(netaddr.IPAddress(int(binascii.b2a_hex(prefix_hex), 16))) + '/' + str(prefix_mask)
Expand Down
7 changes: 4 additions & 3 deletions yabgp/message/attribute/nlri/linkstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""linkstate
"""

from __future__ import division
import struct
import binascii

Expand Down Expand Up @@ -212,6 +213,6 @@ def parse_node_descriptor(cls, data, proto):

@classmethod
def parse_iso_node_id(cls, data):
tmp = binascii.b2a_hex(data)
chunks, chunk_size = len(tmp), len(tmp)/3
return '.'.join([tmp[i:i+chunk_size] for i in range(0, chunks, chunk_size)])
tmp = binascii.b2a_hex(data).decode('utf-8')
chunks, chunk_size = len(tmp), len(tmp) // 3
return '.'.join([str(tmp[i:i+chunk_size]) for i in range(0, chunks, chunk_size)])
11 changes: 5 additions & 6 deletions yabgp/message/attribute/tunnelencaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

"""BGP Attribute MP_REACH_NLRI
"""

import struct

import netaddr
Expand Down Expand Up @@ -82,15 +81,15 @@ def construct_weight_and_seg(cls, segment_list):
struct.pack('!I', segment_list[bgp_cons.BGPSUB_TLV_WEIGHTED])
seg_hex = b''
for seg in segment_list[bgp_cons.BGPSUB_TLV_SID]:
seg_type = int(seg.keys()[0])
seg_type = int(list(seg)[0])
if seg_type == bgp_cons.BGP_SRTE_SEGMENT_SUBTLV_MPLS:
value = seg[seg.keys()[0]]
value = seg[list(seg)[0]]
sum_value = cls.construct_optional_label_sid(value)
seg_hex += struct.pack('!B', bgp_cons.BGP_SRTE_SEGMENT_SUBTLV_MPLS) + struct.pack('!B', 6) + b'\x00\x00' +\
struct.pack('!I', sum_value)
# 3
elif seg_type == bgp_cons.BGP_SRTE_SEGMENT_SUBTLV_IPV4_SID:
value = seg[seg.keys()[0]]
value = seg[list(seg)[0]]
ipv4_node = value['node']
if "SID" not in value.keys():
seg_hex += struct.pack('!B', bgp_cons.BGP_SRTE_SEGMENT_SUBTLV_IPV4_SID) + struct.pack('!B', 6) + b'\x00\x00' +\
Expand All @@ -102,7 +101,7 @@ def construct_weight_and_seg(cls, segment_list):
netaddr.IPAddress(ipv4_node).packed + struct.pack('!I', sum_value)
# 5
elif seg_type == bgp_cons.BGP_SRTE_SEGMENT_SUBTLV_IPV4_INDEX_SID:
value = seg[seg.keys()[0]]
value = seg[list(seg)[0]]
local_int = value['interface']
ipv4_node = value['node']
if "SID" not in value.keys():
Expand All @@ -117,7 +116,7 @@ def construct_weight_and_seg(cls, segment_list):
netaddr.IPAddress(ipv4_node).packed + struct.pack('!I', sum_value)
# 6
elif seg_type == bgp_cons.BGP_SRTE_SEGMENT_SUBTLV_IPV4_ADDR_SID:
value = seg[seg.keys()[0]]
value = seg[list(seg)[0]]
local_ipv4 = value['local']
remote_ipv4 = value['remote']
if "SID" not in value.keys():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def test_construct(self):
{'label': [92], 'prefix': '::2001:2121:1:0/64'},
{'label': [93], 'prefix': '2001:4837:1821::2/127'}]
nlri_hex = b'\x98\x00\x05\xb1\x20\x01\x21\x21\x00\x00\x00\x00\x00\x00\x00' \
'\x00\x00\x00\x00\x01\x58\x00\x05\xc1\x20\x01\x21\x21\x00\x01' \
'\x00\x00\x97\x00\x05\xd1\x20\x01\x48\x37\x18\x21\x00\x00\x00' \
'\x00\x00\x00\x00\x00\x00\x02'
b'\x00\x00\x00\x00\x01\x58\x00\x05\xc1\x20\x01\x21\x21\x00\x01' \
b'\x00\x00\x97\x00\x05\xd1\x20\x01\x48\x37\x18\x21\x00\x00\x00' \
b'\x00\x00\x00\x00\x00\x00\x02'

self.assertEqual(nlri_hex, IPv6LabeledUnicast.construct(nlri_list))

Expand All @@ -38,9 +38,9 @@ def test_parse(self):
{'label': [92], 'prefix': '::2001:2121:1:0/64'},
{'label': [93], 'prefix': '2001:4837:1821::2/127'}]
nlri_hex = b'\x98\x00\x05\xb1\x20\x01\x21\x21\x00\x00\x00\x00\x00\x00\x00' \
'\x00\x00\x00\x00\x01\x58\x00\x05\xc1\x20\x01\x21\x21\x00\x01' \
'\x00\x00\x97\x00\x05\xd1\x20\x01\x48\x37\x18\x21\x00\x00\x00' \
'\x00\x00\x00\x00\x00\x00\x02'
b'\x00\x00\x00\x00\x01\x58\x00\x05\xc1\x20\x01\x21\x21\x00\x01' \
b'\x00\x00\x97\x00\x05\xd1\x20\x01\x48\x37\x18\x21\x00\x00\x00' \
b'\x00\x00\x00\x00\x00\x00\x02'

self.assertEqual(nlri_list, IPv6LabeledUnicast.parse(nlri_hex))

Expand Down
Loading

0 comments on commit 0e40c99

Please sign in to comment.