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 | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 39 | from loxi_globals import OFVersions |
| 40 | import loxi_globals |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 41 | from loxi_ir import * |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 42 | import loxi_utils.loxi_utils as loxi_utils |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 43 | import test_data |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 44 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 45 | import java_gen.java_type as java_type |
Andreas Wundsam | e0d52be | 2013-08-22 07:52:13 -0700 | [diff] [blame] | 46 | from java_gen.java_type import erase_type_annotation |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 47 | |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 48 | logger = logging.getLogger(__name__) |
| 49 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 50 | class JavaModel(object): |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 51 | # registry for enums that should not be generated |
| 52 | # set(${java_enum_name}) |
Andreas Wundsam | acd57d5 | 2013-10-18 17:35:01 -0700 | [diff] [blame] | 53 | enum_blacklist = set(("OFDefinitions", "OFPortNo", "OFVlanId", "OFGroup")) |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 54 | # registry for enum *entry* that should not be generated |
| 55 | # map: ${java_enum_name} -> set(${java_entry_entry_name}) |
Andreas Wundsam | 4352653 | 2013-08-01 22:03:50 -0700 | [diff] [blame] | 56 | enum_entry_blacklist = defaultdict(lambda: set(), OFFlowWildcards=set([ "NW_DST_BITS", "NW_SRC_BITS", "NW_SRC_SHIFT", "NW_DST_SHIFT" ])) |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 57 | |
| 58 | # registry of interfaces that should not be generated |
| 59 | # set(java_names) |
Andreas Wundsam | be168f7 | 2013-08-03 22:49:35 -0700 | [diff] [blame] | 60 | # OFUint structs are there for god-knows what in loci. We certainly don't need them. |
| 61 | interface_blacklist = set( ("OFUint8", "OFUint32",)) |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 62 | # registry of interface properties that should not be generated |
| 63 | # map: $java_type -> set(java_name_property) |
Rich Lane | 97fd13d | 2013-11-13 11:25:48 -0800 | [diff] [blame] | 64 | read_blacklist = defaultdict(lambda: set(), |
| 65 | OFExperimenter=set(('data','subtype')), |
| 66 | OFActionExperimenter=set(('data',)), |
| 67 | OFExperimenterStatsRequest=set(('data','subtype')), |
Rich Lane | f33f32f | 2013-11-18 15:49:54 -0800 | [diff] [blame] | 68 | OFExperimenterStatsReply=set(('data','subtype')), |
| 69 | OFInstructionExperimenter=set(('data',))) |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 70 | # map: $java_type -> set(java_name_property) |
Andreas Wundsam | b1da3f6 | 2013-09-04 18:46:03 -0700 | [diff] [blame] | 71 | write_blacklist = defaultdict(lambda: set(), OFOxm=set(('typeLen',)), OFAction=set(('type',)), OFInstruction=set(('type',)), OFFlowMod=set(('command', )), OFExperimenter=set(('data','subtype')), OFActionExperimenter=set(('data',))) |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 72 | # interfaces that are virtual |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 73 | virtual_interfaces = set(['OFOxm', 'OFInstruction', 'OFFlowMod', 'OFBsnVport' ]) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 74 | |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 75 | # Registry of nullable properties: |
| 76 | # ${java_class_name} -> set(${java_property_name}) |
| 77 | nullable_map = defaultdict(lambda: set(), |
| 78 | ) |
| 79 | |
| 80 | # represents a subgroup of a bitmask enum that is actualy a normal enumerable within a masked part of the enum |
| 81 | # e.g., the flags STP.* in OF1.0 port state are bit mask entries, but instead enumerables according to the mask "STP_MASK" |
| 82 | # name: a name for the group |
| 83 | # mask: java name of the enum entry that defines the mask |
| 84 | # members: set of names of the members of the group |
Andreas Wundsam | c1acfcc | 2013-09-20 13:10:20 -0700 | [diff] [blame] | 85 | MaskedEnumGroup = namedtuple("MaskedEnumGroup", ("name", "mask", "members")) |
| 86 | |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 87 | # registry of MaskedEnumGroups (see above). |
| 88 | # map: ${java_enum_name}: tuple(MaskedEnumGroup) |
Andreas Wundsam | c1acfcc | 2013-09-20 13:10:20 -0700 | [diff] [blame] | 89 | masked_enum_groups = defaultdict(lambda: (), |
Andreas Wundsam | aebe518 | 2013-09-24 13:01:07 -0700 | [diff] [blame] | 90 | OFPortState = (MaskedEnumGroup("stp_flags", mask="STP_MASK", members=set(("STP_LISTEN", "STP_LEARN", "STP_FORWARD", "STP_BLOCK"))), ), |
| 91 | OFConfigFlags = ( |
| 92 | MaskedEnumGroup("frag_flags", mask="FRAG_MASK", members=set(("FRAG_NORMAL", "FRAG_DROP", "FRAG_REASM"))), |
Andreas Wundsam | 376cef5 | 2013-09-24 13:52:18 -0700 | [diff] [blame] | 93 | ), |
| 94 | OFTableConfig = ( |
| 95 | MaskedEnumGroup("table_miss_flags", mask="TABLE_MISS_MASK", members=set(("TABLE_MISS_CONTROLLER", "TABLE_MISS_CONTINUE", "TABLE_MISS_DROP"))), |
| 96 | ), |
Andreas Wundsam | 057540b | 2013-11-19 16:51:36 -0800 | [diff] [blame] | 97 | OFGetConfigReply = ( |
| 98 | MaskedEnumGroup("flags", mask="OFP_FRAG_MASK", members=set(("FRAG_NORMAL", "FRAG_DROP", "FRAG_REASM"))), |
| 99 | ), |
| 100 | OFSetConfig = ( |
| 101 | MaskedEnumGroup("flags", mask="OFP_FRAG_MASK", members=set(("FRAG_NORMAL", "FRAG_DROP", "FRAG_REASM"))), |
| 102 | ), |
Andreas Wundsam | c1acfcc | 2013-09-20 13:10:20 -0700 | [diff] [blame] | 103 | ) |
| 104 | |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 105 | # represents a metadata property associated with an EnumClass |
| 106 | # name: |
Rob Vaterlaus | 6035bf5 | 2013-09-17 19:32:38 -0700 | [diff] [blame] | 107 | class OFEnumPropertyMetadata(namedtuple("OFEnumPropertyMetadata", ("name", "type", "value"))): |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 108 | """ |
| 109 | represents a metadata property associated with an Enum Class |
| 110 | @param name name of metadata property |
| 111 | @param type java_type instance describing the type |
| 112 | @value: Generator function f(entry) that generates the value |
| 113 | """ |
Andreas Wundsam | 8ec3bcc | 2013-09-16 19:44:00 -0700 | [diff] [blame] | 114 | @property |
| 115 | def variable_name(self): |
| 116 | return self.name[0].lower() + self.name[1:] |
| 117 | |
| 118 | @property |
| 119 | def getter_name(self): |
Rob Vaterlaus | bda9ce6 | 2013-09-17 14:45:48 -0700 | [diff] [blame] | 120 | prefix = "is" if self.type == java_type.boolean else "get" |
| 121 | return prefix+self.name |
Andreas Wundsam | 8ec3bcc | 2013-09-16 19:44:00 -0700 | [diff] [blame] | 122 | |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 123 | """ Metadata container. """ |
Rob Vaterlaus | 6035bf5 | 2013-09-17 19:32:38 -0700 | [diff] [blame] | 124 | OFEnumMetadata = namedtuple("OFEnumMetadata", ("properties", "to_string")) |
| 125 | |
Andreas Wundsam | 8ec3bcc | 2013-09-16 19:44:00 -0700 | [diff] [blame] | 126 | def gen_port_speed(enum_entry): |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 127 | """ Generator function for OFortFeatures.PortSpeed""" |
Andreas Wundsam | 8ec3bcc | 2013-09-16 19:44:00 -0700 | [diff] [blame] | 128 | splits = enum_entry.name.split("_") |
| 129 | if len(splits)>=2: |
| 130 | m = re.match(r'\d+[MGTP]B', splits[1]) |
| 131 | if m: |
| 132 | return "PortSpeed.SPEED_{}".format(splits[1]) |
| 133 | return "PortSpeed.SPEED_NONE"; |
| 134 | |
Rob Vaterlaus | bda9ce6 | 2013-09-17 14:45:48 -0700 | [diff] [blame] | 135 | def gen_stp_state(enum_entry): |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 136 | """ Generator function for OFPortState.StpState""" |
Rob Vaterlaus | bda9ce6 | 2013-09-17 14:45:48 -0700 | [diff] [blame] | 137 | splits = enum_entry.name.split("_") |
| 138 | if len(splits)>=1: |
| 139 | if splits[0] == "STP": |
| 140 | return "true" |
| 141 | return "false" |
| 142 | |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 143 | # registry for metadata properties for enums |
| 144 | # map: ${java_enum_name}: OFEnumMetadata |
Rob Vaterlaus | 6035bf5 | 2013-09-17 19:32:38 -0700 | [diff] [blame] | 145 | enum_metadata_map = defaultdict(lambda: JavaModel.OFEnumMetadata((), None), |
Rob Vaterlaus | a049dee | 2013-09-18 16:18:37 -0700 | [diff] [blame] | 146 | OFPortFeatures = OFEnumMetadata((OFEnumPropertyMetadata("PortSpeed", java_type.port_speed, gen_port_speed),), None), |
| 147 | OFPortState = OFEnumMetadata((OFEnumPropertyMetadata("StpState", java_type.boolean, gen_stp_state),), None), |
Andreas Wundsam | 8ec3bcc | 2013-09-16 19:44:00 -0700 | [diff] [blame] | 148 | ) |
| 149 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 150 | @property |
| 151 | @memoize |
| 152 | def versions(self): |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 153 | return OrderedSet( JavaOFVersion(ir_version) for ir_version in OFVersions.target_versions) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 154 | |
| 155 | @property |
| 156 | @memoize |
| 157 | def interfaces(self): |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 158 | interfaces = [ JavaOFInterface(ir_class) for ir_class in loxi_globals.unified.classes ] |
Andreas Wundsam | be168f7 | 2013-08-03 22:49:35 -0700 | [diff] [blame] | 159 | interfaces = [ i for i in interfaces if i.name not in self.interface_blacklist ] |
| 160 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 161 | return interfaces |
| 162 | |
Andreas Wundsam | e0d52be | 2013-08-22 07:52:13 -0700 | [diff] [blame] | 163 | @memoize |
| 164 | def interface_by_name(self, name): |
| 165 | return find(lambda i: erase_type_annotation(i.name) == erase_type_annotation(name), self.interfaces) |
| 166 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 167 | @property |
| 168 | @memoize |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 169 | def all_classes(self): |
| 170 | return [clazz for interface in self.interfaces for clazz in interface.versioned_classes] |
| 171 | |
| 172 | @property |
| 173 | @memoize |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 174 | def enums(self): |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 175 | name_version_enum_map = OrderedDefaultDict(lambda: OrderedDict()) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 176 | |
| 177 | for version in self.versions: |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 178 | logger.info("version: {}".format(version.ir_version)) |
| 179 | of_protocol = loxi_globals.ir[version.ir_version] |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 180 | for enum in of_protocol.enums: |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 181 | name_version_enum_map[enum.name][version] = enum |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 182 | |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 183 | enums = [ JavaEnum(name, version_enum_map) for name, version_enum_map, |
| 184 | in name_version_enum_map.items() ] |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 185 | |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 186 | # inelegant - need java name here |
| 187 | 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] | 188 | return enums |
| 189 | |
| 190 | @memoize |
| 191 | def enum_by_name(self, name): |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 192 | res = find(lambda e: e.name == name, self.enums) |
| 193 | if not res: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 194 | raise KeyError("Could not find enum with name %s" % name) |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 195 | return res |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 196 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 197 | @property |
| 198 | @memoize |
Andreas Wundsam | e0d52be | 2013-08-22 07:52:13 -0700 | [diff] [blame] | 199 | def of_factories(self): |
Yotam Harchol | 791e488 | 2013-09-05 16:32:56 -0700 | [diff] [blame] | 200 | prefix = "org.projectfloodlight.openflow.protocol" |
Andreas Wundsam | e0d52be | 2013-08-22 07:52:13 -0700 | [diff] [blame] | 201 | |
| 202 | factories = OrderedDict() |
| 203 | |
| 204 | sub_factory_classes = ("OFAction", "OFInstruction", "OFMeterBand", "OFOxm", "OFQueueProp") |
| 205 | for base_class in sub_factory_classes: |
| 206 | package = base_class[2:].lower() |
| 207 | remove_prefix = base_class[2].lower() + base_class[3:] |
| 208 | |
| 209 | # HACK need to have a better way to deal with parameterized base classes |
| 210 | annotated_base_class = base_class + "<?>" if base_class == "OFOxm" else base_class |
| 211 | |
| 212 | factories[base_class] = OFFactory(package="%s.%s" % (prefix, package), |
Andreas Wundsam | cb17b23 | 2013-09-28 19:05:36 -0700 | [diff] [blame] | 213 | name=base_class + "s", members=[], remove_prefix=remove_prefix, base_class=annotated_base_class, sub_factories={}, xid_generator=False) |
Andreas Wundsam | e0d52be | 2013-08-22 07:52:13 -0700 | [diff] [blame] | 214 | |
| 215 | factories[""] = OFFactory( |
| 216 | package=prefix, |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 217 | name="OFFactory", |
Andreas Wundsam | e0d52be | 2013-08-22 07:52:13 -0700 | [diff] [blame] | 218 | remove_prefix="", |
| 219 | members=[], base_class="OFMessage", sub_factories=OrderedDict( |
Andreas Wundsam | cb17b23 | 2013-09-28 19:05:36 -0700 | [diff] [blame] | 220 | ("{}{}s".format(n[2].lower(), n[3:]), "{}s".format(n)) for n in sub_factory_classes ), |
| 221 | xid_generator=True) |
Andreas Wundsam | e0d52be | 2013-08-22 07:52:13 -0700 | [diff] [blame] | 222 | |
| 223 | for i in self.interfaces: |
| 224 | for n, factory in factories.items(): |
| 225 | if n == "": |
| 226 | factory.members.append(i) |
| 227 | break |
| 228 | else: |
| 229 | super_class = self.interface_by_name(n) |
| 230 | if i.is_instance_of(super_class): |
| 231 | factory.members.append(i) |
| 232 | break |
| 233 | return factories.values() |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 234 | |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 235 | @memoize |
| 236 | def factory_of(self, interface): |
| 237 | for factory in self.of_factories: |
| 238 | if interface in factory.members: |
| 239 | return factory |
| 240 | return None |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 241 | |
| 242 | def generate_class(self, clazz): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 243 | """ return wether or not to generate implementation class clazz. |
| 244 | Now true for everything except OFTableModVer10. |
| 245 | @param clazz JavaOFClass instance |
| 246 | """ |
Andreas Wundsam | 4352653 | 2013-08-01 22:03:50 -0700 | [diff] [blame] | 247 | if clazz.interface.name.startswith("OFMatchV"): |
| 248 | return True |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 249 | elif clazz.name == "OFTableModVer10": |
| 250 | # tablemod ver 10 is a hack and has no oftype defined |
| 251 | return False |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 252 | if loxi_utils.class_is_message(clazz.interface.c_name): |
| 253 | return True |
| 254 | if loxi_utils.class_is_oxm(clazz.interface.c_name): |
| 255 | return True |
Andreas Wundsam | 4352653 | 2013-08-01 22:03:50 -0700 | [diff] [blame] | 256 | if loxi_utils.class_is_action(clazz.interface.c_name): |
| 257 | return True |
| 258 | if loxi_utils.class_is_instruction(clazz.interface.c_name): |
| 259 | return True |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 260 | else: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 261 | return True |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 262 | |
Andreas Wundsam | 83aebd3 | 2013-11-19 10:43:50 -0800 | [diff] [blame] | 263 | @property |
| 264 | @memoize |
| 265 | def oxm_map(self): |
| 266 | OxmMapEntry = namedtuple("OxmMapEntry", ["type_name", "value", "masked" ]) |
| 267 | return OrderedDict( (oxm.name, OxmMapEntry(type_name=oxm.member_by_name("value").java_type.public_type, |
| 268 | value=re.sub(r'^of_oxm_', r'', re.sub(r'_masked$', r'', oxm.ir_class.name)).upper(), |
| 269 | masked=oxm.ir_class.name.endswith("_masked"))) |
| 270 | for oxm in self.interfaces if oxm.ir_class.is_subclassof("of_oxm") ) |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 271 | |
Andreas Wundsam | cb17b23 | 2013-09-28 19:05:36 -0700 | [diff] [blame] | 272 | class OFFactory(namedtuple("OFFactory", ("package", "name", "members", "remove_prefix", "base_class", "sub_factories", "xid_generator"))): |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 273 | @property |
| 274 | def factory_classes(self): |
| 275 | return [ OFFactoryClass( |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 276 | package="org.projectfloodlight.openflow.protocol.ver{}".format(version.dotless_version), |
| 277 | name="{}Ver{}".format(self.name, version.dotless_version), |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 278 | interface=self, |
| 279 | version=version |
| 280 | ) for version in model.versions ] |
| 281 | |
Andreas Wundsam | e0d52be | 2013-08-22 07:52:13 -0700 | [diff] [blame] | 282 | def method_name(self, member, builder=True): |
| 283 | n = member.variable_name |
| 284 | if n.startswith(self.remove_prefix): |
| 285 | n = n[len(self.remove_prefix):] |
| 286 | n = n[0].lower() + n[1:] |
| 287 | if builder: |
| 288 | return "build" + n[0].upper() + n[1:] |
| 289 | else: |
| 290 | return n |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 291 | |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 292 | def of_version(self, version): |
| 293 | for fc in self.factory_classes: |
| 294 | if fc.version == version: |
| 295 | return fc |
| 296 | return None |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 297 | |
| 298 | OFGenericClass = namedtuple("OFGenericClass", ("package", "name")) |
Andreas Wundsam | e0d52be | 2013-08-22 07:52:13 -0700 | [diff] [blame] | 299 | class OFFactoryClass(namedtuple("OFFactoryClass", ("package", "name", "interface", "version"))): |
| 300 | @property |
| 301 | def base_class(self): |
| 302 | return self.interface.base_class |
| 303 | |
| 304 | @property |
| 305 | def versioned_base_class(self): |
| 306 | base_class_interface = model.interface_by_name(self.interface.base_class) |
| 307 | if base_class_interface and base_class_interface.has_version(self.version): |
| 308 | return base_class_interface.versioned_class(self.version) |
| 309 | else: |
| 310 | return None |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 311 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 312 | model = JavaModel() |
| 313 | |
| 314 | ####################################################################### |
| 315 | ### OFVersion |
| 316 | ####################################################################### |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 317 | |
| 318 | class JavaOFVersion(object): |
| 319 | """ Models a version of OpenFlow. contains methods to convert the internal |
| 320 | Loxi version to a java constant / a string """ |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 321 | def __init__(self, ir_version): |
| 322 | assert isinstance(ir_version, OFVersion) |
| 323 | self.ir_version = ir_version |
| 324 | self.int_version = self.ir_version.wire_version |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 325 | |
| 326 | @property |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 327 | def dotless_version(self): |
| 328 | return self.ir_version.version.replace(".", "") |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 329 | |
| 330 | @property |
| 331 | def constant_version(self): |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 332 | return "OF_" + self.dotless_version |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 333 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 334 | def __repr__(self): |
| 335 | return "JavaOFVersion(%d)" % self.int_version |
| 336 | |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 337 | def __str__(self): |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 338 | return self.ir_version.version |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 339 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 340 | def __hash__(self): |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 341 | return hash(self.ir_version) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 342 | |
| 343 | def __eq__(self, other): |
| 344 | if other is None or type(self) != type(other): |
| 345 | return False |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 346 | return (self.ir_version,) == (other.ir_version,) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 347 | |
| 348 | ####################################################################### |
| 349 | ### Interface |
| 350 | ####################################################################### |
| 351 | |
| 352 | class JavaOFInterface(object): |
| 353 | """ Models an OpenFlow Message class for the purpose of the java class. |
| 354 | Version agnostic, in contrast to the loxi_ir python model. |
| 355 | """ |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 356 | def __init__(self, ir_class): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 357 | """" |
| 358 | @param c_name: loxi style name (e.g., of_flow_add) |
| 359 | @param version_map map of { JavaOFVersion: OFClass (from loxi_ir) } |
| 360 | """ |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 361 | self.ir_class = ir_class |
| 362 | self.c_name = ir_class.name |
| 363 | self.version_map = { JavaOFVersion(v): c for v,c in ir_class.version_classes.items() } |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 364 | # name: the Java Type name, e.g., OFFlowAdd |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 365 | self.name = java_type.name_c_to_caps_camel(self.c_name) if self.c_name != "of_header" else "OFMessage" |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 366 | # variable_name name to use for variables of this type. i.e., flowAdd |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 367 | self.variable_name = self.name[2].lower() + self.name[3:] |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 368 | self.title_name = self.variable_name[0].upper() + self.variable_name[1:] |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 369 | # name for use in constants: FLOW_ADD |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 370 | self.constant_name = self.c_name.upper().replace("OF_", "") |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 371 | |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 372 | pck_suffix, parent_interface, self.type_annotation = self.class_info() |
Yotam Harchol | 791e488 | 2013-09-05 16:32:56 -0700 | [diff] [blame] | 373 | self.package = "org.projectfloodlight.openflow.protocol.%s" % pck_suffix if pck_suffix else "org.projectfloodlight.openflow.protocol" |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 374 | if self.name != parent_interface: |
| 375 | self.parent_interface = parent_interface |
| 376 | else: |
| 377 | self.parent_interface = None |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 378 | |
Andreas Wundsam | a705c6b | 2013-09-04 11:21:09 -0700 | [diff] [blame] | 379 | @property |
| 380 | @memoize |
| 381 | def all_parent_interfaces(self): |
| 382 | return [ "OFObject" ] + \ |
| 383 | ([ self.parent_interface ] if self.parent_interface else [] )+ \ |
| 384 | self.additional_parent_interfaces |
| 385 | @property |
| 386 | @memoize |
| 387 | def additional_parent_interfaces(self): |
| 388 | if loxi_utils.class_is_message(self.c_name) and not self.is_virtual: |
| 389 | m = re.match(r'(.*)Request$', self.name) |
| 390 | if m: |
| 391 | reply_name = m.group(1) + "Reply" |
| 392 | if model.interface_by_name(reply_name): |
| 393 | return ["OFRequest<%s>" % reply_name ] |
| 394 | return [] |
| 395 | |
| 396 | |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 397 | def is_instance_of(self, other_class): |
| 398 | if self == other_class: |
| 399 | return True |
| 400 | parent = self.super_class |
| 401 | if parent is None: |
| 402 | return False |
| 403 | else: |
| 404 | return parent.is_instance_of(other_class) |
| 405 | |
| 406 | @property |
| 407 | def super_class(self): |
| 408 | if not self.parent_interface: |
| 409 | return None |
| 410 | else: |
| 411 | return model.interface_by_name(self.parent_interface) |
| 412 | |
| 413 | |
| 414 | def inherited_declaration(self, type_spec="?"): |
| 415 | if self.type_annotation: |
| 416 | return "%s<%s>" % (self.name, type_spec) |
| 417 | else: |
| 418 | return "%s" % self.name |
| 419 | |
| 420 | @property |
| 421 | def type_variable(self): |
| 422 | if self.type_annotation: |
| 423 | return "<T>" |
| 424 | else: |
| 425 | return ""; |
| 426 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 427 | def class_info(self): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 428 | """ return tuple of (package_prefix, parent_class) for the current JavaOFInterface""" |
| 429 | # FIXME: This duplicates inheritance information that is now available in the loxi_ir |
| 430 | # model (note, that the loxi model is on versioned classes). Should check/infer the |
| 431 | # inheritance information from the versioned lox_ir classes. |
Andreas Wundsam | d6b0cb0 | 2013-09-28 18:55:56 -0700 | [diff] [blame] | 432 | if re.match(r'OFStatsRequest$', self.name): |
| 433 | return ("", "OFMessage", "T extends OFStatsReply") |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 434 | elif self.ir_class.is_subclassof('of_stats_request'): |
Andreas Wundsam | 2c0a2d7 | 2013-11-15 15:16:36 -0800 | [diff] [blame] | 435 | if self.ir_class.is_subclassof('of_bsn_stats_request'): |
| 436 | return ("", "OFBsnStatsRequest", None) |
| 437 | elif self.ir_class.is_subclassof('of_experimenter_stats_request'): |
| 438 | return ("", "OFExperimenterStatsRequest", None) |
| 439 | else: |
| 440 | return ("", "OFStatsRequest<{}>".format(re.sub(r'Request$', 'Reply', self.name)), None) |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 441 | elif self.ir_class.is_subclassof('of_stats_reply'): |
Andreas Wundsam | 2c0a2d7 | 2013-11-15 15:16:36 -0800 | [diff] [blame] | 442 | if self.ir_class.is_subclassof('of_bsn_stats_reply'): |
| 443 | return ("", "OFBsnStatsReply", None) |
| 444 | elif self.ir_class.is_subclassof('of_experimenter_stats_reply'): |
| 445 | return ("", "OFExperimenterStatsReply", None) |
| 446 | else: |
| 447 | return ("", "OFStatsReply", None) |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 448 | elif self.ir_class.is_subclassof('of_error_msg'): |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 449 | return ("", "OFErrorMsg", None) |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 450 | elif self.ir_class.is_subclassof('of_flow_mod'): |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 451 | return ("", "OFFlowMod", None) |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 452 | elif self.ir_class.is_subclassof('of_group_mod'): |
| 453 | return ("", "OFGroupMod", None) |
| 454 | elif self.ir_class.is_subclassof('of_bsn_header'): |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 455 | return ("", "OFBsnHeader", None) |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 456 | elif self.ir_class.is_subclassof('of_nicira_header'): |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 457 | return ("", "OFNiciraHeader", None) |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 458 | elif self.ir_class.is_subclassof('of_experimenter'): |
Andreas Wundsam | b1da3f6 | 2013-09-04 18:46:03 -0700 | [diff] [blame] | 459 | return ("", "OFExperimenter", None) |
Andreas Wundsam | 4352653 | 2013-08-01 22:03:50 -0700 | [diff] [blame] | 460 | elif re.match(r'OFMatch.*', self.name): |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 461 | return ("", "Match", None) |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 462 | elif self.ir_class.is_message: |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 463 | return ("", "OFMessage", None) |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 464 | elif self.ir_class.is_action: |
| 465 | if self.ir_class.is_subclassof('of_action_bsn'): |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 466 | return ("action", "OFActionBsn", None) |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 467 | elif self.ir_class.is_subclassof('of_action_nicira'): |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 468 | return ("action", "OFActionNicira", None) |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 469 | elif self.ir_class.is_subclassof('of_action_experimenter'): |
Andreas Wundsam | b1da3f6 | 2013-09-04 18:46:03 -0700 | [diff] [blame] | 470 | return ("action", "OFActionExperimenter", None) |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 471 | else: |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 472 | return ("action", "OFAction", None) |
Rich Lane | ea35686 | 2013-12-09 18:02:36 -0800 | [diff] [blame^] | 473 | elif self.ir_class.is_instanceof("of_action_id"): |
| 474 | if self.ir_class.is_subclassof('of_action_id_bsn'): |
| 475 | return ("actionid", "OFActionIdBsn", None) |
| 476 | elif self.ir_class.is_subclassof('of_action_id_nicira'): |
| 477 | return ("actionid", "OFActionIdNicira", None) |
| 478 | elif self.ir_class.is_subclassof('of_action_id_experimenter'): |
| 479 | return ("actionid", "OFActionIdExperimenter", None) |
| 480 | else: |
| 481 | return ("actionid", "OFActionId", None) |
Rich Lane | f33f32f | 2013-11-18 15:49:54 -0800 | [diff] [blame] | 482 | elif self.ir_class.is_instruction: |
| 483 | if self.ir_class.is_subclassof('of_instruction_bsn'): |
| 484 | return ("instruction", "OFInstructionBsn", None) |
| 485 | elif self.ir_class.is_subclassof('of_instruction_experimenter'): |
| 486 | return ("instruction", "OFInstructionExperimenter", None) |
| 487 | else: |
| 488 | return ("instruction", "OFInstruction", None) |
Rich Lane | ea35686 | 2013-12-09 18:02:36 -0800 | [diff] [blame^] | 489 | elif self.ir_class.is_instanceof('of_instruction_id'): |
| 490 | if self.ir_class.is_subclassof('of_instruction_id_bsn'): |
| 491 | return ("instructionid", "OFInstructionIdBsn", None) |
| 492 | elif self.ir_class.is_subclassof('of_instruction_id_experimenter'): |
| 493 | return ("instructionid", "OFInstructionIdExperimenter", None) |
| 494 | else: |
| 495 | return ("instructionid", "OFInstructionId", None) |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 496 | elif re.match(r'OFBsnVport.+$', self.name): |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 497 | return ("", "OFBsnVport", None) |
| 498 | elif self.name == "OFOxm": |
| 499 | return ("oxm", None, "T extends OFValueType<T>") |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 500 | elif loxi_utils.class_is_oxm(self.c_name): |
Andreas Wundsam | 83aebd3 | 2013-11-19 10:43:50 -0800 | [diff] [blame] | 501 | if self.member_by_name("value") is not None: |
| 502 | return ("oxm", "OFOxm<%s>" % self.member_by_name("value").java_type.public_type, None) |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 503 | else: |
| 504 | return ("oxm", "OFOxm", None) |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 505 | elif loxi_utils.class_is_instruction(self.c_name): |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 506 | return ("instruction", "OFInstruction", None) |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 507 | elif loxi_utils.class_is_meter_band(self.c_name): |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 508 | return ("meterband", "OFMeterBand", None) |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 509 | elif loxi_utils.class_is_queue_prop(self.c_name): |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 510 | return ("queueprop", "OFQueueProp", None) |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 511 | elif loxi_utils.class_is_hello_elem(self.c_name): |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 512 | return ("", "OFHelloElem", None) |
Rich Lane | 2684756 | 2013-10-08 13:44:47 -0700 | [diff] [blame] | 513 | elif loxi_utils.class_is_table_feature_prop(self.c_name): |
| 514 | return ("", "OFTableFeatureProp", None) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 515 | else: |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 516 | return ("", None, None) |
| 517 | |
| 518 | @property |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 519 | |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 520 | @memoize |
| 521 | def writeable_members(self): |
| 522 | return [ m for m in self.members if m.is_writeable ] |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 523 | |
Andreas Wundsam | cb17b23 | 2013-09-28 19:05:36 -0700 | [diff] [blame] | 524 | @memoize |
| 525 | def member_by_name(self, name): |
| 526 | return find(lambda m: m.name == name, self.members) |
| 527 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 528 | @property |
| 529 | @memoize |
| 530 | def members(self): |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 531 | return self.ir_model_members + self.virtual_members |
| 532 | |
| 533 | @property |
| 534 | @memoize |
| 535 | def ir_model_members(self): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 536 | """return a list of all members to be exposed by this interface. Corresponds to |
| 537 | the union of the members of the vesioned classes without length, fieldlength |
| 538 | and pads (those are handled automatically during (de)serialization and not exposed""" |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 539 | all_versions = [] |
| 540 | member_map = collections.OrderedDict() |
| 541 | |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 542 | member_version_map = {} |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 543 | for (version, of_class) in self.version_map.items(): |
| 544 | for of_member in of_class.members: |
| 545 | if isinstance(of_member, OFLengthMember) or \ |
| 546 | isinstance(of_member, OFFieldLengthMember) or \ |
| 547 | isinstance(of_member, OFPadMember): |
| 548 | continue |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 549 | java_member = JavaMember.for_of_member(self, of_member) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 550 | if of_member.name not in member_map: |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 551 | member_map[of_member.name] = java_member |
| 552 | member_version_map[of_member.name] = version |
| 553 | else: |
| 554 | existing = member_map[of_member.name] |
| 555 | |
| 556 | if existing.java_type.public_type != java_member.java_type.public_type: |
| 557 | raise Exception( |
| 558 | "Error constructing interface {}: type signatures do not match up between versions.\n" |
| 559 | " Member Name: {}\n" |
| 560 | " Existing: Version={}, Java={}, IR={}\n" |
| 561 | " New: Version={}, Java={}, IR={}" |
| 562 | .format(self.name, existing.name, |
| 563 | member_version_map[of_member.name], existing.java_type.public_type, existing.member.oftype, |
| 564 | version, java_member.java_type.public_type, java_member.member.oftype) |
| 565 | ) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 566 | |
Andreas Wundsam | b1da3f6 | 2013-09-04 18:46:03 -0700 | [diff] [blame] | 567 | return tuple(m for m in member_map.values() if m.name not in model.read_blacklist[self.name]) |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 568 | |
| 569 | @property |
| 570 | def virtual_members(self): |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 571 | virtual_members = [] |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 572 | if self.name == "OFOxm": |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 573 | virtual_members += [ |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 574 | JavaVirtualMember(self, "value", java_type.generic_t), |
| 575 | JavaVirtualMember(self, "mask", java_type.generic_t), |
| 576 | JavaVirtualMember(self, "matchField", java_type.make_match_field_jtype("T")), |
| 577 | JavaVirtualMember(self, "masked", java_type.boolean), |
Andreas Wundsam | 661a222 | 2013-11-05 17:18:59 -0800 | [diff] [blame] | 578 | JavaVirtualMember(self, "canonical", java_type.make_oxm_jtype("T")) |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 579 | ] |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 580 | elif self.ir_class.is_subclassof("of_oxm"): |
Andreas Wundsam | 83aebd3 | 2013-11-19 10:43:50 -0800 | [diff] [blame] | 581 | value = find(lambda m: m.name=="value", self.ir_model_members) |
| 582 | if value: |
| 583 | field_type = java_type.make_match_field_jtype(value.java_type.public_type) |
| 584 | else: |
| 585 | field_type = java_type.make_match_field_jtype() |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 586 | |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 587 | virtual_members += [ |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 588 | JavaVirtualMember(self, "matchField", field_type), |
| 589 | JavaVirtualMember(self, "masked", java_type.boolean), |
Andreas Wundsam | 83aebd3 | 2013-11-19 10:43:50 -0800 | [diff] [blame] | 590 | JavaVirtualMember(self, "canonical", java_type.make_oxm_jtype(value.java_type.public_type), |
Andreas Wundsam | 661a222 | 2013-11-05 17:18:59 -0800 | [diff] [blame] | 591 | custom_template=lambda builder: "OFOxm{}_getCanonical.java".format(".Builder" if builder else "")), |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 592 | ] |
| 593 | if not find(lambda x: x.name == "mask", self.ir_model_members): |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 594 | virtual_members.append( |
| 595 | JavaVirtualMember(self, "mask", find(lambda x: x.name == "value", self.ir_model_members).java_type)) |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 596 | |
| 597 | if not find(lambda m: m.name == "version", self.ir_model_members): |
| 598 | virtual_members.append(JavaVirtualMember(self, "version", java_type.of_version)) |
| 599 | |
| 600 | return tuple(virtual_members) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 601 | |
| 602 | @property |
| 603 | @memoize |
| 604 | def is_virtual(self): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 605 | """ Is this interface virtual. If so, do not generate a builder interface """ |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 606 | 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] | 607 | |
| 608 | @property |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 609 | def is_universal(self): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 610 | """ Is this interface universal, i.e., does it exist in all OF versions? """ |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 611 | return len(self.all_versions) == len(model.versions) |
| 612 | |
| 613 | @property |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 614 | @memoize |
| 615 | def all_versions(self): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 616 | """ return list of all versions that this interface exists in """ |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 617 | return self.version_map.keys() |
| 618 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 619 | def has_version(self, version): |
| 620 | return version in self.version_map |
| 621 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 622 | def versioned_class(self, version): |
| 623 | return JavaOFClass(self, version, self.version_map[version]) |
| 624 | |
| 625 | @property |
| 626 | @memoize |
| 627 | def versioned_classes(self): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 628 | return [ self.versioned_class(version) for version in self.all_versions ] |
| 629 | |
| 630 | ####################################################################### |
| 631 | ### (Versioned) Classes |
| 632 | ####################################################################### |
| 633 | |
| 634 | class JavaOFClass(object): |
| 635 | """ Models an OpenFlow Message class for the purpose of the java class. |
Andreas Wundsam | d40ddbf | 2013-07-25 15:40:47 -0700 | [diff] [blame] | 636 | Version specific child of a JavaOFInterface |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 637 | """ |
| 638 | def __init__(self, interface, version, ir_class): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 639 | """ |
| 640 | @param interface JavaOFInterface instance of the parent interface |
| 641 | @param version JavaOFVersion |
| 642 | @param ir_class OFClass from loxi_ir |
| 643 | """ |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 644 | self.interface = interface |
| 645 | self.ir_class = ir_class |
| 646 | self.c_name = self.ir_class.name |
| 647 | self.version = version |
| 648 | self.constant_name = self.c_name.upper().replace("OF_", "") |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 649 | self.package = "org.projectfloodlight.openflow.protocol.ver%s" % version.dotless_version |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 650 | self.generated = False |
| 651 | |
| 652 | @property |
| 653 | @memoize |
| 654 | def unit_test(self): |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 655 | return JavaUnitTestSet(self) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 656 | |
| 657 | @property |
| 658 | def name(self): |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 659 | return "%sVer%s" % (self.interface.name, self.version.dotless_version) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 660 | |
| 661 | @property |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 662 | def variable_name(self): |
Andreas Wundsam | 781d99f | 2013-10-23 15:25:56 -0700 | [diff] [blame] | 663 | return self.name[2].lower() + self.name[3:] |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 664 | |
| 665 | @property |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 666 | def length(self): |
| 667 | if self.is_fixed_length: |
| 668 | return self.min_length |
| 669 | else: |
| 670 | raise Exception("No fixed length for class %s, version %s" % (self.name, self.version)) |
| 671 | |
| 672 | @property |
| 673 | def min_length(self): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 674 | """ @return the minimum wire length of an instance of this class in bytes """ |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 675 | return self.ir_class.base_length |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 676 | |
| 677 | @property |
| 678 | def is_fixed_length(self): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 679 | """ true iff this class serializes to a fixed length on the wire """ |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 680 | return self.ir_class.is_fixed_length and not self.is_virtual |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 681 | |
| 682 | def all_properties(self): |
| 683 | return self.interface.members |
| 684 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 685 | @property |
| 686 | @memoize |
| 687 | def data_members(self): |
| 688 | return [ prop for prop in self.members if prop.is_data ] |
| 689 | |
| 690 | @property |
| 691 | @memoize |
| 692 | def fixed_value_members(self): |
| 693 | return [ prop for prop in self.members if prop.is_fixed_value ] |
| 694 | |
| 695 | @property |
| 696 | @memoize |
| 697 | def public_members(self): |
| 698 | return [ prop for prop in self.members if prop.is_public ] |
| 699 | |
| 700 | @property |
| 701 | @memoize |
| 702 | def members(self): |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 703 | return self.ir_model_members + self.virtual_members |
| 704 | |
| 705 | @property |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 706 | @memoize |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 707 | def ir_model_members(self): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 708 | members = [ JavaMember.for_of_member(self, of_member) for of_member in self.ir_class.members ] |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 709 | return tuple(members) |
| 710 | |
| 711 | @property |
| 712 | def virtual_members(self): |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 713 | virtual_members = [] |
Andreas Wundsam | 83aebd3 | 2013-11-19 10:43:50 -0800 | [diff] [blame] | 714 | if self.ir_class.is_subclassof("of_oxm"): |
| 715 | value_member = find(lambda m: m.name, self.ir_model_members) |
| 716 | if value_member: |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 717 | oxm_entry = model.oxm_map[self.interface.name] |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 718 | virtual_members += [ |
Andreas Wundsam | 83aebd3 | 2013-11-19 10:43:50 -0800 | [diff] [blame] | 719 | JavaVirtualMember(self, "matchField", java_type.make_match_field_jtype(value_member.java_type.public_type), "MatchField.%s" % oxm_entry.value), |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 720 | JavaVirtualMember(self, "masked", java_type.boolean, "true" if oxm_entry.masked else "false"), |
Andreas Wundsam | 661a222 | 2013-11-05 17:18:59 -0800 | [diff] [blame] | 721 | ] |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 722 | else: |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 723 | virtual_members += [ |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 724 | JavaVirtualMember(self, "matchField", java_type.make_match_field_jtype(), "null"), |
| 725 | JavaVirtualMember(self, "masked", java_type.boolean, "false"), |
Andreas Wundsam | 661a222 | 2013-11-05 17:18:59 -0800 | [diff] [blame] | 726 | ] |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 727 | if not find(lambda m: m.name == "version", self.ir_model_members): |
| 728 | virtual_members.append(JavaVirtualMember(self, "version", java_type.of_version, "OFVersion.%s" % self.version.constant_version)) |
| 729 | |
| 730 | return tuple(virtual_members) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 731 | |
Andreas Wundsam | 661a222 | 2013-11-05 17:18:59 -0800 | [diff] [blame] | 732 | @memoize |
| 733 | def member_by_name(self, name): |
| 734 | return find(lambda m: m.name == name, self.members) |
| 735 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 736 | def all_versions(self): |
| 737 | return [ JavaOFVersion(int_version) |
| 738 | for int_version in of_g.unified[self.c_name] |
| 739 | if int_version != 'union' and int_version != 'object_id' ] |
| 740 | |
| 741 | def version_is_inherited(self, version): |
| 742 | return 'use_version' in of_g.unified[self.ir_class.name][version.int_version] |
| 743 | |
| 744 | def inherited_from(self, version): |
| 745 | return JavaOFVersion(of_g.unified[self.ir_class.name][version.int_version]['use_version']) |
| 746 | |
| 747 | @property |
| 748 | def is_virtual(self): |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 749 | return self.ir_class.virtual # type_maps.class_is_virtual(self.c_name) or self.ir_class.virtual |
| 750 | |
| 751 | @property |
| 752 | def discriminator(self): |
| 753 | return find(lambda m: isinstance(m, OFDiscriminatorMember), self.ir_class.members) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 754 | |
| 755 | @property |
| 756 | def is_extension(self): |
| 757 | return type_maps.message_is_extension(self.c_name, -1) |
| 758 | |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 759 | @property |
| 760 | def align(self): |
| 761 | return int(self.ir_class.params['align']) if 'align' in self.ir_class.params else 0 |
| 762 | |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 763 | @property |
Andreas Wundsam | 5da6851 | 2013-10-22 22:18:00 -0700 | [diff] [blame] | 764 | def length_includes_align(self): |
| 765 | return self.ir_class.params['length_includes_align'] == "True" if 'length_includes_align' in self.ir_class.params else False |
| 766 | |
| 767 | @property |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 768 | @memoize |
| 769 | def superclass(self): |
| 770 | return find(lambda c: c.version == self.version and c.c_name == self.ir_class.superclass, model.all_classes) |
| 771 | |
| 772 | @property |
| 773 | @memoize |
| 774 | def subclasses(self): |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 775 | return [ c for c in model.all_classes if c.version == self.version and c.ir_class.superclass |
| 776 | and c.ir_class.superclass.name == self.c_name ] |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 777 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 778 | ####################################################################### |
| 779 | ### Member |
| 780 | ####################################################################### |
| 781 | |
| 782 | |
| 783 | class JavaMember(object): |
| 784 | """ Models a property (member) of an openflow class. """ |
| 785 | def __init__(self, msg, name, java_type, member): |
| 786 | self.msg = msg |
| 787 | self.name = name |
| 788 | self.java_type = java_type |
| 789 | self.member = member |
| 790 | self.c_name = self.member.name if(hasattr(self.member, "name")) else "" |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 791 | |
| 792 | @property |
| 793 | def title_name(self): |
| 794 | return self.name[0].upper() + self.name[1:] |
| 795 | |
| 796 | @property |
| 797 | def constant_name(self): |
| 798 | return self.c_name.upper() |
| 799 | |
| 800 | @property |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 801 | def getter_name(self): |
| 802 | return ("is" if self.java_type.public_type == "boolean" else "get") + self.title_name |
| 803 | |
| 804 | @property |
| 805 | def setter_name(self): |
| 806 | return "set" + self.title_name |
| 807 | |
| 808 | @property |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 809 | def default_name(self): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 810 | if self.is_fixed_value: |
| 811 | return self.constant_name |
| 812 | else: |
| 813 | return "DEFAULT_"+self.constant_name |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 814 | |
| 815 | @property |
| 816 | def default_value(self): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 817 | if self.is_fixed_value: |
| 818 | return self.enum_value |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 819 | else: |
Andreas Wundsam | 142dd2a | 2013-09-23 14:47:37 -0700 | [diff] [blame] | 820 | default = self.java_type.default_op(self.msg.version) |
| 821 | if default == "null" and not self.is_nullable: |
| 822 | return None |
| 823 | else: |
| 824 | return default |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 825 | |
| 826 | @property |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 827 | def enum_value(self): |
| 828 | if self.name == "version": |
| 829 | return "OFVersion.%s" % self.msg.version.constant_version |
| 830 | |
| 831 | java_type = self.java_type.public_type; |
| 832 | try: |
| 833 | global model |
| 834 | enum = model.enum_by_name(java_type) |
| 835 | entry = enum.entry_by_version_value(self.msg.version, self.value) |
| 836 | return "%s.%s" % ( enum.name, entry.name) |
| 837 | except KeyError, e: |
Andreas Wundsam | aa8a918 | 2013-11-19 09:45:01 -0800 | [diff] [blame] | 838 | logger.debug("No enum found", e) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 839 | return self.value |
| 840 | |
| 841 | @property |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 842 | def is_pad(self): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 843 | return isinstance(self.member, OFPadMember) |
| 844 | |
| 845 | def is_type_value(self, version=None): |
| 846 | if(version==None): |
| 847 | return any(self.is_type_value(version) for version in self.msg.all_versions) |
| 848 | try: |
| 849 | return self.c_name in get_type_values(self.msg.c_name, version.int_version) |
| 850 | except: |
| 851 | return False |
| 852 | |
| 853 | @property |
| 854 | def is_field_length_value(self): |
| 855 | return isinstance(self.member, OFFieldLengthMember) |
| 856 | |
| 857 | @property |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 858 | def is_discriminator(self): |
| 859 | return isinstance(self.member, OFDiscriminatorMember) |
| 860 | |
| 861 | @property |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 862 | def is_length_value(self): |
| 863 | return isinstance(self.member, OFLengthMember) |
| 864 | |
| 865 | @property |
| 866 | def is_public(self): |
| 867 | return not (self.is_pad or self.is_length_value) |
| 868 | |
| 869 | @property |
| 870 | def is_data(self): |
| 871 | return isinstance(self.member, OFDataMember) and self.name != "version" |
| 872 | |
| 873 | @property |
| 874 | def is_fixed_value(self): |
| 875 | return hasattr(self.member, "value") or self.name == "version" \ |
| 876 | or ( self.name == "length" and self.msg.is_fixed_length) \ |
| 877 | or ( self.name == "len" and self.msg.is_fixed_length) |
| 878 | |
| 879 | @property |
| 880 | def value(self): |
| 881 | if self.name == "version": |
| 882 | return self.msg.version.int_version |
| 883 | elif self.name == "length" or self.name == "len": |
| 884 | return self.msg.length |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 885 | else: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 886 | return self.java_type.format_value(self.member.value) |
| 887 | |
| 888 | @property |
| 889 | def priv_value(self): |
| 890 | if self.name == "version": |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 891 | return self.java_type.format_value(self.msg.version.int_version, pub_type=False) |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 892 | elif self.name == "length" or self.name == "len": |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 893 | return self.java_type.format_value(self.msg.length, pub_type=False) |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 894 | else: |
| 895 | return self.java_type.format_value(self.member.value, pub_type=False) |
| 896 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 897 | |
| 898 | @property |
| 899 | def is_writeable(self): |
| 900 | return self.is_data and not self.name in model.write_blacklist[self.msg.name] |
| 901 | |
| 902 | def get_type_value_info(self, version): |
| 903 | 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] | 904 | |
| 905 | @property |
| 906 | def length(self): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 907 | if hasattr(self.member, "length"): |
| 908 | return self.member.length |
| 909 | else: |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 910 | count, base = loxi_utils.type_dec_to_count_base(self.member.type) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 911 | return of_g.of_base_types[base]['bytes'] * count |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 912 | |
| 913 | @staticmethod |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 914 | def for_of_member(java_class, member): |
| 915 | if isinstance(member, OFPadMember): |
| 916 | return JavaMember(None, "", None, member) |
| 917 | else: |
| 918 | if member.name == 'len': |
| 919 | name = 'length' |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 920 | elif member.name == 'value_mask': |
| 921 | name = 'mask' |
Andreas Wundsam | acd57d5 | 2013-10-18 17:35:01 -0700 | [diff] [blame] | 922 | elif member.name == 'group_id': |
| 923 | name = 'group' |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 924 | else: |
| 925 | name = java_type.name_c_to_camel(member.name) |
| 926 | j_type = java_type.convert_to_jtype(java_class.c_name, member.name, member.oftype) |
| 927 | return JavaMember(java_class, name, j_type, member) |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 928 | |
| 929 | @property |
| 930 | def is_universal(self): |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 931 | for version, ir_class in self.msg.ir_class.version_classes.items(): |
| 932 | if not ir_class.member_by_name(self.member.name): |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 933 | return False |
| 934 | return True |
| 935 | |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 936 | @property |
| 937 | def is_virtual(self): |
| 938 | return False |
| 939 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 940 | def __hash__(self): |
| 941 | return hash(self.name) |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 942 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 943 | def __eq__(self, other): |
| 944 | if other is None or type(self) != type(other): |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 945 | return False |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 946 | return (self.name,) == (other.name,) |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 947 | |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 948 | @property |
| 949 | def is_nullable(self): |
| 950 | return self.name in model.nullable_map[self.msg.name] |
| 951 | |
| 952 | |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 953 | class JavaVirtualMember(JavaMember): |
| 954 | """ Models a virtual property (member) of an openflow class that is not backed by a loxi ir member """ |
Andreas Wundsam | 4b8661f | 2013-11-05 17:17:28 -0800 | [diff] [blame] | 955 | def __init__(self, msg, name, java_type, value=None, custom_template=None): |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 956 | JavaMember.__init__(self, msg, name, java_type, member=None) |
| 957 | self._value = value |
Andreas Wundsam | 4b8661f | 2013-11-05 17:17:28 -0800 | [diff] [blame] | 958 | self.custom_template = custom_template |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 959 | |
| 960 | @property |
| 961 | def is_fixed_value(self): |
| 962 | return True |
| 963 | |
| 964 | @property |
| 965 | def value(self): |
| 966 | return self._value |
| 967 | |
| 968 | @property |
| 969 | def priv_value(self): |
| 970 | return self._value |
| 971 | |
| 972 | |
| 973 | @property |
| 974 | def is_universal(self): |
| 975 | return True |
| 976 | |
| 977 | @property |
| 978 | def is_virtual(self): |
| 979 | return True |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 980 | |
| 981 | ####################################################################### |
| 982 | ### Unit Test |
| 983 | ####################################################################### |
| 984 | |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 985 | class JavaUnitTestSet(object): |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 986 | def __init__(self, java_class): |
| 987 | self.java_class = java_class |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 988 | first_data_file_name = "of{version}/{name}.data".format(version=java_class.version.dotless_version, |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 989 | name=java_class.c_name[3:]) |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 990 | glob_file_name = "of{version}/{name}__*.data".format(version=java_class.version.dotless_version, |
Andreas Wundsam | aa4f3cd | 2013-10-22 22:19:08 -0700 | [diff] [blame] | 991 | name=java_class.c_name[3:]) |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 992 | test_class_name = self.java_class.name + "Test" |
| 993 | self.test_units = [] |
| 994 | if test_data.exists(first_data_file_name): |
| 995 | self.test_units.append(JavaUnitTest(java_class, first_data_file_name, test_class_name)) |
Andreas Wundsam | aa4f3cd | 2013-10-22 22:19:08 -0700 | [diff] [blame] | 996 | |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 997 | i = 1 |
Andreas Wundsam | aa4f3cd | 2013-10-22 22:19:08 -0700 | [diff] [blame] | 998 | for f in test_data.glob(glob_file_name): |
| 999 | m = re.match(".*__(.*).data", f) |
| 1000 | if m: |
| 1001 | suffix = java_type.name_c_to_caps_camel(m.group(1)) |
| 1002 | else: |
| 1003 | suffix = str(i) |
| 1004 | i += 1 |
| 1005 | test_class_name = self.java_class.name + suffix + "Test" |
| 1006 | self.test_units.append(JavaUnitTest(java_class, f, test_class_name)) |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 1007 | |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 1008 | @property |
| 1009 | def package(self): |
| 1010 | return self.java_class.package |
| 1011 | |
| 1012 | @property |
| 1013 | def has_test_data(self): |
| 1014 | return len(self.test_units) > 0 |
| 1015 | |
| 1016 | @property |
| 1017 | def length(self): |
| 1018 | return len(self.test_units) |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 1019 | |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 1020 | def get_test_unit(self, i): |
| 1021 | return self.test_units[i] |
| 1022 | |
| 1023 | |
| 1024 | class JavaUnitTest(object): |
| 1025 | def __init__(self, java_class, file_name=None, test_class_name=None): |
| 1026 | self.java_class = java_class |
| 1027 | if file_name is None: |
Andreas Wundsam | c8912c1 | 2013-11-15 13:44:48 -0800 | [diff] [blame] | 1028 | self.data_file_name = "of{version}/{name}.data".format(version=java_class.version.dotless_version, |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 1029 | name=java_class.c_name[3:]) |
| 1030 | else: |
| 1031 | self.data_file_name = file_name |
| 1032 | if test_class_name is None: |
| 1033 | self.test_class_name = self.java_class.name + "Test" |
| 1034 | else: |
| 1035 | self.test_class_name = test_class_name |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 1036 | |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 1037 | @property |
| 1038 | def package(self): |
| 1039 | return self.java_class.package |
| 1040 | |
| 1041 | @property |
| 1042 | def name(self): |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 1043 | return self.test_class_name |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 1044 | |
| 1045 | @property |
| 1046 | def interface(self): |
| 1047 | return self.java_class.interface |
| 1048 | |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 1049 | @property |
| 1050 | def has_test_data(self): |
| 1051 | return test_data.exists(self.data_file_name) |
| 1052 | |
| 1053 | @property |
| 1054 | @memoize |
| 1055 | def test_data(self): |
| 1056 | return test_data.read(self.data_file_name) |
| 1057 | |
| 1058 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1059 | ####################################################################### |
| 1060 | ### Enums |
| 1061 | ####################################################################### |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 1062 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1063 | class JavaEnum(object): |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 1064 | def __init__(self, c_name, version_enum_map): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1065 | self.c_name = c_name |
Andreas Wundsam | be168f7 | 2013-08-03 22:49:35 -0700 | [diff] [blame] | 1066 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 1067 | 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] | 1068 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1069 | # Port_features has constants that start with digits |
| 1070 | self.name_prefix = "PF_" if self.name == "OFPortFeatures" else "" |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 1071 | |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 1072 | self.version_enums = version_enum_map |
| 1073 | |
| 1074 | entry_name_version_value_map = OrderedDefaultDict(lambda: OrderedDict()) |
| 1075 | for version, ir_enum in version_enum_map.items(): |
| 1076 | for ir_entry in ir_enum.entries: |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 1077 | entry_name_version_value_map[ir_entry.name][version] = ir_entry.value |
| 1078 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1079 | self.entries = [ JavaEnumEntry(self, name, version_value_map) |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 1080 | for (name, version_value_map) in entry_name_version_value_map.items() ] |
Andreas Wundsam | 4352653 | 2013-08-01 22:03:50 -0700 | [diff] [blame] | 1081 | |
| 1082 | self.entries = [ e for e in self.entries if e.name not in model.enum_entry_blacklist[self.name] ] |
Yotam Harchol | 791e488 | 2013-09-05 16:32:56 -0700 | [diff] [blame] | 1083 | self.package = "org.projectfloodlight.openflow.protocol" |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 1084 | |
Andreas Wundsam | 8ec3bcc | 2013-09-16 19:44:00 -0700 | [diff] [blame] | 1085 | self.metadata = model.enum_metadata_map[self.name] |
| 1086 | |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 1087 | def wire_type(self, version): |
| 1088 | ir_enum = self.version_enums[version] |
| 1089 | if "wire_type" in ir_enum.params: |
| 1090 | return java_type.convert_enum_wire_type_to_jtype(ir_enum.params["wire_type"]) |
| 1091 | else: |
| 1092 | return java_type.u8 |
| 1093 | |
| 1094 | @property |
Andreas Wundsam | 7cfeac3 | 2013-09-17 13:53:48 -0700 | [diff] [blame] | 1095 | @memoize |
| 1096 | def is_bitmask(self): |
| 1097 | return any(ir_enum.is_bitmask for ir_enum in self.version_enums.values()) |
| 1098 | |
| 1099 | @property |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 1100 | def versions(self): |
| 1101 | return self.version_enums.keys() |
| 1102 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1103 | @memoize |
| 1104 | def entry_by_name(self, name): |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 1105 | res = find(lambda e: e.name == name, self.entries) |
| 1106 | if res: |
| 1107 | return res |
| 1108 | else: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1109 | raise KeyError("Enum %s: no entry with name %s" % (self.name, name)) |
| 1110 | |
| 1111 | @memoize |
| 1112 | def entry_by_c_name(self, name): |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 1113 | res = find(lambda e: e.c_name == name, self.entries) |
| 1114 | if res: |
| 1115 | return res |
| 1116 | else: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1117 | raise KeyError("Enum %s: no entry with c_name %s" % (self.name, name)) |
| 1118 | |
| 1119 | @memoize |
| 1120 | def entry_by_version_value(self, version, value): |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 1121 | res = find(lambda e: e.values[version] == value if version in e.values else False, self.entries) |
| 1122 | if res: |
| 1123 | return res |
| 1124 | else: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1125 | raise KeyError("Enum %s: no entry with version %s, value %s" % (self.name, version, value)) |
| 1126 | |
| 1127 | # values: Map JavaVersion->Value |
| 1128 | class JavaEnumEntry(object): |
| 1129 | def __init__(self, enum, name, values): |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 1130 | self.enum = enum |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1131 | self.name = enum.name_prefix + "_".join(name.split("_")[1:]).upper() |
| 1132 | self.values = values |
| 1133 | |
Andreas Wundsam | 8ec3bcc | 2013-09-16 19:44:00 -0700 | [diff] [blame] | 1134 | @property |
| 1135 | def constructor_params(self): |
Rob Vaterlaus | 6035bf5 | 2013-09-17 19:32:38 -0700 | [diff] [blame] | 1136 | return [ m.value(self) for m in self.enum.metadata.properties ] |
Andreas Wundsam | 8ec3bcc | 2013-09-16 19:44:00 -0700 | [diff] [blame] | 1137 | |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 1138 | def has_value(self, version): |
| 1139 | return version in self.values |
| 1140 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1141 | def value(self, version): |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 1142 | return self.values[version] |
| 1143 | |
| 1144 | def format_value(self, version): |
| 1145 | res = self.enum.wire_type(version).format_value(self.values[version]) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1146 | return res |
| 1147 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1148 | def all_values(self, versions, not_present=None): |
| 1149 | return [ self.values[version] if version in self.values else not_present for version in versions ] |
Andreas Wundsam | c1acfcc | 2013-09-20 13:10:20 -0700 | [diff] [blame] | 1150 | |
| 1151 | @property |
| 1152 | @memoize |
| 1153 | def masked_enum_group(self): |
| 1154 | group = find(lambda g: self.name in g.members, model.masked_enum_groups[self.enum.name]) |
| 1155 | return group |
| 1156 | |
| 1157 | @property |
| 1158 | @memoize |
| 1159 | def is_mask(self): |
| 1160 | return any(self.name == g.mask for g in model.masked_enum_groups[self.enum.name]) |