Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -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 | |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 28 | # Prototype of an Intermediate Object model for the java code generator |
| 29 | # A lot of this stuff could/should probably be merged with the python utilities |
| 30 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 31 | import collections |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 32 | from collections import namedtuple, defaultdict, OrderedDict |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 33 | import logging |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 34 | import os |
| 35 | import pdb |
| 36 | import re |
| 37 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 38 | from generic_utils import find, memoize, OrderedSet, OrderedDefaultDict |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 39 | import of_g |
| 40 | from loxi_ir import * |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 41 | import loxi_front_end.type_maps as type_maps |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 42 | import loxi_utils.loxi_utils as loxi_utils |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 43 | import py_gen.util as py_utils |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 44 | import test_data |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 45 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 46 | import java_gen.java_type as java_type |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 47 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 48 | class JavaModel(object): |
Andreas Wundsam | 4352653 | 2013-08-01 22:03:50 -0700 | [diff] [blame] | 49 | enum_blacklist = set(("OFDefinitions",)) |
| 50 | enum_entry_blacklist = defaultdict(lambda: set(), OFFlowWildcards=set([ "NW_DST_BITS", "NW_SRC_BITS", "NW_SRC_SHIFT", "NW_DST_SHIFT" ])) |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 51 | write_blacklist = defaultdict(lambda: set(), OFOxm=set(('typeLen',)), OFAction=set(('type',)), OFInstruction=set(('type',)), OFFlowMod=set(('command', ))) |
| 52 | virtual_interfaces = set(['OFOxm', 'OFInstruction', 'OFFlowMod', 'OFBsnVport' ]) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 53 | |
| 54 | @property |
| 55 | @memoize |
| 56 | def versions(self): |
| 57 | return OrderedSet( JavaOFVersion(raw_version) for raw_version in of_g.target_version_list ) |
| 58 | |
| 59 | @property |
| 60 | @memoize |
| 61 | def interfaces(self): |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 62 | version_map_per_class = collections.OrderedDict() |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 63 | |
| 64 | for raw_version, of_protocol in of_g.ir.items(): |
| 65 | jversion = JavaOFVersion(of_protocol.wire_version) |
| 66 | |
| 67 | for of_class in of_protocol.classes: |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 68 | if not of_class.name in version_map_per_class: |
| 69 | version_map_per_class[of_class.name] = collections.OrderedDict() |
| 70 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 71 | version_map_per_class[of_class.name][jversion] = of_class |
| 72 | |
| 73 | interfaces = [] |
| 74 | for class_name, version_map in version_map_per_class.items(): |
| 75 | interfaces.append(JavaOFInterface(class_name, version_map)) |
| 76 | |
| 77 | return interfaces |
| 78 | |
| 79 | @property |
| 80 | @memoize |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 81 | def all_classes(self): |
| 82 | return [clazz for interface in self.interfaces for clazz in interface.versioned_classes] |
| 83 | |
| 84 | @property |
| 85 | @memoize |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 86 | def enums(self): |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 87 | name_version_enum_map = OrderedDefaultDict(lambda: OrderedDict()) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 88 | |
| 89 | for version in self.versions: |
| 90 | of_protocol = of_g.ir[version.int_version] |
| 91 | for enum in of_protocol.enums: |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 92 | name_version_enum_map[enum.name][version] = enum |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 93 | |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 94 | enums = [ JavaEnum(name, version_enum_map) for name, version_enum_map, |
| 95 | in name_version_enum_map.items() ] |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 96 | |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 97 | # inelegant - need java name here |
| 98 | enums = [ enum for enum in enums if enum.name not in self.enum_blacklist ] |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 99 | return enums |
| 100 | |
| 101 | @memoize |
| 102 | def enum_by_name(self, name): |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 103 | res = find(lambda e: e.name == name, self.enums) |
| 104 | if not res: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 105 | raise KeyError("Could not find enum with name %s" % name) |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 106 | return res |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 107 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 108 | @property |
| 109 | @memoize |
| 110 | def of_factory(self): |
| 111 | return OFFactory( |
| 112 | package="org.openflow.protocol", |
| 113 | name="OFFactory", |
| 114 | members=self.interfaces) |
| 115 | |
| 116 | def generate_class(self, clazz): |
Andreas Wundsam | 4352653 | 2013-08-01 22:03:50 -0700 | [diff] [blame] | 117 | if clazz.interface.name.startswith("OFMatchV"): |
| 118 | return True |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 119 | elif clazz.name == "OFTableModVer10": |
| 120 | # tablemod ver 10 is a hack and has no oftype defined |
| 121 | return False |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 122 | if loxi_utils.class_is_message(clazz.interface.c_name): |
| 123 | return True |
| 124 | if loxi_utils.class_is_oxm(clazz.interface.c_name): |
| 125 | return True |
Andreas Wundsam | 4352653 | 2013-08-01 22:03:50 -0700 | [diff] [blame] | 126 | if loxi_utils.class_is_action(clazz.interface.c_name): |
| 127 | return True |
| 128 | if loxi_utils.class_is_instruction(clazz.interface.c_name): |
| 129 | return True |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 130 | else: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 131 | return True |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 132 | |
| 133 | |
| 134 | class OFFactory(namedtuple("OFFactory", ("package", "name", "members"))): |
| 135 | @property |
| 136 | def factory_classes(self): |
| 137 | return [ OFFactoryClass( |
| 138 | package="org.openflow.protocol.ver{}".format(version.of_version), |
| 139 | name="OFFactoryVer{}".format(version.of_version), |
| 140 | interface=self, |
| 141 | version=version |
| 142 | ) for version in model.versions ] |
| 143 | |
| 144 | |
| 145 | OFGenericClass = namedtuple("OFGenericClass", ("package", "name")) |
| 146 | OFFactoryClass = namedtuple("OFFactory", ("package", "name", "interface", "version")) |
| 147 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 148 | model = JavaModel() |
| 149 | |
| 150 | ####################################################################### |
| 151 | ### OFVersion |
| 152 | ####################################################################### |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 153 | |
| 154 | class JavaOFVersion(object): |
| 155 | """ Models a version of OpenFlow. contains methods to convert the internal |
| 156 | Loxi version to a java constant / a string """ |
| 157 | def __init__(self, int_version): |
| 158 | self.int_version = int(int_version) |
| 159 | |
| 160 | @property |
| 161 | def of_version(self): |
| 162 | return "1" + str(int(self.int_version) - 1) |
| 163 | |
| 164 | @property |
| 165 | def constant_version(self): |
| 166 | return "OF_" + self.of_version |
| 167 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 168 | def __repr__(self): |
| 169 | return "JavaOFVersion(%d)" % self.int_version |
| 170 | |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 171 | def __str__(self): |
| 172 | return of_g.param_version_names[self.int_version] |
| 173 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 174 | def __hash__(self): |
| 175 | return hash(self.int_version) |
| 176 | |
| 177 | def __eq__(self, other): |
| 178 | if other is None or type(self) != type(other): |
| 179 | return False |
| 180 | return (self.int_version,) == (other.int_version,) |
| 181 | |
| 182 | ####################################################################### |
| 183 | ### Interface |
| 184 | ####################################################################### |
| 185 | |
| 186 | class JavaOFInterface(object): |
| 187 | """ Models an OpenFlow Message class for the purpose of the java class. |
| 188 | Version agnostic, in contrast to the loxi_ir python model. |
| 189 | """ |
| 190 | def __init__(self, c_name, version_map): |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 191 | self.c_name = c_name |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 192 | self.version_map = version_map |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 193 | self.name = java_type.name_c_to_caps_camel(c_name) if c_name != "of_header" else "OFMessage" |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 194 | self.variable_name = self.name[2].lower() + self.name[3:] |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 195 | self.constant_name = c_name.upper().replace("OF_", "") |
| 196 | |
| 197 | pck_suffix, parent_interface = self.class_info() |
| 198 | self.package = "org.openflow.protocol.%s" % pck_suffix if pck_suffix else "org.openflow.protocol" |
| 199 | if self.name != parent_interface: |
| 200 | self.parent_interface = parent_interface |
| 201 | else: |
| 202 | self.parent_interface = None |
| 203 | |
| 204 | def class_info(self): |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 205 | if re.match(r'OF.+StatsRequest$', self.name): |
| 206 | return ("", "OFStatsRequest") |
| 207 | elif re.match(r'OF.+StatsReply$', self.name): |
| 208 | return ("", "OFStatsReply") |
| 209 | elif re.match(r'OFFlow(Add|Modify(Strict)?|Delete(Strict)?)$', self.name): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 210 | return ("", "OFFlowMod") |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 211 | elif loxi_utils.class_is_message(self.c_name) and re.match(r'OFBsn.+$', self.name): |
| 212 | return ("", "OFBsnHeader") |
| 213 | elif loxi_utils.class_is_message(self.c_name) and re.match(r'OFNicira.+$', self.name): |
| 214 | return ("", "OFNiciraHeader") |
Andreas Wundsam | 4352653 | 2013-08-01 22:03:50 -0700 | [diff] [blame] | 215 | elif re.match(r'OFMatch.*', self.name): |
| 216 | return ("", "Match") |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 217 | elif loxi_utils.class_is_message(self.c_name): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 218 | return ("", "OFMessage") |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 219 | elif loxi_utils.class_is_action(self.c_name): |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 220 | if re.match(r'OFActionBsn.*', self.name): |
| 221 | return ("action", "OFActionBsn") |
| 222 | elif re.match(r'OFActionNicira.*', self.name): |
| 223 | return ("action", "OFActionNicira") |
| 224 | else: |
| 225 | return ("action", "OFAction") |
| 226 | elif re.match(r'OFBsnVport.+$', self.name): |
| 227 | return ("", "OFBsnVport") |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 228 | elif loxi_utils.class_is_oxm(self.c_name): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 229 | return ("oxm", "OFOxm") |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 230 | elif loxi_utils.class_is_instruction(self.c_name): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 231 | return ("instruction", "OFInstruction") |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 232 | elif loxi_utils.class_is_meter_band(self.c_name): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 233 | return ("meterband", "OFMeterBand") |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 234 | elif loxi_utils.class_is_queue_prop(self.c_name): |
| 235 | return ("queueprop", "OFQueueProp") |
| 236 | elif loxi_utils.class_is_hello_elem(self.c_name): |
| 237 | return ("", "OFHelloElem") |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 238 | else: |
| 239 | return ("", None) |
| 240 | |
| 241 | @property |
| 242 | @memoize |
| 243 | def members(self): |
| 244 | all_versions = [] |
| 245 | member_map = collections.OrderedDict() |
| 246 | |
| 247 | for (version, of_class) in self.version_map.items(): |
| 248 | for of_member in of_class.members: |
| 249 | if isinstance(of_member, OFLengthMember) or \ |
| 250 | isinstance(of_member, OFFieldLengthMember) or \ |
| 251 | isinstance(of_member, OFPadMember): |
| 252 | continue |
| 253 | if of_member.name not in member_map: |
| 254 | member_map[of_member.name] = JavaMember.for_of_member(self, of_member) |
| 255 | |
| 256 | return member_map.values() |
| 257 | |
| 258 | @property |
| 259 | @memoize |
| 260 | def is_virtual(self): |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 261 | return self.name in model.virtual_interfaces or all(ir_class.virtual for ir_class in self.version_map.values()) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 262 | |
| 263 | @property |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 264 | def is_universal(self): |
| 265 | return len(self.all_versions) == len(model.versions) |
| 266 | |
| 267 | @property |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 268 | @memoize |
| 269 | def all_versions(self): |
| 270 | return self.version_map.keys() |
| 271 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 272 | def has_version(self, version): |
| 273 | return version in self.version_map |
| 274 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 275 | def versioned_class(self, version): |
| 276 | return JavaOFClass(self, version, self.version_map[version]) |
| 277 | |
| 278 | @property |
| 279 | @memoize |
| 280 | def versioned_classes(self): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 281 | return [ self.versioned_class(version) for version in self.all_versions ] |
| 282 | |
| 283 | ####################################################################### |
| 284 | ### (Versioned) Classes |
| 285 | ####################################################################### |
| 286 | |
| 287 | class JavaOFClass(object): |
| 288 | """ Models an OpenFlow Message class for the purpose of the java class. |
Andreas Wundsam | d40ddbf | 2013-07-25 15:40:47 -0700 | [diff] [blame] | 289 | Version specific child of a JavaOFInterface |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 290 | """ |
| 291 | def __init__(self, interface, version, ir_class): |
| 292 | self.interface = interface |
| 293 | self.ir_class = ir_class |
| 294 | self.c_name = self.ir_class.name |
| 295 | self.version = version |
| 296 | self.constant_name = self.c_name.upper().replace("OF_", "") |
| 297 | self.package = "org.openflow.protocol.ver%s" % version.of_version |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 298 | self.generated = False |
| 299 | |
| 300 | @property |
| 301 | @memoize |
| 302 | def unit_test(self): |
| 303 | return JavaUnitTest(self) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 304 | |
| 305 | @property |
| 306 | def name(self): |
| 307 | return "%sVer%s" % (self.interface.name, self.version.of_version) |
| 308 | |
| 309 | @property |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 310 | def variable_name(self): |
| 311 | return self.name[3:] |
| 312 | |
| 313 | @property |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 314 | def length(self): |
| 315 | if self.is_fixed_length: |
| 316 | return self.min_length |
| 317 | else: |
| 318 | raise Exception("No fixed length for class %s, version %s" % (self.name, self.version)) |
| 319 | |
| 320 | @property |
| 321 | def min_length(self): |
| 322 | id_tuple = (self.ir_class.name, self.version.int_version) |
| 323 | return of_g.base_length[id_tuple] if id_tuple in of_g.base_length else -1 |
| 324 | |
| 325 | @property |
| 326 | def is_fixed_length(self): |
| 327 | return (self.ir_class.name, self.version.int_version) in of_g.is_fixed_length |
| 328 | |
| 329 | def all_properties(self): |
| 330 | return self.interface.members |
| 331 | |
| 332 | def get_member(self, name): |
| 333 | for m in self.members: |
| 334 | if m.name == name: |
| 335 | return m |
| 336 | |
| 337 | @property |
| 338 | @memoize |
| 339 | def data_members(self): |
| 340 | return [ prop for prop in self.members if prop.is_data ] |
| 341 | |
| 342 | @property |
| 343 | @memoize |
| 344 | def fixed_value_members(self): |
| 345 | return [ prop for prop in self.members if prop.is_fixed_value ] |
| 346 | |
| 347 | @property |
| 348 | @memoize |
| 349 | def public_members(self): |
| 350 | return [ prop for prop in self.members if prop.is_public ] |
| 351 | |
| 352 | @property |
| 353 | @memoize |
| 354 | def members(self): |
| 355 | members = [ JavaMember.for_of_member(self, of_member) for of_member in self.ir_class.members ] |
| 356 | return members |
| 357 | |
| 358 | def all_versions(self): |
| 359 | return [ JavaOFVersion(int_version) |
| 360 | for int_version in of_g.unified[self.c_name] |
| 361 | if int_version != 'union' and int_version != 'object_id' ] |
| 362 | |
| 363 | def version_is_inherited(self, version): |
| 364 | return 'use_version' in of_g.unified[self.ir_class.name][version.int_version] |
| 365 | |
| 366 | def inherited_from(self, version): |
| 367 | return JavaOFVersion(of_g.unified[self.ir_class.name][version.int_version]['use_version']) |
| 368 | |
| 369 | @property |
| 370 | def is_virtual(self): |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 371 | return self.ir_class.virtual # type_maps.class_is_virtual(self.c_name) or self.ir_class.virtual |
| 372 | |
| 373 | @property |
| 374 | def discriminator(self): |
| 375 | return find(lambda m: isinstance(m, OFDiscriminatorMember), self.ir_class.members) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 376 | |
| 377 | @property |
| 378 | def is_extension(self): |
| 379 | return type_maps.message_is_extension(self.c_name, -1) |
| 380 | |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 381 | @property |
| 382 | def align(self): |
| 383 | return int(self.ir_class.params['align']) if 'align' in self.ir_class.params else 0 |
| 384 | |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 385 | @property |
| 386 | @memoize |
| 387 | def superclass(self): |
| 388 | return find(lambda c: c.version == self.version and c.c_name == self.ir_class.superclass, model.all_classes) |
| 389 | |
| 390 | @property |
| 391 | @memoize |
| 392 | def subclasses(self): |
| 393 | return [ c for c in model.all_classes if c.version == self.version and c.ir_class.superclass == self.c_name ] |
| 394 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 395 | ####################################################################### |
| 396 | ### Member |
| 397 | ####################################################################### |
| 398 | |
| 399 | |
| 400 | class JavaMember(object): |
| 401 | """ Models a property (member) of an openflow class. """ |
| 402 | def __init__(self, msg, name, java_type, member): |
| 403 | self.msg = msg |
| 404 | self.name = name |
| 405 | self.java_type = java_type |
| 406 | self.member = member |
| 407 | self.c_name = self.member.name if(hasattr(self.member, "name")) else "" |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 408 | |
| 409 | @property |
| 410 | def title_name(self): |
| 411 | return self.name[0].upper() + self.name[1:] |
| 412 | |
| 413 | @property |
| 414 | def constant_name(self): |
| 415 | return self.c_name.upper() |
| 416 | |
| 417 | @property |
| 418 | def default_name(self): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 419 | if self.is_fixed_value: |
| 420 | return self.constant_name |
| 421 | else: |
| 422 | return "DEFAULT_"+self.constant_name |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 423 | |
| 424 | @property |
| 425 | def default_value(self): |
| 426 | java_type = self.java_type.public_type; |
| 427 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 428 | if self.is_fixed_value: |
| 429 | return self.enum_value |
| 430 | elif re.match(r'List.*', java_type): |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 431 | return "Collections.emptyList()" |
| 432 | elif java_type == "boolean": |
| 433 | return "false"; |
| 434 | elif java_type in ("byte", "char", "short", "int", "long"): |
| 435 | return "({0}) 0".format(java_type); |
| 436 | else: |
| 437 | return "null"; |
| 438 | |
| 439 | @property |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 440 | def enum_value(self): |
| 441 | if self.name == "version": |
| 442 | return "OFVersion.%s" % self.msg.version.constant_version |
| 443 | |
| 444 | java_type = self.java_type.public_type; |
| 445 | try: |
| 446 | global model |
| 447 | enum = model.enum_by_name(java_type) |
| 448 | entry = enum.entry_by_version_value(self.msg.version, self.value) |
| 449 | return "%s.%s" % ( enum.name, entry.name) |
| 450 | except KeyError, e: |
| 451 | print e.message |
| 452 | return self.value |
| 453 | |
| 454 | @property |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 455 | def is_pad(self): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 456 | return isinstance(self.member, OFPadMember) |
| 457 | |
| 458 | def is_type_value(self, version=None): |
| 459 | if(version==None): |
| 460 | return any(self.is_type_value(version) for version in self.msg.all_versions) |
| 461 | try: |
| 462 | return self.c_name in get_type_values(self.msg.c_name, version.int_version) |
| 463 | except: |
| 464 | return False |
| 465 | |
| 466 | @property |
| 467 | def is_field_length_value(self): |
| 468 | return isinstance(self.member, OFFieldLengthMember) |
| 469 | |
| 470 | @property |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 471 | def is_discriminator(self): |
| 472 | return isinstance(self.member, OFDiscriminatorMember) |
| 473 | |
| 474 | @property |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 475 | def is_length_value(self): |
| 476 | return isinstance(self.member, OFLengthMember) |
| 477 | |
| 478 | @property |
| 479 | def is_public(self): |
| 480 | return not (self.is_pad or self.is_length_value) |
| 481 | |
| 482 | @property |
| 483 | def is_data(self): |
| 484 | return isinstance(self.member, OFDataMember) and self.name != "version" |
| 485 | |
| 486 | @property |
| 487 | def is_fixed_value(self): |
| 488 | return hasattr(self.member, "value") or self.name == "version" \ |
| 489 | or ( self.name == "length" and self.msg.is_fixed_length) \ |
| 490 | or ( self.name == "len" and self.msg.is_fixed_length) |
| 491 | |
| 492 | @property |
| 493 | def value(self): |
| 494 | if self.name == "version": |
| 495 | return self.msg.version.int_version |
| 496 | elif self.name == "length" or self.name == "len": |
| 497 | return self.msg.length |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 498 | else: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 499 | return self.java_type.format_value(self.member.value) |
| 500 | |
| 501 | @property |
| 502 | def priv_value(self): |
| 503 | if self.name == "version": |
| 504 | return self.msg.version.int_version |
| 505 | elif self.name == "length" or self.name == "len": |
| 506 | return self.msg.length |
| 507 | else: |
| 508 | return self.java_type.format_value(self.member.value, pub_type=False) |
| 509 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 510 | |
| 511 | @property |
| 512 | def is_writeable(self): |
| 513 | return self.is_data and not self.name in model.write_blacklist[self.msg.name] |
| 514 | |
| 515 | def get_type_value_info(self, version): |
| 516 | return get_type_values(msg.c_name, version.int_version)[self.c_name] |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 517 | |
| 518 | @property |
| 519 | def length(self): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 520 | if hasattr(self.member, "length"): |
| 521 | return self.member.length |
| 522 | else: |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 523 | count, base = loxi_utils.type_dec_to_count_base(self.member.type) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 524 | return of_g.of_base_types[base]['bytes'] * count |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 525 | |
| 526 | @staticmethod |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 527 | def for_of_member(java_class, member): |
| 528 | if isinstance(member, OFPadMember): |
| 529 | return JavaMember(None, "", None, member) |
| 530 | else: |
| 531 | if member.name == 'len': |
| 532 | name = 'length' |
| 533 | else: |
| 534 | name = java_type.name_c_to_camel(member.name) |
| 535 | j_type = java_type.convert_to_jtype(java_class.c_name, member.name, member.oftype) |
| 536 | return JavaMember(java_class, name, j_type, member) |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 537 | |
| 538 | @property |
| 539 | def is_universal(self): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 540 | if not self.msg.c_name in of_g.unified: |
| 541 | print("%s not self.unified" % self.msg.c_name) |
| 542 | return False |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 543 | for version in of_g.unified[self.msg.c_name]: |
| 544 | if version == 'union' or version =='object_id': |
| 545 | continue |
| 546 | if 'use_version' in of_g.unified[self.msg.c_name][version]: |
| 547 | continue |
| 548 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 549 | if not self.member.name in (f['name'] for f in of_g.unified[self.msg.c_name][version]['members']): |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 550 | return False |
| 551 | return True |
| 552 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 553 | def __hash__(self): |
| 554 | return hash(self.name) |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 555 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 556 | def __eq__(self, other): |
| 557 | if other is None or type(self) != type(other): |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 558 | return False |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 559 | return (self.name,) == (other.name,) |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 560 | |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 561 | |
| 562 | ####################################################################### |
| 563 | ### Unit Test |
| 564 | ####################################################################### |
| 565 | |
| 566 | class JavaUnitTest(object): |
| 567 | def __init__(self, java_class): |
| 568 | self.java_class = java_class |
| 569 | self.data_file_name = "of{version}/{name}.data".format(version=java_class.version.of_version, |
| 570 | name=java_class.c_name[3:]) |
| 571 | @property |
| 572 | def package(self): |
| 573 | return self.java_class.package |
| 574 | |
| 575 | @property |
| 576 | def name(self): |
| 577 | return self.java_class.name + "Test" |
| 578 | |
| 579 | @property |
| 580 | def has_test_data(self): |
| 581 | return test_data.exists(self.data_file_name) |
| 582 | |
| 583 | @property |
| 584 | @memoize |
| 585 | def test_data(self): |
| 586 | return test_data.read(self.data_file_name) |
| 587 | |
| 588 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 589 | ####################################################################### |
| 590 | ### Enums |
| 591 | ####################################################################### |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 592 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 593 | class JavaEnum(object): |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 594 | def __init__(self, c_name, version_enum_map): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 595 | self.c_name = c_name |
| 596 | self.name = "OF" + java_type.name_c_to_caps_camel("_".join(c_name.split("_")[1:])) |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 597 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 598 | # Port_features has constants that start with digits |
| 599 | self.name_prefix = "PF_" if self.name == "OFPortFeatures" else "" |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 600 | |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 601 | self.version_enums = version_enum_map |
| 602 | |
| 603 | entry_name_version_value_map = OrderedDefaultDict(lambda: OrderedDict()) |
| 604 | for version, ir_enum in version_enum_map.items(): |
| 605 | for ir_entry in ir_enum.entries: |
| 606 | if "virtual" in ir_entry.params: |
| 607 | continue |
| 608 | entry_name_version_value_map[ir_entry.name][version] = ir_entry.value |
| 609 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 610 | self.entries = [ JavaEnumEntry(self, name, version_value_map) |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 611 | for (name, version_value_map) in entry_name_version_value_map.items() ] |
Andreas Wundsam | 4352653 | 2013-08-01 22:03:50 -0700 | [diff] [blame] | 612 | |
| 613 | self.entries = [ e for e in self.entries if e.name not in model.enum_entry_blacklist[self.name] ] |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 614 | self.package = "org.openflow.protocol" |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 615 | |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 616 | def wire_type(self, version): |
| 617 | ir_enum = self.version_enums[version] |
| 618 | if "wire_type" in ir_enum.params: |
| 619 | return java_type.convert_enum_wire_type_to_jtype(ir_enum.params["wire_type"]) |
| 620 | else: |
| 621 | return java_type.u8 |
| 622 | |
| 623 | @property |
| 624 | def versions(self): |
| 625 | return self.version_enums.keys() |
| 626 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 627 | @memoize |
| 628 | def entry_by_name(self, name): |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 629 | res = find(lambda e: e.name == name, self.entries) |
| 630 | if res: |
| 631 | return res |
| 632 | else: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 633 | raise KeyError("Enum %s: no entry with name %s" % (self.name, name)) |
| 634 | |
| 635 | @memoize |
| 636 | def entry_by_c_name(self, name): |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 637 | res = find(lambda e: e.c_name == name, self.entries) |
| 638 | if res: |
| 639 | return res |
| 640 | else: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 641 | raise KeyError("Enum %s: no entry with c_name %s" % (self.name, name)) |
| 642 | |
| 643 | @memoize |
| 644 | def entry_by_version_value(self, version, value): |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 645 | res = find(lambda e: e.values[version] == value if version in e.values else False, self.entries) |
| 646 | if res: |
| 647 | return res |
| 648 | else: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 649 | raise KeyError("Enum %s: no entry with version %s, value %s" % (self.name, version, value)) |
| 650 | |
| 651 | # values: Map JavaVersion->Value |
| 652 | class JavaEnumEntry(object): |
| 653 | def __init__(self, enum, name, values): |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 654 | self.enum = enum |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 655 | self.name = enum.name_prefix + "_".join(name.split("_")[1:]).upper() |
| 656 | self.values = values |
| 657 | |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 658 | def has_value(self, version): |
| 659 | return version in self.values |
| 660 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 661 | def value(self, version): |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 662 | return self.values[version] |
| 663 | |
| 664 | def format_value(self, version): |
| 665 | res = self.enum.wire_type(version).format_value(self.values[version]) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 666 | return res |
| 667 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 668 | def all_values(self, versions, not_present=None): |
| 669 | return [ self.values[version] if version in self.values else not_present for version in versions ] |