blob: 2dc88fb862b28fe1a795383bdb7395f9d22d4208 [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
Andreas Wundsam99e931d2013-08-22 07:53:53 -070074 //:: include("_field_accessors.java", msg=msg, generate_setters=False, builder=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
98//:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True)
99
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
121//:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True)
122//
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 {
Andreas Wundsam001b1822013-08-02 22:25:55 -0700148 int start = bb.readerIndex();
Andreas Wundsam27303462013-07-16 12:52:35 -0700149//:: fields_with_length_member = {}
150//:: for prop in msg.members:
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700151//:: if prop.is_virtual:
152//:: continue
153//:: elif prop.is_data:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700154 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True,
Andreas Wundsam27303462013-07-16 12:52:35 -0700155 length=fields_with_length_member[prop.c_name] if prop.c_name in fields_with_length_member else None)};
156//:: elif prop.is_pad:
157 // pad: ${prop.length} bytes
158 bb.skipBytes(${prop.length});
159//:: elif prop.is_fixed_value:
160 // fixed value property ${prop.name} == ${prop.value}
Andreas Wundsam001b1822013-08-02 22:25:55 -0700161 ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
162 if(${prop.name} != ${prop.priv_value})
Andreas Wundsam27303462013-07-16 12:52:35 -0700163 throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name});
164//:: elif prop.is_length_value:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700165 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700166 if(${prop.name} < MINIMUM_LENGTH)
167 throw new OFParseError("Wrong ${prop.name}: Expected to be >= " + MINIMUM_LENGTH + ", was: " + ${prop.name});
168//:: elif prop.is_field_length_value:
169//:: fields_with_length_member[prop.member.field_name] = prop.name
Andreas Wundsam001b1822013-08-02 22:25:55 -0700170 int ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700171//:: else:
172 // fixme: todo ${prop.name}
173//:: #endif
Yotam Harchol0178c202013-09-05 16:25:50 -0700174//:: if prop.is_length_value or prop.is_field_length_value:
175 if(bb.readableBytes() + (bb.readerIndex() - start) < ${prop.name}) {
176 // Buffer does not have all data yet
177 bb.readerIndex(start);
178 return null;
179 }
180//:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700181//:: #endfor
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700182 //:: if msg.align:
183 // align message to ${msg.align} bytes
184 bb.skipBytes(((length + ${msg.align-1})/${msg.align} * ${msg.align} ) - length );
185 //:: #endif
186
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700187 //:: if msg.data_members:
Andreas Wundsam27303462013-07-16 12:52:35 -0700188 return new ${impl_class}(
189 ${",\n ".join(
190 [ prop.name for prop in msg.data_members])}
191 );
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700192 //:: else:
193 return INSTANCE;
194 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700195 }
196 }
197
Yotam Harchol5c9d6f42013-08-01 11:09:20 -0700198 public void writeTo(ChannelBuffer bb) {
199 WRITER.write(bb, this);
Andreas Wundsam27303462013-07-16 12:52:35 -0700200 }
201
202 final static Writer WRITER = new Writer();
203 static class Writer implements OFMessageWriter<${impl_class}> {
204 @Override
Andreas Wundsama94273b2013-08-01 22:11:33 -0700205 public void write(ChannelBuffer bb, ${impl_class} message) {
Andreas Wundsam482f6d92013-07-24 16:10:21 -0700206 int startIndex = bb.writerIndex();
Andreas Wundsam27303462013-07-16 12:52:35 -0700207//:: fields_with_length_member = {}
208//:: for prop in msg.members:
209//:: if prop.c_name in fields_with_length_member:
210 int ${prop.name}StartIndex = bb.writerIndex();
211//:: #endif
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700212//:: if prop.is_virtual:
213//:: continue
214//:: elif prop.is_data:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700215 ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700216//:: elif prop.is_pad:
217 // pad: ${prop.length} bytes
218 bb.writeZero(${prop.length});
219//:: elif prop.is_fixed_value:
220 // fixed value property ${prop.name} = ${prop.value}
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700221 ${prop.java_type.write_op(version, prop.priv_value, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700222//:: elif prop.is_length_value:
223 // ${prop.name} is length of variable message, will be updated at the end
Andreas Wundsam001b1822013-08-02 22:25:55 -0700224//:: if not msg.is_fixed_length:
225 int lengthIndex = bb.writerIndex();
226//:: #end
Andreas Wundsam27303462013-07-16 12:52:35 -0700227 ${prop.java_type.write_op(version, 0)};
Andreas Wundsam001b1822013-08-02 22:25:55 -0700228
Andreas Wundsam27303462013-07-16 12:52:35 -0700229//:: elif prop.is_field_length_value:
230//:: fields_with_length_member[prop.member.field_name] = prop.name
231 // ${prop.name} is length indicator for ${prop.member.field_name}, will be
232 // udpated when ${prop.member.field_name} has been written
233 int ${prop.name}Index = bb.writerIndex();
Andreas Wundsam001b1822013-08-02 22:25:55 -0700234 ${prop.java_type.write_op(version, 0, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700235//:: else:
236 // FIXME: todo write ${prop.name}
237//:: #endif
238//:: if prop.c_name in fields_with_length_member:
239//:: length_member_name = fields_with_length_member[prop.c_name]
240 // update field length member ${length_member_name}
241 int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex;
242 bb.setShort(${length_member_name}Index, ${prop.name}Length);
243//:: #endif
244//:: #endfor
245
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700246//:: if not msg.is_fixed_length:
Andreas Wundsam27303462013-07-16 12:52:35 -0700247 // update length field
248 int length = bb.writerIndex() - startIndex;
Andreas Wundsam001b1822013-08-02 22:25:55 -0700249 bb.setShort(lengthIndex, length);
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700250 //:: if msg.align:
251 // align message to ${msg.align} bytes
252 bb.writeZero( ((length + ${msg.align-1})/${msg.align} * ${msg.align}) - length);
253 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700254//:: #end
255
256 }
257 }
258
Andreas Wundsameeefb552013-07-30 11:04:35 -0700259 @Override
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700260 public String toString() {
261 StringBuilder b = new StringBuilder("${msg.name}(");
262 //:: for i, prop in enumerate(msg.data_members):
263 //:: if i > 0:
264 b.append(", ");
265 //:: #endif
266 b.append("${prop.name}=").append(${ "Arrays.toString(%s)" % prop.name if prop.java_type.is_array else prop.name });
267 //:: #endfor
268 b.append(")");
269 return b.toString();
270 }
271
272
273 @Override
Andreas Wundsameeefb552013-07-30 11:04:35 -0700274 public boolean equals(Object obj) {
275 if (this == obj)
276 return true;
277 if (obj == null)
278 return false;
279 if (getClass() != obj.getClass())
280 return false;
281 ${msg.name} other = (${msg.name}) obj;
282
283 //:: for prop in msg.data_members:
284 //:: if prop.java_type.is_primitive:
285 if( ${prop.name} != other.${prop.name})
286 return false;
287 //:: elif prop.java_type.is_array:
288 if (!Arrays.equals(${prop.name}, other.${prop.name}))
289 return false;
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700290 //:: else:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700291 if (${prop.name} == null) {
292 if (other.${prop.name} != null)
293 return false;
294 } else if (!${prop.name}.equals(other.${prop.name}))
295 return false;
296 //:: #endif
297 //:: #endfor
298 return true;
299 }
300
301 @Override
302 public int hashCode() {
303 final int prime = 31;
304 int result = 1;
305
306 //:: for prop in msg.data_members:
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700307 //:: if prop.java_type.pub_type == 'long':
308 result = prime * (int) (${prop.name} ^ (${prop.name} >>> 32));
309 //:: elif prop.java_type.is_primitive:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700310 result = prime * result + ${prop.name};
311 //:: elif prop.java_type.is_array:
312 result = prime * result + Arrays.hashCode(${prop.name});
313 //:: else:
314 result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode());
315 //:: #endif
316 //:: #endfor
317 return result;
318 }
319
Andreas Wundsam27303462013-07-16 12:52:35 -0700320
321}