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