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>
This commit is contained in:
Shinpei Muraoka 2016-10-27 09:55:20 +09:00 committed by FUJITA Tomonori
parent a582b0b0dc
commit 5360524b96
2 changed files with 28 additions and 1 deletions

View File

@ -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)

View File

@ -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))