blob: 8014ee75bebb32bed7a91514b3ac5849620fffb1 [file] [log] [blame]
Andreas Wundsam27303462013-07-16 12:52:35 -07001//:: # Copyright 2013, Big Switch Networks, Inc.
2//:: #
3//:: # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
4//:: # the following special exception:
5//:: #
6//:: # LOXI Exception
7//:: #
8//:: # As a special exception to the terms of the EPL, you may distribute libraries
9//:: # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
10//:: # that copyright and licensing notices generated by LoxiGen are not altered or removed
11//:: # from the LoxiGen Libraries and the notice provided below is (i) included in
12//:: # the LoxiGen Libraries, if distributed in source code form and (ii) included in any
13//:: # documentation for the LoxiGen Libraries, if distributed in binary form.
14//:: #
15//:: # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
16//:: #
17//:: # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
18//:: # a copy of the EPL at:
19//:: #
20//:: # http::: #www.eclipse.org/legal/epl-v10.html
21//:: #
22//:: # Unless required by applicable law or agreed to in writing, software
23//:: # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24//:: # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
25//:: # EPL for the specific language governing permissions and limitations
26//:: # under the EPL.
27//::
28//:: from loxi_ir import *
Andreas Wundsambc679f72013-08-01 22:13:09 -070029//:: import os
Andreas Wundsam27303462013-07-16 12:52:35 -070030//:: import itertools
31//:: import of_g
32//:: include('_copyright.java')
33
34//:: include('_autogen.java')
35
36package ${msg.package};
37
38//:: include("_imports.java", msg=msg)
39
Andreas Wundsam99e931d2013-08-22 07:53:53 -070040class ${impl_class} implements ${msg.interface.inherited_declaration()} {
Andreas Wundsam27303462013-07-16 12:52:35 -070041 // version: ${version}
Yotam Harchol791e4882013-09-05 16:32:56 -070042 final static byte WIRE_VERSION = ${version.int_version};
Andreas Wundsam27303462013-07-16 12:52:35 -070043//:: if msg.is_fixed_length:
Yotam Harchol791e4882013-09-05 16:32:56 -070044 final static int LENGTH = ${msg.length};
Andreas Wundsam27303462013-07-16 12:52:35 -070045//:: else:
Yotam Harchol791e4882013-09-05 16:32:56 -070046 final static int MINIMUM_LENGTH = ${msg.min_length};
Andreas Wundsam27303462013-07-16 12:52:35 -070047//:: #endif
48
49//:: for prop in msg.data_members:
Andreas Wundsamf89f7822013-09-23 14:49:24 -070050 //:: if prop.default_value:
51 private final static ${prop.java_type.public_type} ${prop.default_name} = ${prop.default_value};
52 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -070053//:: #end
54
55 // OF message fields
56//:: for prop in msg.data_members:
57 private final ${prop.java_type.public_type} ${prop.name};
58//:: #endfor
Andreas Wundsame962d372013-10-02 18:15:58 -070059//
60//:: if all(prop.default_value for prop in msg.data_members):
61 // Immutable default instance
62 final static ${impl_class} DEFAULT = new ${impl_class}(
63 ${", ".join(prop.default_name for prop in msg.data_members)}
64 );
65//:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -070066
Andreas Wundsam99e931d2013-08-22 07:53:53 -070067 //:: if msg.data_members:
68 // package private constructor - used by readers, builders, and factory
Andreas Wundsam27303462013-07-16 12:52:35 -070069 ${impl_class}(${
70 ", ".join("%s %s" %(prop.java_type.public_type, prop.name) for prop in msg.data_members) }) {
71//:: for prop in msg.data_members:
72 this.${prop.name} = ${prop.name};
73//:: #endfor
74 }
Andreas Wundsam99e931d2013-08-22 07:53:53 -070075 //:: else:
76 final static ${impl_class} INSTANCE = new ${impl_class}();
77 // private empty constructor - use shared instance!
78 private ${impl_class}() {
79 }
80 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -070081
82 // Accessors for OF message fields
Yotam Harcholf25e8142013-09-09 14:30:13 -070083 //:: include("_field_accessors.java", msg=msg, generate_setters=False, builder=False, has_parent=False)
Andreas Wundsam27303462013-07-16 12:52:35 -070084
Andreas Wundsambc679f72013-08-01 22:13:09 -070085 //:: if os.path.exists("%s/custom/%s.java" % (template_dir, msg.name)):
86 //:: include("custom/%s.java" % msg.name, msg=msg)
87 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -070088
Andreas Wundsam99e931d2013-08-22 07:53:53 -070089 //:: if msg.data_members:
Andreas Wundsam5204de22013-07-30 11:34:45 -070090 public ${msg.interface.name}.Builder createBuilder() {
91 return new BuilderWithParent(this);
Andreas Wundsam27303462013-07-16 12:52:35 -070092 }
93
Andreas Wundsam5204de22013-07-30 11:34:45 -070094 static class BuilderWithParent implements ${msg.interface.name}.Builder {
Andreas Wundsam27303462013-07-16 12:52:35 -070095 final ${impl_class} parentMessage;
96
97 // OF message fields
98//:: for prop in msg.data_members:
99 private boolean ${prop.name}Set;
100 private ${prop.java_type.public_type} ${prop.name};
101//:: #endfor
102
Andreas Wundsam5204de22013-07-30 11:34:45 -0700103 BuilderWithParent(${impl_class} parentMessage) {
Andreas Wundsam27303462013-07-16 12:52:35 -0700104 this.parentMessage = parentMessage;
105 }
106
Yotam Harcholf25e8142013-09-09 14:30:13 -0700107//:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True, has_parent=True)
Andreas Wundsam27303462013-07-16 12:52:35 -0700108
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700109
Andreas Wundsam27303462013-07-16 12:52:35 -0700110 @Override
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700111 public ${msg.interface.name} build() {
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700112 //:: for prop in msg.data_members:
113 ${prop.java_type.public_type} ${prop.name} = this.${prop.name}Set ? this.${prop.name} : parentMessage.${prop.name};
114 //:: if not prop.is_nullable and not prop.java_type.is_primitive:
115 if(${prop.name} == null)
116 throw new NullPointerException("Property ${prop.name} must not be null");
117 //:: #endif
118 //:: #endfor
119
120 //
121 //:: if os.path.exists("%s/custom/%s.Builder_normalize_stanza.java" % (template_dir, msg.name)):
122 //:: include("custom/%s.Builder_normalize_stanza.java" % msg.name, msg=msg, has_parent=False)
123 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700124 return new ${impl_class}(
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700125 //:: for i, prop in enumerate(msg.data_members):
126 //:: comma = "," if i < len(msg.data_members)-1 else ""
127 ${prop.name}${comma}
128 //:: #endfor
129 );
Andreas Wundsam27303462013-07-16 12:52:35 -0700130 }
Andreas Wundsambc679f72013-08-01 22:13:09 -0700131 //:: if os.path.exists("%s/custom/%s.Builder.java" % (template_dir, msg.name)):
Yotam Harchol98af7752013-08-22 14:59:38 -0700132 //:: include("custom/%s.Builder.java" % msg.name, msg=msg, has_parent=True)
Andreas Wundsambc679f72013-08-01 22:13:09 -0700133 //:: #endif
134
Andreas Wundsam27303462013-07-16 12:52:35 -0700135 }
136
Andreas Wundsam5204de22013-07-30 11:34:45 -0700137 static class Builder implements ${msg.interface.name}.Builder {
Andreas Wundsam27303462013-07-16 12:52:35 -0700138 // OF message fields
139//:: for prop in msg.data_members:
140 private boolean ${prop.name}Set;
141 private ${prop.java_type.public_type} ${prop.name};
142//:: #endfor
143
Yotam Harcholf25e8142013-09-09 14:30:13 -0700144//:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True, has_parent=False)
Andreas Wundsam27303462013-07-16 12:52:35 -0700145//
146 @Override
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700147 public ${msg.interface.name} build() {
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700148 //:: for prop in msg.data_members:
149 //:: if prop.default_value:
150 ${prop.java_type.public_type} ${prop.name} = this.${prop.name}Set ? this.${prop.name} : ${prop.default_name};
151 //:: else:
152 if(!this.${prop.name}Set)
153 throw new IllegalStateException("Property ${prop.name} doesn't have default value -- must be set");
154 //:: #endif
155 //:: if not prop.is_nullable and not prop.java_type.is_primitive:
156 if(${prop.name} == null)
157 throw new NullPointerException("Property ${prop.name} must not be null");
158 //:: #endif
159 //:: #endfor
160
161 //:: if os.path.exists("%s/custom/%s.Builder_normalize_stanza.java" % (template_dir, msg.name)):
162 //:: include("custom/%s.Builder_normalize_stanza.java" % msg.name, msg=msg, has_parent=False)
163 //:: #endif
164
Andreas Wundsam27303462013-07-16 12:52:35 -0700165 return new ${impl_class}(
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700166 //:: for i, prop in enumerate(msg.data_members):
167 //:: comma = "," if i < len(msg.data_members)-1 else ""
168 ${prop.name}${comma}
169 //:: #endfor
Andreas Wundsam27303462013-07-16 12:52:35 -0700170 );
171 }
Andreas Wundsambc679f72013-08-01 22:13:09 -0700172 //:: if os.path.exists("%s/custom/%s.Builder.java" % (template_dir, msg.name)):
Yotam Harchol98af7752013-08-22 14:59:38 -0700173 //:: include("custom/%s.Builder.java" % msg.name, msg=msg, has_parent=False)
Andreas Wundsambc679f72013-08-01 22:13:09 -0700174 //:: #endif
175
Andreas Wundsam27303462013-07-16 12:52:35 -0700176 }
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700177 //:: else:
178 // no data members - do not support builder
179 public ${msg.interface.name}.Builder createBuilder() {
180 throw new UnsupportedOperationException("${impl_class} has no mutable properties -- builder unneeded");
181 }
182 //:: #endif
183
Andreas Wundsam27303462013-07-16 12:52:35 -0700184
185 final static Reader READER = new Reader();
186 static class Reader implements OFMessageReader<${msg.interface.name}> {
187 @Override
188 public ${msg.interface.name} readFrom(ChannelBuffer bb) throws OFParseError {
Yotam Harchola86e4252013-09-06 15:36:28 -0700189//:: for prop in msg.members:
190//:: if not prop.is_virtual and (prop.is_length_value or prop.is_field_length_value):
Andreas Wundsam001b1822013-08-02 22:25:55 -0700191 int start = bb.readerIndex();
Yotam Harchola86e4252013-09-06 15:36:28 -0700192//:: break
193//:: #endif
194//:: #endfor
Andreas Wundsam27303462013-07-16 12:52:35 -0700195//:: fields_with_length_member = {}
196//:: for prop in msg.members:
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700197//:: if prop.is_virtual:
198//:: continue
199//:: elif prop.is_data:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700200 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True,
Andreas Wundsam27303462013-07-16 12:52:35 -0700201 length=fields_with_length_member[prop.c_name] if prop.c_name in fields_with_length_member else None)};
202//:: elif prop.is_pad:
203 // pad: ${prop.length} bytes
204 bb.skipBytes(${prop.length});
Andreas Wundsam27303462013-07-16 12:52:35 -0700205//:: elif prop.is_length_value:
Andreas Wundsam83d877a2013-09-30 14:26:44 -0700206 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True)};
207 //:: if prop.is_fixed_value:
208 if(${prop.name} != ${prop.value})
209 throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name});
210 //:: else:
Andreas Wundsam27303462013-07-16 12:52:35 -0700211 if(${prop.name} < MINIMUM_LENGTH)
212 throw new OFParseError("Wrong ${prop.name}: Expected to be >= " + MINIMUM_LENGTH + ", was: " + ${prop.name});
Andreas Wundsam83d877a2013-09-30 14:26:44 -0700213 //:: #endif
Yotam Harchol0178c202013-09-05 16:25:50 -0700214 if(bb.readableBytes() + (bb.readerIndex() - start) < ${prop.name}) {
215 // Buffer does not have all data yet
216 bb.readerIndex(start);
217 return null;
218 }
Andreas Wundsam83d877a2013-09-30 14:26:44 -0700219//:: elif prop.is_fixed_value:
220 // fixed value property ${prop.name} == ${prop.value}
221 ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
222 if(${prop.name} != ${prop.priv_value})
223 throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name});
224//:: elif prop.is_field_length_value:
225//:: fields_with_length_member[prop.member.field_name] = prop.name
226 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True)};
227//:: else:
228 // fixme: todo ${prop.name}
Yotam Harchol0178c202013-09-05 16:25:50 -0700229//:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700230//:: #endfor
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700231 //:: if msg.align:
232 // align message to ${msg.align} bytes
233 bb.skipBytes(((length + ${msg.align-1})/${msg.align} * ${msg.align} ) - length );
234 //:: #endif
235
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700236 //:: if msg.data_members:
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700237 //:: if os.path.exists("%s/custom/%s.Reader_normalize_stanza.java" % (template_dir, msg.name)):
238 //:: include("custom/%s.Reader_normalize_stanza.java" % msg.name, msg=msg, has_parent=False)
239 //:: #endif
240 return new ${impl_class}(
Andreas Wundsam27303462013-07-16 12:52:35 -0700241 ${",\n ".join(
242 [ prop.name for prop in msg.data_members])}
243 );
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700244 //:: else:
245 return INSTANCE;
246 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700247 }
248 }
249
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700250 public void putTo(PrimitiveSink sink) {
251 FUNNEL.funnel(this, sink);
252 }
253
254 final static ${impl_class}Funnel FUNNEL = new ${impl_class}Funnel();
255 static class ${impl_class}Funnel implements Funnel<${impl_class}> {
256 private static final long serialVersionUID = 1L;
257 @Override
258 public void funnel(${impl_class} message, PrimitiveSink sink) {
259//:: for prop in msg.members:
260//:: if prop.is_virtual:
261//:: continue
262//:: elif prop.is_data:
263 ${prop.java_type.funnel_op(version, "message." + prop.name, pub_type=True)};
264//:: elif prop.is_pad:
265 // skip pad (${prop.length} bytes)
266//:: elif prop.is_fixed_value:
267 // fixed value property ${prop.name} = ${prop.value}
268 ${prop.java_type.funnel_op(version, prop.priv_value, pub_type=False)};
269//:: else:
270 // FIXME: skip funnel of ${prop.name}
271//:: #endif
272//:: #endfor
273 }
274 }
275
276
Yotam Harchol5c9d6f42013-08-01 11:09:20 -0700277 public void writeTo(ChannelBuffer bb) {
278 WRITER.write(bb, this);
Andreas Wundsam27303462013-07-16 12:52:35 -0700279 }
280
281 final static Writer WRITER = new Writer();
282 static class Writer implements OFMessageWriter<${impl_class}> {
283 @Override
Andreas Wundsama94273b2013-08-01 22:11:33 -0700284 public void write(ChannelBuffer bb, ${impl_class} message) {
Yotam Harchola86e4252013-09-06 15:36:28 -0700285//:: if not msg.is_fixed_length:
Andreas Wundsam482f6d92013-07-24 16:10:21 -0700286 int startIndex = bb.writerIndex();
Yotam Harchola86e4252013-09-06 15:36:28 -0700287//:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700288//:: fields_with_length_member = {}
289//:: for prop in msg.members:
290//:: if prop.c_name in fields_with_length_member:
291 int ${prop.name}StartIndex = bb.writerIndex();
292//:: #endif
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700293//:: if prop.is_virtual:
294//:: continue
295//:: elif prop.is_data:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700296 ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700297//:: elif prop.is_pad:
298 // pad: ${prop.length} bytes
299 bb.writeZero(${prop.length});
300//:: elif prop.is_fixed_value:
301 // fixed value property ${prop.name} = ${prop.value}
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700302 ${prop.java_type.write_op(version, prop.priv_value, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700303//:: elif prop.is_length_value:
304 // ${prop.name} is length of variable message, will be updated at the end
Andreas Wundsam001b1822013-08-02 22:25:55 -0700305//:: if not msg.is_fixed_length:
306 int lengthIndex = bb.writerIndex();
307//:: #end
Andreas Wundsam27303462013-07-16 12:52:35 -0700308 ${prop.java_type.write_op(version, 0)};
Andreas Wundsam001b1822013-08-02 22:25:55 -0700309
Andreas Wundsam27303462013-07-16 12:52:35 -0700310//:: elif prop.is_field_length_value:
311//:: fields_with_length_member[prop.member.field_name] = prop.name
312 // ${prop.name} is length indicator for ${prop.member.field_name}, will be
313 // udpated when ${prop.member.field_name} has been written
314 int ${prop.name}Index = bb.writerIndex();
Andreas Wundsam001b1822013-08-02 22:25:55 -0700315 ${prop.java_type.write_op(version, 0, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700316//:: else:
317 // FIXME: todo write ${prop.name}
318//:: #endif
319//:: if prop.c_name in fields_with_length_member:
320//:: length_member_name = fields_with_length_member[prop.c_name]
321 // update field length member ${length_member_name}
322 int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex;
323 bb.setShort(${length_member_name}Index, ${prop.name}Length);
324//:: #endif
325//:: #endfor
326
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700327//:: if not msg.is_fixed_length:
Andreas Wundsam27303462013-07-16 12:52:35 -0700328 // update length field
329 int length = bb.writerIndex() - startIndex;
Andreas Wundsam001b1822013-08-02 22:25:55 -0700330 bb.setShort(lengthIndex, length);
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700331 //:: if msg.align:
332 // align message to ${msg.align} bytes
333 bb.writeZero( ((length + ${msg.align-1})/${msg.align} * ${msg.align}) - length);
334 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700335//:: #end
336
337 }
338 }
339
Andreas Wundsameeefb552013-07-30 11:04:35 -0700340 @Override
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700341 public String toString() {
342 StringBuilder b = new StringBuilder("${msg.name}(");
343 //:: for i, prop in enumerate(msg.data_members):
344 //:: if i > 0:
345 b.append(", ");
346 //:: #endif
347 b.append("${prop.name}=").append(${ "Arrays.toString(%s)" % prop.name if prop.java_type.is_array else prop.name });
348 //:: #endfor
349 b.append(")");
350 return b.toString();
351 }
352
353
354 @Override
Andreas Wundsameeefb552013-07-30 11:04:35 -0700355 public boolean equals(Object obj) {
356 if (this == obj)
357 return true;
358 if (obj == null)
359 return false;
360 if (getClass() != obj.getClass())
361 return false;
Yotam Harchola86e4252013-09-06 15:36:28 -0700362 //:: if len(msg.data_members) > 0:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700363 ${msg.name} other = (${msg.name}) obj;
Yotam Harchola86e4252013-09-06 15:36:28 -0700364 //:: #endif
Andreas Wundsameeefb552013-07-30 11:04:35 -0700365
366 //:: for prop in msg.data_members:
367 //:: if prop.java_type.is_primitive:
368 if( ${prop.name} != other.${prop.name})
369 return false;
370 //:: elif prop.java_type.is_array:
371 if (!Arrays.equals(${prop.name}, other.${prop.name}))
372 return false;
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700373 //:: else:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700374 if (${prop.name} == null) {
375 if (other.${prop.name} != null)
376 return false;
377 } else if (!${prop.name}.equals(other.${prop.name}))
378 return false;
379 //:: #endif
380 //:: #endfor
381 return true;
382 }
383
384 @Override
385 public int hashCode() {
Yotam Harchola86e4252013-09-06 15:36:28 -0700386 //:: if len(msg.data_members) > 0:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700387 final int prime = 31;
Yotam Harchola86e4252013-09-06 15:36:28 -0700388 //:: #endif
Andreas Wundsameeefb552013-07-30 11:04:35 -0700389 int result = 1;
390
391 //:: for prop in msg.data_members:
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700392 //:: if prop.java_type.pub_type == 'long':
393 result = prime * (int) (${prop.name} ^ (${prop.name} >>> 32));
394 //:: elif prop.java_type.is_primitive:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700395 result = prime * result + ${prop.name};
396 //:: elif prop.java_type.is_array:
397 result = prime * result + Arrays.hashCode(${prop.name});
398 //:: else:
399 result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode());
400 //:: #endif
401 //:: #endfor
402 return result;
403 }
404
Andreas Wundsam27303462013-07-16 12:52:35 -0700405
406}