Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -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 * |
| 29 | //:: import os |
| 30 | //:: import itertools |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 31 | //:: include('_copyright.java') |
| 32 | |
| 33 | //:: include('_autogen.java') |
| 34 | |
| 35 | package ${msg.package}; |
| 36 | |
| 37 | //:: include("_imports.java", msg=msg) |
| 38 | |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 39 | abstract class ${msg.name} { |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 40 | // version: ${version} |
Yotam Harchol | 791e488 | 2013-09-05 16:32:56 -0700 | [diff] [blame] | 41 | final static byte WIRE_VERSION = ${version.int_version}; |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 42 | //:: if msg.is_fixed_length: |
Yotam Harchol | 791e488 | 2013-09-05 16:32:56 -0700 | [diff] [blame] | 43 | final static int LENGTH = ${msg.length}; |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 44 | //:: else: |
Yotam Harchol | 791e488 | 2013-09-05 16:32:56 -0700 | [diff] [blame] | 45 | final static int MINIMUM_LENGTH = ${msg.min_length}; |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 46 | //:: #endif |
| 47 | |
| 48 | |
| 49 | public final static ${msg.name}.Reader READER = new Reader(); |
| 50 | |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 51 | static class Reader implements OFMessageReader<${msg.interface.inherited_declaration()}> { |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 52 | @Override |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 53 | public ${msg.interface.inherited_declaration()} readFrom(ChannelBuffer bb) throws OFParseError { |
Yotam Harchol | 0178c20 | 2013-09-05 16:25:50 -0700 | [diff] [blame] | 54 | //:: if msg.is_fixed_length: |
| 55 | if(bb.readableBytes() < LENGTH) |
| 56 | //:: else: |
| 57 | if(bb.readableBytes() < MINIMUM_LENGTH) |
| 58 | //:: #endif |
| 59 | return null; |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 60 | int start = bb.readerIndex(); |
| 61 | //:: fields_with_length_member = {} |
| 62 | //:: for prop in msg.members: |
| 63 | //:: if prop.is_data: |
| 64 | ${prop.java_type.skip_op(version, |
| 65 | length=fields_with_length_member[prop.c_name] if prop.c_name in fields_with_length_member else None)}; |
| 66 | //:: elif prop.is_pad: |
| 67 | // pad: ${prop.length} bytes |
| 68 | bb.skipBytes(${prop.length}); |
| 69 | //:: elif prop.is_fixed_value: |
| 70 | // fixed value property ${prop.name} == ${prop.value} |
| 71 | ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)}; |
Andreas Wundsam | 1cc5df4 | 2013-09-04 18:57:39 -0700 | [diff] [blame] | 72 | if(${prop.name} != ${prop.priv_value}) |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 73 | throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name}); |
| 74 | //:: elif prop.is_length_value: |
| 75 | ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True)}; |
| 76 | if(${prop.name} < MINIMUM_LENGTH) |
| 77 | throw new OFParseError("Wrong ${prop.name}: Expected to be >= " + MINIMUM_LENGTH + ", was: " + ${prop.name}); |
| 78 | //:: elif prop.is_field_length_value: |
| 79 | //:: fields_with_length_member[prop.member.field_name] = prop.name |
| 80 | int ${prop.name} = ${prop.java_type.read_op(version)}; |
| 81 | //:: elif prop.is_discriminator: |
| 82 | ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)}; |
| 83 | bb.readerIndex(start); |
| 84 | switch(${prop.name}) { |
| 85 | //:: for sub in msg.subclasses: |
| 86 | //:: if not model.generate_class(sub): |
| 87 | // skip ${sub.name} - excluded from generation |
| 88 | //:: else: |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame^] | 89 | //:: m = sub.member_by_name(prop.name) |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 90 | //:: if not m.is_fixed_value: |
| 91 | //:: raise Exception("subtype %s of %s does not have fixed value for discriminator %s" % |
| 92 | //:: (sub.name, msg.name, prop.name)) |
| 93 | //:: #endif |
| 94 | case ${m.priv_value}: |
| 95 | // discriminator value ${m.enum_value}=${m.value} for class ${sub.name} |
| 96 | return ${sub.name}.READER.readFrom(bb); |
| 97 | //:: #endif # generate_class |
| 98 | //:: #endfor |
| 99 | default: |
| 100 | throw new OFParseError("Unknown value for discriminator ${prop.name} of class ${msg.name}: " + ${prop.name}); |
| 101 | } |
| 102 | //:: break |
| 103 | //:: #endif |
| 104 | //:: #endfor |
| 105 | } |
| 106 | } |
| 107 | } |