blob: d03ffbf4b2ffaa626c13d031aabe1ffba3a770c1 [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:
50 private final static ${prop.java_type.public_type} ${prop.default_name} = ${prop.default_value};
51//:: #end
52
53 // OF message fields
54//:: for prop in msg.data_members:
55 private final ${prop.java_type.public_type} ${prop.name};
56//:: #endfor
57
Andreas Wundsam99e931d2013-08-22 07:53:53 -070058 //:: if msg.data_members:
59 // package private constructor - used by readers, builders, and factory
Andreas Wundsam27303462013-07-16 12:52:35 -070060 ${impl_class}(${
61 ", ".join("%s %s" %(prop.java_type.public_type, prop.name) for prop in msg.data_members) }) {
62//:: for prop in msg.data_members:
63 this.${prop.name} = ${prop.name};
64//:: #endfor
65 }
Andreas Wundsam99e931d2013-08-22 07:53:53 -070066 //:: else:
67 final static ${impl_class} INSTANCE = new ${impl_class}();
68 // private empty constructor - use shared instance!
69 private ${impl_class}() {
70 }
71 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -070072
73 // Accessors for OF message fields
Yotam Harcholf25e8142013-09-09 14:30:13 -070074 //:: include("_field_accessors.java", msg=msg, generate_setters=False, builder=False, has_parent=False)
Andreas Wundsam27303462013-07-16 12:52:35 -070075
Andreas Wundsambc679f72013-08-01 22:13:09 -070076 //:: if os.path.exists("%s/custom/%s.java" % (template_dir, msg.name)):
77 //:: include("custom/%s.java" % msg.name, msg=msg)
78 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -070079
Andreas Wundsam99e931d2013-08-22 07:53:53 -070080 //:: if msg.data_members:
Andreas Wundsam5204de22013-07-30 11:34:45 -070081 public ${msg.interface.name}.Builder createBuilder() {
82 return new BuilderWithParent(this);
Andreas Wundsam27303462013-07-16 12:52:35 -070083 }
84
Andreas Wundsam5204de22013-07-30 11:34:45 -070085 static class BuilderWithParent implements ${msg.interface.name}.Builder {
Andreas Wundsam27303462013-07-16 12:52:35 -070086 final ${impl_class} parentMessage;
87
88 // OF message fields
89//:: for prop in msg.data_members:
90 private boolean ${prop.name}Set;
91 private ${prop.java_type.public_type} ${prop.name};
92//:: #endfor
93
Andreas Wundsam5204de22013-07-30 11:34:45 -070094 BuilderWithParent(${impl_class} parentMessage) {
Andreas Wundsam27303462013-07-16 12:52:35 -070095 this.parentMessage = parentMessage;
96 }
97
Yotam Harcholf25e8142013-09-09 14:30:13 -070098//:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True, has_parent=True)
Andreas Wundsam27303462013-07-16 12:52:35 -070099
100 @Override
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700101 public ${msg.interface.name} build() {
Andreas Wundsam27303462013-07-16 12:52:35 -0700102 return new ${impl_class}(
103 ${",\n ".join(
104 [ "this.{0}Set ? this.{0} : parentMessage.{0}".format(prop.name)
105 for prop in msg.data_members])}
106 );
107 }
Andreas Wundsambc679f72013-08-01 22:13:09 -0700108 //:: if os.path.exists("%s/custom/%s.Builder.java" % (template_dir, msg.name)):
Yotam Harchol98af7752013-08-22 14:59:38 -0700109 //:: include("custom/%s.Builder.java" % msg.name, msg=msg, has_parent=True)
Andreas Wundsambc679f72013-08-01 22:13:09 -0700110 //:: #endif
111
Andreas Wundsam27303462013-07-16 12:52:35 -0700112 }
113
Andreas Wundsam5204de22013-07-30 11:34:45 -0700114 static class Builder implements ${msg.interface.name}.Builder {
Andreas Wundsam27303462013-07-16 12:52:35 -0700115 // OF message fields
116//:: for prop in msg.data_members:
117 private boolean ${prop.name}Set;
118 private ${prop.java_type.public_type} ${prop.name};
119//:: #endfor
120
Yotam Harcholf25e8142013-09-09 14:30:13 -0700121//:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True, has_parent=False)
Andreas Wundsam27303462013-07-16 12:52:35 -0700122//
123 @Override
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700124 public ${msg.interface.name} build() {
Andreas Wundsam27303462013-07-16 12:52:35 -0700125 return new ${impl_class}(
126 ${",\n ".join(
127 [ "this.{0}Set ? this.{0} : {1}.{2}".format(prop.name, impl_class, prop.default_name)
128 for prop in msg.data_members])}
129 );
130 }
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=False)
Andreas Wundsambc679f72013-08-01 22:13:09 -0700133 //:: #endif
134
Andreas Wundsam27303462013-07-16 12:52:35 -0700135 }
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700136 //:: else:
137 // no data members - do not support builder
138 public ${msg.interface.name}.Builder createBuilder() {
139 throw new UnsupportedOperationException("${impl_class} has no mutable properties -- builder unneeded");
140 }
141 //:: #endif
142
Andreas Wundsam27303462013-07-16 12:52:35 -0700143
144 final static Reader READER = new Reader();
145 static class Reader implements OFMessageReader<${msg.interface.name}> {
146 @Override
147 public ${msg.interface.name} readFrom(ChannelBuffer bb) throws OFParseError {
Yotam Harchola86e4252013-09-06 15:36:28 -0700148//:: for prop in msg.members:
149//:: if not prop.is_virtual and (prop.is_length_value or prop.is_field_length_value):
Andreas Wundsam001b1822013-08-02 22:25:55 -0700150 int start = bb.readerIndex();
Yotam Harchola86e4252013-09-06 15:36:28 -0700151//:: break
152//:: #endif
153//:: #endfor
Andreas Wundsam27303462013-07-16 12:52:35 -0700154//:: fields_with_length_member = {}
155//:: for prop in msg.members:
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700156//:: if prop.is_virtual:
157//:: continue
158//:: elif prop.is_data:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700159 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True,
Andreas Wundsam27303462013-07-16 12:52:35 -0700160 length=fields_with_length_member[prop.c_name] if prop.c_name in fields_with_length_member else None)};
161//:: elif prop.is_pad:
162 // pad: ${prop.length} bytes
163 bb.skipBytes(${prop.length});
164//:: elif prop.is_fixed_value:
165 // fixed value property ${prop.name} == ${prop.value}
Andreas Wundsam001b1822013-08-02 22:25:55 -0700166 ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
167 if(${prop.name} != ${prop.priv_value})
Andreas Wundsam27303462013-07-16 12:52:35 -0700168 throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name});
169//:: elif prop.is_length_value:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700170 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700171 if(${prop.name} < MINIMUM_LENGTH)
172 throw new OFParseError("Wrong ${prop.name}: Expected to be >= " + MINIMUM_LENGTH + ", was: " + ${prop.name});
173//:: elif prop.is_field_length_value:
174//:: fields_with_length_member[prop.member.field_name] = prop.name
Andreas Wundsam001b1822013-08-02 22:25:55 -0700175 int ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700176//:: else:
177 // fixme: todo ${prop.name}
178//:: #endif
Yotam Harchol0178c202013-09-05 16:25:50 -0700179//:: if prop.is_length_value or prop.is_field_length_value:
180 if(bb.readableBytes() + (bb.readerIndex() - start) < ${prop.name}) {
181 // Buffer does not have all data yet
182 bb.readerIndex(start);
183 return null;
184 }
185//:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700186//:: #endfor
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700187 //:: if msg.align:
188 // align message to ${msg.align} bytes
189 bb.skipBytes(((length + ${msg.align-1})/${msg.align} * ${msg.align} ) - length );
190 //:: #endif
191
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700192 //:: if msg.data_members:
Andreas Wundsam27303462013-07-16 12:52:35 -0700193 return new ${impl_class}(
194 ${",\n ".join(
195 [ prop.name for prop in msg.data_members])}
196 );
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700197 //:: else:
198 return INSTANCE;
199 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700200 }
201 }
202
Yotam Harchol5c9d6f42013-08-01 11:09:20 -0700203 public void writeTo(ChannelBuffer bb) {
204 WRITER.write(bb, this);
Andreas Wundsam27303462013-07-16 12:52:35 -0700205 }
206
207 final static Writer WRITER = new Writer();
208 static class Writer implements OFMessageWriter<${impl_class}> {
209 @Override
Andreas Wundsama94273b2013-08-01 22:11:33 -0700210 public void write(ChannelBuffer bb, ${impl_class} message) {
Yotam Harchola86e4252013-09-06 15:36:28 -0700211//:: if not msg.is_fixed_length:
Andreas Wundsam482f6d92013-07-24 16:10:21 -0700212 int startIndex = bb.writerIndex();
Yotam Harchola86e4252013-09-06 15:36:28 -0700213//:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700214//:: fields_with_length_member = {}
215//:: for prop in msg.members:
216//:: if prop.c_name in fields_with_length_member:
217 int ${prop.name}StartIndex = bb.writerIndex();
218//:: #endif
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700219//:: if prop.is_virtual:
220//:: continue
221//:: elif prop.is_data:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700222 ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700223//:: elif prop.is_pad:
224 // pad: ${prop.length} bytes
225 bb.writeZero(${prop.length});
226//:: elif prop.is_fixed_value:
227 // fixed value property ${prop.name} = ${prop.value}
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700228 ${prop.java_type.write_op(version, prop.priv_value, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700229//:: elif prop.is_length_value:
230 // ${prop.name} is length of variable message, will be updated at the end
Andreas Wundsam001b1822013-08-02 22:25:55 -0700231//:: if not msg.is_fixed_length:
232 int lengthIndex = bb.writerIndex();
233//:: #end
Andreas Wundsam27303462013-07-16 12:52:35 -0700234 ${prop.java_type.write_op(version, 0)};
Andreas Wundsam001b1822013-08-02 22:25:55 -0700235
Andreas Wundsam27303462013-07-16 12:52:35 -0700236//:: elif prop.is_field_length_value:
237//:: fields_with_length_member[prop.member.field_name] = prop.name
238 // ${prop.name} is length indicator for ${prop.member.field_name}, will be
239 // udpated when ${prop.member.field_name} has been written
240 int ${prop.name}Index = bb.writerIndex();
Andreas Wundsam001b1822013-08-02 22:25:55 -0700241 ${prop.java_type.write_op(version, 0, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700242//:: else:
243 // FIXME: todo write ${prop.name}
244//:: #endif
245//:: if prop.c_name in fields_with_length_member:
246//:: length_member_name = fields_with_length_member[prop.c_name]
247 // update field length member ${length_member_name}
248 int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex;
249 bb.setShort(${length_member_name}Index, ${prop.name}Length);
250//:: #endif
251//:: #endfor
252
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700253//:: if not msg.is_fixed_length:
Andreas Wundsam27303462013-07-16 12:52:35 -0700254 // update length field
255 int length = bb.writerIndex() - startIndex;
Andreas Wundsam001b1822013-08-02 22:25:55 -0700256 bb.setShort(lengthIndex, length);
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700257 //:: if msg.align:
258 // align message to ${msg.align} bytes
259 bb.writeZero( ((length + ${msg.align-1})/${msg.align} * ${msg.align}) - length);
260 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700261//:: #end
262
263 }
264 }
265
Andreas Wundsameeefb552013-07-30 11:04:35 -0700266 @Override
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700267 public String toString() {
268 StringBuilder b = new StringBuilder("${msg.name}(");
269 //:: for i, prop in enumerate(msg.data_members):
270 //:: if i > 0:
271 b.append(", ");
272 //:: #endif
273 b.append("${prop.name}=").append(${ "Arrays.toString(%s)" % prop.name if prop.java_type.is_array else prop.name });
274 //:: #endfor
275 b.append(")");
276 return b.toString();
277 }
278
279
280 @Override
Andreas Wundsameeefb552013-07-30 11:04:35 -0700281 public boolean equals(Object obj) {
282 if (this == obj)
283 return true;
284 if (obj == null)
285 return false;
286 if (getClass() != obj.getClass())
287 return false;
Yotam Harchola86e4252013-09-06 15:36:28 -0700288 //:: if len(msg.data_members) > 0:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700289 ${msg.name} other = (${msg.name}) obj;
Yotam Harchola86e4252013-09-06 15:36:28 -0700290 //:: #endif
Andreas Wundsameeefb552013-07-30 11:04:35 -0700291
292 //:: for prop in msg.data_members:
293 //:: if prop.java_type.is_primitive:
294 if( ${prop.name} != other.${prop.name})
295 return false;
296 //:: elif prop.java_type.is_array:
297 if (!Arrays.equals(${prop.name}, other.${prop.name}))
298 return false;
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700299 //:: else:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700300 if (${prop.name} == null) {
301 if (other.${prop.name} != null)
302 return false;
303 } else if (!${prop.name}.equals(other.${prop.name}))
304 return false;
305 //:: #endif
306 //:: #endfor
307 return true;
308 }
309
310 @Override
311 public int hashCode() {
Yotam Harchola86e4252013-09-06 15:36:28 -0700312 //:: if len(msg.data_members) > 0:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700313 final int prime = 31;
Yotam Harchola86e4252013-09-06 15:36:28 -0700314 //:: #endif
Andreas Wundsameeefb552013-07-30 11:04:35 -0700315 int result = 1;
316
317 //:: for prop in msg.data_members:
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700318 //:: if prop.java_type.pub_type == 'long':
319 result = prime * (int) (${prop.name} ^ (${prop.name} >>> 32));
320 //:: elif prop.java_type.is_primitive:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700321 result = prime * result + ${prop.name};
322 //:: elif prop.java_type.is_array:
323 result = prime * result + Arrays.hashCode(${prop.name});
324 //:: else:
325 result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode());
326 //:: #endif
327 //:: #endfor
328 return result;
329 }
330
Andreas Wundsam27303462013-07-16 12:52:35 -0700331
332}