Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | */ |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 16 | package org.onlab.onos.provider.of.flow.impl; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.flow.FlowRule; |
| 21 | import org.onlab.onos.net.flow.TrafficSelector; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.flow.criteria.Criteria.EthCriterion; |
| 23 | import org.onlab.onos.net.flow.criteria.Criteria.EthTypeCriterion; |
| 24 | import org.onlab.onos.net.flow.criteria.Criteria.IPCriterion; |
| 25 | import org.onlab.onos.net.flow.criteria.Criteria.IPProtocolCriterion; |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.flow.criteria.Criteria.LambdaCriterion; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.flow.criteria.Criteria.PortCriterion; |
Jonathan Hart | 34bc614 | 2014-10-17 11:00:43 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.flow.criteria.Criteria.TcpPortCriterion; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.flow.criteria.Criteria.VlanIdCriterion; |
| 30 | import org.onlab.onos.net.flow.criteria.Criteria.VlanPcpCriterion; |
| 31 | import org.onlab.onos.net.flow.criteria.Criterion; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 32 | import org.projectfloodlight.openflow.protocol.OFFactory; |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 33 | import org.projectfloodlight.openflow.protocol.OFFlowAdd; |
| 34 | import org.projectfloodlight.openflow.protocol.OFFlowDelete; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 35 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 36 | import org.projectfloodlight.openflow.protocol.match.Match; |
| 37 | import org.projectfloodlight.openflow.protocol.match.MatchField; |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 38 | import org.projectfloodlight.openflow.types.CircuitSignalID; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 39 | import org.projectfloodlight.openflow.types.EthType; |
| 40 | import org.projectfloodlight.openflow.types.IPv4Address; |
| 41 | import org.projectfloodlight.openflow.types.IpProtocol; |
| 42 | import org.projectfloodlight.openflow.types.MacAddress; |
| 43 | import org.projectfloodlight.openflow.types.Masked; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 44 | import org.projectfloodlight.openflow.types.OFPort; |
| 45 | import org.projectfloodlight.openflow.types.OFVlanVidMatch; |
Jonathan Hart | 34bc614 | 2014-10-17 11:00:43 -0700 | [diff] [blame] | 46 | import org.projectfloodlight.openflow.types.TransportPort; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 47 | import org.projectfloodlight.openflow.types.VlanPcp; |
| 48 | import org.projectfloodlight.openflow.types.VlanVid; |
| 49 | import org.slf4j.Logger; |
| 50 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Builder for OpenFlow flow mods based on FlowRules. |
| 53 | */ |
| 54 | public abstract class FlowModBuilder { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 55 | |
| 56 | private final Logger log = getLogger(getClass()); |
| 57 | |
| 58 | private final OFFactory factory; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 59 | private final FlowRule flowRule; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 60 | private final TrafficSelector selector; |
| 61 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Creates a new flow mod builder. |
| 64 | * |
| 65 | * @param flowRule the flow rule to transform into a flow mod |
| 66 | * @param factory the OpenFlow factory to use to build the flow mod |
| 67 | * @return the new flow mod builder |
| 68 | */ |
| 69 | public static FlowModBuilder builder(FlowRule flowRule, OFFactory factory) { |
| 70 | switch (factory.getVersion()) { |
| 71 | case OF_10: |
| 72 | return new FlowModBuilderVer10(flowRule, factory); |
| 73 | case OF_13: |
| 74 | return new FlowModBuilderVer13(flowRule, factory); |
| 75 | default: |
| 76 | throw new UnsupportedOperationException( |
| 77 | "No flow mod builder for protocol version " + factory.getVersion()); |
| 78 | } |
| 79 | } |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 80 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Constructs a flow mod builder. |
| 83 | * |
| 84 | * @param flowRule the flow rule to transform into a flow mod |
| 85 | * @param factory the OpenFlow factory to use to build the flow mod |
| 86 | */ |
| 87 | protected FlowModBuilder(FlowRule flowRule, OFFactory factory) { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 88 | this.factory = factory; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 89 | this.flowRule = flowRule; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 90 | this.selector = flowRule.selector(); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 93 | /** |
| 94 | * Builds an ADD flow mod. |
| 95 | * |
| 96 | * @return the flow mod |
| 97 | */ |
| 98 | public abstract OFFlowAdd buildFlowAdd(); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 99 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 100 | /** |
| 101 | * Builds a MODIFY flow mod. |
| 102 | * |
| 103 | * @return the flow mod |
| 104 | */ |
| 105 | public abstract OFFlowMod buildFlowMod(); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 106 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 107 | /** |
| 108 | * Builds a DELETE flow mod. |
| 109 | * |
| 110 | * @return the flow mod |
| 111 | */ |
| 112 | public abstract OFFlowDelete buildFlowDel(); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 113 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Builds the match for the flow mod. |
| 116 | * |
| 117 | * @return the match |
| 118 | */ |
| 119 | protected Match buildMatch() { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 120 | Match.Builder mBuilder = factory.buildMatch(); |
| 121 | EthCriterion eth; |
| 122 | IPCriterion ip; |
Jonathan Hart | 34bc614 | 2014-10-17 11:00:43 -0700 | [diff] [blame] | 123 | TcpPortCriterion tp; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 124 | for (Criterion c : selector.criteria()) { |
| 125 | switch (c.type()) { |
| 126 | case IN_PORT: |
| 127 | PortCriterion inport = (PortCriterion) c; |
| 128 | mBuilder.setExact(MatchField.IN_PORT, OFPort.of((int) inport.port().toLong())); |
| 129 | break; |
| 130 | case ETH_SRC: |
| 131 | eth = (EthCriterion) c; |
| 132 | mBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(eth.mac().toLong())); |
| 133 | break; |
| 134 | case ETH_DST: |
| 135 | eth = (EthCriterion) c; |
| 136 | mBuilder.setExact(MatchField.ETH_DST, MacAddress.of(eth.mac().toLong())); |
| 137 | break; |
| 138 | case ETH_TYPE: |
| 139 | EthTypeCriterion ethType = (EthTypeCriterion) c; |
| 140 | mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(ethType.ethType())); |
| 141 | break; |
| 142 | case IPV4_DST: |
| 143 | ip = (IPCriterion) c; |
| 144 | if (ip.ip().isMasked()) { |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 145 | Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip.ip().toInt()), |
| 146 | IPv4Address.of(ip.ip().netmask().toInt())); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 147 | mBuilder.setMasked(MatchField.IPV4_DST, maskedIp); |
| 148 | } else { |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 149 | mBuilder.setExact(MatchField.IPV4_DST, IPv4Address.of(ip.ip().toInt())); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 150 | } |
| 151 | break; |
| 152 | case IPV4_SRC: |
| 153 | ip = (IPCriterion) c; |
| 154 | if (ip.ip().isMasked()) { |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 155 | Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip.ip().toInt()), |
| 156 | IPv4Address.of(ip.ip().netmask().toInt())); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 157 | mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp); |
| 158 | } else { |
Jonathan Hart | bcae7bd | 2014-10-16 10:24:41 -0700 | [diff] [blame] | 159 | mBuilder.setExact(MatchField.IPV4_SRC, IPv4Address.of(ip.ip().toInt())); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 160 | } |
| 161 | break; |
| 162 | case IP_PROTO: |
| 163 | IPProtocolCriterion p = (IPProtocolCriterion) c; |
| 164 | mBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(p.protocol())); |
| 165 | break; |
| 166 | case VLAN_PCP: |
| 167 | VlanPcpCriterion vpcp = (VlanPcpCriterion) c; |
| 168 | mBuilder.setExact(MatchField.VLAN_PCP, VlanPcp.of(vpcp.priority())); |
| 169 | break; |
| 170 | case VLAN_VID: |
| 171 | VlanIdCriterion vid = (VlanIdCriterion) c; |
| 172 | mBuilder.setExact(MatchField.VLAN_VID, |
| 173 | OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vid.vlanId().toShort()))); |
| 174 | break; |
Jonathan Hart | 34bc614 | 2014-10-17 11:00:43 -0700 | [diff] [blame] | 175 | case TCP_DST: |
| 176 | tp = (TcpPortCriterion) c; |
| 177 | mBuilder.setExact(MatchField.TCP_DST, TransportPort.of(tp.tcpPort())); |
| 178 | break; |
| 179 | case TCP_SRC: |
| 180 | tp = (TcpPortCriterion) c; |
| 181 | mBuilder.setExact(MatchField.TCP_SRC, TransportPort.of(tp.tcpPort())); |
| 182 | break; |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 183 | case OCH_SIGID: |
| 184 | LambdaCriterion lc = (LambdaCriterion) c; |
| 185 | mBuilder.setExact(MatchField.OCH_SIGID, |
| 186 | new CircuitSignalID((byte) 1, (byte) 2, lc.lambda(), (short) 1)); |
| 187 | break; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 188 | case ARP_OP: |
| 189 | case ARP_SHA: |
| 190 | case ARP_SPA: |
| 191 | case ARP_THA: |
| 192 | case ARP_TPA: |
| 193 | case ICMPV4_CODE: |
| 194 | case ICMPV4_TYPE: |
| 195 | case ICMPV6_CODE: |
| 196 | case ICMPV6_TYPE: |
| 197 | case IN_PHY_PORT: |
| 198 | case IPV6_DST: |
| 199 | case IPV6_EXTHDR: |
| 200 | case IPV6_FLABEL: |
| 201 | case IPV6_ND_SLL: |
| 202 | case IPV6_ND_TARGET: |
| 203 | case IPV6_ND_TLL: |
| 204 | case IPV6_SRC: |
| 205 | case IP_DSCP: |
| 206 | case IP_ECN: |
| 207 | case METADATA: |
| 208 | case MPLS_BOS: |
| 209 | case MPLS_LABEL: |
| 210 | case MPLS_TC: |
| 211 | case PBB_ISID: |
| 212 | case SCTP_DST: |
| 213 | case SCTP_SRC: |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 214 | case TUNNEL_ID: |
| 215 | case UDP_DST: |
| 216 | case UDP_SRC: |
| 217 | default: |
| 218 | log.warn("Match type {} not yet implemented.", c.type()); |
| 219 | } |
| 220 | } |
| 221 | return mBuilder.build(); |
| 222 | } |
| 223 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 224 | /** |
| 225 | * Returns the flow rule for this builder. |
| 226 | * |
| 227 | * @return the flow rule |
| 228 | */ |
| 229 | protected FlowRule flowRule() { |
| 230 | return flowRule; |
| 231 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 232 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 233 | /** |
| 234 | * Returns the factory used for building OpenFlow constructs. |
| 235 | * |
| 236 | * @return the factory |
| 237 | */ |
| 238 | protected OFFactory factory() { |
| 239 | return factory; |
| 240 | } |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 241 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 242 | } |