blob: cc34a576c0b5ef27ec47afd61364d09f4f9e7023 [file] [log] [blame]
Andreas Wundsam46d230f2013-08-02 22:24:06 -07001import loxi_utils.loxi_utils as loxi_utils
Andreas Wundsam27303462013-07-16 12:52:35 -07002import os
3import errno
4import re
5import subprocess
6import time
7
8def name_c_to_camel(name):
9 """ 'of_stats_reply' -> 'ofStatsReply' """
10 name = re.sub(r'^_','', name)
11 tokens = name.split('_')
12 for i in range(1, len(tokens)):
13 tokens[i] = tokens[i].title()
14 return "".join(tokens)
15
16def name_c_to_caps_camel(name):
17 """ 'of_stats_reply' to 'OFStatsReply' """
18 camel = name_c_to_camel(name.title())
19 if camel.startswith('Of'):
20 return camel.replace('Of','OF',1)
21 else:
22 return camel
23
Andreas Wundsame916d6f2013-07-30 11:33:58 -070024java_primitive_types = set("byte char short int long".split(" "))
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -070025
26### info table about java primitive types, for casting literals in the source code
27# { name : (signed?, length_in_bits) }
Andreas Wundsam46d230f2013-08-02 22:24:06 -070028java_primitives_info = {
29 'byte' : (True, 8),
30 'char' : (False, 16),
31 'short' : (True, 16),
32 'int' : (True, 32),
33 'long' : (True, 64),
34}
35
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -070036def format_primitive_literal(t, value):
37 """ Format a primitive numeric literal for inclusion in the
38 java source code. Takes care of casting the literal
39 apropriately for correct representation despite Java's
40 signed-craziness
41 """
Andreas Wundsam46d230f2013-08-02 22:24:06 -070042 signed, bits = java_primitives_info[t]
43 max = (1 << bits)-1
44 if value > max:
45 raise Exception("Value %d to large for type %s" % (value, t))
46
47 if signed:
48 max_pos = (1 << (bits-1)) - 1
49
50 if value > max_pos:
51 if t == "long":
52 return str((1 << bits) - value)
53 else:
54 return "(%s) 0x%x" % (t, value)
55 else:
Andreas Wundsam2bf357c2013-08-03 22:50:40 -070056 return "0x%x%s" % (value, "L" if t=="long" else "")
Yotam Harchold7b84202013-07-26 16:08:10 -070057
Andreas Wundsame916d6f2013-07-30 11:33:58 -070058ANY = 0xFFFFFFFFFFFFFFFF
Yotam Harchold7b84202013-07-26 16:08:10 -070059
60class VersionOp:
61 def __init__(self, version=ANY, read=None, write=None):
62 self.version = version
63 self.read = read
64 self.write = write
Andreas Wundsame916d6f2013-07-30 11:33:58 -070065
Yotam Harchold7b84202013-07-26 16:08:10 -070066 def __str__(self):
67 return "[Version: %d, Read: '%s', Write: '%s']" % (self.version, self.read, self.write)
68
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -070069### FIXME: This class should really be cleaned up
Andreas Wundsam27303462013-07-16 12:52:35 -070070class JType(object):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -070071 """ Wrapper class to hold C to Java type conversion information. JTypes can have a 'public'
72 and or 'private' java type associated with them and can define how those types can be
73 read from and written to ChannelBuffers.
74
75 """
Andreas Wundsam2bf357c2013-08-03 22:50:40 -070076 def __init__(self, pub_type, priv_type=None, read_op=None, write_op=None):
Andreas Wundsam27303462013-07-16 12:52:35 -070077 self.pub_type = pub_type # the type we expose externally, e.g. 'U8'
78 if priv_type is None:
79 priv_type = pub_type
80 self.priv_type = priv_type # the internal storage type
Yotam Harchold7b84202013-07-26 16:08:10 -070081 self.ops = {}
Yotam Harchold7b84202013-07-26 16:08:10 -070082
Andreas Wundsam46d230f2013-08-02 22:24:06 -070083 def op(self, version=ANY, read=None, write=None, pub_type=ANY):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -070084 """
85 define operations to be performed for reading and writing this type
86 (when read_op, write_op is called). The operations 'read' and 'write'
87 can either be strings ($name, and $version and $length will be replaced),
88 or callables (name, version and length) will be passed.
89
90 @param version int OF version to define operation for, or ANY for all
91 @param pub_type boolean whether to define operations for the public type (True), the
92 private type(False) or both (ALL)
93 @param read read expression (either string or callable)s
94 @param write write expression (either string or callable)
95 """
96
Andreas Wundsam46d230f2013-08-02 22:24:06 -070097 pub_types = [ pub_type ] if pub_type is not ANY else [ False, True ]
98 for pub_type in pub_types:
Andreas Wundsam2bf357c2013-08-03 22:50:40 -070099 self.ops[(version, pub_type)] = VersionOp(version, read, write)
Yotam Harchold7b84202013-07-26 16:08:10 -0700100 return self
Andreas Wundsam27303462013-07-16 12:52:35 -0700101
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700102 def format_value(self, value, pub_type=True):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700103 # Format a constant value of this type, for inclusion in the java source code
104 # For primitive types, takes care of casting the value appropriately, to
105 # cope with java's signedness limitation
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700106 t = self.pub_type if pub_type else self.priv_type
107 if t in java_primitive_types:
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700108 return format_primitive_literal(t, value)
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700109 else:
110 return value
Andreas Wundsambf1dbbd2013-07-30 11:07:59 -0700111
Andreas Wundsam27303462013-07-16 12:52:35 -0700112 @property
113 def public_type(self):
114 """ return the public type """
115 return self.pub_type
116
117 def priv(self):
118 """ return the public type """
119 return self.priv_type
120
121 def has_priv(self):
122 """ Is the private type different from the public one?"""
123 return self.pub_type != self.priv_type
124
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700125 def read_op(self, version=None, length=None, pub_type=True):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700126 """ return a Java stanza that reads a value of this JType from ChannelBuffer bb.
127 @param version int - OF wire version to generate expression for
128 @param pub_type boolean use this JTypes 'public' (True), or private (False) representation
129 @param length string, for operations that need it (e.g., read a list of unknown length)
130 Java expression evaluating to the byte length to be read. Defaults to the remainig
131 length of the message.
132 @return string containing generated Java expression.
133 """
Andreas Wundsam27303462013-07-16 12:52:35 -0700134 if length is None:
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700135 # assumes that
136 # (1) length of the message has been read to 'length'
137 # (2) readerIndex at the start of the message has been stored in 'start'
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700138 length = "length - (bb.readerIndex() - start)";
Andreas Wundsam27303462013-07-16 12:52:35 -0700139
Yotam Harchold7b84202013-07-26 16:08:10 -0700140 ver = ANY if version is None else version.int_version
141 _read_op = None
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700142 if (ver, pub_type) in self.ops:
143 _read_op = self.ops[(ver, pub_type)].read or self.ops[(ANY, pub_type)].read
144 elif (ANY, pub_type) in self.ops:
145 _read_op = self.ops[(ANY, pub_type)].read
Yotam Harchold7b84202013-07-26 16:08:10 -0700146 if _read_op is None:
Andreas Wundsam951ada32013-08-01 22:05:38 -0700147 _read_op = 'ChannelUtilsVer$version.read%s(bb)' % self.pub_type
Yotam Harchold7b84202013-07-26 16:08:10 -0700148 if callable(_read_op):
149 return _read_op(version)
Andreas Wundsam27303462013-07-16 12:52:35 -0700150 else:
Yotam Harchold7b84202013-07-26 16:08:10 -0700151 return _read_op.replace("$length", str(length)).replace("$version", version.of_version)
Andreas Wundsam27303462013-07-16 12:52:35 -0700152
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700153 def write_op(self, version=None, name=None, pub_type=True):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700154 """ return a Java stanza that writes a value of this JType contained in Java expression
155 'name' to ChannelBuffer bb.
156 @param name string containing Java expression that evaluations to the value to be written
157 @param version int - OF wire version to generate expression for
158 @param pub_type boolean use this JTypes 'public' (True), or private (False) representation
159 @return string containing generated Java expression.
160 """
Yotam Harchold7b84202013-07-26 16:08:10 -0700161 ver = ANY if version is None else version.int_version
162 _write_op = None
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700163 if (ver, pub_type) in self.ops:
164 _write_op = self.ops[(ver, pub_type)].write or self.ops[(ANY, pub_type)].write
165 elif (ANY, pub_type) in self.ops:
166 _write_op = self.ops[(ANY, pub_type)].write
Yotam Harchold7b84202013-07-26 16:08:10 -0700167 if _write_op is None:
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700168
Andreas Wundsam951ada32013-08-01 22:05:38 -0700169 _write_op = 'ChannelUtilsVer$version.write%s(bb, $name)' % self.pub_type
Yotam Harchold7b84202013-07-26 16:08:10 -0700170 if callable(_write_op):
171 return _write_op(version, name)
Andreas Wundsam27303462013-07-16 12:52:35 -0700172 else:
Yotam Harchold7b84202013-07-26 16:08:10 -0700173 return _write_op.replace("$name", str(name)).replace("$version", version.of_version)
Andreas Wundsam27303462013-07-16 12:52:35 -0700174
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700175 def skip_op(self, version=None, length=None):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700176 """ return a java stanza that skips an instance of JType in the input ChannelBuffer 'bb'.
177 This is used in the Reader implementations for virtual classes (because after the
178 discriminator field, the concrete Reader instance will re-read all the fields)
179 Currently just delegates to read_op + throws away the result."""
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700180 return self.read_op(version, length)
181
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700182 @property
183 def is_primitive(self):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700184 """ return true if the pub_type is a java primitive type (and thus needs
185 special treatment, because it doesn't have methods)"""
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700186 return self.pub_type in java_primitive_types
187
188 @property
189 def is_array(self):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700190 """ return true iff the pub_type is a Java array (and thus requires special
191 treament for equals / toString etc.) """
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700192 return self.pub_type.endswith("[]")
193
194
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700195##### Predefined JType mappings
196# FIXME: This list needs to be pruned / cleaned up. Most of these are schematic.
197
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700198u8 = JType('short', 'byte') \
Yotam Harchold7b84202013-07-26 16:08:10 -0700199 .op(read='bb.readByte()', write='bb.writeByte($name)')
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700200u8_list = JType('List<U8>') \
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700201 .op(read='ChannelUtils.readList(bb, $length, U8.READER)', write='ChannelUtils.writeList(bb, $name)')
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700202u16 = JType('int', 'short') \
203 .op(read='U16.f(bb.readShort())', write='bb.writeShort(U16.t($name))', pub_type=True) \
204 .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False)
205u32 = JType('long', 'int') \
206 .op(read='U32.f(bb.readInt())', write='bb.writeInt(U32.t($name))', pub_type=True) \
207 .op(read='bb.readInt()', write='bb.writeInt($name)', pub_type=False)
208u32_list = JType('List<U32>', 'int[]') \
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700209 .op(read='ChannelUtils.readList(bb, $length, U32.READER)', write='ChannelUtils.writeList(bb, $name)')
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700210u64 = JType('U64', 'U64') \
Yotam Harchold7b84202013-07-26 16:08:10 -0700211 .op(read='U64.of(bb.readLong())', write='bb.writeLong($name.getValue())')
212of_port = JType("OFPort") \
213 .op(version=1, read="OFPort.read2Bytes(bb)", write="$name.write2Bytes(bb)") \
214 .op(version=ANY, read="OFPort.read4Bytes(bb)", write="$name.write4Bytes(bb)")
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700215actions_list = JType('List<OFAction>') \
216 .op(read='ChannelUtils.readList(bb, $length, OFActionVer$version.READER)', write='ChannelUtils.writeList(bb, $name);')
217instructions_list = JType('List<OFInstruction>') \
218 .op(read='ChannelUtils.readList(bb, $length, OFInstructionVer$version.READER)', \
219 write='ChannelUtils.writeList(bb, $name)')
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700220buckets_list = JType('List<OFBucket>') \
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700221 .op(read='ChannelUtils.readList(bb, $length, OFBucketVer$version.READER)', write='ChannelUtils.writeList(bb, $name)')
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700222port_desc_list = JType('List<OFPhysicalPort>') \
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700223 .op(read='ChannelUtils.readList(bb, $length, OFPhysicalPort.READER)', write='ChannelUtils.writeList(bb, $name)')
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700224port_desc = JType('OFPortDesc') \
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700225 .op(read='OFPortDescVer$version.READER.readFrom(bb)', \
Yotam Harchold7b84202013-07-26 16:08:10 -0700226 write='$name.writeTo(bb)')
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700227packet_queue_list = JType('List<OFPacketQueue>') \
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700228 .op(read='ChannelUtils.readList(bb, $length, OFPacketQueueVer$version.READER)', write='ChannelUtils.writeList(bb, $name);')
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700229octets = JType('byte[]') \
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700230 .op(read='ChannelUtils.readBytes(bb, $length)', \
Yotam Harchold7b84202013-07-26 16:08:10 -0700231 write='bb.writeBytes($name)')
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700232of_match = JType('Match') \
Andreas Wundsam951ada32013-08-01 22:05:38 -0700233 .op(read='ChannelUtilsVer$version.readOFMatch(bb)', \
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700234 write='$name.writeTo(bb)');
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700235flow_mod_cmd = JType('OFFlowModCommand', 'short') \
Yotam Harchold7b84202013-07-26 16:08:10 -0700236 .op(version=1, read="bb.readShort()", write="bb.writeShort($name)") \
237 .op(version=ANY, read="bb.readByte()", write="bb.writeByte($name)")
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700238mac_addr = JType('MacAddress') \
Yotam Harchold7b84202013-07-26 16:08:10 -0700239 .op(read="MacAddress.read6Bytes(bb)", \
240 write="$name.write6Bytes(bb)")
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700241port_name = JType('String') \
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700242 .op(read='ChannelUtils.readFixedLengthString(bb, 16)', \
243 write='ChannelUtils.writeFixedLengthString(bb, $name, 16)')
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700244desc_str = JType('String') \
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700245 .op(read='ChannelUtils.readFixedLengthString(bb, 256)', \
246 write='ChannelUtils.writeFixedLengthString(bb, $name, 256)')
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700247serial_num = JType('String') \
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700248 .op(read='ChannelUtils.readFixedLengthString(bb, 32)', \
249 write='ChannelUtils.writeFixedLengthString(bb, $name, 32)')
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700250table_name = JType('String') \
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700251 .op(read='ChannelUtils.readFixedLengthString(bb, 32)', \
252 write='ChannelUtils.writeFixedLengthString(bb, $name, 32)')
Yotam Harchold7b84202013-07-26 16:08:10 -0700253ipv4 = JType("IPv4") \
254 .op(read="IPv4.read4Bytes(bb)", \
255 write="$name.write4Bytes(bb)")
256ipv6 = JType("IPv6") \
257 .op(read="IPv6.read16Bytes(bb)", \
258 write="$name.write16Bytes(bb)")
Andreas Wundsambf1dbbd2013-07-30 11:07:59 -0700259packetin_reason = JType("OFPacketInReason")\
260 .op(read="OFPacketInReasonSerializerVer$version.readFrom(bb)", write="OFPacketInReasonSerializerVer$version.writeTo(bb, $name)")
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700261wildcards = JType("Wildcards")\
262 .op(read="Wildcards.of(bb.readInt())", write="bb.writeInt($name.getInt())");
263transport_port = JType("TransportPort")\
264 .op(read="TransportPort.read2Bytes(bb)", write="$name.write2Bytes(bb)")
265oxm = JType("OFOxm")\
266 .op(read="OFOxmVer$version.READER.readFrom(bb)", write="$name.writeTo(bb)")
267meter_features = JType("OFMeterFeatures")\
268 .op(read="OFMeterFeaturesVer$version.READER.readFrom(bb)", write="$name.writeTo(bb)")
269
Andreas Wundsam27303462013-07-16 12:52:35 -0700270
271default_mtype_to_jtype_convert_map = {
272 'uint8_t' : u8,
273 'uint16_t' : u16,
274 'uint32_t' : u32,
275 'uint64_t' : u64,
Andreas Wundsam27303462013-07-16 12:52:35 -0700276 'of_port_no_t' : of_port,
277 'list(of_action_t)' : actions_list,
278 'list(of_instruction_t)' : instructions_list,
279 'list(of_bucket_t)': buckets_list,
280 'list(of_port_desc_t)' : port_desc_list,
281 'list(of_packet_queue_t)' : packet_queue_list,
282 'list(of_uint32_t)' : u32_list,
283 'list(of_uint8_t)' : u8_list,
284 'of_octets_t' : octets,
285 'of_match_t': of_match,
286 'of_fm_cmd_t': flow_mod_cmd,
287 'of_mac_addr_t': mac_addr,
288 'of_port_desc_t': port_desc,
289 'of_desc_str_t': desc_str,
290 'of_serial_num_t': serial_num,
291 'of_port_name_t': port_name,
292 'of_table_name_t': table_name,
293 'of_ipv4_t': ipv4,
294 'of_ipv6_t': ipv6,
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700295 'of_wc_bmap_t': wildcards,
296 'of_oxm_t': oxm,
297 'of_meter_features_t': meter_features,
Andreas Wundsam27303462013-07-16 12:52:35 -0700298 }
299
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700300## Map that defines exceptions from the standard loxi->java mapping scheme
301# map of {<loxi_class_name> : { <loxi_member_name> : <JType instance> } }
Andreas Wundsam27303462013-07-16 12:52:35 -0700302exceptions = {
Andreas Wundsambf1dbbd2013-07-30 11:07:59 -0700303 'of_packet_in': {
304 'data' : octets,
305 'reason': packetin_reason
Andreas Wundsam27303462013-07-16 12:52:35 -0700306 },
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700307 'of_oxm_tcp_src' : {
308 'value' : transport_port
309 },
Andreas Wundsam27303462013-07-16 12:52:35 -0700310}
311
312
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700313# Create a default mapping for a list type. Type defauls to List<${java_mapping_of_name}>
Andreas Wundsam27303462013-07-16 12:52:35 -0700314def make_standard_list_jtype(c_type):
315 m = re.match(r'list\(of_([a-zA-Z_]+)_t\)', c_type)
316 if not m:
317 raise Exception("Not a recgonized standard list type declaration: %s" % c_type)
318 base_name = m.group(1)
319 java_base_name = name_c_to_caps_camel(base_name)
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700320
321 # read op assumes the class has a public final static field READER that implements
322 # OFMessageReader<$class> i.e., can deserialize an instance of class from a ChannelBuffer
323 # write op assumes class implements Writeable
Yotam Harchold7b84202013-07-26 16:08:10 -0700324 return JType("List<OF%s>" % java_base_name) \
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700325 .op(
326 read= 'ChannelUtils.readList(bb, $length, OF%sVer$version.READER)' % java_base_name, \
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700327 write='ChannelUtils.writeList(bb, $name)')
Andreas Wundsam27303462013-07-16 12:52:35 -0700328
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700329
330#### main entry point for conversion of LOXI types (c_types) Java types.
331# FIXME: This badly needs a refactoring
332
Andreas Wundsam27303462013-07-16 12:52:35 -0700333def convert_to_jtype(obj_name, field_name, c_type):
334 """ Convert from a C type ("uint_32") to a java type ("U32")
335 and return a JType object with the size, internal type, and marshalling functions"""
336 if obj_name in exceptions and field_name in exceptions[obj_name]:
337 return exceptions[obj_name][field_name]
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700338 elif ( obj_name == "of_header" or loxi_utils.class_is_message(obj_name)) and field_name == "type" and c_type == "uint8_t":
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700339 return JType("OFType", 'byte') \
Yotam Harchold7b84202013-07-26 16:08:10 -0700340 .op(read='bb.readByte()', write='bb.writeByte($name)')
Andreas Wundsam27303462013-07-16 12:52:35 -0700341 elif field_name == "type" and re.match(r'of_action.*', obj_name):
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700342 return JType("OFActionType", 'short') \
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700343 .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False)\
344 .op(read="OFActionTypeSerializerVer$version.readFrom(bb)", write="OFActionTypeSerializerVer$version.writeTo(bb, $name)", pub_type=True)
Andreas Wundsam27303462013-07-16 12:52:35 -0700345 elif field_name == "version" and c_type == "uint8_t":
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700346 return JType("OFVersion", 'byte') \
Yotam Harchold7b84202013-07-26 16:08:10 -0700347 .op(read='bb.readByte()', write='bb.writeByte($name)')
Andreas Wundsam27303462013-07-16 12:52:35 -0700348 elif c_type in default_mtype_to_jtype_convert_map:
349 return default_mtype_to_jtype_convert_map[c_type]
350 elif re.match(r'list\(of_([a-zA-Z_]+)_t\)', c_type):
351 return make_standard_list_jtype(c_type)
352 else:
353 print "WARN: Couldn't find java type conversion for '%s' in %s:%s" % (c_type, obj_name, field_name)
354 jtype = name_c_to_caps_camel(re.sub(r'_t$', "", c_type))
355 return JType(jtype)
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700356
357
358#### Enum specific wiretype definitions
359enum_wire_types = {
360 "uint8_t": JType("byte").op(read="bb.readByte()", write="bb.writeByte($name)"),
361 "uint16_t": JType("short").op(read="bb.readShort()", write="bb.writeShort($name)"),
362 "uint32_t": JType("int").op(read="bb.readInt()", write="bb.writeInt($name)"),
363 "uint64_t": JType("long").op(read="bb.readLong()", write="bb.writeLong($name)"),
364}
365
366def convert_enum_wire_type_to_jtype(wire_type):
367 return enum_wire_types[wire_type]