blob: 9665c497f246b87025794924a15696c9d95c11ea [file] [log] [blame]
Andreas Wundsam27303462013-07-16 12:52:35 -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 itertools
30//:: import of_g
31//:: include('_copyright.java')
32
33//:: include('_autogen.java')
34
35package ${msg.package};
36
37//:: include("_imports.java", msg=msg)
38
39class ${impl_class} implements ${msg.interface.name} {
40 // version: ${version}
41 private final static byte WIRE_VERSION = ${version.int_version};
42//:: if msg.is_fixed_length:
43 private final static int LENGTH = ${msg.length};
44//:: else:
45 private final static int MINIMUM_LENGTH = ${msg.min_length};
46//:: #endif
47
48//:: for prop in msg.data_members:
49 private final static ${prop.java_type.public_type} ${prop.default_name} = ${prop.default_value};
50//:: #end
51
52 // OF message fields
53//:: for prop in msg.data_members:
54 private final ${prop.java_type.public_type} ${prop.name};
55//:: #endfor
56
57 ${impl_class}(${
58 ", ".join("%s %s" %(prop.java_type.public_type, prop.name) for prop in msg.data_members) }) {
59//:: for prop in msg.data_members:
60 this.${prop.name} = ${prop.name};
61//:: #endfor
62 }
63
64 // Accessors for OF message fields
65//:: include("_field_accessors.java", msg=msg, generate_setters=False, builder=False)
66
67
68 public ${msg.name}.Builder createBuilder() {
69 return new BuilderImplWithParent(this);
70 }
71
72 static class BuilderImplWithParent implements ${msg.interface.name}.Builder {
73 final ${impl_class} parentMessage;
74
75 // OF message fields
76//:: for prop in msg.data_members:
77 private boolean ${prop.name}Set;
78 private ${prop.java_type.public_type} ${prop.name};
79//:: #endfor
80
81 BuilderImplWithParent(${impl_class} parentMessage) {
82 this.parentMessage = parentMessage;
83 }
84
85//:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True)
86
87 @Override
88 public ${msg.interface.name} getMessage() {
89 return new ${impl_class}(
90 ${",\n ".join(
91 [ "this.{0}Set ? this.{0} : parentMessage.{0}".format(prop.name)
92 for prop in msg.data_members])}
93 );
94 }
95 }
96
97 static class BuilderImpl implements ${msg.interface.name}.Builder {
98 // OF message fields
99//:: for prop in msg.data_members:
100 private boolean ${prop.name}Set;
101 private ${prop.java_type.public_type} ${prop.name};
102//:: #endfor
103
104//:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True)
105//
106 @Override
107 public ${msg.interface.name} getMessage() {
108 return new ${impl_class}(
109 ${",\n ".join(
110 [ "this.{0}Set ? this.{0} : {1}.{2}".format(prop.name, impl_class, prop.default_name)
111 for prop in msg.data_members])}
112 );
113 }
114 }
115
116 final static Reader READER = new Reader();
117 static class Reader implements OFMessageReader<${msg.interface.name}> {
118 @Override
119 public ${msg.interface.name} readFrom(ChannelBuffer bb) throws OFParseError {
120//:: fields_with_length_member = {}
121//:: for prop in msg.members:
122//:: if prop.is_data:
123 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version,
124 length=fields_with_length_member[prop.c_name] if prop.c_name in fields_with_length_member else None)};
125//:: elif prop.is_pad:
126 // pad: ${prop.length} bytes
127 bb.skipBytes(${prop.length});
128//:: elif prop.is_fixed_value:
129 // fixed value property ${prop.name} == ${prop.value}
130 ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version)};
131 if(${prop.name} != ${prop.value})
132 throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name});
133//:: elif prop.is_length_value:
134 ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version)};
135 if(${prop.name} < MINIMUM_LENGTH)
136 throw new OFParseError("Wrong ${prop.name}: Expected to be >= " + MINIMUM_LENGTH + ", was: " + ${prop.name});
137//:: elif prop.is_field_length_value:
138//:: fields_with_length_member[prop.member.field_name] = prop.name
139 int ${prop.name} = ${prop.java_type.read_op(version)};
140//:: else:
141 // fixme: todo ${prop.name}
142//:: #endif
143//:: #endfor
144 return new ${impl_class}(
145 ${",\n ".join(
146 [ prop.name for prop in msg.data_members])}
147 );
148 }
149 }
150
Yotam Harchol5c9d6f42013-08-01 11:09:20 -0700151 public void writeTo(ChannelBuffer bb) {
152 WRITER.write(bb, this);
Andreas Wundsam27303462013-07-16 12:52:35 -0700153 }
154
155 final static Writer WRITER = new Writer();
156 static class Writer implements OFMessageWriter<${impl_class}> {
157 @Override
158 public int write(ChannelBuffer bb, ${impl_class} message) {
159//:: if not msg.is_fixed_length:
Andreas Wundsam482f6d92013-07-24 16:10:21 -0700160 int startIndex = bb.writerIndex();
Andreas Wundsam27303462013-07-16 12:52:35 -0700161//:: #end
162
163//:: fields_with_length_member = {}
164//:: for prop in msg.members:
165//:: if prop.c_name in fields_with_length_member:
166 int ${prop.name}StartIndex = bb.writerIndex();
167//:: #endif
168//:: if prop.is_data:
169 ${prop.java_type.write_op(version, "message." + prop.name)};
170//:: elif prop.is_pad:
171 // pad: ${prop.length} bytes
172 bb.writeZero(${prop.length});
173//:: elif prop.is_fixed_value:
174 // fixed value property ${prop.name} = ${prop.value}
175 ${prop.java_type.write_op(version, prop.value)};
176//:: elif prop.is_length_value:
177 // ${prop.name} is length of variable message, will be updated at the end
178 ${prop.java_type.write_op(version, 0)};
179//:: elif prop.is_field_length_value:
180//:: fields_with_length_member[prop.member.field_name] = prop.name
181 // ${prop.name} is length indicator for ${prop.member.field_name}, will be
182 // udpated when ${prop.member.field_name} has been written
183 int ${prop.name}Index = bb.writerIndex();
184 ${prop.java_type.write_op(version, 0)};
185//:: else:
186 // FIXME: todo write ${prop.name}
187//:: #endif
188//:: if prop.c_name in fields_with_length_member:
189//:: length_member_name = fields_with_length_member[prop.c_name]
190 // update field length member ${length_member_name}
191 int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex;
192 bb.setShort(${length_member_name}Index, ${prop.name}Length);
193//:: #endif
194//:: #endfor
195
196//:: if msg.is_fixed_length:
197 return LENGTH;
198//:: else:
199 // update length field
200 int length = bb.writerIndex() - startIndex;
201 bb.setShort(startIndex + 2, length);
202 return length;
203//:: #end
204
205 }
206 }
207
208
209}