blob: 749b4a9f1ec70fd95a3744499d09fd8a668114d8 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
alshabibeec3a062014-09-17 18:01:26 -070016package org.onlab.onos.provider.of.flow.impl;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
alshabibeec3a062014-09-17 18:01:26 -070020import org.onlab.onos.net.flow.FlowRule;
21import org.onlab.onos.net.flow.TrafficSelector;
alshabibeec3a062014-09-17 18:01:26 -070022import org.onlab.onos.net.flow.criteria.Criteria.EthCriterion;
23import org.onlab.onos.net.flow.criteria.Criteria.EthTypeCriterion;
24import org.onlab.onos.net.flow.criteria.Criteria.IPCriterion;
25import org.onlab.onos.net.flow.criteria.Criteria.IPProtocolCriterion;
Marc De Leenheer49087752014-10-23 13:54:09 -070026import org.onlab.onos.net.flow.criteria.Criteria.LambdaCriterion;
alshabibeec3a062014-09-17 18:01:26 -070027import org.onlab.onos.net.flow.criteria.Criteria.PortCriterion;
Jonathan Hart34bc6142014-10-17 11:00:43 -070028import org.onlab.onos.net.flow.criteria.Criteria.TcpPortCriterion;
alshabibeec3a062014-09-17 18:01:26 -070029import org.onlab.onos.net.flow.criteria.Criteria.VlanIdCriterion;
30import org.onlab.onos.net.flow.criteria.Criteria.VlanPcpCriterion;
31import org.onlab.onos.net.flow.criteria.Criterion;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070032import org.onlab.packet.IpAddress;
33import org.onlab.packet.IpPrefix;
alshabibeec3a062014-09-17 18:01:26 -070034import org.projectfloodlight.openflow.protocol.OFFactory;
alshabib193525b2014-10-08 18:58:03 -070035import org.projectfloodlight.openflow.protocol.OFFlowAdd;
36import org.projectfloodlight.openflow.protocol.OFFlowDelete;
alshabibeec3a062014-09-17 18:01:26 -070037import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabibeec3a062014-09-17 18:01:26 -070038import org.projectfloodlight.openflow.protocol.match.Match;
39import org.projectfloodlight.openflow.protocol.match.MatchField;
Marc De Leenheer49087752014-10-23 13:54:09 -070040import org.projectfloodlight.openflow.types.CircuitSignalID;
alshabibeec3a062014-09-17 18:01:26 -070041import org.projectfloodlight.openflow.types.EthType;
42import org.projectfloodlight.openflow.types.IPv4Address;
43import org.projectfloodlight.openflow.types.IpProtocol;
44import org.projectfloodlight.openflow.types.MacAddress;
45import org.projectfloodlight.openflow.types.Masked;
alshabibeec3a062014-09-17 18:01:26 -070046import org.projectfloodlight.openflow.types.OFPort;
47import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Jonathan Hart34bc6142014-10-17 11:00:43 -070048import org.projectfloodlight.openflow.types.TransportPort;
alshabibeec3a062014-09-17 18:01:26 -070049import org.projectfloodlight.openflow.types.VlanPcp;
50import org.projectfloodlight.openflow.types.VlanVid;
51import org.slf4j.Logger;
52
Jonathan Hart86e59352014-10-22 10:42:16 -070053/**
54 * Builder for OpenFlow flow mods based on FlowRules.
55 */
56public abstract class FlowModBuilder {
alshabibeec3a062014-09-17 18:01:26 -070057
58 private final Logger log = getLogger(getClass());
59
60 private final OFFactory factory;
Jonathan Hart86e59352014-10-22 10:42:16 -070061 private final FlowRule flowRule;
alshabibeec3a062014-09-17 18:01:26 -070062 private final TrafficSelector selector;
63
Jonathan Hart86e59352014-10-22 10:42:16 -070064 /**
65 * Creates a new flow mod builder.
66 *
67 * @param flowRule the flow rule to transform into a flow mod
68 * @param factory the OpenFlow factory to use to build the flow mod
69 * @return the new flow mod builder
70 */
71 public static FlowModBuilder builder(FlowRule flowRule, OFFactory factory) {
72 switch (factory.getVersion()) {
73 case OF_10:
74 return new FlowModBuilderVer10(flowRule, factory);
75 case OF_13:
76 return new FlowModBuilderVer13(flowRule, factory);
77 default:
78 throw new UnsupportedOperationException(
79 "No flow mod builder for protocol version " + factory.getVersion());
80 }
81 }
alshabibeec3a062014-09-17 18:01:26 -070082
Jonathan Hart86e59352014-10-22 10:42:16 -070083 /**
84 * Constructs a flow mod builder.
85 *
86 * @param flowRule the flow rule to transform into a flow mod
87 * @param factory the OpenFlow factory to use to build the flow mod
88 */
89 protected FlowModBuilder(FlowRule flowRule, OFFactory factory) {
alshabibeec3a062014-09-17 18:01:26 -070090 this.factory = factory;
Jonathan Hart86e59352014-10-22 10:42:16 -070091 this.flowRule = flowRule;
alshabibeec3a062014-09-17 18:01:26 -070092 this.selector = flowRule.selector();
alshabibeec3a062014-09-17 18:01:26 -070093 }
94
Jonathan Hart86e59352014-10-22 10:42:16 -070095 /**
96 * Builds an ADD flow mod.
97 *
98 * @return the flow mod
99 */
100 public abstract OFFlowAdd buildFlowAdd();
alshabibeec3a062014-09-17 18:01:26 -0700101
Jonathan Hart86e59352014-10-22 10:42:16 -0700102 /**
103 * Builds a MODIFY flow mod.
104 *
105 * @return the flow mod
106 */
107 public abstract OFFlowMod buildFlowMod();
alshabibeec3a062014-09-17 18:01:26 -0700108
Jonathan Hart86e59352014-10-22 10:42:16 -0700109 /**
110 * Builds a DELETE flow mod.
111 *
112 * @return the flow mod
113 */
114 public abstract OFFlowDelete buildFlowDel();
alshabibeec3a062014-09-17 18:01:26 -0700115
Jonathan Hart86e59352014-10-22 10:42:16 -0700116 /**
117 * Builds the match for the flow mod.
118 *
119 * @return the match
120 */
121 protected Match buildMatch() {
alshabibeec3a062014-09-17 18:01:26 -0700122 Match.Builder mBuilder = factory.buildMatch();
123 EthCriterion eth;
124 IPCriterion ip;
Jonathan Hart34bc6142014-10-17 11:00:43 -0700125 TcpPortCriterion tp;
alshabibeec3a062014-09-17 18:01:26 -0700126 for (Criterion c : selector.criteria()) {
127 switch (c.type()) {
128 case IN_PORT:
129 PortCriterion inport = (PortCriterion) c;
130 mBuilder.setExact(MatchField.IN_PORT, OFPort.of((int) inport.port().toLong()));
131 break;
132 case ETH_SRC:
133 eth = (EthCriterion) c;
134 mBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(eth.mac().toLong()));
135 break;
136 case ETH_DST:
137 eth = (EthCriterion) c;
138 mBuilder.setExact(MatchField.ETH_DST, MacAddress.of(eth.mac().toLong()));
139 break;
140 case ETH_TYPE:
141 EthTypeCriterion ethType = (EthTypeCriterion) c;
142 mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(ethType.ethType()));
143 break;
144 case IPV4_DST:
145 ip = (IPCriterion) c;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700146 if (ip.ip().prefixLength() != IpPrefix.MAX_INET_MASK_LENGTH) {
147 IpAddress maskAddr =
148 IpAddress.makeMaskPrefix(ip.ip().prefixLength());
149 Masked<IPv4Address> maskedIp =
150 Masked.of(IPv4Address.of(ip.ip().address().toInt()),
151 IPv4Address.of(maskAddr.toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700152 mBuilder.setMasked(MatchField.IPV4_DST, maskedIp);
153 } else {
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700154 mBuilder.setExact(MatchField.IPV4_DST,
155 IPv4Address.of(ip.ip().address().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700156 }
157 break;
158 case IPV4_SRC:
159 ip = (IPCriterion) c;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700160 if (ip.ip().prefixLength() != IpPrefix.MAX_INET_MASK_LENGTH) {
161 IpAddress maskAddr =
162 IpAddress.makeMaskPrefix(ip.ip().prefixLength());
163 Masked<IPv4Address> maskedIp =
164 Masked.of(IPv4Address.of(ip.ip().address().toInt()),
165 IPv4Address.of(maskAddr.toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700166 mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp);
167 } else {
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700168 mBuilder.setExact(MatchField.IPV4_SRC,
169 IPv4Address.of(ip.ip().address().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700170 }
171 break;
172 case IP_PROTO:
173 IPProtocolCriterion p = (IPProtocolCriterion) c;
174 mBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(p.protocol()));
175 break;
176 case VLAN_PCP:
177 VlanPcpCriterion vpcp = (VlanPcpCriterion) c;
178 mBuilder.setExact(MatchField.VLAN_PCP, VlanPcp.of(vpcp.priority()));
179 break;
180 case VLAN_VID:
181 VlanIdCriterion vid = (VlanIdCriterion) c;
182 mBuilder.setExact(MatchField.VLAN_VID,
183 OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vid.vlanId().toShort())));
184 break;
Jonathan Hart34bc6142014-10-17 11:00:43 -0700185 case TCP_DST:
186 tp = (TcpPortCriterion) c;
187 mBuilder.setExact(MatchField.TCP_DST, TransportPort.of(tp.tcpPort()));
188 break;
189 case TCP_SRC:
190 tp = (TcpPortCriterion) c;
191 mBuilder.setExact(MatchField.TCP_SRC, TransportPort.of(tp.tcpPort()));
192 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700193 case OCH_SIGID:
194 LambdaCriterion lc = (LambdaCriterion) c;
195 mBuilder.setExact(MatchField.OCH_SIGID,
196 new CircuitSignalID((byte) 1, (byte) 2, lc.lambda(), (short) 1));
197 break;
alshabibeec3a062014-09-17 18:01:26 -0700198 case ARP_OP:
199 case ARP_SHA:
200 case ARP_SPA:
201 case ARP_THA:
202 case ARP_TPA:
203 case ICMPV4_CODE:
204 case ICMPV4_TYPE:
205 case ICMPV6_CODE:
206 case ICMPV6_TYPE:
207 case IN_PHY_PORT:
208 case IPV6_DST:
209 case IPV6_EXTHDR:
210 case IPV6_FLABEL:
211 case IPV6_ND_SLL:
212 case IPV6_ND_TARGET:
213 case IPV6_ND_TLL:
214 case IPV6_SRC:
215 case IP_DSCP:
216 case IP_ECN:
217 case METADATA:
218 case MPLS_BOS:
219 case MPLS_LABEL:
220 case MPLS_TC:
221 case PBB_ISID:
222 case SCTP_DST:
223 case SCTP_SRC:
alshabibeec3a062014-09-17 18:01:26 -0700224 case TUNNEL_ID:
225 case UDP_DST:
226 case UDP_SRC:
227 default:
228 log.warn("Match type {} not yet implemented.", c.type());
229 }
230 }
231 return mBuilder.build();
232 }
233
Jonathan Hart86e59352014-10-22 10:42:16 -0700234 /**
235 * Returns the flow rule for this builder.
236 *
237 * @return the flow rule
238 */
239 protected FlowRule flowRule() {
240 return flowRule;
241 }
alshabib219ebaa2014-09-22 15:41:24 -0700242
Jonathan Hart86e59352014-10-22 10:42:16 -0700243 /**
244 * Returns the factory used for building OpenFlow constructs.
245 *
246 * @return the factory
247 */
248 protected OFFactory factory() {
249 return factory;
250 }
alshabib219ebaa2014-09-22 15:41:24 -0700251
alshabibeec3a062014-09-17 18:01:26 -0700252}