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