blob: 93525418de30b01f28c55f48c2529d80f9ee36e2 [file] [log] [blame]
Andreas Wundsam5204de22013-07-30 11:34:45 -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//:: import itertools
29//:: import of_g
Yotam Harchola86e4252013-09-06 15:36:28 -070030//:: import re
Andreas Wundsam5204de22013-07-30 11:34:45 -070031//:: include('_copyright.java')
32
33//:: include('_autogen.java')
34
35package ${factory.package};
36
Yotam Harchola86e4252013-09-06 15:36:28 -070037import org.projectfloodlight.openflow.protocol.OFOxmList;
38
Andreas Wundsam5204de22013-07-30 11:34:45 -070039//:: include("_imports.java")
40
41public class ${factory.name} implements ${factory.interface.name} {
Andreas Wundsame0d52be2013-08-22 07:52:13 -070042 public final static ${factory.name} INSTANCE = new ${factory.name}();
Andreas Wundsamcb17b232013-09-28 19:05:36 -070043
44 //:: if factory.interface.xid_generator:
45 private final XidGenerator xidGenerator = XidGenerators.global();
46 //:: #endif
Andreas Wundsame0d52be2013-08-22 07:52:13 -070047
Rob Vaterlausfeee3712013-09-30 11:24:19 -070048 public OFVersion getOFVersion() {
49 return OFVersion.OF_${factory.version.of_version};
50 }
51
Andreas Wundsame0d52be2013-08-22 07:52:13 -070052 //:: for name, clazz in factory.interface.sub_factories.items():
53 public ${clazz} ${name}() {
54 return ${clazz}Ver${factory.version.of_version}.INSTANCE;
55 }
56 //:: #endfor
57
Yotam Harchola86e4252013-09-06 15:36:28 -070058//:: general_get_match_func_written = False
Andreas Wundsam5204de22013-07-30 11:34:45 -070059//:: for i in factory.interface.members:
60 //:: if i.is_virtual:
61 //:: continue
62 //:: #endif
Yotam Harchola86e4252013-09-06 15:36:28 -070063 //:: is_match_object = re.match('OFMatch.*', i.name) # i.has_version(factory.version) and model.generate_class(i.versioned_class(factory.version)) and i.versioned_class(factory.version).interface.parent_interface == 'Match'
64 //:: unsupported_match_object = is_match_object and not i.has_version(factory.version)
Andreas Wundsam5204de22013-07-30 11:34:45 -070065
Andreas Wundsame0d52be2013-08-22 07:52:13 -070066 //:: if len(i.writeable_members) > 0:
67 public ${i.name}.Builder ${factory.interface.method_name(i, builder=True)}() {
68 //:: if i.has_version(factory.version) and model.generate_class(i.versioned_class(factory.version)):
Andreas Wundsamcb17b232013-09-28 19:05:36 -070069 return new ${i.versioned_class(factory.version).name}.Builder()${".setXid(nextXid())" if i.member_by_name("xid") else ""};
Andreas Wundsame0d52be2013-08-22 07:52:13 -070070 //:: else:
Andreas Wundsam5204de22013-07-30 11:34:45 -070071 throw new UnsupportedOperationException("${i.name} not supported in version ${factory.version}");
Andreas Wundsame0d52be2013-08-22 07:52:13 -070072 //:: #endif
Andreas Wundsam5204de22013-07-30 11:34:45 -070073 }
Andreas Wundsame0d52be2013-08-22 07:52:13 -070074 //:: #endif
Yotam Harchola86e4252013-09-06 15:36:28 -070075 //:: if not general_get_match_func_written and is_match_object and not unsupported_match_object:
76 public Match.Builder buildMatch() {
77 return new ${i.versioned_class(factory.version).name}.Builder();
78 }
Andreas Wundsame962d372013-10-02 18:15:58 -070079
80 final static Match MATCH_WILDCARD_ALL = ${i.versioned_class(factory.version).name}.DEFAULT;
81
82 public Match matchWildcardAll() {
83 return MATCH_WILDCARD_ALL;
84 }
Yotam Harchola86e4252013-09-06 15:36:28 -070085 //:: general_get_match_func_written = True
86 //:: #endif
Andreas Wundsame0d52be2013-08-22 07:52:13 -070087 //:: if len(i.writeable_members) <= 2:
Andreas Wundsamcb17b232013-09-28 19:05:36 -070088 public ${i.name} ${factory.interface.method_name(i, builder=False)}(${", ".join("%s %s" % (p.java_type.public_type, p.name) for p in i.writeable_members if p.name != "xid" )}) {
Andreas Wundsame0d52be2013-08-22 07:52:13 -070089 //:: if i.has_version(factory.version) and model.generate_class(i.versioned_class(factory.version)):
90 //:: if len(i.writeable_members) > 0:
91 return new ${i.versioned_class(factory.version).name}(
92 ${",\n ".join(
Andreas Wundsamcb17b232013-09-28 19:05:36 -070093 [ prop.name if prop.name != "xid" else "nextXid()" for prop in i.versioned_class(factory.version).data_members])}
Andreas Wundsame0d52be2013-08-22 07:52:13 -070094 );
95 //:: else:
96 return ${i.versioned_class(factory.version).name}.INSTANCE;
97 //:: #endif
98 //:: else:
99 throw new UnsupportedOperationException("${i.name} not supported in version ${factory.version}");
100 //:: #endif
101 }
102 //:: #endif
Andreas Wundsam5204de22013-07-30 11:34:45 -0700103//:: #endfor
104
Andreas Wundsame0d52be2013-08-22 07:52:13 -0700105 public OFMessageReader<${factory.base_class}> getReader() {
106//:: if factory.versioned_base_class:
107 return ${factory.versioned_base_class.name}.READER;
108//:: else:
109 throw new UnsupportedOperationException("Reader<${factory.base_class}> not supported in version ${factory.version}");
110//:: #endif
Andreas Wundsam3ad98162013-08-15 11:08:56 -0700111 }
112
Yotam Harchol98af7752013-08-22 14:59:38 -0700113//:: if factory.interface.name == 'OFOxms':
114 @SuppressWarnings("unchecked")
115 public <F extends OFValueType<F>> OFOxm<F> fromValue(F value, MatchField<F> field) {
116 switch (field.id) {
117 //:: for oxm_name in model.oxm_map:
118 //:: type_name, value, masked = model.oxm_map[oxm_name]
119 //:: if masked:
120 //:: continue
121 //:: #endif
122 //:: method_name = oxm_name.replace('OFOxm', '')
123 //:: method_name = method_name[0].lower() + method_name[1:]
124 case ${value}:
Yotam Harcholbd1d7282013-08-22 20:10:31 -0700125 //:: # The cast to Object is done to avoid some javac bug that in some versions cannot handle cast from generic type to other types but Object
126 return (OFOxm<F>)((Object)${method_name}((${type_name})((Object)value)));
Yotam Harchol98af7752013-08-22 14:59:38 -0700127 //:: #endfor
128 default:
129 return null;
130 }
131 }
132
133 @SuppressWarnings("unchecked")
134 public <F extends OFValueType<F>> OFOxm<F> fromValueAndMask(F value, F mask, MatchField<F> field) {
135 switch (field.id) {
136 //:: for oxm_name in model.oxm_map:
137 //:: type_name, value, masked = model.oxm_map[oxm_name]
138 //:: if not masked:
139 //:: continue
140 //:: #endif
141 //:: method_name = oxm_name.replace('OFOxm', '')
142 //:: method_name = method_name[0].lower() + method_name[1:]
143 case ${value}:
Yotam Harcholbd1d7282013-08-22 20:10:31 -0700144 //:: # The cast to Object is done to avoid some javac bug that in some versions cannot handle cast from generic type to other types but Object
145 return (OFOxm<F>)((Object)${method_name}((${type_name})((Object)value), (${type_name})((Object)mask)));
Yotam Harchol98af7752013-08-22 14:59:38 -0700146 //:: #endfor
147 default:
148 return null;
149 }
150 }
151
152 @SuppressWarnings("unchecked")
153 public <F extends OFValueType<F>> OFOxm<F> fromMasked(Masked<F> masked, MatchField<F> field) {
154 switch (field.id) {
155 //:: for oxm_name in model.oxm_map:
156 //:: type_name, value, masked = model.oxm_map[oxm_name]
157 //:: if not masked:
158 //:: continue
159 //:: #endif
160 //:: method_name = oxm_name.replace('OFOxm', '')
161 //:: method_name = method_name[0].lower() + method_name[1:]
162 case ${value}:
Yotam Harcholbd1d7282013-08-22 20:10:31 -0700163 //:: # The cast to Object is done to avoid some javac bug that in some versions cannot handle cast from generic type to other types but Object
164 return (OFOxm<F>)((Object)${method_name}((${type_name})((Object)(masked.getValue())), (${type_name})((Object)(masked.getMask()))));
Yotam Harchol98af7752013-08-22 14:59:38 -0700165 //:: #endfor
166 default:
167 return null;
168 }
169 }
170//:: #endif
Andreas Wundsamcb17b232013-09-28 19:05:36 -0700171//:: if factory.interface.xid_generator:
172 public long nextXid() {
173 return xidGenerator.nextXid();
174 }
175//:: #endif
Yotam Harchol98af7752013-08-22 14:59:38 -0700176
Andreas Wundsam5204de22013-07-30 11:34:45 -0700177}