summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--ryu/lib/packet/vxlan.py22
-rw-r--r--ryu/tests/unit/packet/test_vxlan.py7
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))