diff --git a/yabgp/message/attribute/linkstate/linkstate.py b/yabgp/message/attribute/linkstate/linkstate.py index 235fb09..a2537f4 100644 --- a/yabgp/message/attribute/linkstate/linkstate.py +++ b/yabgp/message/attribute/linkstate/linkstate.py @@ -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()) diff --git a/yabgp/message/attribute/linkstate/prefix/prefix_sid.py b/yabgp/message/attribute/linkstate/prefix/prefix_sid.py index 918929a..4444bb7 100644 --- a/yabgp/message/attribute/linkstate/prefix/prefix_sid.py +++ b/yabgp/message/attribute/linkstate/prefix/prefix_sid.py @@ -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): """ @@ -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)}) diff --git a/yabgp/tests/unit/message/attribute/nlri/test_linkstate_prefix_sid.py b/yabgp/tests/unit/message/attribute/nlri/test_linkstate_prefix_sid.py new file mode 100644 index 0000000..d40483f --- /dev/null +++ b/yabgp/tests/unit/message/attribute/nlri/test_linkstate_prefix_sid.py @@ -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())