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 | |
| 40 | class ${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 Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 68 | //:: if os.path.exists("%s/custom/%s.java" % (template_dir, msg.name)): |
| 69 | //:: include("custom/%s.java" % msg.name, msg=msg) |
| 70 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 71 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 72 | public ${msg.interface.name}.Builder createBuilder() { |
| 73 | return new BuilderWithParent(this); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 76 | static class BuilderWithParent implements ${msg.interface.name}.Builder { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 77 | 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 Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 85 | BuilderWithParent(${impl_class} parentMessage) { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 86 | 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 Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 99 | //:: 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 Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 105 | static class Builder implements ${msg.interface.name}.Builder { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 106 | // 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 Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 122 | //:: 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 Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 126 | } |
| 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 Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 132 | int start = bb.readerIndex(); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 133 | //:: fields_with_length_member = {} |
| 134 | //:: for prop in msg.members: |
| 135 | //:: if prop.is_data: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 136 | ${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] | 137 | 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 Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 143 | ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)}; |
| 144 | if(${prop.name} != ${prop.priv_value}) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 145 | throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name}); |
| 146 | //:: elif prop.is_length_value: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 147 | ${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] | 148 | 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 Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 152 | int ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 153 | //:: else: |
| 154 | // fixme: todo ${prop.name} |
| 155 | //:: #endif |
| 156 | //:: #endfor |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 157 | //:: 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 Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 162 | return new ${impl_class}( |
| 163 | ${",\n ".join( |
| 164 | [ prop.name for prop in msg.data_members])} |
| 165 | ); |
| 166 | } |
| 167 | } |
| 168 | |
Yotam Harchol | 5c9d6f4 | 2013-08-01 11:09:20 -0700 | [diff] [blame] | 169 | public void writeTo(ChannelBuffer bb) { |
| 170 | WRITER.write(bb, this); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | final static Writer WRITER = new Writer(); |
| 174 | static class Writer implements OFMessageWriter<${impl_class}> { |
| 175 | @Override |
Andreas Wundsam | a94273b | 2013-08-01 22:11:33 -0700 | [diff] [blame] | 176 | public void write(ChannelBuffer bb, ${impl_class} message) { |
Andreas Wundsam | 482f6d9 | 2013-07-24 16:10:21 -0700 | [diff] [blame] | 177 | int startIndex = bb.writerIndex(); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 178 | //:: 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 Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 184 | ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 185 | //:: 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 Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 190 | ${prop.java_type.write_op(version, prop.value, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 191 | //:: elif prop.is_length_value: |
| 192 | // ${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] | 193 | //:: if not msg.is_fixed_length: |
| 194 | int lengthIndex = bb.writerIndex(); |
| 195 | //:: #end |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 196 | ${prop.java_type.write_op(version, 0)}; |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 197 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 198 | //:: 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 Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 203 | ${prop.java_type.write_op(version, 0, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 204 | //:: 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 Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 215 | //:: if not msg.is_fixed_length: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 216 | // update length field |
| 217 | int length = bb.writerIndex() - startIndex; |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 218 | bb.setShort(lengthIndex, length); |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 219 | //:: 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 Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 223 | //:: #end |
| 224 | |
| 225 | } |
| 226 | } |
| 227 | |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 228 | @Override |
Andreas Wundsam | d7d5bb3 | 2013-07-30 12:26:31 -0700 | [diff] [blame] | 229 | 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 Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 243 | 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 Wundsam | d7d5bb3 | 2013-07-30 12:26:31 -0700 | [diff] [blame] | 259 | //:: else: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 260 | 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: |
| 276 | //:: if prop.java_type.is_primitive: |
| 277 | result = prime * result + ${prop.name}; |
| 278 | //:: elif prop.java_type.is_array: |
| 279 | result = prime * result + Arrays.hashCode(${prop.name}); |
| 280 | //:: else: |
| 281 | result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode()); |
| 282 | //:: #endif |
| 283 | //:: #endfor |
| 284 | return result; |
| 285 | } |
| 286 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 287 | |
| 288 | } |