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 { |
| 132 | //:: fields_with_length_member = {} |
| 133 | //:: for prop in msg.members: |
| 134 | //:: if prop.is_data: |
| 135 | ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, |
| 136 | length=fields_with_length_member[prop.c_name] if prop.c_name in fields_with_length_member else None)}; |
| 137 | //:: elif prop.is_pad: |
| 138 | // pad: ${prop.length} bytes |
| 139 | bb.skipBytes(${prop.length}); |
| 140 | //:: elif prop.is_fixed_value: |
| 141 | // fixed value property ${prop.name} == ${prop.value} |
| 142 | ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version)}; |
| 143 | if(${prop.name} != ${prop.value}) |
| 144 | throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name}); |
| 145 | //:: elif prop.is_length_value: |
| 146 | ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version)}; |
| 147 | if(${prop.name} < MINIMUM_LENGTH) |
| 148 | throw new OFParseError("Wrong ${prop.name}: Expected to be >= " + MINIMUM_LENGTH + ", was: " + ${prop.name}); |
| 149 | //:: elif prop.is_field_length_value: |
| 150 | //:: fields_with_length_member[prop.member.field_name] = prop.name |
| 151 | int ${prop.name} = ${prop.java_type.read_op(version)}; |
| 152 | //:: else: |
| 153 | // fixme: todo ${prop.name} |
| 154 | //:: #endif |
| 155 | //:: #endfor |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 156 | //:: if msg.align: |
| 157 | // align message to ${msg.align} bytes |
| 158 | bb.skipBytes(((length + ${msg.align-1})/${msg.align} * ${msg.align} ) - length ); |
| 159 | //:: #endif |
| 160 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 161 | return new ${impl_class}( |
| 162 | ${",\n ".join( |
| 163 | [ prop.name for prop in msg.data_members])} |
| 164 | ); |
| 165 | } |
| 166 | } |
| 167 | |
Yotam Harchol | 5c9d6f4 | 2013-08-01 11:09:20 -0700 | [diff] [blame] | 168 | public void writeTo(ChannelBuffer bb) { |
| 169 | WRITER.write(bb, this); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | final static Writer WRITER = new Writer(); |
| 173 | static class Writer implements OFMessageWriter<${impl_class}> { |
| 174 | @Override |
Andreas Wundsam | a94273b | 2013-08-01 22:11:33 -0700 | [diff] [blame] | 175 | public void write(ChannelBuffer bb, ${impl_class} message) { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 176 | //:: if not msg.is_fixed_length: |
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 | //:: #end |
| 179 | |
| 180 | //:: fields_with_length_member = {} |
| 181 | //:: for prop in msg.members: |
| 182 | //:: if prop.c_name in fields_with_length_member: |
| 183 | int ${prop.name}StartIndex = bb.writerIndex(); |
| 184 | //:: #endif |
| 185 | //:: if prop.is_data: |
| 186 | ${prop.java_type.write_op(version, "message." + prop.name)}; |
| 187 | //:: elif prop.is_pad: |
| 188 | // pad: ${prop.length} bytes |
| 189 | bb.writeZero(${prop.length}); |
| 190 | //:: elif prop.is_fixed_value: |
| 191 | // fixed value property ${prop.name} = ${prop.value} |
| 192 | ${prop.java_type.write_op(version, prop.value)}; |
| 193 | //:: elif prop.is_length_value: |
| 194 | // ${prop.name} is length of variable message, will be updated at the end |
| 195 | ${prop.java_type.write_op(version, 0)}; |
| 196 | //:: elif prop.is_field_length_value: |
| 197 | //:: fields_with_length_member[prop.member.field_name] = prop.name |
| 198 | // ${prop.name} is length indicator for ${prop.member.field_name}, will be |
| 199 | // udpated when ${prop.member.field_name} has been written |
| 200 | int ${prop.name}Index = bb.writerIndex(); |
| 201 | ${prop.java_type.write_op(version, 0)}; |
| 202 | //:: else: |
| 203 | // FIXME: todo write ${prop.name} |
| 204 | //:: #endif |
| 205 | //:: if prop.c_name in fields_with_length_member: |
| 206 | //:: length_member_name = fields_with_length_member[prop.c_name] |
| 207 | // update field length member ${length_member_name} |
| 208 | int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex; |
| 209 | bb.setShort(${length_member_name}Index, ${prop.name}Length); |
| 210 | //:: #endif |
| 211 | //:: #endfor |
| 212 | |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 213 | //:: if not msg.is_fixed_length: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 214 | // update length field |
| 215 | int length = bb.writerIndex() - startIndex; |
| 216 | bb.setShort(startIndex + 2, length); |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 217 | //:: if msg.align: |
| 218 | // align message to ${msg.align} bytes |
| 219 | bb.writeZero( ((length + ${msg.align-1})/${msg.align} * ${msg.align}) - length); |
| 220 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 221 | //:: #end |
| 222 | |
| 223 | } |
| 224 | } |
| 225 | |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 226 | @Override |
Andreas Wundsam | d7d5bb3 | 2013-07-30 12:26:31 -0700 | [diff] [blame] | 227 | public String toString() { |
| 228 | StringBuilder b = new StringBuilder("${msg.name}("); |
| 229 | //:: for i, prop in enumerate(msg.data_members): |
| 230 | //:: if i > 0: |
| 231 | b.append(", "); |
| 232 | //:: #endif |
| 233 | b.append("${prop.name}=").append(${ "Arrays.toString(%s)" % prop.name if prop.java_type.is_array else prop.name }); |
| 234 | //:: #endfor |
| 235 | b.append(")"); |
| 236 | return b.toString(); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | @Override |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 241 | public boolean equals(Object obj) { |
| 242 | if (this == obj) |
| 243 | return true; |
| 244 | if (obj == null) |
| 245 | return false; |
| 246 | if (getClass() != obj.getClass()) |
| 247 | return false; |
| 248 | ${msg.name} other = (${msg.name}) obj; |
| 249 | |
| 250 | //:: for prop in msg.data_members: |
| 251 | //:: if prop.java_type.is_primitive: |
| 252 | if( ${prop.name} != other.${prop.name}) |
| 253 | return false; |
| 254 | //:: elif prop.java_type.is_array: |
| 255 | if (!Arrays.equals(${prop.name}, other.${prop.name})) |
| 256 | return false; |
Andreas Wundsam | d7d5bb3 | 2013-07-30 12:26:31 -0700 | [diff] [blame] | 257 | //:: else: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 258 | if (${prop.name} == null) { |
| 259 | if (other.${prop.name} != null) |
| 260 | return false; |
| 261 | } else if (!${prop.name}.equals(other.${prop.name})) |
| 262 | return false; |
| 263 | //:: #endif |
| 264 | //:: #endfor |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public int hashCode() { |
| 270 | final int prime = 31; |
| 271 | int result = 1; |
| 272 | |
| 273 | //:: for prop in msg.data_members: |
| 274 | //:: if prop.java_type.is_primitive: |
| 275 | result = prime * result + ${prop.name}; |
| 276 | //:: elif prop.java_type.is_array: |
| 277 | result = prime * result + Arrays.hashCode(${prop.name}); |
| 278 | //:: else: |
| 279 | result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode()); |
| 280 | //:: #endif |
| 281 | //:: #endfor |
| 282 | return result; |
| 283 | } |
| 284 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 285 | |
| 286 | } |