blob: 4ea6ac9cdab26684c476d876aa3b3aa1ac0af838 [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}
42 private final static byte WIRE_VERSION = ${version.int_version};
43//:: if msg.is_fixed_length:
44 private final static int LENGTH = ${msg.length};
45//:: else:
46 private final static int MINIMUM_LENGTH = ${msg.min_length};
47//:: #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
174//:: #endfor
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700175 //:: if msg.align:
176 // align message to ${msg.align} bytes
177 bb.skipBytes(((length + ${msg.align-1})/${msg.align} * ${msg.align} ) - length );
178 //:: #endif
179
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700180 //:: if msg.data_members:
Andreas Wundsam27303462013-07-16 12:52:35 -0700181 return new ${impl_class}(
182 ${",\n ".join(
183 [ prop.name for prop in msg.data_members])}
184 );
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700185 //:: else:
186 return INSTANCE;
187 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700188 }
189 }
190
Yotam Harchol5c9d6f42013-08-01 11:09:20 -0700191 public void writeTo(ChannelBuffer bb) {
192 WRITER.write(bb, this);
Andreas Wundsam27303462013-07-16 12:52:35 -0700193 }
194
195 final static Writer WRITER = new Writer();
196 static class Writer implements OFMessageWriter<${impl_class}> {
197 @Override
Andreas Wundsama94273b2013-08-01 22:11:33 -0700198 public void write(ChannelBuffer bb, ${impl_class} message) {
Andreas Wundsam482f6d92013-07-24 16:10:21 -0700199 int startIndex = bb.writerIndex();
Andreas Wundsam27303462013-07-16 12:52:35 -0700200//:: fields_with_length_member = {}
201//:: for prop in msg.members:
202//:: if prop.c_name in fields_with_length_member:
203 int ${prop.name}StartIndex = bb.writerIndex();
204//:: #endif
Andreas Wundsam99e931d2013-08-22 07:53:53 -0700205//:: if prop.is_virtual:
206//:: continue
207//:: elif prop.is_data:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700208 ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700209//:: elif prop.is_pad:
210 // pad: ${prop.length} bytes
211 bb.writeZero(${prop.length});
212//:: elif prop.is_fixed_value:
213 // fixed value property ${prop.name} = ${prop.value}
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700214 ${prop.java_type.write_op(version, prop.priv_value, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700215//:: elif prop.is_length_value:
216 // ${prop.name} is length of variable message, will be updated at the end
Andreas Wundsam001b1822013-08-02 22:25:55 -0700217//:: if not msg.is_fixed_length:
218 int lengthIndex = bb.writerIndex();
219//:: #end
Andreas Wundsam27303462013-07-16 12:52:35 -0700220 ${prop.java_type.write_op(version, 0)};
Andreas Wundsam001b1822013-08-02 22:25:55 -0700221
Andreas Wundsam27303462013-07-16 12:52:35 -0700222//:: elif prop.is_field_length_value:
223//:: fields_with_length_member[prop.member.field_name] = prop.name
224 // ${prop.name} is length indicator for ${prop.member.field_name}, will be
225 // udpated when ${prop.member.field_name} has been written
226 int ${prop.name}Index = bb.writerIndex();
Andreas Wundsam001b1822013-08-02 22:25:55 -0700227 ${prop.java_type.write_op(version, 0, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700228//:: else:
229 // FIXME: todo write ${prop.name}
230//:: #endif
231//:: if prop.c_name in fields_with_length_member:
232//:: length_member_name = fields_with_length_member[prop.c_name]
233 // update field length member ${length_member_name}
234 int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex;
235 bb.setShort(${length_member_name}Index, ${prop.name}Length);
236//:: #endif
237//:: #endfor
238
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700239//:: if not msg.is_fixed_length:
Andreas Wundsam27303462013-07-16 12:52:35 -0700240 // update length field
241 int length = bb.writerIndex() - startIndex;
Andreas Wundsam001b1822013-08-02 22:25:55 -0700242 bb.setShort(lengthIndex, length);
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700243 //:: if msg.align:
244 // align message to ${msg.align} bytes
245 bb.writeZero( ((length + ${msg.align-1})/${msg.align} * ${msg.align}) - length);
246 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700247//:: #end
248
249 }
250 }
251
Andreas Wundsameeefb552013-07-30 11:04:35 -0700252 @Override
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700253 public String toString() {
254 StringBuilder b = new StringBuilder("${msg.name}(");
255 //:: for i, prop in enumerate(msg.data_members):
256 //:: if i > 0:
257 b.append(", ");
258 //:: #endif
259 b.append("${prop.name}=").append(${ "Arrays.toString(%s)" % prop.name if prop.java_type.is_array else prop.name });
260 //:: #endfor
261 b.append(")");
262 return b.toString();
263 }
264
265
266 @Override
Andreas Wundsameeefb552013-07-30 11:04:35 -0700267 public boolean equals(Object obj) {
268 if (this == obj)
269 return true;
270 if (obj == null)
271 return false;
272 if (getClass() != obj.getClass())
273 return false;
274 ${msg.name} other = (${msg.name}) obj;
275
276 //:: for prop in msg.data_members:
277 //:: if prop.java_type.is_primitive:
278 if( ${prop.name} != other.${prop.name})
279 return false;
280 //:: elif prop.java_type.is_array:
281 if (!Arrays.equals(${prop.name}, other.${prop.name}))
282 return false;
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700283 //:: else:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700284 if (${prop.name} == null) {
285 if (other.${prop.name} != null)
286 return false;
287 } else if (!${prop.name}.equals(other.${prop.name}))
288 return false;
289 //:: #endif
290 //:: #endfor
291 return true;
292 }
293
294 @Override
295 public int hashCode() {
296 final int prime = 31;
297 int result = 1;
298
299 //:: for prop in msg.data_members:
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700300 //:: if prop.java_type.pub_type == 'long':
301 result = prime * (int) (${prop.name} ^ (${prop.name} >>> 32));
302 //:: elif prop.java_type.is_primitive:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700303 result = prime * result + ${prop.name};
304 //:: elif prop.java_type.is_array:
305 result = prime * result + Arrays.hashCode(${prop.name});
306 //:: else:
307 result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode());
308 //:: #endif
309 //:: #endfor
310 return result;
311 }
312
Andreas Wundsam27303462013-07-16 12:52:35 -0700313
314}