Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.of.flow.impl; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 18 | import org.onlab.packet.Ip4Address; |
| 19 | import org.onlab.packet.Ip4Prefix; |
Charles M.C. Chan | 52fae7d | 2015-01-17 00:35:53 +0800 | [diff] [blame] | 20 | import org.onlab.packet.Ip6Address; |
| 21 | import org.onlab.packet.Ip6Prefix; |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 22 | import org.onlab.packet.VlanId; |
Jonathan Hart | 26a8d95 | 2015-12-02 15:16:35 -0800 | [diff] [blame^] | 23 | import org.onosproject.net.DeviceId; |
Sho SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 24 | import org.onosproject.net.OchSignal; |
Jonathan Hart | 26a8d95 | 2015-12-02 15:16:35 -0800 | [diff] [blame^] | 25 | import org.onosproject.net.driver.DefaultDriverData; |
| 26 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 27 | import org.onosproject.net.driver.Driver; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 28 | import org.onosproject.net.driver.DriverService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.flow.FlowRule; |
| 30 | import org.onosproject.net.flow.TrafficSelector; |
BitOhenry | 7643085 | 2015-11-20 19:04:49 +0800 | [diff] [blame] | 31 | import org.onosproject.net.flow.criteria.ArpHaCriterion; |
BitOhenry | b40129a | 2015-11-30 12:41:18 +0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.criteria.ArpOpCriterion; |
BitOhenry | c1e5fcc | 2015-11-23 20:47:53 +0800 | [diff] [blame] | 33 | import org.onosproject.net.flow.criteria.ArpPaCriterion; |
Saurav Das | ffc5bbc | 2015-08-18 23:30:19 -0700 | [diff] [blame] | 34 | import org.onosproject.net.flow.criteria.Criterion; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 35 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 36 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
Jonathan Hart | 26a8d95 | 2015-12-02 15:16:35 -0800 | [diff] [blame^] | 37 | import org.onosproject.net.flow.criteria.ExtensionCriterion; |
| 38 | import org.onosproject.net.flow.criteria.ExtensionSelector; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 39 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 40 | import org.onosproject.net.flow.criteria.IPDscpCriterion; |
| 41 | import org.onosproject.net.flow.criteria.IPEcnCriterion; |
| 42 | import org.onosproject.net.flow.criteria.IPProtocolCriterion; |
| 43 | import org.onosproject.net.flow.criteria.IPv6ExthdrFlagsCriterion; |
| 44 | import org.onosproject.net.flow.criteria.IPv6FlowLabelCriterion; |
| 45 | import org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion; |
| 46 | import org.onosproject.net.flow.criteria.IPv6NDTargetAddressCriterion; |
| 47 | import org.onosproject.net.flow.criteria.IcmpCodeCriterion; |
| 48 | import org.onosproject.net.flow.criteria.IcmpTypeCriterion; |
| 49 | import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion; |
| 50 | import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 51 | import org.onosproject.net.flow.criteria.MetadataCriterion; |
Saurav Das | ffc5bbc | 2015-08-18 23:30:19 -0700 | [diff] [blame] | 52 | import org.onosproject.net.flow.criteria.MplsBosCriterion; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 53 | import org.onosproject.net.flow.criteria.MplsCriterion; |
Sho SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 54 | import org.onosproject.net.flow.criteria.OchSignalCriterion; |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 55 | import org.onosproject.net.flow.criteria.OchSignalTypeCriterion; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 56 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 57 | import org.onosproject.net.flow.criteria.SctpPortCriterion; |
| 58 | import org.onosproject.net.flow.criteria.TcpPortCriterion; |
Hyunsun Moon | a08c5d0 | 2015-07-14 17:53:00 -0700 | [diff] [blame] | 59 | import org.onosproject.net.flow.criteria.TunnelIdCriterion; |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 60 | import org.onosproject.net.flow.criteria.UdpPortCriterion; |
| 61 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 62 | import org.onosproject.net.flow.criteria.VlanPcpCriterion; |
Jonathan Hart | 26a8d95 | 2015-12-02 15:16:35 -0800 | [diff] [blame^] | 63 | import org.onosproject.openflow.controller.ExtensionSelectorInterpreter; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 64 | import org.projectfloodlight.openflow.protocol.OFFactory; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 65 | import org.projectfloodlight.openflow.protocol.OFFlowAdd; |
| 66 | import org.projectfloodlight.openflow.protocol.OFFlowDelete; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 67 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 68 | import org.projectfloodlight.openflow.protocol.match.Match; |
| 69 | import org.projectfloodlight.openflow.protocol.match.MatchField; |
Jonathan Hart | 26a8d95 | 2015-12-02 15:16:35 -0800 | [diff] [blame^] | 70 | import org.projectfloodlight.openflow.protocol.oxm.OFOxm; |
BitOhenry | b40129a | 2015-11-30 12:41:18 +0800 | [diff] [blame] | 71 | import org.projectfloodlight.openflow.types.ArpOpcode; |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 72 | import org.projectfloodlight.openflow.types.CircuitSignalID; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 73 | import org.projectfloodlight.openflow.types.EthType; |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 74 | import org.projectfloodlight.openflow.types.ICMPv4Code; |
| 75 | import org.projectfloodlight.openflow.types.ICMPv4Type; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 76 | import org.projectfloodlight.openflow.types.IPv4Address; |
Charles M.C. Chan | 52fae7d | 2015-01-17 00:35:53 +0800 | [diff] [blame] | 77 | import org.projectfloodlight.openflow.types.IPv6Address; |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 78 | import org.projectfloodlight.openflow.types.IPv6FlowLabel; |
Pavlin Radoslavov | d0fd841 | 2015-02-04 13:57:00 -0800 | [diff] [blame] | 79 | import org.projectfloodlight.openflow.types.IpDscp; |
| 80 | import org.projectfloodlight.openflow.types.IpEcn; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 81 | import org.projectfloodlight.openflow.types.IpProtocol; |
| 82 | import org.projectfloodlight.openflow.types.MacAddress; |
| 83 | import org.projectfloodlight.openflow.types.Masked; |
Saurav Das | ffc5bbc | 2015-08-18 23:30:19 -0700 | [diff] [blame] | 84 | import org.projectfloodlight.openflow.types.OFBooleanValue; |
Pavlin Radoslavov | d0fd841 | 2015-02-04 13:57:00 -0800 | [diff] [blame] | 85 | import org.projectfloodlight.openflow.types.OFMetadata; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 86 | import org.projectfloodlight.openflow.types.OFPort; |
| 87 | import org.projectfloodlight.openflow.types.OFVlanVidMatch; |
Jonathan Hart | 34bc614 | 2014-10-17 11:00:43 -0700 | [diff] [blame] | 88 | import org.projectfloodlight.openflow.types.TransportPort; |
Pavlin Radoslavov | 5e4f754 | 2015-02-06 18:18:21 -0800 | [diff] [blame] | 89 | import org.projectfloodlight.openflow.types.U16; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 90 | import org.projectfloodlight.openflow.types.U32; |
Hyunsun Moon | a08c5d0 | 2015-07-14 17:53:00 -0700 | [diff] [blame] | 91 | import org.projectfloodlight.openflow.types.U64; |
Praseed Balakrishnan | 2dd5abd | 2014-11-03 14:56:28 -0800 | [diff] [blame] | 92 | import org.projectfloodlight.openflow.types.U8; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 93 | import org.projectfloodlight.openflow.types.VlanPcp; |
| 94 | import org.projectfloodlight.openflow.types.VlanVid; |
| 95 | import org.slf4j.Logger; |
| 96 | |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 97 | import java.util.Optional; |
| 98 | |
| 99 | import static org.slf4j.LoggerFactory.getLogger; |
| 100 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Builder for OpenFlow flow mods based on FlowRules. |
| 103 | */ |
| 104 | public abstract class FlowModBuilder { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 105 | |
| 106 | private final Logger log = getLogger(getClass()); |
| 107 | |
| 108 | private final OFFactory factory; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 109 | private final FlowRule flowRule; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 110 | private final TrafficSelector selector; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 111 | protected final Long xid; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 112 | protected final Optional<DriverService> driverService; |
Jonathan Hart | 26a8d95 | 2015-12-02 15:16:35 -0800 | [diff] [blame^] | 113 | protected final DeviceId deviceId; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 114 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 115 | /** |
| 116 | * Creates a new flow mod builder. |
| 117 | * |
| 118 | * @param flowRule the flow rule to transform into a flow mod |
| 119 | * @param factory the OpenFlow factory to use to build the flow mod |
Pavlin Radoslavov | 119fd5c | 2014-11-25 19:08:19 -0800 | [diff] [blame] | 120 | * @param xid the transaction ID |
Charles Chan | 30ba400 | 2015-11-05 14:45:16 -0800 | [diff] [blame] | 121 | * @param driverService the device driver service |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 122 | * @return the new flow mod builder |
| 123 | */ |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 124 | public static FlowModBuilder builder(FlowRule flowRule, |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 125 | OFFactory factory, |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 126 | Optional<Long> xid, |
| 127 | Optional<DriverService> driverService) { |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 128 | switch (factory.getVersion()) { |
| 129 | case OF_10: |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 130 | return new FlowModBuilderVer10(flowRule, factory, xid, driverService); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 131 | case OF_13: |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 132 | return new FlowModBuilderVer13(flowRule, factory, xid, driverService); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 133 | default: |
| 134 | throw new UnsupportedOperationException( |
| 135 | "No flow mod builder for protocol version " + factory.getVersion()); |
| 136 | } |
| 137 | } |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 138 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 139 | /** |
| 140 | * Constructs a flow mod builder. |
| 141 | * |
| 142 | * @param flowRule the flow rule to transform into a flow mod |
| 143 | * @param factory the OpenFlow factory to use to build the flow mod |
Charles Chan | 30ba400 | 2015-11-05 14:45:16 -0800 | [diff] [blame] | 144 | * @param driverService the device driver service |
Pavlin Radoslavov | 119fd5c | 2014-11-25 19:08:19 -0800 | [diff] [blame] | 145 | * @param xid the transaction ID |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 146 | */ |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 147 | protected FlowModBuilder(FlowRule flowRule, OFFactory factory, Optional<Long> xid, |
| 148 | Optional<DriverService> driverService) { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 149 | this.factory = factory; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 150 | this.flowRule = flowRule; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 151 | this.selector = flowRule.selector(); |
Sho SHIMIZU | c14b2a2 | 2015-05-05 18:15:13 -0700 | [diff] [blame] | 152 | this.xid = xid.orElse(0L); |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 153 | this.driverService = driverService; |
Jonathan Hart | 26a8d95 | 2015-12-02 15:16:35 -0800 | [diff] [blame^] | 154 | this.deviceId = flowRule.deviceId(); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 157 | /** |
| 158 | * Builds an ADD flow mod. |
| 159 | * |
| 160 | * @return the flow mod |
| 161 | */ |
| 162 | public abstract OFFlowAdd buildFlowAdd(); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 163 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 164 | /** |
| 165 | * Builds a MODIFY flow mod. |
| 166 | * |
| 167 | * @return the flow mod |
| 168 | */ |
| 169 | public abstract OFFlowMod buildFlowMod(); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 170 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 171 | /** |
| 172 | * Builds a DELETE flow mod. |
| 173 | * |
| 174 | * @return the flow mod |
| 175 | */ |
| 176 | public abstract OFFlowDelete buildFlowDel(); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 177 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 178 | /** |
| 179 | * Builds the match for the flow mod. |
| 180 | * |
| 181 | * @return the match |
| 182 | */ |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 183 | // CHECKSTYLE IGNORE MethodLength FOR NEXT 300 LINES |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 184 | protected Match buildMatch() { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 185 | Match.Builder mBuilder = factory.buildMatch(); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 186 | Ip6Address ip6Address; |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 187 | Ip4Prefix ip4Prefix; |
Charles M.C. Chan | 52fae7d | 2015-01-17 00:35:53 +0800 | [diff] [blame] | 188 | Ip6Prefix ip6Prefix; |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 189 | EthCriterion ethCriterion; |
| 190 | IPCriterion ipCriterion; |
| 191 | TcpPortCriterion tcpPortCriterion; |
| 192 | UdpPortCriterion udpPortCriterion; |
| 193 | SctpPortCriterion sctpPortCriterion; |
| 194 | IPv6NDLinkLayerAddressCriterion llAddressCriterion; |
BitOhenry | 7643085 | 2015-11-20 19:04:49 +0800 | [diff] [blame] | 195 | ArpHaCriterion arpHaCriterion; |
BitOhenry | c1e5fcc | 2015-11-23 20:47:53 +0800 | [diff] [blame] | 196 | ArpPaCriterion arpPaCriterion; |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 197 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 198 | for (Criterion c : selector.criteria()) { |
| 199 | switch (c.type()) { |
| 200 | case IN_PORT: |
Pavlin Radoslavov | d0fd841 | 2015-02-04 13:57:00 -0800 | [diff] [blame] | 201 | PortCriterion inPort = (PortCriterion) c; |
| 202 | mBuilder.setExact(MatchField.IN_PORT, |
| 203 | OFPort.of((int) inPort.port().toLong())); |
| 204 | break; |
| 205 | case IN_PHY_PORT: |
| 206 | PortCriterion inPhyPort = (PortCriterion) c; |
| 207 | mBuilder.setExact(MatchField.IN_PORT, |
| 208 | OFPort.of((int) inPhyPort.port().toLong())); |
| 209 | break; |
| 210 | case METADATA: |
| 211 | MetadataCriterion metadata = (MetadataCriterion) c; |
| 212 | mBuilder.setExact(MatchField.METADATA, |
| 213 | OFMetadata.ofRaw(metadata.metadata())); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 214 | break; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 215 | case ETH_DST: |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 216 | ethCriterion = (EthCriterion) c; |
| 217 | mBuilder.setExact(MatchField.ETH_DST, |
| 218 | MacAddress.of(ethCriterion.mac().toLong())); |
| 219 | break; |
| 220 | case ETH_SRC: |
| 221 | ethCriterion = (EthCriterion) c; |
| 222 | mBuilder.setExact(MatchField.ETH_SRC, |
| 223 | MacAddress.of(ethCriterion.mac().toLong())); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 224 | break; |
| 225 | case ETH_TYPE: |
| 226 | EthTypeCriterion ethType = (EthTypeCriterion) c; |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 227 | mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(ethType.ethType().toShort())); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 228 | break; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 229 | case VLAN_VID: |
| 230 | VlanIdCriterion vid = (VlanIdCriterion) c; |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 231 | |
| 232 | if (vid.vlanId().equals(VlanId.ANY)) { |
| 233 | mBuilder.setMasked(MatchField.VLAN_VID, OFVlanVidMatch.PRESENT, |
| 234 | OFVlanVidMatch.PRESENT); |
Jonathan Hart | 1468fee | 2015-07-16 18:50:34 -0700 | [diff] [blame] | 235 | } else if (vid.vlanId().equals(VlanId.NONE)) { |
| 236 | mBuilder.setExact(MatchField.VLAN_VID, OFVlanVidMatch.NONE); |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 237 | } else { |
| 238 | mBuilder.setExact(MatchField.VLAN_VID, |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 239 | OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vid.vlanId().toShort()))); |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 240 | } |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 241 | break; |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 242 | case VLAN_PCP: |
| 243 | VlanPcpCriterion vpcp = (VlanPcpCriterion) c; |
| 244 | mBuilder.setExact(MatchField.VLAN_PCP, VlanPcp.of(vpcp.priority())); |
| 245 | break; |
Pavlin Radoslavov | d0fd841 | 2015-02-04 13:57:00 -0800 | [diff] [blame] | 246 | case IP_DSCP: |
| 247 | IPDscpCriterion ipDscpCriterion = (IPDscpCriterion) c; |
| 248 | mBuilder.setExact(MatchField.IP_DSCP, |
| 249 | IpDscp.of(ipDscpCriterion.ipDscp())); |
| 250 | break; |
| 251 | case IP_ECN: |
| 252 | IPEcnCriterion ipEcnCriterion = (IPEcnCriterion) c; |
| 253 | mBuilder.setExact(MatchField.IP_ECN, |
| 254 | IpEcn.of(ipEcnCriterion.ipEcn())); |
| 255 | break; |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 256 | case IP_PROTO: |
| 257 | IPProtocolCriterion p = (IPProtocolCriterion) c; |
| 258 | mBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(p.protocol())); |
| 259 | break; |
| 260 | case IPV4_SRC: |
| 261 | ipCriterion = (IPCriterion) c; |
| 262 | ip4Prefix = ipCriterion.ip().getIp4Prefix(); |
| 263 | if (ip4Prefix.prefixLength() != Ip4Prefix.MAX_MASK_LENGTH) { |
| 264 | Ip4Address maskAddr = |
| 265 | Ip4Address.makeMaskPrefix(ip4Prefix.prefixLength()); |
| 266 | Masked<IPv4Address> maskedIp = |
| 267 | Masked.of(IPv4Address.of(ip4Prefix.address().toInt()), |
| 268 | IPv4Address.of(maskAddr.toInt())); |
| 269 | mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp); |
| 270 | } else { |
| 271 | mBuilder.setExact(MatchField.IPV4_SRC, |
| 272 | IPv4Address.of(ip4Prefix.address().toInt())); |
| 273 | } |
| 274 | break; |
| 275 | case IPV4_DST: |
| 276 | ipCriterion = (IPCriterion) c; |
| 277 | ip4Prefix = ipCriterion.ip().getIp4Prefix(); |
| 278 | if (ip4Prefix.prefixLength() != Ip4Prefix.MAX_MASK_LENGTH) { |
| 279 | Ip4Address maskAddr = |
| 280 | Ip4Address.makeMaskPrefix(ip4Prefix.prefixLength()); |
| 281 | Masked<IPv4Address> maskedIp = |
| 282 | Masked.of(IPv4Address.of(ip4Prefix.address().toInt()), |
| 283 | IPv4Address.of(maskAddr.toInt())); |
| 284 | mBuilder.setMasked(MatchField.IPV4_DST, maskedIp); |
| 285 | } else { |
| 286 | mBuilder.setExact(MatchField.IPV4_DST, |
| 287 | IPv4Address.of(ip4Prefix.address().toInt())); |
| 288 | } |
Jonathan Hart | 34bc614 | 2014-10-17 11:00:43 -0700 | [diff] [blame] | 289 | break; |
| 290 | case TCP_SRC: |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 291 | tcpPortCriterion = (TcpPortCriterion) c; |
| 292 | mBuilder.setExact(MatchField.TCP_SRC, |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 293 | TransportPort.of(tcpPortCriterion.tcpPort().toInt())); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 294 | break; |
| 295 | case TCP_DST: |
| 296 | tcpPortCriterion = (TcpPortCriterion) c; |
| 297 | mBuilder.setExact(MatchField.TCP_DST, |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 298 | TransportPort.of(tcpPortCriterion.tcpPort().toInt())); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 299 | break; |
| 300 | case UDP_SRC: |
| 301 | udpPortCriterion = (UdpPortCriterion) c; |
| 302 | mBuilder.setExact(MatchField.UDP_SRC, |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 303 | TransportPort.of(udpPortCriterion.udpPort().toInt())); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 304 | break; |
| 305 | case UDP_DST: |
| 306 | udpPortCriterion = (UdpPortCriterion) c; |
| 307 | mBuilder.setExact(MatchField.UDP_DST, |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 308 | TransportPort.of(udpPortCriterion.udpPort().toInt())); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 309 | break; |
| 310 | case SCTP_SRC: |
| 311 | sctpPortCriterion = (SctpPortCriterion) c; |
| 312 | mBuilder.setExact(MatchField.SCTP_SRC, |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 313 | TransportPort.of(sctpPortCriterion.sctpPort().toInt())); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 314 | break; |
| 315 | case SCTP_DST: |
| 316 | sctpPortCriterion = (SctpPortCriterion) c; |
| 317 | mBuilder.setExact(MatchField.SCTP_DST, |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 318 | TransportPort.of(sctpPortCriterion.sctpPort().toInt())); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 319 | break; |
| 320 | case ICMPV4_TYPE: |
| 321 | IcmpTypeCriterion icmpType = (IcmpTypeCriterion) c; |
| 322 | mBuilder.setExact(MatchField.ICMPV4_TYPE, |
| 323 | ICMPv4Type.of(icmpType.icmpType())); |
| 324 | break; |
| 325 | case ICMPV4_CODE: |
| 326 | IcmpCodeCriterion icmpCode = (IcmpCodeCriterion) c; |
| 327 | mBuilder.setExact(MatchField.ICMPV4_CODE, |
| 328 | ICMPv4Code.of(icmpCode.icmpCode())); |
| 329 | break; |
| 330 | case IPV6_SRC: |
| 331 | ipCriterion = (IPCriterion) c; |
| 332 | ip6Prefix = ipCriterion.ip().getIp6Prefix(); |
| 333 | if (ip6Prefix.prefixLength() != Ip6Prefix.MAX_MASK_LENGTH) { |
| 334 | Ip6Address maskAddr = |
| 335 | Ip6Address.makeMaskPrefix(ip6Prefix.prefixLength()); |
| 336 | Masked<IPv6Address> maskedIp = |
| 337 | Masked.of(IPv6Address.of(ip6Prefix.address().toString()), |
| 338 | IPv6Address.of(maskAddr.toString())); |
| 339 | mBuilder.setMasked(MatchField.IPV6_SRC, maskedIp); |
| 340 | } else { |
| 341 | mBuilder.setExact(MatchField.IPV6_SRC, |
| 342 | IPv6Address.of(ip6Prefix.address().toString())); |
| 343 | } |
| 344 | break; |
| 345 | case IPV6_DST: |
| 346 | ipCriterion = (IPCriterion) c; |
| 347 | ip6Prefix = ipCriterion.ip().getIp6Prefix(); |
| 348 | if (ip6Prefix.prefixLength() != Ip6Prefix.MAX_MASK_LENGTH) { |
| 349 | Ip6Address maskAddr = |
| 350 | Ip6Address.makeMaskPrefix(ip6Prefix.prefixLength()); |
| 351 | Masked<IPv6Address> maskedIp = |
| 352 | Masked.of(IPv6Address.of(ip6Prefix.address().toString()), |
| 353 | IPv6Address.of(maskAddr.toString())); |
| 354 | mBuilder.setMasked(MatchField.IPV6_DST, maskedIp); |
| 355 | } else { |
| 356 | mBuilder.setExact(MatchField.IPV6_DST, |
| 357 | IPv6Address.of(ip6Prefix.address().toString())); |
| 358 | } |
| 359 | break; |
| 360 | case IPV6_FLABEL: |
| 361 | IPv6FlowLabelCriterion flowLabelCriterion = |
| 362 | (IPv6FlowLabelCriterion) c; |
| 363 | mBuilder.setExact(MatchField.IPV6_FLABEL, |
| 364 | IPv6FlowLabel.of(flowLabelCriterion.flowLabel())); |
| 365 | break; |
| 366 | case ICMPV6_TYPE: |
| 367 | Icmpv6TypeCriterion icmpv6Type = (Icmpv6TypeCriterion) c; |
| 368 | mBuilder.setExact(MatchField.ICMPV6_TYPE, |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 369 | U8.of(icmpv6Type.icmpv6Type())); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 370 | break; |
| 371 | case ICMPV6_CODE: |
| 372 | Icmpv6CodeCriterion icmpv6Code = (Icmpv6CodeCriterion) c; |
| 373 | mBuilder.setExact(MatchField.ICMPV6_CODE, |
Pavlin Radoslavov | f3b6933 | 2015-02-06 15:47:05 -0800 | [diff] [blame] | 374 | U8.of(icmpv6Code.icmpv6Code())); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 375 | break; |
| 376 | case IPV6_ND_TARGET: |
| 377 | IPv6NDTargetAddressCriterion targetAddressCriterion = |
| 378 | (IPv6NDTargetAddressCriterion) c; |
| 379 | ip6Address = targetAddressCriterion.targetAddress(); |
| 380 | mBuilder.setExact(MatchField.IPV6_ND_TARGET, |
| 381 | IPv6Address.of(ip6Address.toOctets())); |
| 382 | break; |
| 383 | case IPV6_ND_SLL: |
| 384 | llAddressCriterion = |
| 385 | (IPv6NDLinkLayerAddressCriterion) c; |
| 386 | mBuilder.setExact(MatchField.IPV6_ND_SLL, |
| 387 | MacAddress.of(llAddressCriterion.mac().toLong())); |
| 388 | break; |
| 389 | case IPV6_ND_TLL: |
| 390 | llAddressCriterion = |
| 391 | (IPv6NDLinkLayerAddressCriterion) c; |
| 392 | mBuilder.setExact(MatchField.IPV6_ND_TLL, |
| 393 | MacAddress.of(llAddressCriterion.mac().toLong())); |
Jonathan Hart | 34bc614 | 2014-10-17 11:00:43 -0700 | [diff] [blame] | 394 | break; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 395 | case MPLS_LABEL: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 396 | MplsCriterion mp = (MplsCriterion) c; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 397 | mBuilder.setExact(MatchField.MPLS_LABEL, U32.of(mp.label().toInt())); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 398 | break; |
Pavlin Radoslavov | 5e4f754 | 2015-02-06 18:18:21 -0800 | [diff] [blame] | 399 | case IPV6_EXTHDR: |
Sho SHIMIZU | fbc80e5 | 2015-04-28 10:41:58 -0700 | [diff] [blame] | 400 | IPv6ExthdrFlagsCriterion exthdrFlagsCriterion = |
| 401 | (IPv6ExthdrFlagsCriterion) c; |
Pavlin Radoslavov | 5e4f754 | 2015-02-06 18:18:21 -0800 | [diff] [blame] | 402 | mBuilder.setExact(MatchField.IPV6_EXTHDR, |
| 403 | U16.of(exthdrFlagsCriterion.exthdrFlags())); |
| 404 | break; |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 405 | case OCH_SIGID: |
Sho SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 406 | try { |
| 407 | OchSignalCriterion ochSignalCriterion = (OchSignalCriterion) c; |
| 408 | OchSignal signal = ochSignalCriterion.lambda(); |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 409 | byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType()); |
| 410 | byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing()); |
Sho SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 411 | mBuilder.setExact(MatchField.OCH_SIGID, |
| 412 | new CircuitSignalID(gridType, channelSpacing, |
| 413 | (short) signal.spacingMultiplier(), (short) signal.slotGranularity())); |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 414 | } catch (NoMappingFoundException e) { |
Sho SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 415 | log.warn(e.getMessage()); |
| 416 | } |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 417 | break; |
Praseed Balakrishnan | 2dd5abd | 2014-11-03 14:56:28 -0800 | [diff] [blame] | 418 | case OCH_SIGTYPE: |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 419 | OchSignalTypeCriterion sc = (OchSignalTypeCriterion) c; |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 420 | byte signalType = OpenFlowValueMapper.lookupOchSignalType(sc.signalType()); |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 421 | mBuilder.setExact(MatchField.OCH_SIGTYPE, U8.of(signalType)); |
Praseed Balakrishnan | 2dd5abd | 2014-11-03 14:56:28 -0800 | [diff] [blame] | 422 | break; |
Hyunsun Moon | a08c5d0 | 2015-07-14 17:53:00 -0700 | [diff] [blame] | 423 | case TUNNEL_ID: |
| 424 | TunnelIdCriterion tunnelId = (TunnelIdCriterion) c; |
| 425 | mBuilder.setExact(MatchField.TUNNEL_ID, |
| 426 | U64.of(tunnelId.tunnelId())); |
| 427 | break; |
Saurav Das | ffc5bbc | 2015-08-18 23:30:19 -0700 | [diff] [blame] | 428 | case MPLS_BOS: |
| 429 | MplsBosCriterion mplsBos = (MplsBosCriterion) c; |
| 430 | mBuilder.setExact(MatchField.MPLS_BOS, |
| 431 | mplsBos.mplsBos() ? OFBooleanValue.TRUE |
| 432 | : OFBooleanValue.FALSE); |
| 433 | break; |
BitOhenry | b40129a | 2015-11-30 12:41:18 +0800 | [diff] [blame] | 434 | case ARP_OP: |
| 435 | ArpOpCriterion arpOp = (ArpOpCriterion) c; |
| 436 | mBuilder.setExact(MatchField.ARP_OP, |
| 437 | ArpOpcode.of(arpOp.arpOp())); |
| 438 | break; |
BitOhenry | a331b18 | 2015-11-23 08:39:37 +0800 | [diff] [blame] | 439 | case ARP_SHA: |
| 440 | arpHaCriterion = (ArpHaCriterion) c; |
| 441 | mBuilder.setExact(MatchField.ARP_SHA, |
| 442 | MacAddress.of(arpHaCriterion.mac().toLong())); |
| 443 | break; |
BitOhenry | 296b454 | 2015-11-24 08:41:58 +0800 | [diff] [blame] | 444 | case ARP_SPA: |
| 445 | arpPaCriterion = (ArpPaCriterion) c; |
| 446 | mBuilder.setExact(MatchField.ARP_SPA, |
| 447 | IPv4Address.of(arpPaCriterion.ip().toInt())); |
| 448 | break; |
BitOhenry | 7643085 | 2015-11-20 19:04:49 +0800 | [diff] [blame] | 449 | case ARP_THA: |
| 450 | arpHaCriterion = (ArpHaCriterion) c; |
| 451 | mBuilder.setExact(MatchField.ARP_THA, |
| 452 | MacAddress.of(arpHaCriterion.mac().toLong())); |
| 453 | break; |
BitOhenry | c1e5fcc | 2015-11-23 20:47:53 +0800 | [diff] [blame] | 454 | case ARP_TPA: |
| 455 | arpPaCriterion = (ArpPaCriterion) c; |
| 456 | mBuilder.setExact(MatchField.ARP_TPA, |
| 457 | IPv4Address.of(arpPaCriterion.ip().toInt())); |
| 458 | break; |
Jonathan Hart | 26a8d95 | 2015-12-02 15:16:35 -0800 | [diff] [blame^] | 459 | case EXTENSION: |
| 460 | ExtensionCriterion extensionCriterion = (ExtensionCriterion) c; |
| 461 | OFOxm oxm = buildExtensionOxm(extensionCriterion.extensionSelector()); |
| 462 | if (oxm == null) { |
| 463 | log.warn("Unable to build extension selector"); |
| 464 | break; |
| 465 | } |
| 466 | |
| 467 | if (oxm.isMasked()) { |
| 468 | mBuilder.setMasked(oxm.getMatchField(), oxm.getValue(), oxm.getMask()); |
| 469 | } else { |
| 470 | mBuilder.setExact(oxm.getMatchField(), oxm.getValue()); |
| 471 | } |
| 472 | |
| 473 | break; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 474 | case MPLS_TC: |
| 475 | case PBB_ISID: |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 476 | default: |
| 477 | log.warn("Match type {} not yet implemented.", c.type()); |
| 478 | } |
| 479 | } |
| 480 | return mBuilder.build(); |
| 481 | } |
| 482 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 483 | /** |
| 484 | * Returns the flow rule for this builder. |
| 485 | * |
| 486 | * @return the flow rule |
| 487 | */ |
| 488 | protected FlowRule flowRule() { |
| 489 | return flowRule; |
| 490 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 491 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 492 | /** |
| 493 | * Returns the factory used for building OpenFlow constructs. |
| 494 | * |
| 495 | * @return the factory |
| 496 | */ |
| 497 | protected OFFactory factory() { |
| 498 | return factory; |
| 499 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 500 | |
Jonathan Hart | 26a8d95 | 2015-12-02 15:16:35 -0800 | [diff] [blame^] | 501 | private OFOxm buildExtensionOxm(ExtensionSelector extension) { |
| 502 | if (!driverService.isPresent()) { |
| 503 | log.error("No driver service present"); |
| 504 | return null; |
| 505 | } |
| 506 | Driver driver = driverService.get().getDriver(deviceId); |
| 507 | if (driver.hasBehaviour(ExtensionSelectorInterpreter.class)) { |
| 508 | DefaultDriverHandler handler = |
| 509 | new DefaultDriverHandler(new DefaultDriverData(driver, deviceId)); |
| 510 | ExtensionSelectorInterpreter interpreter = handler.behaviour(ExtensionSelectorInterpreter.class); |
| 511 | |
| 512 | return interpreter.mapSelector(factory(), extension); |
| 513 | } |
| 514 | |
| 515 | return null; |
| 516 | } |
| 517 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 518 | } |