diff options
author | Shinpei Muraoka <shinpei.muraoka@gmail.com> | 2016-10-27 09:55:20 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2016-10-29 06:32:05 +0900 |
commit | 5360524b966417d042d441c208a7cf99eb052532 (patch) | |
tree | 6116f8246aceedac51eb2342ccc4ef6fdff7b798 | |
parent | a582b0b0dcce1061a85ff43daba896411d33b05c (diff) |
packet/vxlan: Add method to convert the format of vni
Signed-off-by: Shinpei Muraoka <shinpei.muraoka@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | ryu/lib/packet/vxlan.py | 22 | ||||
-rw-r--r-- | ryu/tests/unit/packet/test_vxlan.py | 7 |
2 files changed, 28 insertions, 1 deletions
diff --git a/ryu/lib/packet/vxlan.py b/ryu/lib/packet/vxlan.py index d68b9b62..c7de04b2 100644 --- a/ryu/lib/packet/vxlan.py +++ b/ryu/lib/packet/vxlan.py @@ -42,8 +42,10 @@ VXLAN Header: import struct import logging -from . import packet_base +import six +from . import packet_base +from ryu.lib import type_desc LOG = logging.getLogger(__name__) @@ -88,3 +90,21 @@ class vxlan(packet_base.PacketBase): def serialize(self, payload, prev): return struct.pack(self._PACK_STR, 1 << (3 + 24), self.vni << 8) + + +def vni_from_bin(buf): + """ + Converts binary representation VNI to integer. + :param buf: binary representation of VNI. + :return: VNI integer. + """ + return type_desc.Int3.to_user(six.binary_type(buf)) + + +def vni_to_bin(vni): + """ + Converts integer VNI to binary representation. + :param vni: integer of VNI + :return: binary representation of VNI. + """ + return type_desc.Int3.from_user(vni) diff --git a/ryu/tests/unit/packet/test_vxlan.py b/ryu/tests/unit/packet/test_vxlan.py index fe418ff7..8cace581 100644 --- a/ryu/tests/unit/packet/test_vxlan.py +++ b/ryu/tests/unit/packet/test_vxlan.py @@ -73,3 +73,10 @@ class Test_vxlan(unittest.TestCase): def test_to_jsondict(self): jsondict_from_pkt = self.pkt.to_jsondict() eq_(self.jsondict, jsondict_from_pkt) + + def test_vni_from_bin(self): + vni = vxlan.vni_from_bin(b'\x12\x34\x56') + eq_(self.vni, vni) + + def test_vni_to_bin(self): + eq_(b'\x12\x34\x56', vxlan.vni_to_bin(self.vni)) |