blob: 458d3f060acdf2d555f84e9f29f9d4d6bfa7e161 [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
Yotam Harchol5c9d6f42013-08-01 11:09:20 -0700250 public void writeTo(ChannelBuffer bb) {
251 WRITER.write(bb, this);
Andreas Wundsam27303462013-07-16 12:52:35 -0700252 }
253
254 final static Writer WRITER = new Writer();
255 static class Writer implements OFMessageWriter<${impl_class}> {
256 @Override
Andreas Wundsama94273b2013-08-01 22:11:33 -0700257 public void write(ChannelBuffer bb, ${impl_class} message) {
Yotam Harchola86e4252013-09-06 15:36:28 -0700258//:: if not msg.is_fixed_length:
Andreas Wundsam482f6d92013-07-24 16:10:21 -0700259 int startIndex = bb.writerIndex();
Yotam Harchola86e4252013-09-06 15:36:28 -0700260//:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700261//:: fields_with_length_member = {}
262//:: for prop in msg.members:
263//:: if prop.c_name in fields_with_length_member:
264 int ${prop.name}StartIndex = bb.writerIndex();
265//:: #endif
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700266//:: if prop.is_virtual:
267//:: continue
268//:: elif prop.is_data:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700269 ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700270//:: elif prop.is_pad:
271 // pad: ${prop.length} bytes
272 bb.writeZero(${prop.length});
273//:: elif prop.is_fixed_value:
274 // fixed value property ${prop.name} = ${prop.value}
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700275 ${prop.java_type.write_op(version, prop.priv_value, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700276//:: elif prop.is_length_value:
277 // ${prop.name} is length of variable message, will be updated at the end
Andreas Wundsam001b1822013-08-02 22:25:55 -0700278//:: if not msg.is_fixed_length:
279 int lengthIndex = bb.writerIndex();
280//:: #end
Andreas Wundsam27303462013-07-16 12:52:35 -0700281 ${prop.java_type.write_op(version, 0)};
Andreas Wundsam001b1822013-08-02 22:25:55 -0700282
Andreas Wundsam27303462013-07-16 12:52:35 -0700283//:: elif prop.is_field_length_value:
284//:: fields_with_length_member[prop.member.field_name] = prop.name
285 // ${prop.name} is length indicator for ${prop.member.field_name}, will be
286 // udpated when ${prop.member.field_name} has been written
287 int ${prop.name}Index = bb.writerIndex();
Andreas Wundsam001b1822013-08-02 22:25:55 -0700288 ${prop.java_type.write_op(version, 0, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700289//:: else:
290 // FIXME: todo write ${prop.name}
291//:: #endif
292//:: if prop.c_name in fields_with_length_member:
293//:: length_member_name = fields_with_length_member[prop.c_name]
294 // update field length member ${length_member_name}
295 int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex;
296 bb.setShort(${length_member_name}Index, ${prop.name}Length);
297//:: #endif
298//:: #endfor
299
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700300//:: if not msg.is_fixed_length:
Andreas Wundsam27303462013-07-16 12:52:35 -0700301 // update length field
302 int length = bb.writerIndex() - startIndex;
Andreas Wundsam001b1822013-08-02 22:25:55 -0700303 bb.setShort(lengthIndex, length);
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700304 //:: if msg.align:
305 // align message to ${msg.align} bytes
306 bb.writeZero( ((length + ${msg.align-1})/${msg.align} * ${msg.align}) - length);
307 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700308//:: #end
309
310 }
311 }
312
Andreas Wundsameeefb552013-07-30 11:04:35 -0700313 @Override
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700314 public String toString() {
315 StringBuilder b = new StringBuilder("${msg.name}(");
316 //:: for i, prop in enumerate(msg.data_members):
317 //:: if i > 0:
318 b.append(", ");
319 //:: #endif
320 b.append("${prop.name}=").append(${ "Arrays.toString(%s)" % prop.name if prop.java_type.is_array else prop.name });
321 //:: #endfor
322 b.append(")");
323 return b.toString();
324 }
325
326
327 @Override
Andreas Wundsameeefb552013-07-30 11:04:35 -0700328 public boolean equals(Object obj) {
329 if (this == obj)
330 return true;
331 if (obj == null)
332 return false;
333 if (getClass() != obj.getClass())
334 return false;
Yotam Harchola86e4252013-09-06 15:36:28 -0700335 //:: if len(msg.data_members) > 0:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700336 ${msg.name} other = (${msg.name}) obj;
Yotam Harchola86e4252013-09-06 15:36:28 -0700337 //:: #endif
Andreas Wundsameeefb552013-07-30 11:04:35 -0700338
339 //:: for prop in msg.data_members:
340 //:: if prop.java_type.is_primitive:
341 if( ${prop.name} != other.${prop.name})
342 return false;
343 //:: elif prop.java_type.is_array:
344 if (!Arrays.equals(${prop.name}, other.${prop.name}))
345 return false;
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700346 //:: else:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700347 if (${prop.name} == null) {
348 if (other.${prop.name} != null)
349 return false;
350 } else if (!${prop.name}.equals(other.${prop.name}))
351 return false;
352 //:: #endif
353 //:: #endfor
354 return true;
355 }
356
357 @Override
358 public int hashCode() {
Yotam Harchola86e4252013-09-06 15:36:28 -0700359 //:: if len(msg.data_members) > 0:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700360 final int prime = 31;
Yotam Harchola86e4252013-09-06 15:36:28 -0700361 //:: #endif
Andreas Wundsameeefb552013-07-30 11:04:35 -0700362 int result = 1;
363
364 //:: for prop in msg.data_members:
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700365 //:: if prop.java_type.pub_type == 'long':
366 result = prime * (int) (${prop.name} ^ (${prop.name} >>> 32));
Rob Vaterlausfeee3712013-09-30 11:24:19 -0700367 //:: elif prop.java_type.pub_type == 'boolean':
368 result = prime * result + (${prop.name} ? 1231 : 1237);
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700369 //:: elif prop.java_type.is_primitive:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700370 result = prime * result + ${prop.name};
371 //:: elif prop.java_type.is_array:
372 result = prime * result + Arrays.hashCode(${prop.name});
373 //:: else:
374 result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode());
375 //:: #endif
376 //:: #endfor
377 return result;
378 }
379
Andreas Wundsam27303462013-07-16 12:52:35 -0700380
381}