blob: 8a8d09d2a28a80b9f2da8f42fc9461dae224fa52 [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
59
Andreas Wundsam99e931d2013-08-22 07:53:53 -070060 //:: if msg.data_members:
61 // package private constructor - used by readers, builders, and factory
Andreas Wundsam27303462013-07-16 12:52:35 -070062 ${impl_class}(${
63 ", ".join("%s %s" %(prop.java_type.public_type, prop.name) for prop in msg.data_members) }) {
64//:: for prop in msg.data_members:
65 this.${prop.name} = ${prop.name};
66//:: #endfor
67 }
Andreas Wundsam99e931d2013-08-22 07:53:53 -070068 //:: else:
69 final static ${impl_class} INSTANCE = new ${impl_class}();
70 // private empty constructor - use shared instance!
71 private ${impl_class}() {
72 }
73 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -070074
75 // Accessors for OF message fields
Yotam Harcholf25e8142013-09-09 14:30:13 -070076 //:: include("_field_accessors.java", msg=msg, generate_setters=False, builder=False, has_parent=False)
Andreas Wundsam27303462013-07-16 12:52:35 -070077
Andreas Wundsambc679f72013-08-01 22:13:09 -070078 //:: if os.path.exists("%s/custom/%s.java" % (template_dir, msg.name)):
79 //:: include("custom/%s.java" % msg.name, msg=msg)
80 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -070081
Andreas Wundsam99e931d2013-08-22 07:53:53 -070082 //:: if msg.data_members:
Andreas Wundsam5204de22013-07-30 11:34:45 -070083 public ${msg.interface.name}.Builder createBuilder() {
84 return new BuilderWithParent(this);
Andreas Wundsam27303462013-07-16 12:52:35 -070085 }
86
Andreas Wundsam5204de22013-07-30 11:34:45 -070087 static class BuilderWithParent implements ${msg.interface.name}.Builder {
Andreas Wundsam27303462013-07-16 12:52:35 -070088 final ${impl_class} parentMessage;
89
90 // OF message fields
91//:: for prop in msg.data_members:
92 private boolean ${prop.name}Set;
93 private ${prop.java_type.public_type} ${prop.name};
94//:: #endfor
95
Andreas Wundsam5204de22013-07-30 11:34:45 -070096 BuilderWithParent(${impl_class} parentMessage) {
Andreas Wundsam27303462013-07-16 12:52:35 -070097 this.parentMessage = parentMessage;
98 }
99
Yotam Harcholf25e8142013-09-09 14:30:13 -0700100//:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True, has_parent=True)
Andreas Wundsam27303462013-07-16 12:52:35 -0700101
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700102
Andreas Wundsam27303462013-07-16 12:52:35 -0700103 @Override
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700104 public ${msg.interface.name} build() {
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700105 //:: for prop in msg.data_members:
106 ${prop.java_type.public_type} ${prop.name} = this.${prop.name}Set ? this.${prop.name} : parentMessage.${prop.name};
107 //:: if not prop.is_nullable and not prop.java_type.is_primitive:
108 if(${prop.name} == null)
109 throw new NullPointerException("Property ${prop.name} must not be null");
110 //:: #endif
111 //:: #endfor
112
113 //
114 //:: if os.path.exists("%s/custom/%s.Builder_normalize_stanza.java" % (template_dir, msg.name)):
115 //:: include("custom/%s.Builder_normalize_stanza.java" % msg.name, msg=msg, has_parent=False)
116 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700117 return new ${impl_class}(
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700118 //:: for i, prop in enumerate(msg.data_members):
119 //:: comma = "," if i < len(msg.data_members)-1 else ""
120 ${prop.name}${comma}
121 //:: #endfor
122 );
Andreas Wundsam27303462013-07-16 12:52:35 -0700123 }
Andreas Wundsambc679f72013-08-01 22:13:09 -0700124 //:: if os.path.exists("%s/custom/%s.Builder.java" % (template_dir, msg.name)):
Yotam Harchol98af7752013-08-22 14:59:38 -0700125 //:: include("custom/%s.Builder.java" % msg.name, msg=msg, has_parent=True)
Andreas Wundsambc679f72013-08-01 22:13:09 -0700126 //:: #endif
127
Andreas Wundsam27303462013-07-16 12:52:35 -0700128 }
129
Andreas Wundsam5204de22013-07-30 11:34:45 -0700130 static class Builder implements ${msg.interface.name}.Builder {
Andreas Wundsam27303462013-07-16 12:52:35 -0700131 // OF message fields
132//:: for prop in msg.data_members:
133 private boolean ${prop.name}Set;
134 private ${prop.java_type.public_type} ${prop.name};
135//:: #endfor
136
Yotam Harcholf25e8142013-09-09 14:30:13 -0700137//:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True, has_parent=False)
Andreas Wundsam27303462013-07-16 12:52:35 -0700138//
139 @Override
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700140 public ${msg.interface.name} build() {
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700141 //:: for prop in msg.data_members:
142 //:: if prop.default_value:
143 ${prop.java_type.public_type} ${prop.name} = this.${prop.name}Set ? this.${prop.name} : ${prop.default_name};
144 //:: else:
145 if(!this.${prop.name}Set)
146 throw new IllegalStateException("Property ${prop.name} doesn't have default value -- must be set");
147 //:: #endif
148 //:: if not prop.is_nullable and not prop.java_type.is_primitive:
149 if(${prop.name} == null)
150 throw new NullPointerException("Property ${prop.name} must not be null");
151 //:: #endif
152 //:: #endfor
153
154 //:: if os.path.exists("%s/custom/%s.Builder_normalize_stanza.java" % (template_dir, msg.name)):
155 //:: include("custom/%s.Builder_normalize_stanza.java" % msg.name, msg=msg, has_parent=False)
156 //:: #endif
157
Andreas Wundsam27303462013-07-16 12:52:35 -0700158 return new ${impl_class}(
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700159 //:: for i, prop in enumerate(msg.data_members):
160 //:: comma = "," if i < len(msg.data_members)-1 else ""
161 ${prop.name}${comma}
162 //:: #endfor
Andreas Wundsam27303462013-07-16 12:52:35 -0700163 );
164 }
Andreas Wundsambc679f72013-08-01 22:13:09 -0700165 //:: if os.path.exists("%s/custom/%s.Builder.java" % (template_dir, msg.name)):
Yotam Harchol98af7752013-08-22 14:59:38 -0700166 //:: include("custom/%s.Builder.java" % msg.name, msg=msg, has_parent=False)
Andreas Wundsambc679f72013-08-01 22:13:09 -0700167 //:: #endif
168
Andreas Wundsam27303462013-07-16 12:52:35 -0700169 }
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700170 //:: else:
171 // no data members - do not support builder
172 public ${msg.interface.name}.Builder createBuilder() {
173 throw new UnsupportedOperationException("${impl_class} has no mutable properties -- builder unneeded");
174 }
175 //:: #endif
176
Andreas Wundsam27303462013-07-16 12:52:35 -0700177
178 final static Reader READER = new Reader();
179 static class Reader implements OFMessageReader<${msg.interface.name}> {
180 @Override
181 public ${msg.interface.name} readFrom(ChannelBuffer bb) throws OFParseError {
Yotam Harchola86e4252013-09-06 15:36:28 -0700182//:: for prop in msg.members:
183//:: if not prop.is_virtual and (prop.is_length_value or prop.is_field_length_value):
Andreas Wundsam001b1822013-08-02 22:25:55 -0700184 int start = bb.readerIndex();
Yotam Harchola86e4252013-09-06 15:36:28 -0700185//:: break
186//:: #endif
187//:: #endfor
Andreas Wundsam27303462013-07-16 12:52:35 -0700188//:: fields_with_length_member = {}
189//:: for prop in msg.members:
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700190//:: if prop.is_virtual:
191//:: continue
192//:: elif prop.is_data:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700193 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True,
Andreas Wundsam27303462013-07-16 12:52:35 -0700194 length=fields_with_length_member[prop.c_name] if prop.c_name in fields_with_length_member else None)};
195//:: elif prop.is_pad:
196 // pad: ${prop.length} bytes
197 bb.skipBytes(${prop.length});
Andreas Wundsam27303462013-07-16 12:52:35 -0700198//:: elif prop.is_length_value:
Andreas Wundsam83d877a2013-09-30 14:26:44 -0700199 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True)};
200 //:: if prop.is_fixed_value:
201 if(${prop.name} != ${prop.value})
202 throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name});
203 //:: else:
Andreas Wundsam27303462013-07-16 12:52:35 -0700204 if(${prop.name} < MINIMUM_LENGTH)
205 throw new OFParseError("Wrong ${prop.name}: Expected to be >= " + MINIMUM_LENGTH + ", was: " + ${prop.name});
Andreas Wundsam83d877a2013-09-30 14:26:44 -0700206 //:: #endif
Yotam Harchol0178c202013-09-05 16:25:50 -0700207 if(bb.readableBytes() + (bb.readerIndex() - start) < ${prop.name}) {
208 // Buffer does not have all data yet
209 bb.readerIndex(start);
210 return null;
211 }
Andreas Wundsam83d877a2013-09-30 14:26:44 -0700212//:: elif prop.is_fixed_value:
213 // fixed value property ${prop.name} == ${prop.value}
214 ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
215 if(${prop.name} != ${prop.priv_value})
216 throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name});
217//:: elif prop.is_field_length_value:
218//:: fields_with_length_member[prop.member.field_name] = prop.name
219 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True)};
220//:: else:
221 // fixme: todo ${prop.name}
Yotam Harchol0178c202013-09-05 16:25:50 -0700222//:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700223//:: #endfor
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700224 //:: if msg.align:
225 // align message to ${msg.align} bytes
226 bb.skipBytes(((length + ${msg.align-1})/${msg.align} * ${msg.align} ) - length );
227 //:: #endif
228
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700229 //:: if msg.data_members:
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700230 //:: if os.path.exists("%s/custom/%s.Reader_normalize_stanza.java" % (template_dir, msg.name)):
231 //:: include("custom/%s.Reader_normalize_stanza.java" % msg.name, msg=msg, has_parent=False)
232 //:: #endif
233 return new ${impl_class}(
Andreas Wundsam27303462013-07-16 12:52:35 -0700234 ${",\n ".join(
235 [ prop.name for prop in msg.data_members])}
236 );
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700237 //:: else:
238 return INSTANCE;
239 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700240 }
241 }
242
Yotam Harchol5c9d6f42013-08-01 11:09:20 -0700243 public void writeTo(ChannelBuffer bb) {
244 WRITER.write(bb, this);
Andreas Wundsam27303462013-07-16 12:52:35 -0700245 }
246
247 final static Writer WRITER = new Writer();
248 static class Writer implements OFMessageWriter<${impl_class}> {
249 @Override
Andreas Wundsama94273b2013-08-01 22:11:33 -0700250 public void write(ChannelBuffer bb, ${impl_class} message) {
Yotam Harchola86e4252013-09-06 15:36:28 -0700251//:: if not msg.is_fixed_length:
Andreas Wundsam482f6d92013-07-24 16:10:21 -0700252 int startIndex = bb.writerIndex();
Yotam Harchola86e4252013-09-06 15:36:28 -0700253//:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700254//:: fields_with_length_member = {}
255//:: for prop in msg.members:
256//:: if prop.c_name in fields_with_length_member:
257 int ${prop.name}StartIndex = bb.writerIndex();
258//:: #endif
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700259//:: if prop.is_virtual:
260//:: continue
261//:: elif prop.is_data:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700262 ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700263//:: elif prop.is_pad:
264 // pad: ${prop.length} bytes
265 bb.writeZero(${prop.length});
266//:: elif prop.is_fixed_value:
267 // fixed value property ${prop.name} = ${prop.value}
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700268 ${prop.java_type.write_op(version, prop.priv_value, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700269//:: elif prop.is_length_value:
270 // ${prop.name} is length of variable message, will be updated at the end
Andreas Wundsam001b1822013-08-02 22:25:55 -0700271//:: if not msg.is_fixed_length:
272 int lengthIndex = bb.writerIndex();
273//:: #end
Andreas Wundsam27303462013-07-16 12:52:35 -0700274 ${prop.java_type.write_op(version, 0)};
Andreas Wundsam001b1822013-08-02 22:25:55 -0700275
Andreas Wundsam27303462013-07-16 12:52:35 -0700276//:: elif prop.is_field_length_value:
277//:: fields_with_length_member[prop.member.field_name] = prop.name
278 // ${prop.name} is length indicator for ${prop.member.field_name}, will be
279 // udpated when ${prop.member.field_name} has been written
280 int ${prop.name}Index = bb.writerIndex();
Andreas Wundsam001b1822013-08-02 22:25:55 -0700281 ${prop.java_type.write_op(version, 0, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700282//:: else:
283 // FIXME: todo write ${prop.name}
284//:: #endif
285//:: if prop.c_name in fields_with_length_member:
286//:: length_member_name = fields_with_length_member[prop.c_name]
287 // update field length member ${length_member_name}
288 int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex;
289 bb.setShort(${length_member_name}Index, ${prop.name}Length);
290//:: #endif
291//:: #endfor
292
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700293//:: if not msg.is_fixed_length:
Andreas Wundsam27303462013-07-16 12:52:35 -0700294 // update length field
295 int length = bb.writerIndex() - startIndex;
Andreas Wundsam001b1822013-08-02 22:25:55 -0700296 bb.setShort(lengthIndex, length);
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700297 //:: if msg.align:
298 // align message to ${msg.align} bytes
299 bb.writeZero( ((length + ${msg.align-1})/${msg.align} * ${msg.align}) - length);
300 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700301//:: #end
302
303 }
304 }
305
Andreas Wundsameeefb552013-07-30 11:04:35 -0700306 @Override
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700307 public String toString() {
308 StringBuilder b = new StringBuilder("${msg.name}(");
309 //:: for i, prop in enumerate(msg.data_members):
310 //:: if i > 0:
311 b.append(", ");
312 //:: #endif
313 b.append("${prop.name}=").append(${ "Arrays.toString(%s)" % prop.name if prop.java_type.is_array else prop.name });
314 //:: #endfor
315 b.append(")");
316 return b.toString();
317 }
318
319
320 @Override
Andreas Wundsameeefb552013-07-30 11:04:35 -0700321 public boolean equals(Object obj) {
322 if (this == obj)
323 return true;
324 if (obj == null)
325 return false;
326 if (getClass() != obj.getClass())
327 return false;
Yotam Harchola86e4252013-09-06 15:36:28 -0700328 //:: if len(msg.data_members) > 0:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700329 ${msg.name} other = (${msg.name}) obj;
Yotam Harchola86e4252013-09-06 15:36:28 -0700330 //:: #endif
Andreas Wundsameeefb552013-07-30 11:04:35 -0700331
332 //:: for prop in msg.data_members:
333 //:: if prop.java_type.is_primitive:
334 if( ${prop.name} != other.${prop.name})
335 return false;
336 //:: elif prop.java_type.is_array:
337 if (!Arrays.equals(${prop.name}, other.${prop.name}))
338 return false;
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700339 //:: else:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700340 if (${prop.name} == null) {
341 if (other.${prop.name} != null)
342 return false;
343 } else if (!${prop.name}.equals(other.${prop.name}))
344 return false;
345 //:: #endif
346 //:: #endfor
347 return true;
348 }
349
350 @Override
351 public int hashCode() {
Yotam Harchola86e4252013-09-06 15:36:28 -0700352 //:: if len(msg.data_members) > 0:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700353 final int prime = 31;
Yotam Harchola86e4252013-09-06 15:36:28 -0700354 //:: #endif
Andreas Wundsameeefb552013-07-30 11:04:35 -0700355 int result = 1;
356
357 //:: for prop in msg.data_members:
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700358 //:: if prop.java_type.pub_type == 'long':
359 result = prime * (int) (${prop.name} ^ (${prop.name} >>> 32));
360 //:: elif prop.java_type.is_primitive:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700361 result = prime * result + ${prop.name};
362 //:: elif prop.java_type.is_array:
363 result = prime * result + Arrays.hashCode(${prop.name});
364 //:: else:
365 result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode());
366 //:: #endif
367 //:: #endfor
368 return result;
369 }
370
Andreas Wundsam27303462013-07-16 12:52:35 -0700371
372}