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