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