blob: ce31049e42dc27778850086f628cabb451f0c011 [file] [log] [blame]
Rich Laneca3da272013-05-05 09:07:33 -07001# Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior University
2# Copyright (c) 2011, 2012 Open Networking Foundation
3# Copyright (c) 2012, 2013 Big Switch Networks, Inc.
Dan Talaycof6202252013-07-02 01:00:29 -07004# See the file LICENSE.pyloxi which should have been included in the source distribution
Rich Laneca3da272013-05-05 09:07:33 -07005"""
6Utility functions independent of the protocol version
7"""
8
9# Automatically generated by LOXI from template generic_util.py
10# Do not modify
11
12import loxi
13import struct
14
15def unpack_list(reader, deserializer):
16 """
17 The deserializer function should take an OFReader and return the new object.
18 """
19 entries = []
20 while not reader.is_empty():
21 entries.append(deserializer(reader))
22 return entries
23
24def unpack_list_lv16(reader, deserializer):
25 """
26 The deserializer function should take an OFReader and return the new object.
27 """
28 def wrapper(reader):
29 length, = reader.peek('!H')
30 return deserializer(reader.slice(length))
31 return unpack_list(reader, wrapper)
32
33def unpack_list_tlv16(reader, deserializer):
34 """
35 The deserializer function should take an OFReader and an integer type
36 and return the new object.
37 """
38 def wrapper(reader):
39 typ, length, = reader.peek('!HH')
40 return deserializer(reader.slice(length), typ)
41 return unpack_list(reader, wrapper)
42
43class OFReader(object):
44 """
45 Cursor over a read-only buffer
46
47 OpenFlow messages are best thought of as a sequence of elements of
48 variable size, rather than a C-style struct with fixed offsets and
49 known field lengths. This class supports efficiently reading
50 fields sequentially and is intended to be used recursively by the
51 parsers of child objects which will implicitly update the offset.
52 """
53 def __init__(self, buf):
54 self.buf = buf
55 self.offset = 0
56
57 def read(self, fmt):
58 st = struct.Struct(fmt)
59 if self.offset + st.size > len(self.buf):
60 raise loxi.ProtocolError("Buffer too short")
61 result = st.unpack_from(self.buf, self.offset)
62 self.offset += st.size
63 return result
64
65 def read_all(self):
66 buf = buffer(self.buf, self.offset)
67 self.offset += len(buf)
68 return str(buf)
69
70 def peek(self, fmt):
71 st = struct.Struct(fmt)
72 if self.offset + st.size > len(self.buf):
73 raise loxi.ProtocolError("Buffer too short")
74 result = st.unpack_from(self.buf, self.offset)
75 return result
76
77 def skip(self, length):
78 if self.offset + length > len(self.buf):
79 raise loxi.ProtocolError("Buffer too short")
80 self.offset += length
81
82 def is_empty(self):
83 return self.offset == len(self.buf)
84
85 # Used when parsing variable length objects which have external length
86 # fields (e.g. the actions list in an OF 1.0 packet-out message).
87 def slice(self, length):
88 if self.offset + length > len(self.buf):
89 raise loxi.ProtocolError("Buffer too short")
90 buf = OFReader(buffer(self.buf, self.offset, length))
91 self.offset += length
92 return buf