blob: ef26ca0fa5f21478f630f5c603312473c8365eaa [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
Yotam Harchola86e4252013-09-06 15:36:28 -070029//:: import re
Andreas Wundsam5204de22013-07-30 11:34:45 -070030//:: include('_copyright.java')
31
32//:: include('_autogen.java')
33
34package ${factory.package};
35
Yotam Harchola86e4252013-09-06 15:36:28 -070036import org.projectfloodlight.openflow.protocol.OFOxmList;
37
Andreas Wundsam5204de22013-07-30 11:34:45 -070038//:: include("_imports.java")
39
40public class ${factory.name} implements ${factory.interface.name} {
Andreas Wundsame0d52be2013-08-22 07:52:13 -070041 public final static ${factory.name} INSTANCE = new ${factory.name}();
Andreas Wundsamcb17b232013-09-28 19:05:36 -070042
43 //:: if factory.interface.xid_generator:
44 private final XidGenerator xidGenerator = XidGenerators.global();
45 //:: #endif
Andreas Wundsame0d52be2013-08-22 07:52:13 -070046
47 //:: for name, clazz in factory.interface.sub_factories.items():
48 public ${clazz} ${name}() {
Andreas Wundsamc8912c12013-11-15 13:44:48 -080049 return ${clazz}Ver${factory.version.dotless_version}.INSTANCE;
Andreas Wundsame0d52be2013-08-22 07:52:13 -070050 }
51 //:: #endfor
52
Yotam Harchola86e4252013-09-06 15:36:28 -070053//:: general_get_match_func_written = False
Andreas Wundsam5204de22013-07-30 11:34:45 -070054//:: for i in factory.interface.members:
55 //:: if i.is_virtual:
56 //:: continue
57 //:: #endif
Yotam Harchola86e4252013-09-06 15:36:28 -070058 //:: 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'
59 //:: unsupported_match_object = is_match_object and not i.has_version(factory.version)
Andreas Wundsam5204de22013-07-30 11:34:45 -070060
Andreas Wundsame0d52be2013-08-22 07:52:13 -070061 //:: if len(i.writeable_members) > 0:
62 public ${i.name}.Builder ${factory.interface.method_name(i, builder=True)}() {
63 //:: if i.has_version(factory.version) and model.generate_class(i.versioned_class(factory.version)):
Andreas Wundsamcb17b232013-09-28 19:05:36 -070064 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 -070065 //:: else:
Andreas Wundsam5204de22013-07-30 11:34:45 -070066 throw new UnsupportedOperationException("${i.name} not supported in version ${factory.version}");
Andreas Wundsame0d52be2013-08-22 07:52:13 -070067 //:: #endif
Andreas Wundsam5204de22013-07-30 11:34:45 -070068 }
Andreas Wundsame0d52be2013-08-22 07:52:13 -070069 //:: #endif
Yotam Harchola86e4252013-09-06 15:36:28 -070070 //:: if not general_get_match_func_written and is_match_object and not unsupported_match_object:
71 public Match.Builder buildMatch() {
72 return new ${i.versioned_class(factory.version).name}.Builder();
73 }
Andreas Wundsame962d372013-10-02 18:15:58 -070074
75 final static Match MATCH_WILDCARD_ALL = ${i.versioned_class(factory.version).name}.DEFAULT;
76
77 public Match matchWildcardAll() {
78 return MATCH_WILDCARD_ALL;
79 }
Yotam Harchola86e4252013-09-06 15:36:28 -070080 //:: general_get_match_func_written = True
81 //:: #endif
Andreas Wundsame0d52be2013-08-22 07:52:13 -070082 //:: if len(i.writeable_members) <= 2:
Andreas Wundsamcb17b232013-09-28 19:05:36 -070083 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 -070084 //:: if i.has_version(factory.version) and model.generate_class(i.versioned_class(factory.version)):
85 //:: if len(i.writeable_members) > 0:
86 return new ${i.versioned_class(factory.version).name}(
87 ${",\n ".join(
Andreas Wundsamcb17b232013-09-28 19:05:36 -070088 [ 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 -070089 );
90 //:: else:
91 return ${i.versioned_class(factory.version).name}.INSTANCE;
92 //:: #endif
93 //:: else:
94 throw new UnsupportedOperationException("${i.name} not supported in version ${factory.version}");
95 //:: #endif
96 }
97 //:: #endif
Andreas Wundsam5204de22013-07-30 11:34:45 -070098//:: #endfor
99
Andreas Wundsame0d52be2013-08-22 07:52:13 -0700100 public OFMessageReader<${factory.base_class}> getReader() {
101//:: if factory.versioned_base_class:
102 return ${factory.versioned_base_class.name}.READER;
103//:: else:
104 throw new UnsupportedOperationException("Reader<${factory.base_class}> not supported in version ${factory.version}");
105//:: #endif
Andreas Wundsam3ad98162013-08-15 11:08:56 -0700106 }
107
Yotam Harchol98af7752013-08-22 14:59:38 -0700108//:: if factory.interface.name == 'OFOxms':
109 @SuppressWarnings("unchecked")
110 public <F extends OFValueType<F>> OFOxm<F> fromValue(F value, MatchField<F> field) {
111 switch (field.id) {
112 //:: for oxm_name in model.oxm_map:
113 //:: type_name, value, masked = model.oxm_map[oxm_name]
114 //:: if masked:
115 //:: continue
116 //:: #endif
117 //:: method_name = oxm_name.replace('OFOxm', '')
118 //:: method_name = method_name[0].lower() + method_name[1:]
119 case ${value}:
Yotam Harcholbd1d7282013-08-22 20:10:31 -0700120 //:: # 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
121 return (OFOxm<F>)((Object)${method_name}((${type_name})((Object)value)));
Yotam Harchol98af7752013-08-22 14:59:38 -0700122 //:: #endfor
123 default:
Andreas Wundsambcb73c62013-11-19 09:46:02 -0800124 throw new IllegalArgumentException("No OXM known for match field " + field);
Yotam Harchol98af7752013-08-22 14:59:38 -0700125 }
126 }
127
128 @SuppressWarnings("unchecked")
129 public <F extends OFValueType<F>> OFOxm<F> fromValueAndMask(F value, F mask, MatchField<F> field) {
130 switch (field.id) {
131 //:: for oxm_name in model.oxm_map:
132 //:: type_name, value, masked = model.oxm_map[oxm_name]
133 //:: if not masked:
134 //:: continue
135 //:: #endif
136 //:: method_name = oxm_name.replace('OFOxm', '')
137 //:: method_name = method_name[0].lower() + method_name[1:]
138 case ${value}:
Yotam Harcholbd1d7282013-08-22 20:10:31 -0700139 //:: # 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
140 return (OFOxm<F>)((Object)${method_name}((${type_name})((Object)value), (${type_name})((Object)mask)));
Yotam Harchol98af7752013-08-22 14:59:38 -0700141 //:: #endfor
142 default:
Andreas Wundsambcb73c62013-11-19 09:46:02 -0800143 throw new IllegalArgumentException("No OXM known for match field " + field);
Yotam Harchol98af7752013-08-22 14:59:38 -0700144 }
145 }
146
147 @SuppressWarnings("unchecked")
148 public <F extends OFValueType<F>> OFOxm<F> fromMasked(Masked<F> masked, MatchField<F> field) {
149 switch (field.id) {
150 //:: for oxm_name in model.oxm_map:
151 //:: type_name, value, masked = model.oxm_map[oxm_name]
152 //:: if not masked:
153 //:: continue
154 //:: #endif
155 //:: method_name = oxm_name.replace('OFOxm', '')
156 //:: method_name = method_name[0].lower() + method_name[1:]
157 case ${value}:
Yotam Harcholbd1d7282013-08-22 20:10:31 -0700158 //:: # 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
159 return (OFOxm<F>)((Object)${method_name}((${type_name})((Object)(masked.getValue())), (${type_name})((Object)(masked.getMask()))));
Yotam Harchol98af7752013-08-22 14:59:38 -0700160 //:: #endfor
161 default:
162 return null;
163 }
164 }
165//:: #endif
Andreas Wundsamcb17b232013-09-28 19:05:36 -0700166//:: if factory.interface.xid_generator:
167 public long nextXid() {
168 return xidGenerator.nextXid();
169 }
170//:: #endif
Yotam Harchol98af7752013-08-22 14:59:38 -0700171
Andreas Wundsam47e077c2013-10-04 15:56:51 -0700172 public OFVersion getVersion() {
173 return OFVersion.${factory.version.constant_version};
174 }
Andreas Wundsam5204de22013-07-30 11:34:45 -0700175}