Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1 | //:: # 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 Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 29 | //:: import os |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 30 | //:: import itertools |
| 31 | //:: import of_g |
| 32 | //:: include('_copyright.java') |
| 33 | |
| 34 | //:: include('_autogen.java') |
| 35 | |
| 36 | package ${msg.package}; |
| 37 | |
| 38 | //:: include("_imports.java", msg=msg) |
| 39 | |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 40 | class ${impl_class} implements ${msg.interface.inherited_declaration()} { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 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 | |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 58 | //:: if msg.data_members: |
| 59 | // package private constructor - used by readers, builders, and factory |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 60 | ${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 Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 66 | //:: 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 Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 72 | |
| 73 | // Accessors for OF message fields |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 74 | //:: include("_field_accessors.java", msg=msg, generate_setters=False, builder=False) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 75 | |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 76 | //:: if os.path.exists("%s/custom/%s.java" % (template_dir, msg.name)): |
| 77 | //:: include("custom/%s.java" % msg.name, msg=msg) |
| 78 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 79 | |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 80 | //:: if msg.data_members: |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 81 | public ${msg.interface.name}.Builder createBuilder() { |
| 82 | return new BuilderWithParent(this); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 85 | static class BuilderWithParent implements ${msg.interface.name}.Builder { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 86 | 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 Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 94 | BuilderWithParent(${impl_class} parentMessage) { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 95 | this.parentMessage = parentMessage; |
| 96 | } |
| 97 | |
| 98 | //:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True) |
| 99 | |
| 100 | @Override |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 101 | public ${msg.interface.name} build() { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 102 | 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 Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 108 | //:: if os.path.exists("%s/custom/%s.Builder.java" % (template_dir, msg.name)): |
Yotam Harchol | 98af775 | 2013-08-22 14:59:38 -0700 | [diff] [blame] | 109 | //:: include("custom/%s.Builder.java" % msg.name, msg=msg, has_parent=True) |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 110 | //:: #endif |
| 111 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 114 | static class Builder implements ${msg.interface.name}.Builder { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 115 | // 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 Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 124 | public ${msg.interface.name} build() { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 125 | 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 Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 131 | //:: if os.path.exists("%s/custom/%s.Builder.java" % (template_dir, msg.name)): |
Yotam Harchol | 98af775 | 2013-08-22 14:59:38 -0700 | [diff] [blame] | 132 | //:: include("custom/%s.Builder.java" % msg.name, msg=msg, has_parent=False) |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 133 | //:: #endif |
| 134 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 135 | } |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 136 | //:: 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 Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 143 | |
| 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 Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 148 | int start = bb.readerIndex(); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 149 | //:: fields_with_length_member = {} |
| 150 | //:: for prop in msg.members: |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 151 | //:: if prop.is_virtual: |
| 152 | //:: continue |
| 153 | //:: elif prop.is_data: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 154 | ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True, |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 155 | 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 Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 161 | ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)}; |
| 162 | if(${prop.name} != ${prop.priv_value}) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 163 | throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name}); |
| 164 | //:: elif prop.is_length_value: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 165 | ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 166 | 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 Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 170 | int ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 171 | //:: else: |
| 172 | // fixme: todo ${prop.name} |
| 173 | //:: #endif |
| 174 | //:: #endfor |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 175 | //:: 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 Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 180 | //:: if msg.data_members: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 181 | return new ${impl_class}( |
| 182 | ${",\n ".join( |
| 183 | [ prop.name for prop in msg.data_members])} |
| 184 | ); |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 185 | //:: else: |
| 186 | return INSTANCE; |
| 187 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Yotam Harchol | 5c9d6f4 | 2013-08-01 11:09:20 -0700 | [diff] [blame] | 191 | public void writeTo(ChannelBuffer bb) { |
| 192 | WRITER.write(bb, this); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | final static Writer WRITER = new Writer(); |
| 196 | static class Writer implements OFMessageWriter<${impl_class}> { |
| 197 | @Override |
Andreas Wundsam | a94273b | 2013-08-01 22:11:33 -0700 | [diff] [blame] | 198 | public void write(ChannelBuffer bb, ${impl_class} message) { |
Andreas Wundsam | 482f6d9 | 2013-07-24 16:10:21 -0700 | [diff] [blame] | 199 | int startIndex = bb.writerIndex(); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 200 | //:: 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 Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 205 | //:: if prop.is_virtual: |
| 206 | //:: continue |
| 207 | //:: elif prop.is_data: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 208 | ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 209 | //:: 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 Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 214 | ${prop.java_type.write_op(version, prop.priv_value, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 215 | //:: elif prop.is_length_value: |
| 216 | // ${prop.name} is length of variable message, will be updated at the end |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 217 | //:: if not msg.is_fixed_length: |
| 218 | int lengthIndex = bb.writerIndex(); |
| 219 | //:: #end |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 220 | ${prop.java_type.write_op(version, 0)}; |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 221 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 222 | //:: 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 Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 227 | ${prop.java_type.write_op(version, 0, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 228 | //:: 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 Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 239 | //:: if not msg.is_fixed_length: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 240 | // update length field |
| 241 | int length = bb.writerIndex() - startIndex; |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 242 | bb.setShort(lengthIndex, length); |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 243 | //:: 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 Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 247 | //:: #end |
| 248 | |
| 249 | } |
| 250 | } |
| 251 | |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 252 | @Override |
Andreas Wundsam | d7d5bb3 | 2013-07-30 12:26:31 -0700 | [diff] [blame] | 253 | 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 Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 267 | 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 Wundsam | d7d5bb3 | 2013-07-30 12:26:31 -0700 | [diff] [blame] | 283 | //:: else: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 284 | 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 Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 300 | //:: if prop.java_type.pub_type == 'long': |
| 301 | result = prime * (int) (${prop.name} ^ (${prop.name} >>> 32)); |
| 302 | //:: elif prop.java_type.is_primitive: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 303 | 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 Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 313 | |
| 314 | } |