blob: ac1e67213fe9527f765918e3414b2165188a7961 [file] [log] [blame]
Yotam Harcholc742e202013-08-15 12:16:24 -07001import errno
Andreas Wundsam27303462013-07-16 12:52:35 -07002import os
Andreas Wundsam27303462013-07-16 12:52:35 -07003import re
4import subprocess
5import time
6
Andreas Wundsamc8912c12013-11-15 13:44:48 -08007import loxi_globals
Andreas Wundsam7cfeac32013-09-17 13:53:48 -07008from generic_utils import memoize
9import loxi_utils.loxi_utils as loxi_utils
Andreas Wundsam7cfeac32013-09-17 13:53:48 -070010
Andreas Wundsam2be7da52013-08-22 07:34:25 -070011def erase_type_annotation(class_name):
12 m=re.match(r'(.*)<.*>', class_name)
13 if m:
14 return m.group(1)
15 else:
16 return class_name
17
Andreas Wundsam27303462013-07-16 12:52:35 -070018def name_c_to_camel(name):
19 """ 'of_stats_reply' -> 'ofStatsReply' """
20 name = re.sub(r'^_','', name)
21 tokens = name.split('_')
22 for i in range(1, len(tokens)):
23 tokens[i] = tokens[i].title()
24 return "".join(tokens)
25
26def name_c_to_caps_camel(name):
27 """ 'of_stats_reply' to 'OFStatsReply' """
28 camel = name_c_to_camel(name.title())
Andreas Wundsam7cfeac32013-09-17 13:53:48 -070029 if camel.startswith('Ofp'):
30 return camel.replace('Ofp','OF',1)
31 elif camel.startswith('Of'):
Andreas Wundsam27303462013-07-16 12:52:35 -070032 return camel.replace('Of','OF',1)
33 else:
34 return camel
35
Rob Vaterlausfeee3712013-09-30 11:24:19 -070036java_primitive_types = set("boolean byte char short int long".split(" "))
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -070037
38### info table about java primitive types, for casting literals in the source code
39# { name : (signed?, length_in_bits) }
Andreas Wundsam46d230f2013-08-02 22:24:06 -070040java_primitives_info = {
Andreas Wundsam438a9c32013-10-07 16:18:52 -070041 'boolean' : (False, 8, False),
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070042 'byte' : (True, 8, True),
43 'char' : (False, 16, True),
44 'short' : (True, 16, True),
45 'int' : (True, 32, False),
46 'long' : (True, 64, False),
Andreas Wundsam46d230f2013-08-02 22:24:06 -070047}
48
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -070049def format_primitive_literal(t, value):
50 """ Format a primitive numeric literal for inclusion in the
51 java source code. Takes care of casting the literal
Rob Vaterlausfeee3712013-09-30 11:24:19 -070052 appropriately for correct representation despite Java's
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -070053 signed-craziness
54 """
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070055 signed, bits, cast_needed = java_primitives_info[t]
Andreas Wundsam46d230f2013-08-02 22:24:06 -070056 max = (1 << bits)-1
57 if value > max:
58 raise Exception("Value %d to large for type %s" % (value, t))
59
60 if signed:
61 max_pos = (1 << (bits-1)) - 1
62
63 if value > max_pos:
64 if t == "long":
65 return str((1 << bits) - value)
66 else:
67 return "(%s) 0x%x" % (t, value)
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070068 return "%s0x%x%s" % ("(%s) " % t if cast_needed else "", value, "L" if t=="long" else "")
69
Yotam Harchold7b84202013-07-26 16:08:10 -070070
Andreas Wundsame916d6f2013-07-30 11:33:58 -070071ANY = 0xFFFFFFFFFFFFFFFF
Yotam Harchold7b84202013-07-26 16:08:10 -070072
73class VersionOp:
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070074 def __init__(self, version=ANY, read=None, write=None, default=None, funnel=None):
Yotam Harchold7b84202013-07-26 16:08:10 -070075 self.version = version
76 self.read = read
77 self.write = write
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -070078 self.default = default
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070079 self.funnel = funnel
Andreas Wundsame916d6f2013-07-30 11:33:58 -070080
Yotam Harchold7b84202013-07-26 16:08:10 -070081 def __str__(self):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070082 return "[Version: %d, Read: '%s', Write: '%s', Default: '%s', Funnel: '%s' ]" % (self.version, self.read, self.write, self.default, self.funnel )
Yotam Harchold7b84202013-07-26 16:08:10 -070083
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -070084### FIXME: This class should really be cleaned up
Andreas Wundsam27303462013-07-16 12:52:35 -070085class JType(object):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -070086 """ Wrapper class to hold C to Java type conversion information. JTypes can have a 'public'
87 and or 'private' java type associated with them and can define how those types can be
88 read from and written to ChannelBuffers.
89
90 """
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -070091 def __init__(self, pub_type, priv_type=None):
Andreas Wundsam27303462013-07-16 12:52:35 -070092 self.pub_type = pub_type # the type we expose externally, e.g. 'U8'
93 if priv_type is None:
94 priv_type = pub_type
95 self.priv_type = priv_type # the internal storage type
Yotam Harchold7b84202013-07-26 16:08:10 -070096 self.ops = {}
Yotam Harchold7b84202013-07-26 16:08:10 -070097
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070098 def set_priv_type(self, priv_type):
99 self.priv_type = priv_type
100 return self
101
102 def op(self, version=ANY, read=None, write=None, default=None, funnel=None, pub_type=ANY):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700103 """
104 define operations to be performed for reading and writing this type
105 (when read_op, write_op is called). The operations 'read' and 'write'
106 can either be strings ($name, and $version and $length will be replaced),
107 or callables (name, version and length) will be passed.
108
109 @param version int OF version to define operation for, or ANY for all
110 @param pub_type boolean whether to define operations for the public type (True), the
111 private type(False) or both (ALL)
112 @param read read expression (either string or callable)s
113 @param write write expression (either string or callable)
114 """
115
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700116 pub_types = [ pub_type ] if pub_type is not ANY else [ False, True ]
117 for pub_type in pub_types:
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700118 self.ops[(version, pub_type)] = VersionOp(version, read, write, default, funnel)
Yotam Harchold7b84202013-07-26 16:08:10 -0700119 return self
Andreas Wundsam27303462013-07-16 12:52:35 -0700120
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700121 def format_value(self, value, pub_type=True):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700122 # Format a constant value of this type, for inclusion in the java source code
123 # For primitive types, takes care of casting the value appropriately, to
124 # cope with java's signedness limitation
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700125 t = self.pub_type if pub_type else self.priv_type
126 if t in java_primitive_types:
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700127 return format_primitive_literal(t, value)
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700128 else:
129 return value
Andreas Wundsambf1dbbd2013-07-30 11:07:59 -0700130
Andreas Wundsam27303462013-07-16 12:52:35 -0700131 @property
132 def public_type(self):
133 """ return the public type """
134 return self.pub_type
135
136 def priv(self):
137 """ return the public type """
138 return self.priv_type
139
140 def has_priv(self):
141 """ Is the private type different from the public one?"""
142 return self.pub_type != self.priv_type
143
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700144 def get_op(self, op_type, version, pub_type, default_value, arguments):
145 ver = ANY if version is None else version.int_version
146
147 if not "version" in arguments:
Andreas Wundsamc8912c12013-11-15 13:44:48 -0800148 arguments["version"] = version.dotless_version
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700149
150 def lookup(ver, pub_type):
151 if (ver, pub_type) in self.ops:
152 return getattr(self.ops[(ver, pub_type)], op_type)
153 else:
154 return None
155
156 _op = lookup(ver, pub_type) or lookup(ANY, pub_type) or default_value
157 if callable(_op):
158 return _op(**arguments)
159 else:
160 return reduce(lambda a,repl: a.replace("$%s" % repl[0], str(repl[1])), arguments.items(), _op)
161
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700162 def read_op(self, version=None, length=None, pub_type=True):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700163 """ return a Java stanza that reads a value of this JType from ChannelBuffer bb.
164 @param version int - OF wire version to generate expression for
165 @param pub_type boolean use this JTypes 'public' (True), or private (False) representation
166 @param length string, for operations that need it (e.g., read a list of unknown length)
167 Java expression evaluating to the byte length to be read. Defaults to the remainig
168 length of the message.
169 @return string containing generated Java expression.
170 """
Andreas Wundsam27303462013-07-16 12:52:35 -0700171 if length is None:
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700172 # assumes that
173 # (1) length of the message has been read to 'length'
174 # (2) readerIndex at the start of the message has been stored in 'start'
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700175 length = "length - (bb.readerIndex() - start)"
Andreas Wundsam27303462013-07-16 12:52:35 -0700176
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700177 return self.get_op("read", version, pub_type,
178 default_value='ChannelUtilsVer$version.read%s(bb)' % self.pub_type,
179 arguments=dict(length=length)
180 )
Andreas Wundsam27303462013-07-16 12:52:35 -0700181
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700182 def write_op(self, version=None, name=None, pub_type=True):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700183 """ return a Java stanza that writes a value of this JType contained in Java expression
184 'name' to ChannelBuffer bb.
185 @param name string containing Java expression that evaluations to the value to be written
186 @param version int - OF wire version to generate expression for
187 @param pub_type boolean use this JTypes 'public' (True), or private (False) representation
188 @return string containing generated Java expression.
189 """
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700190
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700191 return self.get_op("write", version, pub_type,
192 default_value='ChannelUtilsVer$version.write%s(bb, $name)' % self.pub_type,
193 arguments=dict(name=name)
194 )
195
Andreas Wundsam27303462013-07-16 12:52:35 -0700196
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700197 def default_op(self, version=None, pub_type=True):
198 """ return a Java stanza that returns a default value of this JType.
199 @param version JavaOFVersion
200 @return string containing generated Java expression.
201 """
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700202 return self.get_op("default", version, pub_type,
203 arguments = dict(),
204 default_value = self.format_value(0) if self.is_primitive else "null"
205 )
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700206
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700207 def skip_op(self, version=None, length=None):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700208 """ return a java stanza that skips an instance of JType in the input ChannelBuffer 'bb'.
209 This is used in the Reader implementations for virtual classes (because after the
210 discriminator field, the concrete Reader instance will re-read all the fields)
211 Currently just delegates to read_op + throws away the result."""
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700212 return self.read_op(version, length)
213
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700214 def funnel_op(self, version=None, name=None, pub_type=True):
215 t = self.pub_type if pub_type else self.priv_type
216 return self.get_op("funnel", version, pub_type,
217 arguments = dict(name=name),
218 default_value = '$name.putTo(sink)' if not self._is_primitive(pub_type) else "sink.put{}($name)".format(t[0].upper() + t[1:])
219 )
220
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700221 @property
222 def is_primitive(self):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700223 return self._is_primitive()
224
225 def _is_primitive(self, pub_type=True):
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700226 """ return true if the pub_type is a java primitive type (and thus needs
227 special treatment, because it doesn't have methods)"""
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700228 t = self.pub_type if pub_type else self.priv_type
229 return t in java_primitive_types
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700230
231 @property
232 def is_array(self):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700233 return self._is_array()
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700234
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700235 def _is_array(self, pub_type=True):
236 t = self.pub_type if pub_type else self.priv_type
237 return t.endswith("[]")
238
239# Create a default mapping for a list type. Type defauls to List<${java_mapping_of_name}>
240def gen_enum_jtype(java_name, is_bitmask=False):
241 if is_bitmask:
242 java_type = "Set<{}>".format(java_name)
243 default_value = "ImmutableSet.<{}>of()".format(java_name)
244 else:
245 java_type = java_name
246 default_value = "null"
247
248 serializer = "{}SerializerVer$version".format(java_name)
249
250 return JType(java_type)\
251 .op(read="{}.readFrom(bb)".format(serializer),
252 write="{}.writeTo(bb, $name)".format(serializer),
253 default=default_value,
254 funnel="{}.putTo($name, sink)".format(serializer)
255 )
256
257def gen_list_jtype(java_base_name):
258 # read op assumes the class has a public final static field READER that implements
259 # OFMessageReader<$class> i.e., can deserialize an instance of class from a ChannelBuffer
260 # write op assumes class implements Writeable
261 return JType("List<{}>".format(java_base_name)) \
262 .op(
263 read= 'ChannelUtils.readList(bb, $length, {}Ver$version.READER)'.format(java_base_name), \
264 write='ChannelUtils.writeList(bb, $name)',
265 default="ImmutableList.<{}>of()".format(java_base_name),
266 funnel='FunnelUtils.putList($name, sink)'
267 )
268
269def gen_fixed_length_string_jtype(length):
270 return JType('String').op(
271 read='ChannelUtils.readFixedLengthString(bb, {})'.format(length),
272 write='ChannelUtils.writeFixedLengthString(bb, $name, {})'.format(length),
273 default='""',
274 funnel='sink.putUnencodedChars($name)'
275 )
Andreas Wundsame916d6f2013-07-30 11:33:58 -0700276
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700277##### Predefined JType mappings
278# FIXME: This list needs to be pruned / cleaned up. Most of these are schematic.
279
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700280u8 = JType('short', 'byte') \
Andreas Wundsam83d877a2013-09-30 14:26:44 -0700281 .op(read='U8.f(bb.readByte())', write='bb.writeByte(U8.t($name))', pub_type=True) \
282 .op(read='bb.readByte()', write='bb.writeByte($name)', pub_type=False)
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700283u8_list = JType('List<U8>') \
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700284 .op(read='ChannelUtils.readList(bb, $length, U8.READER)',
285 write='ChannelUtils.writeList(bb, $name)',
286 default='ImmutableList.<U8>of()',
287 funnel='FunnelUtils.putList($name, sink)'
288 )
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700289u16 = JType('int', 'short') \
290 .op(read='U16.f(bb.readShort())', write='bb.writeShort(U16.t($name))', pub_type=True) \
291 .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False)
292u32 = JType('long', 'int') \
293 .op(read='U32.f(bb.readInt())', write='bb.writeInt(U32.t($name))', pub_type=True) \
294 .op(read='bb.readInt()', write='bb.writeInt($name)', pub_type=False)
295u32_list = JType('List<U32>', 'int[]') \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700296 .op(
297 read='ChannelUtils.readList(bb, $length, U32.READER)',
298 write='ChannelUtils.writeList(bb, $name)',
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700299 default="ImmutableList.<U32>of()",
300 funnel="FunnelUtils.putList($name, sink)")
xinwuf08ef682013-12-05 18:29:20 -0800301u64_list = JType('List<U64>', 'int[]') \
302 .op(
303 read='ChannelUtils.readList(bb, $length, U64.READER)',
304 write='ChannelUtils.writeList(bb, $name)',
305 default="ImmutableList.<U64>of()",
306 funnel="FunnelUtils.putList($name, sink)")
Yotam Harchol5804f772013-08-21 17:35:31 -0700307u8obj = JType('U8', 'U8') \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700308 .op(read='U8.of(bb.readByte())', write='bb.writeByte($name.getRaw())', default="U8.ZERO")
Yotam Harchol5804f772013-08-21 17:35:31 -0700309u32obj = JType('U32', 'U32') \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700310 .op(read='U32.of(bb.readInt())', write='bb.writeInt($name.getRaw())', default="U32.ZERO")
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700311u64 = JType('U64', 'U64') \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700312 .op(read='U64.ofRaw(bb.readLong())', write='bb.writeLong($name.getValue())', default="U64.ZERO")
Yotam Harchold7b84202013-07-26 16:08:10 -0700313of_port = JType("OFPort") \
Andreas Wundsamad499c92013-09-28 18:56:49 -0700314 .op(version=1, read="OFPort.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="OFPort.ANY") \
315 .op(version=ANY, read="OFPort.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="OFPort.ANY")
Andreas Wundsame962d372013-10-02 18:15:58 -0700316# the same OFPort, but with a default value of ZERO, only for OF10 match
317of_port_match_v1 = JType("OFPort") \
318 .op(version=1, read="OFPort.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="OFPort.ZERO")
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700319actions_list = gen_list_jtype("OFAction")
320instructions_list = gen_list_jtype("OFInstruction")
321buckets_list = gen_list_jtype("OFBucket")
322port_desc_list = gen_list_jtype("OFPortDesc")
323packet_queue_list = gen_list_jtype("OFPacketQueue")
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700324port_desc = JType('OFPortDesc') \
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700325 .op(read='OFPortDescVer$version.READER.readFrom(bb)', \
Yotam Harchold7b84202013-07-26 16:08:10 -0700326 write='$name.writeTo(bb)')
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700327octets = JType('byte[]')\
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700328 .op(read='ChannelUtils.readBytes(bb, $length)', \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700329 write='bb.writeBytes($name)', \
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700330 default="new byte[0]",
331 funnel="sink.putBytes($name)"
332 );
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700333of_match = JType('Match') \
Andreas Wundsam951ada32013-08-01 22:05:38 -0700334 .op(read='ChannelUtilsVer$version.readOFMatch(bb)', \
Andreas Wundsame962d372013-10-02 18:15:58 -0700335 write='$name.writeTo(bb)',
336 default="OFFactoryVer$version.MATCH_WILDCARD_ALL");
Andreas Wundsam5812cf32013-11-15 13:51:24 -0800337group_mod_cmd = JType('OFGroupModCommand', 'short') \
338 .op(version=ANY, read="bb.readShort()", write="bb.writeShort($name)")
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700339flow_mod_cmd = JType('OFFlowModCommand', 'short') \
Yotam Harchold7b84202013-07-26 16:08:10 -0700340 .op(version=1, read="bb.readShort()", write="bb.writeShort($name)") \
341 .op(version=ANY, read="bb.readByte()", write="bb.writeByte($name)")
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700342mac_addr = JType('MacAddress') \
Yotam Harchold7b84202013-07-26 16:08:10 -0700343 .op(read="MacAddress.read6Bytes(bb)", \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700344 write="$name.write6Bytes(bb)",
345 default="MacAddress.NONE")
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700346
347port_name = gen_fixed_length_string_jtype(16)
348desc_str = gen_fixed_length_string_jtype(256)
349serial_num = gen_fixed_length_string_jtype(32)
350table_name = gen_fixed_length_string_jtype(32)
Yotam Harchola289d552013-09-16 10:10:40 -0700351ipv4 = JType("IPv4Address") \
352 .op(read="IPv4Address.read4Bytes(bb)", \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700353 write="$name.write4Bytes(bb)",
354 default='IPv4Address.NONE')
Yotam Harchola289d552013-09-16 10:10:40 -0700355ipv6 = JType("IPv6Address") \
356 .op(read="IPv6Address.read16Bytes(bb)", \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700357 write="$name.write16Bytes(bb)",
358 default='IPv6Address.NONE')
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700359packetin_reason = gen_enum_jtype("OFPacketInReason")
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700360transport_port = JType("TransportPort")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700361 .op(read="TransportPort.read2Bytes(bb)",
362 write="$name.write2Bytes(bb)",
363 default="TransportPort.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700364eth_type = JType("EthType")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700365 .op(read="EthType.read2Bytes(bb)",
366 write="$name.write2Bytes(bb)",
367 default="EthType.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700368vlan_vid = JType("VlanVid")\
Andreas Wundsam98a18632013-11-05 11:34:24 -0800369 .op(version=ANY, read="VlanVid.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="VlanVid.ZERO")
370vlan_vid_match = JType("OFVlanVidMatch")\
371 .op(version=1, read="OFVlanVidMatch.read2BytesOF10(bb)", write="$name.write2BytesOF10(bb)", default="OFVlanVidMatch.NONE") \
372 .op(version=2, read="OFVlanVidMatch.read2BytesOF10(bb)", write="$name.write2BytesOF10(bb)", default="OFVlanVidMatch.NONE") \
373 .op(version=ANY, read="OFVlanVidMatch.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="OFVlanVidMatch.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700374vlan_pcp = JType("VlanPcp")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700375 .op(read="VlanPcp.readByte(bb)",
376 write="$name.writeByte(bb)",
377 default="VlanPcp.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700378ip_dscp = JType("IpDscp")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700379 .op(read="IpDscp.readByte(bb)",
380 write="$name.writeByte(bb)",
381 default="IpDscp.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700382ip_ecn = JType("IpEcn")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700383 .op(read="IpEcn.readByte(bb)",
384 write="$name.writeByte(bb)",
385 default="IpEcn.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700386ip_proto = JType("IpProtocol")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700387 .op(read="IpProtocol.readByte(bb)",
388 write="$name.writeByte(bb)",
389 default="IpProtocol.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700390icmpv4_type = JType("ICMPv4Type")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700391 .op(read="ICMPv4Type.readByte(bb)",
392 write="$name.writeByte(bb)",
393 default="ICMPv4Type.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700394icmpv4_code = JType("ICMPv4Code")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700395 .op(read="ICMPv4Code.readByte(bb)",
396 write="$name.writeByte(bb)",
397 default="ICMPv4Code.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700398arp_op = JType("ArpOpcode")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700399 .op(read="ArpOpcode.read2Bytes(bb)",
400 write="$name.write2Bytes(bb)",
401 default="ArpOpcode.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700402ipv6_flabel = JType("IPv6FlowLabel")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700403 .op(read="IPv6FlowLabel.read4Bytes(bb)",
404 write="$name.write4Bytes(bb)",
405 default="IPv6FlowLabel.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700406metadata = JType("OFMetadata")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700407 .op(read="OFMetadata.read8Bytes(bb)",
408 write="$name.write8Bytes(bb)",
409 default="OFMetadata.NONE")
Andreas Wundsam2be7da52013-08-22 07:34:25 -0700410oxm = JType("OFOxm<?>")\
411 .op( read="OFOxmVer$version.READER.readFrom(bb)",
412 write="$name.writeTo(bb)")
413oxm_list = JType("OFOxmList") \
414 .op(
415 read= 'OFOxmList.readFrom(bb, $length, OFOxmVer$version.READER)', \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700416 write='$name.writeTo(bb)',
417 default="OFOxmList.EMPTY")
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700418meter_features = JType("OFMeterFeatures")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700419 .op(read="OFMeterFeaturesVer$version.READER.readFrom(bb)",
420 write="$name.writeTo(bb)")
421flow_wildcards = JType("int") \
422 .op(read='bb.readInt()',
423 write='bb.writeInt($name)',
424 default="OFFlowWildcardsSerializerVer$version.ALL_VAL")
425table_stats_wildcards = JType("int") \
426 .op(read='bb.readInt()',
427 write='bb.writeInt($name)')
Yotam Harchol2c535582013-10-01 15:50:20 -0700428port_bitmap = JType('OFBitMask128') \
429 .op(read='OFBitMask128.read16Bytes(bb)',
Yotam Harchola11f38b2013-09-26 15:38:17 -0700430 write='$name.write16Bytes(bb)',
Yotam Harchol2c535582013-10-01 15:50:20 -0700431 default='OFBitMask128.NONE')
Andreas Wundsam37e0fb12013-09-28 18:57:57 -0700432table_id = JType("TableId") \
433 .op(read='TableId.readByte(bb)',
434 write='$name.writeByte(bb)',
435 default='TableId.ALL')
Andreas Wundsam45c95f82013-10-08 15:04:23 -0700436table_id_default_zero = JType("TableId") \
437 .op(read='TableId.readByte(bb)',
438 write='$name.writeByte(bb)',
439 default='TableId.ZERO')
Andreas Wundsama0981022013-10-02 18:15:06 -0700440of_version = JType("OFVersion", 'byte') \
441 .op(read='bb.readByte()', write='bb.writeByte($name)')
Rob Vaterlausb10ae552013-09-23 14:39:39 -0700442
Andreas Wundsam8ec3bcc2013-09-16 19:44:00 -0700443port_speed = JType("PortSpeed")
Rob Vaterlaus4d311942013-09-24 13:41:44 -0700444error_type = JType("OFErrorType")
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700445of_type = JType("OFType", 'byte') \
446 .op(read='bb.readByte()', write='bb.writeByte($name)')
447action_type= gen_enum_jtype("OFActionType")\
448 .set_priv_type("short")\
449 .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False)
450instruction_type = gen_enum_jtype("OFInstructionType")\
451 .set_priv_type('short') \
452 .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False)
453buffer_id = JType("OFBufferId") \
454 .op(read="OFBufferId.of(bb.readInt())", write="bb.writeInt($name.getInt())", default="OFBufferId.NO_BUFFER")
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700455boolean = JType("boolean", "byte") \
456 .op(read='(bb.readByte() != 0)',
457 write='bb.writeByte($name ? 1 : 0)',
458 default="false")
459datapath_id = JType("DatapathId") \
460 .op(read='DatapathId.of(bb.readLong())',
461 write='bb.writeLong($name.getLong())',
462 default='DatapathId.NONE')
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -0700463action_type_set = JType("Set<OFActionType>") \
464 .op(read='ChannelUtilsVer10.readSupportedActions(bb)',
465 write='ChannelUtilsVer10.writeSupportedActions(bb, $name)',
466 default='ImmutableSet.<OFActionType>of()',
467 funnel='ChannelUtilsVer10.putSupportedActionsTo($name, sink)')
Andreas Wundsamacd57d52013-10-18 17:35:01 -0700468of_group = JType("OFGroup") \
469 .op(version=ANY, read="OFGroup.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="OFGroup.ALL")
470# the outgroup field of of_flow_stats_request has a special default value
471of_group_default_any = JType("OFGroup") \
472 .op(version=ANY, read="OFGroup.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="OFGroup.ANY")
473buffer_id = JType("OFBufferId") \
474 .op(read="OFBufferId.of(bb.readInt())", write="bb.writeInt($name.getInt())", default="OFBufferId.NO_BUFFER")
Rich Lane376cafe2013-10-27 21:49:14 -0700475lag_id = JType("LagId") \
476 .op(version=ANY, read="LagId.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="LagId.NONE")
Rich Laneeb21c4f2013-10-28 17:34:41 -0700477vrf = JType("VRF") \
478 .op(version=ANY, read="VRF.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="VRF.ZERO")
479class_id = JType("ClassId") \
480 .op(version=ANY, read="ClassId.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="ClassId.NONE")
Rich Lane9c27b5d2013-10-30 17:14:32 -0700481boolean_value = JType('OFBooleanValue', 'OFBooleanValue') \
482 .op(read='OFBooleanValue.of(bb.readByte() != 0)', write='bb.writeByte($name.getInt())', default="OFBooleanValue.FALSE")
Rich Lane2d1c5c72013-12-03 13:02:10 -0800483checksum = JType("OFChecksum128") \
484 .op(read='OFChecksum128.read16Bytes(bb)',
485 write='$name.write16Bytes(bb)',
486 default='OFChecksum128.ZERO')
Andreas Wundsam2be7da52013-08-22 07:34:25 -0700487
488generic_t = JType("T")
489
Andreas Wundsam27303462013-07-16 12:52:35 -0700490
491default_mtype_to_jtype_convert_map = {
492 'uint8_t' : u8,
493 'uint16_t' : u16,
494 'uint32_t' : u32,
495 'uint64_t' : u64,
Andreas Wundsam27303462013-07-16 12:52:35 -0700496 'of_port_no_t' : of_port,
497 'list(of_action_t)' : actions_list,
498 'list(of_instruction_t)' : instructions_list,
499 'list(of_bucket_t)': buckets_list,
500 'list(of_port_desc_t)' : port_desc_list,
501 'list(of_packet_queue_t)' : packet_queue_list,
xinwuf08ef682013-12-05 18:29:20 -0800502 'list(of_uint64_t)' : u64_list,
Andreas Wundsam27303462013-07-16 12:52:35 -0700503 'list(of_uint32_t)' : u32_list,
504 'list(of_uint8_t)' : u8_list,
Andreas Wundsam2be7da52013-08-22 07:34:25 -0700505 'list(of_oxm_t)' : oxm_list,
Andreas Wundsam27303462013-07-16 12:52:35 -0700506 'of_octets_t' : octets,
507 'of_match_t': of_match,
508 'of_fm_cmd_t': flow_mod_cmd,
509 'of_mac_addr_t': mac_addr,
510 'of_port_desc_t': port_desc,
511 'of_desc_str_t': desc_str,
512 'of_serial_num_t': serial_num,
513 'of_port_name_t': port_name,
514 'of_table_name_t': table_name,
515 'of_ipv4_t': ipv4,
516 'of_ipv6_t': ipv6,
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700517 'of_wc_bmap_t': flow_wildcards,
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700518 'of_oxm_t': oxm,
519 'of_meter_features_t': meter_features,
Rich Lane2d1c5c72013-12-03 13:02:10 -0800520 'of_bitmap_128_t': port_bitmap,
521 'of_checksum_128_t': checksum,
Andreas Wundsam27303462013-07-16 12:52:35 -0700522 }
523
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700524## Map that defines exceptions from the standard loxi->java mapping scheme
525# map of {<loxi_class_name> : { <loxi_member_name> : <JType instance> } }
Andreas Wundsam27303462013-07-16 12:52:35 -0700526exceptions = {
Yotam Harcholc742e202013-08-15 12:16:24 -0700527 'of_packet_in': { 'data' : octets, 'reason': packetin_reason },
528 'of_oxm_tcp_src' : { 'value' : transport_port },
529 'of_oxm_tcp_src_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
530 'of_oxm_tcp_dst' : { 'value' : transport_port },
531 'of_oxm_tcp_dst_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
532 'of_oxm_udp_src' : { 'value' : transport_port },
533 'of_oxm_udp_src_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
534 'of_oxm_udp_dst' : { 'value' : transport_port },
535 'of_oxm_udp_dst_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
536 'of_oxm_sctp_src' : { 'value' : transport_port },
537 'of_oxm_sctp_src_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
538 'of_oxm_sctp_dst' : { 'value' : transport_port },
539 'of_oxm_sctp_dst_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
540 'of_oxm_eth_type' : { 'value' : eth_type },
541 'of_oxm_eth_type_masked' : { 'value' : eth_type, 'value_mask' : eth_type },
Andreas Wundsam98a18632013-11-05 11:34:24 -0800542 'of_oxm_vlan_vid' : { 'value' : vlan_vid_match },
543 'of_oxm_vlan_vid_masked' : { 'value' : vlan_vid_match, 'value_mask' : vlan_vid_match },
Yotam Harcholc742e202013-08-15 12:16:24 -0700544 'of_oxm_vlan_pcp' : { 'value' : vlan_pcp },
545 'of_oxm_vlan_pcp_masked' : { 'value' : vlan_pcp, 'value_mask' : vlan_pcp },
546 'of_oxm_ip_dscp' : { 'value' : ip_dscp },
547 'of_oxm_ip_dscp_masked' : { 'value' : ip_dscp, 'value_mask' : ip_dscp },
548 'of_oxm_ip_ecn' : { 'value' : ip_ecn },
549 'of_oxm_ip_ecn_masked' : { 'value' : ip_ecn, 'value_mask' : ip_ecn },
550 'of_oxm_ip_proto' : { 'value' : ip_proto },
551 'of_oxm_ip_proto_masked' : { 'value' : ip_proto, 'value_mask' : ip_proto },
552 'of_oxm_icmpv4_type' : { 'value' : icmpv4_type },
553 'of_oxm_icmpv4_type_masked' : { 'value' : icmpv4_type, 'value_mask' : icmpv4_type },
554 'of_oxm_icmpv4_code' : { 'value' : icmpv4_code },
555 'of_oxm_icmpv4_code_masked' : { 'value' : icmpv4_code, 'value_mask' : icmpv4_code },
556 'of_oxm_arp_op' : { 'value' : arp_op },
557 'of_oxm_arp_op_masked' : { 'value' : arp_op, 'value_mask' : arp_op },
558 'of_oxm_arp_spa' : { 'value' : ipv4 },
559 'of_oxm_arp_spa_masked' : { 'value' : ipv4, 'value_mask' : ipv4 },
560 'of_oxm_arp_tpa' : { 'value' : ipv4 },
561 'of_oxm_arp_tpa_masked' : { 'value' : ipv4, 'value_mask' : ipv4 },
562 'of_oxm_ipv6_flabel' : { 'value' : ipv6_flabel },
563 'of_oxm_ipv6_flabel_masked' : { 'value' : ipv6_flabel, 'value_mask' : ipv6_flabel },
564 'of_oxm_metadata' : { 'value' : metadata },
565 'of_oxm_metadata_masked' : { 'value' : metadata, 'value_mask' : metadata },
Yotam Harchol5804f772013-08-21 17:35:31 -0700566
567 'of_oxm_icmpv6_code' : { 'value' : u8obj },
568 'of_oxm_icmpv6_code_masked' : { 'value' : u8obj, 'value_mask' : u8obj },
569 'of_oxm_icmpv6_type' : { 'value' : u8obj },
570 'of_oxm_icmpv6_type_masked' : { 'value' : u8obj, 'value_mask' : u8obj },
571 'of_oxm_mpls_label' : { 'value' : u32obj },
572 'of_oxm_mpls_label_masked' : { 'value' : u32obj, 'value_mask' : u32obj },
573 'of_oxm_mpls_tc' : { 'value' : u8obj },
574 'of_oxm_mpls_tc_masked' : { 'value' : u8obj, 'value_mask' : u8obj },
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700575
Yotam Harchol595c6442013-09-27 16:29:08 -0700576 'of_oxm_bsn_in_ports_128' : { 'value': port_bitmap },
577 'of_oxm_bsn_in_ports_128_masked' : { 'value': port_bitmap, 'value_mask': port_bitmap },
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700578
Rich Lane376cafe2013-10-27 21:49:14 -0700579 'of_oxm_bsn_lag_id' : { 'value' : lag_id },
580 'of_oxm_bsn_lag_id_masked' : { 'value' : lag_id, 'value_mask' : lag_id },
Rich Lane1424d0b2013-10-24 17:16:24 -0700581
Rich Laneeb21c4f2013-10-28 17:34:41 -0700582 'of_oxm_bsn_vrf' : { 'value' : vrf },
583 'of_oxm_bsn_vrf_masked' : { 'value' : vrf, 'value_mask' : vrf },
584
Rich Lane9c27b5d2013-10-30 17:14:32 -0700585 'of_oxm_bsn_global_vrf_allowed' : { 'value' : boolean_value },
586 'of_oxm_bsn_global_vrf_allowed_masked' : { 'value' : boolean_value, 'value_mask' : boolean_value },
Rich Laneeb21c4f2013-10-28 17:34:41 -0700587
588 'of_oxm_bsn_l3_interface_class_id' : { 'value' : class_id },
589 'of_oxm_bsn_l3_interface_class_id_masked' : { 'value' : class_id, 'value_mask' : class_id },
590
591 'of_oxm_bsn_l3_src_class_id' : { 'value' : class_id },
592 'of_oxm_bsn_l3_src_class_id_masked' : { 'value' : class_id, 'value_mask' : class_id },
593
594 'of_oxm_bsn_l3_dst_class_id' : { 'value' : class_id },
595 'of_oxm_bsn_l3_dst_class_id_masked' : { 'value' : class_id, 'value_mask' : class_id },
596
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700597 'of_table_stats_entry': { 'wildcards': table_stats_wildcards },
Andreas Wundsam98a18632013-11-05 11:34:24 -0800598 'of_match_v1': { 'vlan_vid' : vlan_vid_match, 'vlan_pcp': vlan_pcp,
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700599 'eth_type': eth_type, 'ip_dscp': ip_dscp, 'ip_proto': ip_proto,
Andreas Wundsame962d372013-10-02 18:15:58 -0700600 'tcp_src': transport_port, 'tcp_dst': transport_port,
601 'in_port': of_port_match_v1
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700602 },
603 'of_bsn_set_l2_table_request': { 'l2_table_enable': boolean },
604 'of_bsn_set_l2_table_reply': { 'l2_table_enable': boolean },
Rob Vaterlause1b86842013-10-18 13:29:19 -0700605 'of_bsn_set_pktin_suppression_request': { 'enabled': boolean },
Andreas Wundsamacd57d52013-10-18 17:35:01 -0700606 'of_flow_stats_request': { 'out_group': of_group_default_any },
Andreas Wundsam98a18632013-11-05 11:34:24 -0800607
608 'of_action_bsn_mirror': { 'dest_port': of_port },
609 'of_action_push_mpls': { 'ethertype': eth_type },
610 'of_action_push_pbb': { 'ethertype': eth_type },
611 'of_action_push_vlan': { 'ethertype': eth_type },
Ronald Li4fb8abb2013-11-21 19:53:02 -0800612 'of_action_pop_mpls': { 'ethertype': eth_type },
Andreas Wundsam98a18632013-11-05 11:34:24 -0800613 'of_action_set_nw_dst': { 'nw_addr': ipv4 },
614 'of_action_set_nw_ecn': { 'nw_ecn': ip_ecn },
615 'of_action_set_nw_src': { 'nw_addr': ipv4 },
Andreas Wundsam98a18632013-11-05 11:34:24 -0800616 'of_action_set_tp_dst': { 'tp_port': transport_port },
617 'of_action_set_tp_src': { 'tp_port': transport_port },
618 'of_action_set_vlan_pcp': { 'vlan_pcp': vlan_pcp },
619 'of_action_set_vlan_vid': { 'vlan_vid': vlan_vid },
Andreas Wundsam5812cf32013-11-15 13:51:24 -0800620
621 'of_group_mod' : { 'command' : group_mod_cmd },
622 'of_group_add' : { 'command' : group_mod_cmd },
623 'of_group_modify' : { 'command' : group_mod_cmd },
624 'of_group_delete' : { 'command' : group_mod_cmd },
625
626 'of_bucket' : { 'watch_group': of_group },
Andreas Wundsam27303462013-07-16 12:52:35 -0700627}
628
Andreas Wundsam7cfeac32013-09-17 13:53:48 -0700629
630@memoize
631def enum_java_types():
632 enum_types = {}
Andreas Wundsamc8912c12013-11-15 13:44:48 -0800633 for enum in loxi_globals.unified.enums:
634 java_name = name_c_to_caps_camel(re.sub(r'_t$', "", enum.name))
635 enum_types[enum.name] = gen_enum_jtype(java_name, enum.is_bitmask)
Andreas Wundsam7cfeac32013-09-17 13:53:48 -0700636 return enum_types
637
Andreas Wundsam2be7da52013-08-22 07:34:25 -0700638def make_match_field_jtype(sub_type_name="?"):
639 return JType("MatchField<{}>".format(sub_type_name))
640
Andreas Wundsam661a2222013-11-05 17:18:59 -0800641def make_oxm_jtype(sub_type_name="?"):
642 return JType("OFOxm<{}>".format(sub_type_name))
Andreas Wundsam27303462013-07-16 12:52:35 -0700643
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700644def list_cname_to_java_name(c_type):
Andreas Wundsam27303462013-07-16 12:52:35 -0700645 m = re.match(r'list\(of_([a-zA-Z_]+)_t\)', c_type)
646 if not m:
647 raise Exception("Not a recgonized standard list type declaration: %s" % c_type)
648 base_name = m.group(1)
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700649 return "OF" + name_c_to_caps_camel(base_name)
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700650
651#### main entry point for conversion of LOXI types (c_types) Java types.
652# FIXME: This badly needs a refactoring
653
Andreas Wundsam27303462013-07-16 12:52:35 -0700654def convert_to_jtype(obj_name, field_name, c_type):
655 """ Convert from a C type ("uint_32") to a java type ("U32")
656 and return a JType object with the size, internal type, and marshalling functions"""
657 if obj_name in exceptions and field_name in exceptions[obj_name]:
658 return exceptions[obj_name][field_name]
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700659 elif ( obj_name == "of_header" or loxi_utils.class_is_message(obj_name)) and field_name == "type" and c_type == "uint8_t":
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700660 return of_type
Andreas Wundsam27303462013-07-16 12:52:35 -0700661 elif field_name == "type" and re.match(r'of_action.*', obj_name):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700662 return action_type
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700663 elif field_name == "err_type":
664 return JType("OFErrorType", 'short') \
665 .op(read='bb.readShort()', write='bb.writeShort($name)')
666 elif field_name == "stats_type":
667 return JType("OFStatsType", 'short') \
668 .op(read='bb.readShort()', write='bb.writeShort($name)')
Andreas Wundsam999c0732013-10-01 19:29:16 -0700669 elif field_name == "type" and re.match(r'of_instruction.*', obj_name):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700670 return instruction_type
Andreas Wundsam45c95f82013-10-08 15:04:23 -0700671 elif obj_name in ("of_flow_add", "of_flow_modify", "of_flow_modify_strict", "of_delete_strict") and field_name == "table_id" and c_type == "uint8_t":
672 return table_id_default_zero
Andreas Wundsam37e0fb12013-09-28 18:57:57 -0700673 elif field_name == "table_id" and c_type == "uint8_t":
674 return table_id
Andreas Wundsam27303462013-07-16 12:52:35 -0700675 elif field_name == "version" and c_type == "uint8_t":
Andreas Wundsama0981022013-10-02 18:15:06 -0700676 return of_version
Rob Vaterlausb10ae552013-09-23 14:39:39 -0700677 elif field_name == "buffer_id" and c_type == "uint32_t":
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700678 return buffer_id
Andreas Wundsamacd57d52013-10-18 17:35:01 -0700679 elif field_name == "group_id" and c_type == "uint32_t":
680 return of_group
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700681 elif field_name == 'datapath_id':
682 return datapath_id
683 elif field_name == 'actions' and obj_name == 'of_features_reply':
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -0700684 return action_type_set
Andreas Wundsam27303462013-07-16 12:52:35 -0700685 elif c_type in default_mtype_to_jtype_convert_map:
686 return default_mtype_to_jtype_convert_map[c_type]
687 elif re.match(r'list\(of_([a-zA-Z_]+)_t\)', c_type):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700688 return gen_list_jtype(list_cname_to_java_name(c_type))
Andreas Wundsam7cfeac32013-09-17 13:53:48 -0700689 elif c_type in enum_java_types():
690 return enum_java_types()[c_type]
Andreas Wundsam27303462013-07-16 12:52:35 -0700691 else:
692 print "WARN: Couldn't find java type conversion for '%s' in %s:%s" % (c_type, obj_name, field_name)
693 jtype = name_c_to_caps_camel(re.sub(r'_t$', "", c_type))
694 return JType(jtype)
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700695
696
697#### Enum specific wiretype definitions
698enum_wire_types = {
699 "uint8_t": JType("byte").op(read="bb.readByte()", write="bb.writeByte($name)"),
700 "uint16_t": JType("short").op(read="bb.readShort()", write="bb.writeShort($name)"),
701 "uint32_t": JType("int").op(read="bb.readInt()", write="bb.writeInt($name)"),
702 "uint64_t": JType("long").op(read="bb.readLong()", write="bb.writeLong($name)"),
703}
704
705def convert_enum_wire_type_to_jtype(wire_type):
706 return enum_wire_types[wire_type]