blob: aa68d048abeb9c9522b60f1b06d99359c0ceee71 [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});
198//:: elif prop.is_fixed_value:
199 // fixed value property ${prop.name} == ${prop.value}
Andreas Wundsam001b1822013-08-02 22:25:55 -0700200 ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
201 if(${prop.name} != ${prop.priv_value})
Andreas Wundsam27303462013-07-16 12:52:35 -0700202 throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name});
203//:: elif prop.is_length_value:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700204 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700205 if(${prop.name} < MINIMUM_LENGTH)
206 throw new OFParseError("Wrong ${prop.name}: Expected to be >= " + MINIMUM_LENGTH + ", was: " + ${prop.name});
207//:: elif prop.is_field_length_value:
208//:: fields_with_length_member[prop.member.field_name] = prop.name
Andreas Wundsam001b1822013-08-02 22:25:55 -0700209 int ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700210//:: else:
211 // fixme: todo ${prop.name}
212//:: #endif
Yotam Harchol0178c202013-09-05 16:25:50 -0700213//:: if prop.is_length_value or prop.is_field_length_value:
214 if(bb.readableBytes() + (bb.readerIndex() - start) < ${prop.name}) {
215 // Buffer does not have all data yet
216 bb.readerIndex(start);
217 return null;
218 }
219//:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700220//:: #endfor
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700221 //:: if msg.align:
222 // align message to ${msg.align} bytes
223 bb.skipBytes(((length + ${msg.align-1})/${msg.align} * ${msg.align} ) - length );
224 //:: #endif
225
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700226 //:: if msg.data_members:
Andreas Wundsamf89f7822013-09-23 14:49:24 -0700227 //:: if os.path.exists("%s/custom/%s.Reader_normalize_stanza.java" % (template_dir, msg.name)):
228 //:: include("custom/%s.Reader_normalize_stanza.java" % msg.name, msg=msg, has_parent=False)
229 //:: #endif
230 return new ${impl_class}(
Andreas Wundsam27303462013-07-16 12:52:35 -0700231 ${",\n ".join(
232 [ prop.name for prop in msg.data_members])}
233 );
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700234 //:: else:
235 return INSTANCE;
236 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700237 }
238 }
239
Yotam Harchol5c9d6f42013-08-01 11:09:20 -0700240 public void writeTo(ChannelBuffer bb) {
241 WRITER.write(bb, this);
Andreas Wundsam27303462013-07-16 12:52:35 -0700242 }
243
244 final static Writer WRITER = new Writer();
245 static class Writer implements OFMessageWriter<${impl_class}> {
246 @Override
Andreas Wundsama94273b2013-08-01 22:11:33 -0700247 public void write(ChannelBuffer bb, ${impl_class} message) {
Yotam Harchola86e4252013-09-06 15:36:28 -0700248//:: if not msg.is_fixed_length:
Andreas Wundsam482f6d92013-07-24 16:10:21 -0700249 int startIndex = bb.writerIndex();
Yotam Harchola86e4252013-09-06 15:36:28 -0700250//:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700251//:: fields_with_length_member = {}
252//:: for prop in msg.members:
253//:: if prop.c_name in fields_with_length_member:
254 int ${prop.name}StartIndex = bb.writerIndex();
255//:: #endif
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700256//:: if prop.is_virtual:
257//:: continue
258//:: elif prop.is_data:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700259 ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700260//:: elif prop.is_pad:
261 // pad: ${prop.length} bytes
262 bb.writeZero(${prop.length});
263//:: elif prop.is_fixed_value:
264 // fixed value property ${prop.name} = ${prop.value}
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700265 ${prop.java_type.write_op(version, prop.priv_value, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700266//:: elif prop.is_length_value:
267 // ${prop.name} is length of variable message, will be updated at the end
Andreas Wundsam001b1822013-08-02 22:25:55 -0700268//:: if not msg.is_fixed_length:
269 int lengthIndex = bb.writerIndex();
270//:: #end
Andreas Wundsam27303462013-07-16 12:52:35 -0700271 ${prop.java_type.write_op(version, 0)};
Andreas Wundsam001b1822013-08-02 22:25:55 -0700272
Andreas Wundsam27303462013-07-16 12:52:35 -0700273//:: elif prop.is_field_length_value:
274//:: fields_with_length_member[prop.member.field_name] = prop.name
275 // ${prop.name} is length indicator for ${prop.member.field_name}, will be
276 // udpated when ${prop.member.field_name} has been written
277 int ${prop.name}Index = bb.writerIndex();
Andreas Wundsam001b1822013-08-02 22:25:55 -0700278 ${prop.java_type.write_op(version, 0, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700279//:: else:
280 // FIXME: todo write ${prop.name}
281//:: #endif
282//:: if prop.c_name in fields_with_length_member:
283//:: length_member_name = fields_with_length_member[prop.c_name]
284 // update field length member ${length_member_name}
285 int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex;
286 bb.setShort(${length_member_name}Index, ${prop.name}Length);
287//:: #endif
288//:: #endfor
289
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700290//:: if not msg.is_fixed_length:
Andreas Wundsam27303462013-07-16 12:52:35 -0700291 // update length field
292 int length = bb.writerIndex() - startIndex;
Andreas Wundsam001b1822013-08-02 22:25:55 -0700293 bb.setShort(lengthIndex, length);
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700294 //:: if msg.align:
295 // align message to ${msg.align} bytes
296 bb.writeZero( ((length + ${msg.align-1})/${msg.align} * ${msg.align}) - length);
297 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700298//:: #end
299
300 }
301 }
302
Andreas Wundsameeefb552013-07-30 11:04:35 -0700303 @Override
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700304 public String toString() {
305 StringBuilder b = new StringBuilder("${msg.name}(");
306 //:: for i, prop in enumerate(msg.data_members):
307 //:: if i > 0:
308 b.append(", ");
309 //:: #endif
310 b.append("${prop.name}=").append(${ "Arrays.toString(%s)" % prop.name if prop.java_type.is_array else prop.name });
311 //:: #endfor
312 b.append(")");
313 return b.toString();
314 }
315
316
317 @Override
Andreas Wundsameeefb552013-07-30 11:04:35 -0700318 public boolean equals(Object obj) {
319 if (this == obj)
320 return true;
321 if (obj == null)
322 return false;
323 if (getClass() != obj.getClass())
324 return false;
Yotam Harchola86e4252013-09-06 15:36:28 -0700325 //:: if len(msg.data_members) > 0:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700326 ${msg.name} other = (${msg.name}) obj;
Yotam Harchola86e4252013-09-06 15:36:28 -0700327 //:: #endif
Andreas Wundsameeefb552013-07-30 11:04:35 -0700328
329 //:: for prop in msg.data_members:
330 //:: if prop.java_type.is_primitive:
331 if( ${prop.name} != other.${prop.name})
332 return false;
333 //:: elif prop.java_type.is_array:
334 if (!Arrays.equals(${prop.name}, other.${prop.name}))
335 return false;
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700336 //:: else:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700337 if (${prop.name} == null) {
338 if (other.${prop.name} != null)
339 return false;
340 } else if (!${prop.name}.equals(other.${prop.name}))
341 return false;
342 //:: #endif
343 //:: #endfor
344 return true;
345 }
346
347 @Override
348 public int hashCode() {
Yotam Harchola86e4252013-09-06 15:36:28 -0700349 //:: if len(msg.data_members) > 0:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700350 final int prime = 31;
Yotam Harchola86e4252013-09-06 15:36:28 -0700351 //:: #endif
Andreas Wundsameeefb552013-07-30 11:04:35 -0700352 int result = 1;
353
354 //:: for prop in msg.data_members:
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700355 //:: if prop.java_type.pub_type == 'long':
356 result = prime * (int) (${prop.name} ^ (${prop.name} >>> 32));
357 //:: elif prop.java_type.is_primitive:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700358 result = prime * result + ${prop.name};
359 //:: elif prop.java_type.is_array:
360 result = prime * result + Arrays.hashCode(${prop.name});
361 //:: else:
362 result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode());
363 //:: #endif
364 //:: #endfor
365 return result;
366 }
367
Andreas Wundsam27303462013-07-16 12:52:35 -0700368
369}