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), |
| 82 | "OFOxmVlanVid": OxmMapEntry("VlanVid", "VLAN_VID", False), |
| 83 | "OFOxmVlanVidMasked": OxmMapEntry("VlanVid", "VLAN_VID", True), |
| 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 | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 605 | ] |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 606 | elif self.parent_interface and self.parent_interface.startswith("OFOxm"): |
| 607 | field_type = java_type.make_match_field_jtype(model.oxm_map[self.name].type_name) \ |
| 608 | if self.name in model.oxm_map \ |
| 609 | else java_type.make_match_field_jtype() |
| 610 | |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 611 | virtual_members += [ |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 612 | JavaVirtualMember(self, "matchField", field_type), |
| 613 | JavaVirtualMember(self, "masked", java_type.boolean), |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 614 | ] |
| 615 | if not find(lambda x: x.name == "mask", self.ir_model_members): |
| 616 | virtual_members.append(JavaVirtualMember(self, "mask", find(lambda x: x.name == "value", self.ir_model_members).java_type)) |
| 617 | |
| 618 | if not find(lambda m: m.name == "version", self.ir_model_members): |
| 619 | virtual_members.append(JavaVirtualMember(self, "version", java_type.of_version)) |
| 620 | |
| 621 | return tuple(virtual_members) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 622 | |
| 623 | @property |
| 624 | @memoize |
| 625 | def is_virtual(self): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 626 | """ Is this interface virtual. If so, do not generate a builder interface """ |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 627 | 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] | 628 | |
| 629 | @property |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 630 | def is_universal(self): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 631 | """ 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] | 632 | return len(self.all_versions) == len(model.versions) |
| 633 | |
| 634 | @property |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 635 | @memoize |
| 636 | def all_versions(self): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 637 | """ return list of all versions that this interface exists in """ |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 638 | return self.version_map.keys() |
| 639 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 640 | def has_version(self, version): |
| 641 | return version in self.version_map |
| 642 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 643 | def versioned_class(self, version): |
| 644 | return JavaOFClass(self, version, self.version_map[version]) |
| 645 | |
| 646 | @property |
| 647 | @memoize |
| 648 | def versioned_classes(self): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 649 | return [ self.versioned_class(version) for version in self.all_versions ] |
| 650 | |
| 651 | ####################################################################### |
| 652 | ### (Versioned) Classes |
| 653 | ####################################################################### |
| 654 | |
| 655 | class JavaOFClass(object): |
| 656 | """ Models an OpenFlow Message class for the purpose of the java class. |
Andreas Wundsam | d40ddbf | 2013-07-25 15:40:47 -0700 | [diff] [blame] | 657 | Version specific child of a JavaOFInterface |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 658 | """ |
| 659 | def __init__(self, interface, version, ir_class): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 660 | """ |
| 661 | @param interface JavaOFInterface instance of the parent interface |
| 662 | @param version JavaOFVersion |
| 663 | @param ir_class OFClass from loxi_ir |
| 664 | """ |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 665 | self.interface = interface |
| 666 | self.ir_class = ir_class |
| 667 | self.c_name = self.ir_class.name |
| 668 | self.version = version |
| 669 | self.constant_name = self.c_name.upper().replace("OF_", "") |
Yotam Harchol | 791e488 | 2013-09-05 16:32:56 -0700 | [diff] [blame] | 670 | self.package = "org.projectfloodlight.openflow.protocol.ver%s" % version.of_version |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 671 | self.generated = False |
| 672 | |
| 673 | @property |
| 674 | @memoize |
| 675 | def unit_test(self): |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 676 | return JavaUnitTestSet(self) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 677 | |
| 678 | @property |
| 679 | def name(self): |
| 680 | return "%sVer%s" % (self.interface.name, self.version.of_version) |
| 681 | |
| 682 | @property |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 683 | def variable_name(self): |
Andreas Wundsam | 781d99f | 2013-10-23 15:25:56 -0700 | [diff] [blame] | 684 | return self.name[2].lower() + self.name[3:] |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 685 | |
| 686 | @property |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 687 | def length(self): |
| 688 | if self.is_fixed_length: |
| 689 | return self.min_length |
| 690 | else: |
| 691 | raise Exception("No fixed length for class %s, version %s" % (self.name, self.version)) |
| 692 | |
| 693 | @property |
| 694 | def min_length(self): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 695 | """ @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] | 696 | id_tuple = (self.ir_class.name, self.version.int_version) |
| 697 | return of_g.base_length[id_tuple] if id_tuple in of_g.base_length else -1 |
| 698 | |
| 699 | @property |
| 700 | def is_fixed_length(self): |
Andreas Wundsam | d8bcedf | 2013-08-03 21:23:37 -0700 | [diff] [blame] | 701 | """ true iff this class serializes to a fixed length on the wire """ |
Andreas Wundsam | bbf85e1 | 2013-09-13 14:18:01 -0700 | [diff] [blame] | 702 | return (self.ir_class.name, self.version.int_version) in of_g.is_fixed_length and \ |
| 703 | not self.is_virtual |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 704 | |
| 705 | def all_properties(self): |
| 706 | return self.interface.members |
| 707 | |
| 708 | def get_member(self, name): |
| 709 | for m in self.members: |
| 710 | if m.name == name: |
| 711 | return m |
| 712 | |
| 713 | @property |
| 714 | @memoize |
| 715 | def data_members(self): |
| 716 | return [ prop for prop in self.members if prop.is_data ] |
| 717 | |
| 718 | @property |
| 719 | @memoize |
| 720 | def fixed_value_members(self): |
| 721 | return [ prop for prop in self.members if prop.is_fixed_value ] |
| 722 | |
| 723 | @property |
| 724 | @memoize |
| 725 | def public_members(self): |
| 726 | return [ prop for prop in self.members if prop.is_public ] |
| 727 | |
| 728 | @property |
| 729 | @memoize |
| 730 | def members(self): |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 731 | return self.ir_model_members + self.virtual_members |
| 732 | |
| 733 | @property |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 734 | @memoize |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 735 | def ir_model_members(self): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 736 | 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] | 737 | return tuple(members) |
| 738 | |
| 739 | @property |
| 740 | def virtual_members(self): |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 741 | virtual_members = [] |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 742 | if self.interface.parent_interface and self.interface.parent_interface.startswith("OFOxm"): |
| 743 | if self.interface.name in model.oxm_map: |
| 744 | oxm_entry = model.oxm_map[self.interface.name] |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 745 | virtual_members += [ |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 746 | JavaVirtualMember(self, "matchField", java_type.make_match_field_jtype(oxm_entry.type_name), "MatchField.%s" % oxm_entry.value), |
| 747 | JavaVirtualMember(self, "masked", java_type.boolean, "true" if oxm_entry.masked else "false"), |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 748 | ] |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 749 | else: |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 750 | virtual_members += [ |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 751 | JavaVirtualMember(self, "matchField", java_type.make_match_field_jtype(), "null"), |
| 752 | JavaVirtualMember(self, "masked", java_type.boolean, "false"), |
Andreas Wundsam | a098102 | 2013-10-02 18:15:06 -0700 | [diff] [blame] | 753 | ] |
| 754 | |
| 755 | if not find(lambda m: m.name == "version", self.ir_model_members): |
| 756 | virtual_members.append(JavaVirtualMember(self, "version", java_type.of_version, "OFVersion.%s" % self.version.constant_version)) |
| 757 | |
| 758 | return tuple(virtual_members) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 759 | |
| 760 | def all_versions(self): |
| 761 | return [ JavaOFVersion(int_version) |
| 762 | for int_version in of_g.unified[self.c_name] |
| 763 | if int_version != 'union' and int_version != 'object_id' ] |
| 764 | |
| 765 | def version_is_inherited(self, version): |
| 766 | return 'use_version' in of_g.unified[self.ir_class.name][version.int_version] |
| 767 | |
| 768 | def inherited_from(self, version): |
| 769 | return JavaOFVersion(of_g.unified[self.ir_class.name][version.int_version]['use_version']) |
| 770 | |
| 771 | @property |
| 772 | def is_virtual(self): |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 773 | return self.ir_class.virtual # type_maps.class_is_virtual(self.c_name) or self.ir_class.virtual |
| 774 | |
| 775 | @property |
| 776 | def discriminator(self): |
| 777 | return find(lambda m: isinstance(m, OFDiscriminatorMember), self.ir_class.members) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 778 | |
| 779 | @property |
| 780 | def is_extension(self): |
| 781 | return type_maps.message_is_extension(self.c_name, -1) |
| 782 | |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 783 | @property |
| 784 | def align(self): |
| 785 | return int(self.ir_class.params['align']) if 'align' in self.ir_class.params else 0 |
| 786 | |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 787 | @property |
Andreas Wundsam | 5da6851 | 2013-10-22 22:18:00 -0700 | [diff] [blame] | 788 | def length_includes_align(self): |
| 789 | return self.ir_class.params['length_includes_align'] == "True" if 'length_includes_align' in self.ir_class.params else False |
| 790 | |
| 791 | @property |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 792 | @memoize |
| 793 | def superclass(self): |
| 794 | return find(lambda c: c.version == self.version and c.c_name == self.ir_class.superclass, model.all_classes) |
| 795 | |
| 796 | @property |
| 797 | @memoize |
| 798 | def subclasses(self): |
| 799 | return [ c for c in model.all_classes if c.version == self.version and c.ir_class.superclass == self.c_name ] |
| 800 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 801 | ####################################################################### |
| 802 | ### Member |
| 803 | ####################################################################### |
| 804 | |
| 805 | |
| 806 | class JavaMember(object): |
| 807 | """ Models a property (member) of an openflow class. """ |
| 808 | def __init__(self, msg, name, java_type, member): |
| 809 | self.msg = msg |
| 810 | self.name = name |
| 811 | self.java_type = java_type |
| 812 | self.member = member |
| 813 | self.c_name = self.member.name if(hasattr(self.member, "name")) else "" |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 814 | |
| 815 | @property |
| 816 | def title_name(self): |
| 817 | return self.name[0].upper() + self.name[1:] |
| 818 | |
| 819 | @property |
| 820 | def constant_name(self): |
| 821 | return self.c_name.upper() |
| 822 | |
| 823 | @property |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 824 | def getter_name(self): |
| 825 | return ("is" if self.java_type.public_type == "boolean" else "get") + self.title_name |
| 826 | |
| 827 | @property |
| 828 | def setter_name(self): |
| 829 | return "set" + self.title_name |
| 830 | |
| 831 | @property |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 832 | def default_name(self): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 833 | if self.is_fixed_value: |
| 834 | return self.constant_name |
| 835 | else: |
| 836 | return "DEFAULT_"+self.constant_name |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 837 | |
| 838 | @property |
| 839 | def default_value(self): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 840 | if self.is_fixed_value: |
| 841 | return self.enum_value |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 842 | else: |
Andreas Wundsam | 142dd2a | 2013-09-23 14:47:37 -0700 | [diff] [blame] | 843 | default = self.java_type.default_op(self.msg.version) |
| 844 | if default == "null" and not self.is_nullable: |
| 845 | return None |
| 846 | else: |
| 847 | return default |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 848 | |
| 849 | @property |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 850 | def enum_value(self): |
| 851 | if self.name == "version": |
| 852 | return "OFVersion.%s" % self.msg.version.constant_version |
| 853 | |
| 854 | java_type = self.java_type.public_type; |
| 855 | try: |
| 856 | global model |
| 857 | enum = model.enum_by_name(java_type) |
| 858 | entry = enum.entry_by_version_value(self.msg.version, self.value) |
| 859 | return "%s.%s" % ( enum.name, entry.name) |
| 860 | except KeyError, e: |
| 861 | print e.message |
| 862 | return self.value |
| 863 | |
| 864 | @property |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 865 | def is_pad(self): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 866 | return isinstance(self.member, OFPadMember) |
| 867 | |
| 868 | def is_type_value(self, version=None): |
| 869 | if(version==None): |
| 870 | return any(self.is_type_value(version) for version in self.msg.all_versions) |
| 871 | try: |
| 872 | return self.c_name in get_type_values(self.msg.c_name, version.int_version) |
| 873 | except: |
| 874 | return False |
| 875 | |
| 876 | @property |
| 877 | def is_field_length_value(self): |
| 878 | return isinstance(self.member, OFFieldLengthMember) |
| 879 | |
| 880 | @property |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 881 | def is_discriminator(self): |
| 882 | return isinstance(self.member, OFDiscriminatorMember) |
| 883 | |
| 884 | @property |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 885 | def is_length_value(self): |
| 886 | return isinstance(self.member, OFLengthMember) |
| 887 | |
| 888 | @property |
| 889 | def is_public(self): |
| 890 | return not (self.is_pad or self.is_length_value) |
| 891 | |
| 892 | @property |
| 893 | def is_data(self): |
| 894 | return isinstance(self.member, OFDataMember) and self.name != "version" |
| 895 | |
| 896 | @property |
| 897 | def is_fixed_value(self): |
| 898 | return hasattr(self.member, "value") or self.name == "version" \ |
| 899 | or ( self.name == "length" and self.msg.is_fixed_length) \ |
| 900 | or ( self.name == "len" and self.msg.is_fixed_length) |
| 901 | |
| 902 | @property |
| 903 | def value(self): |
| 904 | if self.name == "version": |
| 905 | return self.msg.version.int_version |
| 906 | elif self.name == "length" or self.name == "len": |
| 907 | return self.msg.length |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 908 | else: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 909 | return self.java_type.format_value(self.member.value) |
| 910 | |
| 911 | @property |
| 912 | def priv_value(self): |
| 913 | if self.name == "version": |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 914 | 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] | 915 | elif self.name == "length" or self.name == "len": |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 916 | return self.java_type.format_value(self.msg.length, pub_type=False) |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 917 | else: |
| 918 | return self.java_type.format_value(self.member.value, pub_type=False) |
| 919 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 920 | |
| 921 | @property |
| 922 | def is_writeable(self): |
| 923 | return self.is_data and not self.name in model.write_blacklist[self.msg.name] |
| 924 | |
| 925 | def get_type_value_info(self, version): |
| 926 | 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] | 927 | |
| 928 | @property |
| 929 | def length(self): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 930 | if hasattr(self.member, "length"): |
| 931 | return self.member.length |
| 932 | else: |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 933 | count, base = loxi_utils.type_dec_to_count_base(self.member.type) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 934 | return of_g.of_base_types[base]['bytes'] * count |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 935 | |
| 936 | @staticmethod |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 937 | def for_of_member(java_class, member): |
| 938 | if isinstance(member, OFPadMember): |
| 939 | return JavaMember(None, "", None, member) |
| 940 | else: |
| 941 | if member.name == 'len': |
| 942 | name = 'length' |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 943 | elif member.name == 'value_mask': |
| 944 | name = 'mask' |
Andreas Wundsam | acd57d5 | 2013-10-18 17:35:01 -0700 | [diff] [blame] | 945 | elif member.name == 'group_id': |
| 946 | name = 'group' |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 947 | else: |
| 948 | name = java_type.name_c_to_camel(member.name) |
| 949 | j_type = java_type.convert_to_jtype(java_class.c_name, member.name, member.oftype) |
| 950 | return JavaMember(java_class, name, j_type, member) |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 951 | |
| 952 | @property |
| 953 | def is_universal(self): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 954 | if not self.msg.c_name in of_g.unified: |
| 955 | print("%s not self.unified" % self.msg.c_name) |
| 956 | return False |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 957 | for version in of_g.unified[self.msg.c_name]: |
| 958 | if version == 'union' or version =='object_id': |
| 959 | continue |
| 960 | if 'use_version' in of_g.unified[self.msg.c_name][version]: |
| 961 | continue |
| 962 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 963 | 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] | 964 | return False |
| 965 | return True |
| 966 | |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 967 | @property |
| 968 | def is_virtual(self): |
| 969 | return False |
| 970 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 971 | def __hash__(self): |
| 972 | return hash(self.name) |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 973 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 974 | def __eq__(self, other): |
| 975 | if other is None or type(self) != type(other): |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 976 | return False |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 977 | return (self.name,) == (other.name,) |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 978 | |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 979 | @property |
| 980 | def is_nullable(self): |
| 981 | return self.name in model.nullable_map[self.msg.name] |
| 982 | |
| 983 | |
Andreas Wundsam | 2be7da5 | 2013-08-22 07:34:25 -0700 | [diff] [blame] | 984 | class JavaVirtualMember(JavaMember): |
| 985 | """ Models a virtual property (member) of an openflow class that is not backed by a loxi ir member """ |
| 986 | def __init__(self, msg, name, java_type, value=None): |
| 987 | JavaMember.__init__(self, msg, name, java_type, member=None) |
| 988 | self._value = value |
| 989 | |
| 990 | @property |
| 991 | def is_fixed_value(self): |
| 992 | return True |
| 993 | |
| 994 | @property |
| 995 | def value(self): |
| 996 | return self._value |
| 997 | |
| 998 | @property |
| 999 | def priv_value(self): |
| 1000 | return self._value |
| 1001 | |
| 1002 | |
| 1003 | @property |
| 1004 | def is_universal(self): |
| 1005 | return True |
| 1006 | |
| 1007 | @property |
| 1008 | def is_virtual(self): |
| 1009 | return True |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 1010 | |
| 1011 | ####################################################################### |
| 1012 | ### Unit Test |
| 1013 | ####################################################################### |
| 1014 | |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 1015 | class JavaUnitTestSet(object): |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 1016 | def __init__(self, java_class): |
| 1017 | self.java_class = java_class |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 1018 | 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] | 1019 | name=java_class.c_name[3:]) |
Andreas Wundsam | aa4f3cd | 2013-10-22 22:19:08 -0700 | [diff] [blame] | 1020 | glob_file_name = "of{version}/{name}__*.data".format(version=java_class.version.of_version, |
| 1021 | name=java_class.c_name[3:]) |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 1022 | test_class_name = self.java_class.name + "Test" |
| 1023 | self.test_units = [] |
| 1024 | if test_data.exists(first_data_file_name): |
| 1025 | 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] | 1026 | |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 1027 | i = 1 |
Andreas Wundsam | aa4f3cd | 2013-10-22 22:19:08 -0700 | [diff] [blame] | 1028 | for f in test_data.glob(glob_file_name): |
| 1029 | m = re.match(".*__(.*).data", f) |
| 1030 | if m: |
| 1031 | suffix = java_type.name_c_to_caps_camel(m.group(1)) |
| 1032 | else: |
| 1033 | suffix = str(i) |
| 1034 | i += 1 |
| 1035 | test_class_name = self.java_class.name + suffix + "Test" |
| 1036 | self.test_units.append(JavaUnitTest(java_class, f, test_class_name)) |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 1037 | |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 1038 | @property |
| 1039 | def package(self): |
| 1040 | return self.java_class.package |
| 1041 | |
| 1042 | @property |
| 1043 | def has_test_data(self): |
| 1044 | return len(self.test_units) > 0 |
| 1045 | |
| 1046 | @property |
| 1047 | def length(self): |
| 1048 | return len(self.test_units) |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 1049 | |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 1050 | def get_test_unit(self, i): |
| 1051 | return self.test_units[i] |
| 1052 | |
| 1053 | |
| 1054 | class JavaUnitTest(object): |
| 1055 | def __init__(self, java_class, file_name=None, test_class_name=None): |
| 1056 | self.java_class = java_class |
| 1057 | if file_name is None: |
| 1058 | self.data_file_name = "of{version}/{name}.data".format(version=java_class.version.of_version, |
| 1059 | name=java_class.c_name[3:]) |
| 1060 | else: |
| 1061 | self.data_file_name = file_name |
| 1062 | if test_class_name is None: |
| 1063 | self.test_class_name = self.java_class.name + "Test" |
| 1064 | else: |
| 1065 | self.test_class_name = test_class_name |
Andreas Wundsam | f194968 | 2013-09-23 14:48:31 -0700 | [diff] [blame] | 1066 | |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 1067 | @property |
| 1068 | def package(self): |
| 1069 | return self.java_class.package |
| 1070 | |
| 1071 | @property |
| 1072 | def name(self): |
Yotam Harchol | 466b321 | 2013-08-15 12:14:46 -0700 | [diff] [blame] | 1073 | return self.test_class_name |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame] | 1074 | |
| 1075 | @property |
| 1076 | def interface(self): |
| 1077 | return self.java_class.interface |
| 1078 | |
Andreas Wundsam | e916d6f | 2013-07-30 11:33:58 -0700 | [diff] [blame] | 1079 | @property |
| 1080 | def has_test_data(self): |
| 1081 | return test_data.exists(self.data_file_name) |
| 1082 | |
| 1083 | @property |
| 1084 | @memoize |
| 1085 | def test_data(self): |
| 1086 | return test_data.read(self.data_file_name) |
| 1087 | |
| 1088 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1089 | ####################################################################### |
| 1090 | ### Enums |
| 1091 | ####################################################################### |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 1092 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1093 | class JavaEnum(object): |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 1094 | def __init__(self, c_name, version_enum_map): |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1095 | self.c_name = c_name |
Andreas Wundsam | be168f7 | 2013-08-03 22:49:35 -0700 | [diff] [blame] | 1096 | |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 1097 | 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] | 1098 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1099 | # Port_features has constants that start with digits |
| 1100 | self.name_prefix = "PF_" if self.name == "OFPortFeatures" else "" |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 1101 | |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 1102 | self.version_enums = version_enum_map |
| 1103 | |
| 1104 | entry_name_version_value_map = OrderedDefaultDict(lambda: OrderedDict()) |
| 1105 | for version, ir_enum in version_enum_map.items(): |
| 1106 | for ir_entry in ir_enum.entries: |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 1107 | entry_name_version_value_map[ir_entry.name][version] = ir_entry.value |
| 1108 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1109 | self.entries = [ JavaEnumEntry(self, name, version_value_map) |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 1110 | for (name, version_value_map) in entry_name_version_value_map.items() ] |
Andreas Wundsam | 4352653 | 2013-08-01 22:03:50 -0700 | [diff] [blame] | 1111 | |
| 1112 | 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] | 1113 | self.package = "org.projectfloodlight.openflow.protocol" |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 1114 | |
Andreas Wundsam | 8ec3bcc | 2013-09-16 19:44:00 -0700 | [diff] [blame] | 1115 | self.metadata = model.enum_metadata_map[self.name] |
| 1116 | |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 1117 | def wire_type(self, version): |
| 1118 | ir_enum = self.version_enums[version] |
| 1119 | if "wire_type" in ir_enum.params: |
| 1120 | return java_type.convert_enum_wire_type_to_jtype(ir_enum.params["wire_type"]) |
| 1121 | else: |
| 1122 | return java_type.u8 |
| 1123 | |
| 1124 | @property |
Andreas Wundsam | 7cfeac3 | 2013-09-17 13:53:48 -0700 | [diff] [blame] | 1125 | @memoize |
| 1126 | def is_bitmask(self): |
| 1127 | return any(ir_enum.is_bitmask for ir_enum in self.version_enums.values()) |
| 1128 | |
| 1129 | @property |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 1130 | def versions(self): |
| 1131 | return self.version_enums.keys() |
| 1132 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1133 | @memoize |
| 1134 | def entry_by_name(self, name): |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 1135 | res = find(lambda e: e.name == name, self.entries) |
| 1136 | if res: |
| 1137 | return res |
| 1138 | else: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1139 | raise KeyError("Enum %s: no entry with name %s" % (self.name, name)) |
| 1140 | |
| 1141 | @memoize |
| 1142 | def entry_by_c_name(self, name): |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 1143 | res = find(lambda e: e.c_name == name, self.entries) |
| 1144 | if res: |
| 1145 | return res |
| 1146 | else: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1147 | raise KeyError("Enum %s: no entry with c_name %s" % (self.name, name)) |
| 1148 | |
| 1149 | @memoize |
| 1150 | def entry_by_version_value(self, version, value): |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 1151 | res = find(lambda e: e.values[version] == value if version in e.values else False, self.entries) |
| 1152 | if res: |
| 1153 | return res |
| 1154 | else: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1155 | raise KeyError("Enum %s: no entry with version %s, value %s" % (self.name, version, value)) |
| 1156 | |
| 1157 | # values: Map JavaVersion->Value |
| 1158 | class JavaEnumEntry(object): |
| 1159 | def __init__(self, enum, name, values): |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 1160 | self.enum = enum |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1161 | self.name = enum.name_prefix + "_".join(name.split("_")[1:]).upper() |
| 1162 | self.values = values |
| 1163 | |
Andreas Wundsam | 8ec3bcc | 2013-09-16 19:44:00 -0700 | [diff] [blame] | 1164 | @property |
| 1165 | def constructor_params(self): |
Rob Vaterlaus | 6035bf5 | 2013-09-17 19:32:38 -0700 | [diff] [blame] | 1166 | return [ m.value(self) for m in self.enum.metadata.properties ] |
Andreas Wundsam | 8ec3bcc | 2013-09-16 19:44:00 -0700 | [diff] [blame] | 1167 | |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 1168 | def has_value(self, version): |
| 1169 | return version in self.values |
| 1170 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1171 | def value(self, version): |
Andreas Wundsam | bf1dbbd | 2013-07-30 11:07:59 -0700 | [diff] [blame] | 1172 | return self.values[version] |
| 1173 | |
| 1174 | def format_value(self, version): |
| 1175 | res = self.enum.wire_type(version).format_value(self.values[version]) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1176 | return res |
| 1177 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1178 | def all_values(self, versions, not_present=None): |
| 1179 | 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] | 1180 | |
| 1181 | @property |
| 1182 | @memoize |
| 1183 | def masked_enum_group(self): |
| 1184 | group = find(lambda g: self.name in g.members, model.masked_enum_groups[self.enum.name]) |
| 1185 | return group |
| 1186 | |
| 1187 | @property |
| 1188 | @memoize |
| 1189 | def is_mask(self): |
| 1190 | return any(self.name == g.mask for g in model.masked_enum_groups[self.enum.name]) |