blob: 14878c39bebaa99bc5689c958bd88b75ad2f9eeb [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")
Rich Lane9b178072014-05-19 15:31:55 -0700309u16obj = JType('U16', 'U16') \
Rich Lane99f1b432014-05-22 11:15:15 -0700310 .op(read='U16.of(bb.readShort())', write='bb.writeShort($name.getRaw())', default="U16.ZERO")
Yotam Harchol5804f772013-08-21 17:35:31 -0700311u32obj = JType('U32', 'U32') \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700312 .op(read='U32.of(bb.readInt())', write='bb.writeInt($name.getRaw())', default="U32.ZERO")
Sovietaced3d42dbe2014-01-23 15:55:48 -0800313u64 = JType('U64', 'long') \
314 .op(read='U64.ofRaw(bb.readLong())', write='bb.writeLong($name.getValue())', default="U64.ZERO", pub_type=True) \
315 .op(read='bb.readLong()', write='bb.writeLong($name)', pub_type=False)
Andreas Wundsam7c15b172014-04-24 19:02:40 -0700316u128 = JType("U128") \
317 .op(read='U128.read16Bytes(bb)',
318 write='$name.write16Bytes(bb)',
319 default='U128.ZERO')
Yotam Harchold7b84202013-07-26 16:08:10 -0700320of_port = JType("OFPort") \
Andreas Wundsamad499c92013-09-28 18:56:49 -0700321 .op(version=1, read="OFPort.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="OFPort.ANY") \
322 .op(version=ANY, read="OFPort.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="OFPort.ANY")
Andreas Wundsame962d372013-10-02 18:15:58 -0700323# the same OFPort, but with a default value of ZERO, only for OF10 match
324of_port_match_v1 = JType("OFPort") \
325 .op(version=1, read="OFPort.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="OFPort.ZERO")
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700326actions_list = gen_list_jtype("OFAction")
327instructions_list = gen_list_jtype("OFInstruction")
328buckets_list = gen_list_jtype("OFBucket")
329port_desc_list = gen_list_jtype("OFPortDesc")
330packet_queue_list = gen_list_jtype("OFPacketQueue")
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700331port_desc = JType('OFPortDesc') \
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700332 .op(read='OFPortDescVer$version.READER.readFrom(bb)', \
Yotam Harchold7b84202013-07-26 16:08:10 -0700333 write='$name.writeTo(bb)')
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700334octets = JType('byte[]')\
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700335 .op(read='ChannelUtils.readBytes(bb, $length)', \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700336 write='bb.writeBytes($name)', \
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700337 default="new byte[0]",
338 funnel="sink.putBytes($name)"
339 );
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700340of_match = JType('Match') \
Andreas Wundsam951ada32013-08-01 22:05:38 -0700341 .op(read='ChannelUtilsVer$version.readOFMatch(bb)', \
Andreas Wundsame962d372013-10-02 18:15:58 -0700342 write='$name.writeTo(bb)',
343 default="OFFactoryVer$version.MATCH_WILDCARD_ALL");
Andreas Wundsam5812cf32013-11-15 13:51:24 -0800344group_mod_cmd = JType('OFGroupModCommand', 'short') \
345 .op(version=ANY, read="bb.readShort()", write="bb.writeShort($name)")
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700346flow_mod_cmd = JType('OFFlowModCommand', 'short') \
Yotam Harchold7b84202013-07-26 16:08:10 -0700347 .op(version=1, read="bb.readShort()", write="bb.writeShort($name)") \
348 .op(version=ANY, read="bb.readByte()", write="bb.writeByte($name)")
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700349mac_addr = JType('MacAddress') \
Yotam Harchold7b84202013-07-26 16:08:10 -0700350 .op(read="MacAddress.read6Bytes(bb)", \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700351 write="$name.write6Bytes(bb)",
352 default="MacAddress.NONE")
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700353
354port_name = gen_fixed_length_string_jtype(16)
355desc_str = gen_fixed_length_string_jtype(256)
356serial_num = gen_fixed_length_string_jtype(32)
357table_name = gen_fixed_length_string_jtype(32)
Rich Lanef8a3d002014-03-19 13:33:52 -0700358str64 = gen_fixed_length_string_jtype(64)
Yotam Harchola289d552013-09-16 10:10:40 -0700359ipv4 = JType("IPv4Address") \
360 .op(read="IPv4Address.read4Bytes(bb)", \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700361 write="$name.write4Bytes(bb)",
362 default='IPv4Address.NONE')
Yotam Harchola289d552013-09-16 10:10:40 -0700363ipv6 = JType("IPv6Address") \
364 .op(read="IPv6Address.read16Bytes(bb)", \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700365 write="$name.write16Bytes(bb)",
366 default='IPv6Address.NONE')
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700367packetin_reason = gen_enum_jtype("OFPacketInReason")
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700368transport_port = JType("TransportPort")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700369 .op(read="TransportPort.read2Bytes(bb)",
370 write="$name.write2Bytes(bb)",
371 default="TransportPort.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700372eth_type = JType("EthType")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700373 .op(read="EthType.read2Bytes(bb)",
374 write="$name.write2Bytes(bb)",
375 default="EthType.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700376vlan_vid = JType("VlanVid")\
Andreas Wundsam98a18632013-11-05 11:34:24 -0800377 .op(version=ANY, read="VlanVid.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="VlanVid.ZERO")
378vlan_vid_match = JType("OFVlanVidMatch")\
379 .op(version=1, read="OFVlanVidMatch.read2BytesOF10(bb)", write="$name.write2BytesOF10(bb)", default="OFVlanVidMatch.NONE") \
380 .op(version=2, read="OFVlanVidMatch.read2BytesOF10(bb)", write="$name.write2BytesOF10(bb)", default="OFVlanVidMatch.NONE") \
381 .op(version=ANY, read="OFVlanVidMatch.read2Bytes(bb)", write="$name.write2Bytes(bb)", default="OFVlanVidMatch.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700382vlan_pcp = JType("VlanPcp")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700383 .op(read="VlanPcp.readByte(bb)",
384 write="$name.writeByte(bb)",
385 default="VlanPcp.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700386ip_dscp = JType("IpDscp")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700387 .op(read="IpDscp.readByte(bb)",
388 write="$name.writeByte(bb)",
389 default="IpDscp.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700390ip_ecn = JType("IpEcn")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700391 .op(read="IpEcn.readByte(bb)",
392 write="$name.writeByte(bb)",
393 default="IpEcn.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700394ip_proto = JType("IpProtocol")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700395 .op(read="IpProtocol.readByte(bb)",
396 write="$name.writeByte(bb)",
397 default="IpProtocol.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700398icmpv4_type = JType("ICMPv4Type")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700399 .op(read="ICMPv4Type.readByte(bb)",
400 write="$name.writeByte(bb)",
401 default="ICMPv4Type.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700402icmpv4_code = JType("ICMPv4Code")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700403 .op(read="ICMPv4Code.readByte(bb)",
404 write="$name.writeByte(bb)",
405 default="ICMPv4Code.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700406arp_op = JType("ArpOpcode")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700407 .op(read="ArpOpcode.read2Bytes(bb)",
408 write="$name.write2Bytes(bb)",
409 default="ArpOpcode.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700410ipv6_flabel = JType("IPv6FlowLabel")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700411 .op(read="IPv6FlowLabel.read4Bytes(bb)",
412 write="$name.write4Bytes(bb)",
413 default="IPv6FlowLabel.NONE")
Yotam Harcholc742e202013-08-15 12:16:24 -0700414metadata = JType("OFMetadata")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700415 .op(read="OFMetadata.read8Bytes(bb)",
416 write="$name.write8Bytes(bb)",
417 default="OFMetadata.NONE")
Andreas Wundsam2be7da52013-08-22 07:34:25 -0700418oxm = JType("OFOxm<?>")\
419 .op( read="OFOxmVer$version.READER.readFrom(bb)",
420 write="$name.writeTo(bb)")
421oxm_list = JType("OFOxmList") \
422 .op(
423 read= 'OFOxmList.readFrom(bb, $length, OFOxmVer$version.READER)', \
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700424 write='$name.writeTo(bb)',
425 default="OFOxmList.EMPTY")
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700426meter_features = JType("OFMeterFeatures")\
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700427 .op(read="OFMeterFeaturesVer$version.READER.readFrom(bb)",
428 write="$name.writeTo(bb)")
Wilson Ngd6181882014-04-14 16:28:35 -0700429bsn_vport = JType("OFBsnVport")\
430 .op(read="OFBsnVportVer$version.READER.readFrom(bb)",
Andreas Wundsam113d25b2014-01-22 20:17:37 -0800431 write="$name.writeTo(bb)")
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700432flow_wildcards = JType("int") \
433 .op(read='bb.readInt()',
434 write='bb.writeInt($name)',
435 default="OFFlowWildcardsSerializerVer$version.ALL_VAL")
436table_stats_wildcards = JType("int") \
437 .op(read='bb.readInt()',
438 write='bb.writeInt($name)')
Yotam Harchol2c535582013-10-01 15:50:20 -0700439port_bitmap = JType('OFBitMask128') \
440 .op(read='OFBitMask128.read16Bytes(bb)',
Yotam Harchola11f38b2013-09-26 15:38:17 -0700441 write='$name.write16Bytes(bb)',
Yotam Harchol2c535582013-10-01 15:50:20 -0700442 default='OFBitMask128.NONE')
Andreas Wundsam37e0fb12013-09-28 18:57:57 -0700443table_id = JType("TableId") \
444 .op(read='TableId.readByte(bb)',
445 write='$name.writeByte(bb)',
446 default='TableId.ALL')
Andreas Wundsam45c95f82013-10-08 15:04:23 -0700447table_id_default_zero = JType("TableId") \
448 .op(read='TableId.readByte(bb)',
449 write='$name.writeByte(bb)',
450 default='TableId.ZERO')
Sovietaced0f9dddf2014-01-28 19:45:07 -0800451of_aux_id = JType("OFAuxId") \
452 .op(read='OFAuxId.readByte(bb)',
Sovietaced92603b02014-01-28 18:23:24 -0800453 write='$name.writeByte(bb)',
Sovietaced0f9dddf2014-01-28 19:45:07 -0800454 default='OFAuxId.MAIN')
Andreas Wundsama0981022013-10-02 18:15:06 -0700455of_version = JType("OFVersion", 'byte') \
456 .op(read='bb.readByte()', write='bb.writeByte($name)')
Rob Vaterlausb10ae552013-09-23 14:39:39 -0700457
Andreas Wundsam8ec3bcc2013-09-16 19:44:00 -0700458port_speed = JType("PortSpeed")
Rob Vaterlaus4d311942013-09-24 13:41:44 -0700459error_type = JType("OFErrorType")
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700460of_type = JType("OFType", 'byte') \
461 .op(read='bb.readByte()', write='bb.writeByte($name)')
462action_type= gen_enum_jtype("OFActionType")\
463 .set_priv_type("short")\
464 .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False)
465instruction_type = gen_enum_jtype("OFInstructionType")\
466 .set_priv_type('short') \
467 .op(read='bb.readShort()', write='bb.writeShort($name)', pub_type=False)
468buffer_id = JType("OFBufferId") \
469 .op(read="OFBufferId.of(bb.readInt())", write="bb.writeInt($name.getInt())", default="OFBufferId.NO_BUFFER")
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700470boolean = JType("boolean", "byte") \
471 .op(read='(bb.readByte() != 0)',
472 write='bb.writeByte($name ? 1 : 0)',
473 default="false")
474datapath_id = JType("DatapathId") \
475 .op(read='DatapathId.of(bb.readLong())',
476 write='bb.writeLong($name.getLong())',
477 default='DatapathId.NONE')
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -0700478action_type_set = JType("Set<OFActionType>") \
479 .op(read='ChannelUtilsVer10.readSupportedActions(bb)',
480 write='ChannelUtilsVer10.writeSupportedActions(bb, $name)',
481 default='ImmutableSet.<OFActionType>of()',
482 funnel='ChannelUtilsVer10.putSupportedActionsTo($name, sink)')
Andreas Wundsamacd57d52013-10-18 17:35:01 -0700483of_group = JType("OFGroup") \
484 .op(version=ANY, read="OFGroup.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="OFGroup.ALL")
Andreas Wundsamee8b42c2014-01-24 17:38:19 -0800485of_group_default_any = JType("OFGroup") \
486 .op(version=ANY, read="OFGroup.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="OFGroup.ANY")
Andreas Wundsamacd57d52013-10-18 17:35:01 -0700487# the outgroup field of of_flow_stats_request has a special default value
488of_group_default_any = JType("OFGroup") \
489 .op(version=ANY, read="OFGroup.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="OFGroup.ANY")
490buffer_id = JType("OFBufferId") \
491 .op(read="OFBufferId.of(bb.readInt())", write="bb.writeInt($name.getInt())", default="OFBufferId.NO_BUFFER")
Rich Lane376cafe2013-10-27 21:49:14 -0700492lag_id = JType("LagId") \
493 .op(version=ANY, read="LagId.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="LagId.NONE")
Rich Laneeb21c4f2013-10-28 17:34:41 -0700494vrf = JType("VRF") \
495 .op(version=ANY, read="VRF.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="VRF.ZERO")
496class_id = JType("ClassId") \
497 .op(version=ANY, read="ClassId.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="ClassId.NONE")
Rich Lane9c27b5d2013-10-30 17:14:32 -0700498boolean_value = JType('OFBooleanValue', 'OFBooleanValue') \
499 .op(read='OFBooleanValue.of(bb.readByte() != 0)', write='bb.writeByte($name.getInt())', default="OFBooleanValue.FALSE")
Andreas Wundsamd4b22692014-01-14 14:17:26 -0800500gen_table_id = JType("GenTableId") \
501 .op(read='GenTableId.read2Bytes(bb)',
502 write='$name.write2Bytes(bb)',
503 )
Rich Lane53ddf5c2014-03-20 15:24:08 -0700504udf = JType("UDF") \
505 .op(version=ANY, read="UDF.read4Bytes(bb)", write="$name.write4Bytes(bb)", default="UDF.ZERO")
Andreas Wundsam62cbcff2014-05-09 16:35:00 -0700506error_cause_data = JType("OFErrorCauseData") \
Andreas Wundsam39efd1c2014-05-13 19:41:51 -0700507 .op(version=ANY, read="OFErrorCauseData.read(bb, $length, OFVersion.OF_$version)", write="$name.writeTo(bb)", default="OFErrorCauseData.NONE");
Andreas Wundsam2be7da52013-08-22 07:34:25 -0700508
509generic_t = JType("T")
510
Andreas Wundsam27303462013-07-16 12:52:35 -0700511
512default_mtype_to_jtype_convert_map = {
513 'uint8_t' : u8,
514 'uint16_t' : u16,
515 'uint32_t' : u32,
516 'uint64_t' : u64,
Andreas Wundsam27303462013-07-16 12:52:35 -0700517 'of_port_no_t' : of_port,
518 'list(of_action_t)' : actions_list,
519 'list(of_instruction_t)' : instructions_list,
520 'list(of_bucket_t)': buckets_list,
521 'list(of_port_desc_t)' : port_desc_list,
522 'list(of_packet_queue_t)' : packet_queue_list,
xinwuf08ef682013-12-05 18:29:20 -0800523 'list(of_uint64_t)' : u64_list,
Andreas Wundsam27303462013-07-16 12:52:35 -0700524 'list(of_uint32_t)' : u32_list,
525 'list(of_uint8_t)' : u8_list,
Andreas Wundsam2be7da52013-08-22 07:34:25 -0700526 'list(of_oxm_t)' : oxm_list,
Andreas Wundsam27303462013-07-16 12:52:35 -0700527 'of_octets_t' : octets,
528 'of_match_t': of_match,
529 'of_fm_cmd_t': flow_mod_cmd,
530 'of_mac_addr_t': mac_addr,
531 'of_port_desc_t': port_desc,
532 'of_desc_str_t': desc_str,
533 'of_serial_num_t': serial_num,
534 'of_port_name_t': port_name,
535 'of_table_name_t': table_name,
Rich Lanef8a3d002014-03-19 13:33:52 -0700536 'of_str64_t': str64,
Andreas Wundsam27303462013-07-16 12:52:35 -0700537 'of_ipv4_t': ipv4,
538 'of_ipv6_t': ipv6,
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700539 'of_wc_bmap_t': flow_wildcards,
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700540 'of_oxm_t': oxm,
541 'of_meter_features_t': meter_features,
Rich Lane2d1c5c72013-12-03 13:02:10 -0800542 'of_bitmap_128_t': port_bitmap,
Andreas Wundsam7c15b172014-04-24 19:02:40 -0700543 'of_checksum_128_t': u128,
Wilson Ngd6181882014-04-14 16:28:35 -0700544 'of_bsn_vport_t': bsn_vport,
Andreas Wundsam27303462013-07-16 12:52:35 -0700545 }
546
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700547## Map that defines exceptions from the standard loxi->java mapping scheme
548# map of {<loxi_class_name> : { <loxi_member_name> : <JType instance> } }
Andreas Wundsam27303462013-07-16 12:52:35 -0700549exceptions = {
Yotam Harcholc742e202013-08-15 12:16:24 -0700550 'of_packet_in': { 'data' : octets, 'reason': packetin_reason },
551 'of_oxm_tcp_src' : { 'value' : transport_port },
552 'of_oxm_tcp_src_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
553 'of_oxm_tcp_dst' : { 'value' : transport_port },
554 'of_oxm_tcp_dst_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
555 'of_oxm_udp_src' : { 'value' : transport_port },
556 'of_oxm_udp_src_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
557 'of_oxm_udp_dst' : { 'value' : transport_port },
558 'of_oxm_udp_dst_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
559 'of_oxm_sctp_src' : { 'value' : transport_port },
560 'of_oxm_sctp_src_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
561 'of_oxm_sctp_dst' : { 'value' : transport_port },
562 'of_oxm_sctp_dst_masked' : { 'value' : transport_port, 'value_mask' : transport_port },
563 'of_oxm_eth_type' : { 'value' : eth_type },
564 'of_oxm_eth_type_masked' : { 'value' : eth_type, 'value_mask' : eth_type },
Andreas Wundsam98a18632013-11-05 11:34:24 -0800565 'of_oxm_vlan_vid' : { 'value' : vlan_vid_match },
566 'of_oxm_vlan_vid_masked' : { 'value' : vlan_vid_match, 'value_mask' : vlan_vid_match },
Yotam Harcholc742e202013-08-15 12:16:24 -0700567 'of_oxm_vlan_pcp' : { 'value' : vlan_pcp },
568 'of_oxm_vlan_pcp_masked' : { 'value' : vlan_pcp, 'value_mask' : vlan_pcp },
569 'of_oxm_ip_dscp' : { 'value' : ip_dscp },
570 'of_oxm_ip_dscp_masked' : { 'value' : ip_dscp, 'value_mask' : ip_dscp },
571 'of_oxm_ip_ecn' : { 'value' : ip_ecn },
572 'of_oxm_ip_ecn_masked' : { 'value' : ip_ecn, 'value_mask' : ip_ecn },
573 'of_oxm_ip_proto' : { 'value' : ip_proto },
574 'of_oxm_ip_proto_masked' : { 'value' : ip_proto, 'value_mask' : ip_proto },
575 'of_oxm_icmpv4_type' : { 'value' : icmpv4_type },
576 'of_oxm_icmpv4_type_masked' : { 'value' : icmpv4_type, 'value_mask' : icmpv4_type },
577 'of_oxm_icmpv4_code' : { 'value' : icmpv4_code },
578 'of_oxm_icmpv4_code_masked' : { 'value' : icmpv4_code, 'value_mask' : icmpv4_code },
579 'of_oxm_arp_op' : { 'value' : arp_op },
580 'of_oxm_arp_op_masked' : { 'value' : arp_op, 'value_mask' : arp_op },
581 'of_oxm_arp_spa' : { 'value' : ipv4 },
582 'of_oxm_arp_spa_masked' : { 'value' : ipv4, 'value_mask' : ipv4 },
583 'of_oxm_arp_tpa' : { 'value' : ipv4 },
584 'of_oxm_arp_tpa_masked' : { 'value' : ipv4, 'value_mask' : ipv4 },
585 'of_oxm_ipv6_flabel' : { 'value' : ipv6_flabel },
586 'of_oxm_ipv6_flabel_masked' : { 'value' : ipv6_flabel, 'value_mask' : ipv6_flabel },
587 'of_oxm_metadata' : { 'value' : metadata },
588 'of_oxm_metadata_masked' : { 'value' : metadata, 'value_mask' : metadata },
Yotam Harchol5804f772013-08-21 17:35:31 -0700589
590 'of_oxm_icmpv6_code' : { 'value' : u8obj },
591 'of_oxm_icmpv6_code_masked' : { 'value' : u8obj, 'value_mask' : u8obj },
592 'of_oxm_icmpv6_type' : { 'value' : u8obj },
593 'of_oxm_icmpv6_type_masked' : { 'value' : u8obj, 'value_mask' : u8obj },
594 'of_oxm_mpls_label' : { 'value' : u32obj },
595 'of_oxm_mpls_label_masked' : { 'value' : u32obj, 'value_mask' : u32obj },
596 'of_oxm_mpls_tc' : { 'value' : u8obj },
597 'of_oxm_mpls_tc_masked' : { 'value' : u8obj, 'value_mask' : u8obj },
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700598
Yotam Harchol595c6442013-09-27 16:29:08 -0700599 'of_oxm_bsn_in_ports_128' : { 'value': port_bitmap },
600 'of_oxm_bsn_in_ports_128_masked' : { 'value': port_bitmap, 'value_mask': port_bitmap },
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700601
Rich Lane376cafe2013-10-27 21:49:14 -0700602 'of_oxm_bsn_lag_id' : { 'value' : lag_id },
603 'of_oxm_bsn_lag_id_masked' : { 'value' : lag_id, 'value_mask' : lag_id },
Rich Lane1424d0b2013-10-24 17:16:24 -0700604
Rich Laneeb21c4f2013-10-28 17:34:41 -0700605 'of_oxm_bsn_vrf' : { 'value' : vrf },
606 'of_oxm_bsn_vrf_masked' : { 'value' : vrf, 'value_mask' : vrf },
607
Rich Lane9c27b5d2013-10-30 17:14:32 -0700608 'of_oxm_bsn_global_vrf_allowed' : { 'value' : boolean_value },
609 'of_oxm_bsn_global_vrf_allowed_masked' : { 'value' : boolean_value, 'value_mask' : boolean_value },
Rich Laneeb21c4f2013-10-28 17:34:41 -0700610
611 'of_oxm_bsn_l3_interface_class_id' : { 'value' : class_id },
612 'of_oxm_bsn_l3_interface_class_id_masked' : { 'value' : class_id, 'value_mask' : class_id },
613
614 'of_oxm_bsn_l3_src_class_id' : { 'value' : class_id },
615 'of_oxm_bsn_l3_src_class_id_masked' : { 'value' : class_id, 'value_mask' : class_id },
616
617 'of_oxm_bsn_l3_dst_class_id' : { 'value' : class_id },
618 'of_oxm_bsn_l3_dst_class_id_masked' : { 'value' : class_id, 'value_mask' : class_id },
619
Rich Lane917bb9a2014-03-11 17:49:56 -0700620 'of_oxm_bsn_egr_port_group_id' : { 'value' : class_id },
621 'of_oxm_bsn_egr_port_group_id_masked' : { 'value' : class_id, 'value_mask' : class_id },
622
Rich Lane53ddf5c2014-03-20 15:24:08 -0700623 'of_oxm_bsn_udf0' : { 'value' : udf },
624 'of_oxm_bsn_udf0_masked' : { 'value' : udf, 'value_mask' : udf },
625
626 'of_oxm_bsn_udf1' : { 'value' : udf },
627 'of_oxm_bsn_udf1_masked' : { 'value' : udf, 'value_mask' : udf },
628
629 'of_oxm_bsn_udf2' : { 'value' : udf },
630 'of_oxm_bsn_udf2_masked' : { 'value' : udf, 'value_mask' : udf },
631
632 'of_oxm_bsn_udf3' : { 'value' : udf },
633 'of_oxm_bsn_udf3_masked' : { 'value' : udf, 'value_mask' : udf },
634
635 'of_oxm_bsn_udf4' : { 'value' : udf },
636 'of_oxm_bsn_udf4_masked' : { 'value' : udf, 'value_mask' : udf },
637
638 'of_oxm_bsn_udf5' : { 'value' : udf },
639 'of_oxm_bsn_udf5_masked' : { 'value' : udf, 'value_mask' : udf },
640
641 'of_oxm_bsn_udf6' : { 'value' : udf },
642 'of_oxm_bsn_udf6_masked' : { 'value' : udf, 'value_mask' : udf },
643
644 'of_oxm_bsn_udf7' : { 'value' : udf },
645 'of_oxm_bsn_udf7_masked' : { 'value' : udf, 'value_mask' : udf },
646
Rich Lane9b178072014-05-19 15:31:55 -0700647 'of_oxm_bsn_tcp_flags' : { 'value' : u16obj },
648 'of_oxm_bsn_tcp_flags_masked' : { 'value' : u16obj, 'value_mask' : u16obj },
649
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700650 'of_table_stats_entry': { 'wildcards': table_stats_wildcards },
Andreas Wundsam98a18632013-11-05 11:34:24 -0800651 'of_match_v1': { 'vlan_vid' : vlan_vid_match, 'vlan_pcp': vlan_pcp,
Andreas Wundsamb3ed3ff2013-09-23 14:46:29 -0700652 'eth_type': eth_type, 'ip_dscp': ip_dscp, 'ip_proto': ip_proto,
Andreas Wundsame962d372013-10-02 18:15:58 -0700653 'tcp_src': transport_port, 'tcp_dst': transport_port,
654 'in_port': of_port_match_v1
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700655 },
656 'of_bsn_set_l2_table_request': { 'l2_table_enable': boolean },
657 'of_bsn_set_l2_table_reply': { 'l2_table_enable': boolean },
Rob Vaterlause1b86842013-10-18 13:29:19 -0700658 'of_bsn_set_pktin_suppression_request': { 'enabled': boolean },
Sovietaced768c3e22014-04-04 16:46:48 -0700659 'of_bsn_controller_connection': { 'auxiliary_id' : of_aux_id},
Andreas Wundsamacd57d52013-10-18 17:35:01 -0700660 'of_flow_stats_request': { 'out_group': of_group_default_any },
Andreas Wundsam384ccc12014-03-12 14:52:42 -0700661 'of_aggregate_stats_request': { 'out_group': of_group_default_any },
Andreas Wundsam98a18632013-11-05 11:34:24 -0800662
663 'of_action_bsn_mirror': { 'dest_port': of_port },
664 'of_action_push_mpls': { 'ethertype': eth_type },
665 'of_action_push_pbb': { 'ethertype': eth_type },
666 'of_action_push_vlan': { 'ethertype': eth_type },
Ronald Li4fb8abb2013-11-21 19:53:02 -0800667 'of_action_pop_mpls': { 'ethertype': eth_type },
Andreas Wundsam98a18632013-11-05 11:34:24 -0800668 'of_action_set_nw_dst': { 'nw_addr': ipv4 },
669 'of_action_set_nw_ecn': { 'nw_ecn': ip_ecn },
670 'of_action_set_nw_src': { 'nw_addr': ipv4 },
Andreas Wundsam98a18632013-11-05 11:34:24 -0800671 'of_action_set_tp_dst': { 'tp_port': transport_port },
672 'of_action_set_tp_src': { 'tp_port': transport_port },
673 'of_action_set_vlan_pcp': { 'vlan_pcp': vlan_pcp },
674 'of_action_set_vlan_vid': { 'vlan_vid': vlan_vid },
Andreas Wundsam5812cf32013-11-15 13:51:24 -0800675
676 'of_group_mod' : { 'command' : group_mod_cmd },
677 'of_group_add' : { 'command' : group_mod_cmd },
678 'of_group_modify' : { 'command' : group_mod_cmd },
679 'of_group_delete' : { 'command' : group_mod_cmd },
680
681 'of_bucket' : { 'watch_group': of_group },
Andreas Wundsamd4b22692014-01-14 14:17:26 -0800682
683 'of_bsn_tlv_vlan_vid' : { 'value' : vlan_vid },
684 'of_bsn_gentable_entry_add' : { 'table_id' : gen_table_id },
Sovietaced92603b02014-01-28 18:23:24 -0800685
Sovietaced0f9dddf2014-01-28 19:45:07 -0800686 'of_features_reply' : { 'auxiliary_id' : of_aux_id},
Andreas Wundsam27303462013-07-16 12:52:35 -0700687}
688
Andreas Wundsam7cfeac32013-09-17 13:53:48 -0700689
690@memoize
691def enum_java_types():
692 enum_types = {}
Andreas Wundsamc8912c12013-11-15 13:44:48 -0800693 for enum in loxi_globals.unified.enums:
694 java_name = name_c_to_caps_camel(re.sub(r'_t$', "", enum.name))
695 enum_types[enum.name] = gen_enum_jtype(java_name, enum.is_bitmask)
Andreas Wundsam7cfeac32013-09-17 13:53:48 -0700696 return enum_types
697
Andreas Wundsam2be7da52013-08-22 07:34:25 -0700698def make_match_field_jtype(sub_type_name="?"):
699 return JType("MatchField<{}>".format(sub_type_name))
700
Andreas Wundsam661a2222013-11-05 17:18:59 -0800701def make_oxm_jtype(sub_type_name="?"):
702 return JType("OFOxm<{}>".format(sub_type_name))
Andreas Wundsam27303462013-07-16 12:52:35 -0700703
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700704def list_cname_to_java_name(c_type):
Andreas Wundsam27303462013-07-16 12:52:35 -0700705 m = re.match(r'list\(of_([a-zA-Z_]+)_t\)', c_type)
706 if not m:
707 raise Exception("Not a recgonized standard list type declaration: %s" % c_type)
708 base_name = m.group(1)
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700709 return "OF" + name_c_to_caps_camel(base_name)
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700710
711#### main entry point for conversion of LOXI types (c_types) Java types.
712# FIXME: This badly needs a refactoring
713
Andreas Wundsam27303462013-07-16 12:52:35 -0700714def convert_to_jtype(obj_name, field_name, c_type):
715 """ Convert from a C type ("uint_32") to a java type ("U32")
716 and return a JType object with the size, internal type, and marshalling functions"""
717 if obj_name in exceptions and field_name in exceptions[obj_name]:
718 return exceptions[obj_name][field_name]
Andreas Wundsam46d230f2013-08-02 22:24:06 -0700719 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 -0700720 return of_type
Andreas Wundsam27303462013-07-16 12:52:35 -0700721 elif field_name == "type" and re.match(r'of_action.*', obj_name):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700722 return action_type
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700723 elif field_name == "err_type":
724 return JType("OFErrorType", 'short') \
725 .op(read='bb.readShort()', write='bb.writeShort($name)')
Andreas Wundsam62cbcff2014-05-09 16:35:00 -0700726 elif loxi_utils.class_is(obj_name, "of_error_msg") and field_name == "data":
727 return error_cause_data
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700728 elif field_name == "stats_type":
729 return JType("OFStatsType", 'short') \
730 .op(read='bb.readShort()', write='bb.writeShort($name)')
Andreas Wundsam999c0732013-10-01 19:29:16 -0700731 elif field_name == "type" and re.match(r'of_instruction.*', obj_name):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700732 return instruction_type
Andreas Wundsamee8b42c2014-01-24 17:38:19 -0800733 elif loxi_utils.class_is(obj_name, "of_flow_mod") and field_name == "table_id" and c_type == "uint8_t":
Andreas Wundsam45c95f82013-10-08 15:04:23 -0700734 return table_id_default_zero
Andreas Wundsamee8b42c2014-01-24 17:38:19 -0800735 elif loxi_utils.class_is(obj_name, "of_flow_mod") and field_name == "out_group" and c_type == "uint32_t":
736 return of_group_default_any
Andreas Wundsam37e0fb12013-09-28 18:57:57 -0700737 elif field_name == "table_id" and c_type == "uint8_t":
738 return table_id
Andreas Wundsam27303462013-07-16 12:52:35 -0700739 elif field_name == "version" and c_type == "uint8_t":
Andreas Wundsama0981022013-10-02 18:15:06 -0700740 return of_version
Rob Vaterlausb10ae552013-09-23 14:39:39 -0700741 elif field_name == "buffer_id" and c_type == "uint32_t":
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700742 return buffer_id
Andreas Wundsamacd57d52013-10-18 17:35:01 -0700743 elif field_name == "group_id" and c_type == "uint32_t":
744 return of_group
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700745 elif field_name == 'datapath_id':
746 return datapath_id
747 elif field_name == 'actions' and obj_name == 'of_features_reply':
Andreas Wundsam5ea1aca2013-10-07 17:00:24 -0700748 return action_type_set
Andreas Wundsamd4b22692014-01-14 14:17:26 -0800749 elif field_name == "table_id" and re.match(r'of_bsn_gentable.*', obj_name):
750 return gen_table_id
Andreas Wundsam27303462013-07-16 12:52:35 -0700751 elif c_type in default_mtype_to_jtype_convert_map:
752 return default_mtype_to_jtype_convert_map[c_type]
753 elif re.match(r'list\(of_([a-zA-Z_]+)_t\)', c_type):
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700754 return gen_list_jtype(list_cname_to_java_name(c_type))
Andreas Wundsam7cfeac32013-09-17 13:53:48 -0700755 elif c_type in enum_java_types():
756 return enum_java_types()[c_type]
Andreas Wundsam27303462013-07-16 12:52:35 -0700757 else:
758 print "WARN: Couldn't find java type conversion for '%s' in %s:%s" % (c_type, obj_name, field_name)
759 jtype = name_c_to_caps_camel(re.sub(r'_t$', "", c_type))
760 return JType(jtype)
Andreas Wundsamd8bcedf2013-08-03 21:23:37 -0700761
762
763#### Enum specific wiretype definitions
764enum_wire_types = {
765 "uint8_t": JType("byte").op(read="bb.readByte()", write="bb.writeByte($name)"),
766 "uint16_t": JType("short").op(read="bb.readShort()", write="bb.writeShort($name)"),
767 "uint32_t": JType("int").op(read="bb.readInt()", write="bb.writeInt($name)"),
768 "uint64_t": JType("long").op(read="bb.readLong()", write="bb.writeLong($name)"),
769}
770
771def convert_enum_wire_type_to_jtype(wire_type):
772 return enum_wire_types[wire_type]