blob: 2c31c7531465942de72f832c86b5a75fdadd80aa [file] [log] [blame]
Andreas Wundsam001b1822013-08-02 22:25:55 -07001//:: # 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 Wundsam001b1822013-08-02 22:25:55 -070031//:: include('_copyright.java')
32
33//:: include('_autogen.java')
34
35package ${msg.package};
36
37//:: include("_imports.java", msg=msg)
38
Andreas Wundsam2be7da52013-08-22 07:34:25 -070039abstract class ${msg.name} {
Andreas Wundsam001b1822013-08-02 22:25:55 -070040 // version: ${version}
Yotam Harchol791e4882013-09-05 16:32:56 -070041 final static byte WIRE_VERSION = ${version.int_version};
Andreas Wundsam001b1822013-08-02 22:25:55 -070042//:: if msg.is_fixed_length:
Yotam Harchol791e4882013-09-05 16:32:56 -070043 final static int LENGTH = ${msg.length};
Andreas Wundsam001b1822013-08-02 22:25:55 -070044//:: else:
Yotam Harchol791e4882013-09-05 16:32:56 -070045 final static int MINIMUM_LENGTH = ${msg.min_length};
Andreas Wundsam001b1822013-08-02 22:25:55 -070046//:: #endif
47
48
49 public final static ${msg.name}.Reader READER = new Reader();
50
Andreas Wundsam2be7da52013-08-22 07:34:25 -070051 static class Reader implements OFMessageReader<${msg.interface.inherited_declaration()}> {
Andreas Wundsam001b1822013-08-02 22:25:55 -070052 @Override
Yotam Harchola86e4252013-09-06 15:36:28 -070053 public ${msg.interface.inherited_declaration()} readFrom(ChannelBuffer bb) throws OFParseError {
Yotam Harchol0178c202013-09-05 16:25:50 -070054//:: if msg.is_fixed_length:
55 if(bb.readableBytes() < LENGTH)
56//:: else:
57 if(bb.readableBytes() < MINIMUM_LENGTH)
58//:: #endif
59 return null;
Andreas Wundsam001b1822013-08-02 22:25:55 -070060 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 Wundsam1cc5df42013-09-04 18:57:39 -070072 if(${prop.name} != ${prop.priv_value})
Andreas Wundsam001b1822013-08-02 22:25:55 -070073 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 Wundsamc8912c12013-11-15 13:44:48 -080089//:: m = sub.member_by_name(prop.name)
Andreas Wundsam001b1822013-08-02 22:25:55 -070090//:: 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}