blob: 43d989366bf9200cb5934bac1922e045686305a1 [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
40class ${impl_class} implements ${msg.interface.name} {
41 // 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
58 ${impl_class}(${
59 ", ".join("%s %s" %(prop.java_type.public_type, prop.name) for prop in msg.data_members) }) {
60//:: for prop in msg.data_members:
61 this.${prop.name} = ${prop.name};
62//:: #endfor
63 }
64
65 // Accessors for OF message fields
66//:: include("_field_accessors.java", msg=msg, generate_setters=False, builder=False)
67
Andreas Wundsambc679f72013-08-01 22:13:09 -070068 //:: if os.path.exists("%s/custom/%s.java" % (template_dir, msg.name)):
69 //:: include("custom/%s.java" % msg.name, msg=msg)
70 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -070071
Andreas Wundsam5204de22013-07-30 11:34:45 -070072 public ${msg.interface.name}.Builder createBuilder() {
73 return new BuilderWithParent(this);
Andreas Wundsam27303462013-07-16 12:52:35 -070074 }
75
Andreas Wundsam5204de22013-07-30 11:34:45 -070076 static class BuilderWithParent implements ${msg.interface.name}.Builder {
Andreas Wundsam27303462013-07-16 12:52:35 -070077 final ${impl_class} parentMessage;
78
79 // OF message fields
80//:: for prop in msg.data_members:
81 private boolean ${prop.name}Set;
82 private ${prop.java_type.public_type} ${prop.name};
83//:: #endfor
84
Andreas Wundsam5204de22013-07-30 11:34:45 -070085 BuilderWithParent(${impl_class} parentMessage) {
Andreas Wundsam27303462013-07-16 12:52:35 -070086 this.parentMessage = parentMessage;
87 }
88
89//:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True)
90
91 @Override
92 public ${msg.interface.name} getMessage() {
93 return new ${impl_class}(
94 ${",\n ".join(
95 [ "this.{0}Set ? this.{0} : parentMessage.{0}".format(prop.name)
96 for prop in msg.data_members])}
97 );
98 }
Andreas Wundsambc679f72013-08-01 22:13:09 -070099 //:: if os.path.exists("%s/custom/%s.Builder.java" % (template_dir, msg.name)):
100 //:: include("custom/%s.Builder.java" % msg.name, msg=msg)
101 //:: #endif
102
Andreas Wundsam27303462013-07-16 12:52:35 -0700103 }
104
Andreas Wundsam5204de22013-07-30 11:34:45 -0700105 static class Builder implements ${msg.interface.name}.Builder {
Andreas Wundsam27303462013-07-16 12:52:35 -0700106 // OF message fields
107//:: for prop in msg.data_members:
108 private boolean ${prop.name}Set;
109 private ${prop.java_type.public_type} ${prop.name};
110//:: #endfor
111
112//:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True)
113//
114 @Override
115 public ${msg.interface.name} getMessage() {
116 return new ${impl_class}(
117 ${",\n ".join(
118 [ "this.{0}Set ? this.{0} : {1}.{2}".format(prop.name, impl_class, prop.default_name)
119 for prop in msg.data_members])}
120 );
121 }
Andreas Wundsambc679f72013-08-01 22:13:09 -0700122 //:: if os.path.exists("%s/custom/%s.Builder.java" % (template_dir, msg.name)):
123 //:: include("custom/%s.Builder.java" % msg.name, msg=msg)
124 //:: #endif
125
Andreas Wundsam27303462013-07-16 12:52:35 -0700126 }
127
128 final static Reader READER = new Reader();
129 static class Reader implements OFMessageReader<${msg.interface.name}> {
130 @Override
131 public ${msg.interface.name} readFrom(ChannelBuffer bb) throws OFParseError {
Andreas Wundsam001b1822013-08-02 22:25:55 -0700132 int start = bb.readerIndex();
Andreas Wundsam27303462013-07-16 12:52:35 -0700133//:: fields_with_length_member = {}
134//:: for prop in msg.members:
135//:: if prop.is_data:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700136 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True,
Andreas Wundsam27303462013-07-16 12:52:35 -0700137 length=fields_with_length_member[prop.c_name] if prop.c_name in fields_with_length_member else None)};
138//:: elif prop.is_pad:
139 // pad: ${prop.length} bytes
140 bb.skipBytes(${prop.length});
141//:: elif prop.is_fixed_value:
142 // fixed value property ${prop.name} == ${prop.value}
Andreas Wundsam001b1822013-08-02 22:25:55 -0700143 ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
144 if(${prop.name} != ${prop.priv_value})
Andreas Wundsam27303462013-07-16 12:52:35 -0700145 throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name});
146//:: elif prop.is_length_value:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700147 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700148 if(${prop.name} < MINIMUM_LENGTH)
149 throw new OFParseError("Wrong ${prop.name}: Expected to be >= " + MINIMUM_LENGTH + ", was: " + ${prop.name});
150//:: elif prop.is_field_length_value:
151//:: fields_with_length_member[prop.member.field_name] = prop.name
Andreas Wundsam001b1822013-08-02 22:25:55 -0700152 int ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700153//:: else:
154 // fixme: todo ${prop.name}
155//:: #endif
156//:: #endfor
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700157 //:: if msg.align:
158 // align message to ${msg.align} bytes
159 bb.skipBytes(((length + ${msg.align-1})/${msg.align} * ${msg.align} ) - length );
160 //:: #endif
161
Andreas Wundsam27303462013-07-16 12:52:35 -0700162 return new ${impl_class}(
163 ${",\n ".join(
164 [ prop.name for prop in msg.data_members])}
165 );
166 }
167 }
168
Yotam Harchol5c9d6f42013-08-01 11:09:20 -0700169 public void writeTo(ChannelBuffer bb) {
170 WRITER.write(bb, this);
Andreas Wundsam27303462013-07-16 12:52:35 -0700171 }
172
173 final static Writer WRITER = new Writer();
174 static class Writer implements OFMessageWriter<${impl_class}> {
175 @Override
Andreas Wundsama94273b2013-08-01 22:11:33 -0700176 public void write(ChannelBuffer bb, ${impl_class} message) {
Andreas Wundsam482f6d92013-07-24 16:10:21 -0700177 int startIndex = bb.writerIndex();
Andreas Wundsam27303462013-07-16 12:52:35 -0700178//:: fields_with_length_member = {}
179//:: for prop in msg.members:
180//:: if prop.c_name in fields_with_length_member:
181 int ${prop.name}StartIndex = bb.writerIndex();
182//:: #endif
183//:: if prop.is_data:
Andreas Wundsam001b1822013-08-02 22:25:55 -0700184 ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700185//:: elif prop.is_pad:
186 // pad: ${prop.length} bytes
187 bb.writeZero(${prop.length});
188//:: elif prop.is_fixed_value:
189 // fixed value property ${prop.name} = ${prop.value}
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700190 ${prop.java_type.write_op(version, prop.priv_value, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700191//:: elif prop.is_length_value:
192 // ${prop.name} is length of variable message, will be updated at the end
Andreas Wundsam001b1822013-08-02 22:25:55 -0700193//:: if not msg.is_fixed_length:
194 int lengthIndex = bb.writerIndex();
195//:: #end
Andreas Wundsam27303462013-07-16 12:52:35 -0700196 ${prop.java_type.write_op(version, 0)};
Andreas Wundsam001b1822013-08-02 22:25:55 -0700197
Andreas Wundsam27303462013-07-16 12:52:35 -0700198//:: elif prop.is_field_length_value:
199//:: fields_with_length_member[prop.member.field_name] = prop.name
200 // ${prop.name} is length indicator for ${prop.member.field_name}, will be
201 // udpated when ${prop.member.field_name} has been written
202 int ${prop.name}Index = bb.writerIndex();
Andreas Wundsam001b1822013-08-02 22:25:55 -0700203 ${prop.java_type.write_op(version, 0, pub_type=False)};
Andreas Wundsam27303462013-07-16 12:52:35 -0700204//:: else:
205 // FIXME: todo write ${prop.name}
206//:: #endif
207//:: if prop.c_name in fields_with_length_member:
208//:: length_member_name = fields_with_length_member[prop.c_name]
209 // update field length member ${length_member_name}
210 int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex;
211 bb.setShort(${length_member_name}Index, ${prop.name}Length);
212//:: #endif
213//:: #endfor
214
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700215//:: if not msg.is_fixed_length:
Andreas Wundsam27303462013-07-16 12:52:35 -0700216 // update length field
217 int length = bb.writerIndex() - startIndex;
Andreas Wundsam001b1822013-08-02 22:25:55 -0700218 bb.setShort(lengthIndex, length);
Andreas Wundsam758c9cc2013-08-01 22:16:06 -0700219 //:: if msg.align:
220 // align message to ${msg.align} bytes
221 bb.writeZero( ((length + ${msg.align-1})/${msg.align} * ${msg.align}) - length);
222 //:: #endif
Andreas Wundsam27303462013-07-16 12:52:35 -0700223//:: #end
224
225 }
226 }
227
Andreas Wundsameeefb552013-07-30 11:04:35 -0700228 @Override
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700229 public String toString() {
230 StringBuilder b = new StringBuilder("${msg.name}(");
231 //:: for i, prop in enumerate(msg.data_members):
232 //:: if i > 0:
233 b.append(", ");
234 //:: #endif
235 b.append("${prop.name}=").append(${ "Arrays.toString(%s)" % prop.name if prop.java_type.is_array else prop.name });
236 //:: #endfor
237 b.append(")");
238 return b.toString();
239 }
240
241
242 @Override
Andreas Wundsameeefb552013-07-30 11:04:35 -0700243 public boolean equals(Object obj) {
244 if (this == obj)
245 return true;
246 if (obj == null)
247 return false;
248 if (getClass() != obj.getClass())
249 return false;
250 ${msg.name} other = (${msg.name}) obj;
251
252 //:: for prop in msg.data_members:
253 //:: if prop.java_type.is_primitive:
254 if( ${prop.name} != other.${prop.name})
255 return false;
256 //:: elif prop.java_type.is_array:
257 if (!Arrays.equals(${prop.name}, other.${prop.name}))
258 return false;
Andreas Wundsamd7d5bb32013-07-30 12:26:31 -0700259 //:: else:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700260 if (${prop.name} == null) {
261 if (other.${prop.name} != null)
262 return false;
263 } else if (!${prop.name}.equals(other.${prop.name}))
264 return false;
265 //:: #endif
266 //:: #endfor
267 return true;
268 }
269
270 @Override
271 public int hashCode() {
272 final int prime = 31;
273 int result = 1;
274
275 //:: for prop in msg.data_members:
Andreas Wundsam2bf357c2013-08-03 22:50:40 -0700276 //:: if prop.java_type.pub_type == 'long':
277 result = prime * (int) (${prop.name} ^ (${prop.name} >>> 32));
278 //:: elif prop.java_type.is_primitive:
Andreas Wundsameeefb552013-07-30 11:04:35 -0700279 result = prime * result + ${prop.name};
280 //:: elif prop.java_type.is_array:
281 result = prime * result + Arrays.hashCode(${prop.name});
282 //:: else:
283 result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode());
284 //:: #endif
285 //:: #endfor
286 return result;
287 }
288
Andreas Wundsam27303462013-07-16 12:52:35 -0700289
290}