Skip to content

Commit

Permalink
add flags in prefix sid (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
zlpqingmei authored and xiaopeng163 committed Aug 15, 2018
1 parent f2c52ba commit 034eeee
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
2 changes: 1 addition & 1 deletion yabgp/message/attribute/linkstate/linkstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def unpack(cls, data, bgpls_pro_id=None):
while data:
type_code, length = struct.unpack('!HH', data[:4])
value = data[4: 4+length]
if type_code in [1099, 1100] and type_code in cls.registered_tlvs:
if type_code in [1099, 1100, 1158] and type_code in cls.registered_tlvs:
tlvs.append(cls.registered_tlvs[type_code].unpack(value, bgpls_pro_id).dict())
elif type_code in cls.registered_tlvs:
tlvs.append(cls.registered_tlvs[type_code].unpack(value).dict())
Expand Down
40 changes: 38 additions & 2 deletions yabgp/message/attribute/linkstate/prefix/prefix_sid.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+


# isis
# 0 1 2 3 4 5 6 7
# +-+-+-+-+-+-+-+-+
# |R|N|P|E|V|L| |
# +-+-+-+-+-+-+-+-+

# ospf
# 0 1 2 3 4 5 6 7
# +-+--+-+-+-+-+-+-+
# | |NP|M|E|V|L| | |
# +-+--+-+-+-+-+-+-+

# ospfv3
# 0 1 2 3 4 5 6 7
# +-+--+-+-+-+-+-+-+
# | |NP|M|E|V|L| | |
# +-+--+-+-+-+-+-+-+


@LinkState.register()
class PrefixSID(TLV):
"""
Expand All @@ -38,6 +57,23 @@ class PrefixSID(TLV):
TYPE_STR = 'prefix_sid'

@classmethod
def unpack(cls, data):
def unpack(cls, data, pro_id):

flags = ord(data[0:1])
flag = {}
if pro_id in [1, 2]:
flag['R'] = flags >> 7
flag['N'] = (flags << 1) % 256 >> 7
flag['P'] = (flags << 2) % 256 >> 7
flag['E'] = (flags << 3) % 256 >> 7
flag['V'] = (flags << 4) % 256 >> 7
flag['L'] = (flags << 5) % 256 >> 7
else: # 3, 6
flag['NP'] = (flags << 1) % 256 >> 7
flag['M'] = (flags << 2) % 256 >> 7
flag['E'] = (flags << 3) % 256 >> 7
flag['V'] = (flags << 4) % 256 >> 7
flag['L'] = (flags << 5) % 256 >> 7

algorithm = ord(data[1:2])
return cls(value={"algorithm": algorithm, "sid": int(binascii.b2a_hex(data[4:]), 16)})
return cls(value={"flags": flag, "algorithm": algorithm, "sid": int(binascii.b2a_hex(data[4:]), 16)})
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2015-2017 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.

""" Test Link State attribute"""

import unittest
from yabgp.message.attribute.linkstate.linkstate import LinkState


class TestLinkStatePrefixSID(unittest.TestCase):

def test_prefix_sid_flags_ospf(self):
self.maxDiff = None
data_bin = b'\x04\x86\x00\x07\xb4\x00\x00\x00\x00\x61\xa9'

data_dict = {29: [{
'type': 'prefix_sid',
'value': {
'sid': 25001,
'flags': {
'NP': 0,
'M': 1,
'E': 1,
'V': 0,
'L': 1
},
'algorithm': 0
}
}]}
self.assertEqual(data_dict, LinkState.unpack(data_bin, 3).dict())

def test_prefix_id_flags_isis(self):
self.maxDiff = None
data_bin = b'\x04\x86\x00\x07\xb4\x00\x00\x00\x00\x61\xa9'

data_dict = {29: [{
'type': 'prefix_sid',
'value': {
'sid': 25001,
'flags': {
'R': 1,
'N': 0,
'P': 1,
'E': 1,
'V': 0,
'L': 1
},
'algorithm': 0
}
}]}
self.assertEqual(data_dict, LinkState.unpack(data_bin, 1).dict())

0 comments on commit 034eeee

Please sign in to comment.