Skip to content

Commit

Permalink
Add new class MPLSVPN for ipv4/ipv6 MPLS processing
Browse files Browse the repository at this point in the history
Signed-off-by: Peng Xiao <xiaoquwl@gmail.com>
  • Loading branch information
xiaopeng163 committed Jan 20, 2016
1 parent 8fd9371 commit a183887
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
44 changes: 44 additions & 0 deletions yabgp/message/attribute/nlri/mpls_vpn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright 2016 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 struct


class MPLSVPN(object):
"""The base class for ipv4/ipv6 mpls vpn
"""
WITHDARW_LABEL_HEX = b'\x80\x00\x00'
WITHDARW_LABEL = 524288

@classmethod
def parse_mpls_label_stack(cls, data):
labels = []
while len(data) >= 3:
label = struct.unpack('!L', b'\00'+data[:3])[0]
data = data[3:]
labels.append(label >> 4)
if label & 0x001:
break
return labels

@classmethod
def construct_mpls_label_stack(cls, labels, bos=True):
data = b''
for label in labels:
if bos:
data += struct.pack('!L', (label << 4 | 1))[1:]
continue
data += struct.pack('!L', label << 4)[1:]
return data
38 changes: 38 additions & 0 deletions yabgp/tests/unit/message/attribute/nlri/test_mpls_vpn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2016 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 MPLS VPN """

import unittest

from yabgp.message.attribute.nlri.mpls_vpn import MPLSVPN


class TestMPLSVPN(unittest.TestCase):

def test_parse_mpls_label_stack(self):
label_bin = b'\x00\x01\x41'
self.assertEqual([20], MPLSVPN.parse_mpls_label_stack(label_bin))
label_bin = b'\x00\x01\x10\x00\x01\x31'
self.assertEqual([17, 19], MPLSVPN.parse_mpls_label_stack(label_bin))
label_bin = b'\x80\x00\x00'
self.assertEqual([524288], MPLSVPN.parse_mpls_label_stack(label_bin))

def test_construct_mpls_label_stack(self):
label_bin = b'\x00\x01\x41'
self.assertEqual(label_bin, MPLSVPN.construct_mpls_label_stack(labels=[20]))

if __name__ == '__main__':
unittest.main()

0 comments on commit a183887

Please sign in to comment.