blob: dc954da39023186b61676b6f95acf4fcad07ef29 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.of.flow.impl;
alshabibeec3a062014-09-17 18:01:26 -070017
Jonathan Hart6cd2f352015-01-13 17:44:45 -080018import org.onlab.packet.Ip4Address;
19import org.onlab.packet.Ip4Prefix;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080020import org.onlab.packet.Ip6Address;
21import org.onlab.packet.Ip6Prefix;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080022import org.onlab.packet.VlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.flow.FlowRule;
24import org.onosproject.net.flow.TrafficSelector;
25import org.onosproject.net.flow.criteria.Criteria;
26import org.onosproject.net.flow.criteria.Criteria.EthCriterion;
27import org.onosproject.net.flow.criteria.Criteria.EthTypeCriterion;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -080028import org.onosproject.net.flow.criteria.Criteria.IcmpCodeCriterion;
29import org.onosproject.net.flow.criteria.Criteria.IcmpTypeCriterion;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080030import org.onosproject.net.flow.criteria.Criteria.Icmpv6CodeCriterion;
31import org.onosproject.net.flow.criteria.Criteria.Icmpv6TypeCriterion;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -080032import org.onosproject.net.flow.criteria.Criteria.IPCriterion;
33import org.onosproject.net.flow.criteria.Criteria.IPProtocolCriterion;
34import org.onosproject.net.flow.criteria.Criteria.IPv6FlowLabelCriterion;
35import org.onosproject.net.flow.criteria.Criteria.IPv6NDLinkLayerAddressCriterion;
36import org.onosproject.net.flow.criteria.Criteria.IPv6NDTargetAddressCriterion;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.flow.criteria.Criteria.LambdaCriterion;
38import org.onosproject.net.flow.criteria.Criteria.PortCriterion;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -080039import org.onosproject.net.flow.criteria.Criteria.SctpPortCriterion;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.flow.criteria.Criteria.TcpPortCriterion;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -080041import org.onosproject.net.flow.criteria.Criteria.UdpPortCriterion;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.flow.criteria.Criteria.VlanIdCriterion;
43import org.onosproject.net.flow.criteria.Criteria.VlanPcpCriterion;
44import org.onosproject.net.flow.criteria.Criterion;
alshabibeec3a062014-09-17 18:01:26 -070045import org.projectfloodlight.openflow.protocol.OFFactory;
alshabib193525b2014-10-08 18:58:03 -070046import org.projectfloodlight.openflow.protocol.OFFlowAdd;
47import org.projectfloodlight.openflow.protocol.OFFlowDelete;
alshabibeec3a062014-09-17 18:01:26 -070048import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabibeec3a062014-09-17 18:01:26 -070049import org.projectfloodlight.openflow.protocol.match.Match;
50import org.projectfloodlight.openflow.protocol.match.MatchField;
Marc De Leenheer49087752014-10-23 13:54:09 -070051import org.projectfloodlight.openflow.types.CircuitSignalID;
alshabibeec3a062014-09-17 18:01:26 -070052import org.projectfloodlight.openflow.types.EthType;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -080053import org.projectfloodlight.openflow.types.ICMPv4Code;
54import org.projectfloodlight.openflow.types.ICMPv4Type;
alshabibeec3a062014-09-17 18:01:26 -070055import org.projectfloodlight.openflow.types.IPv4Address;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080056import org.projectfloodlight.openflow.types.IPv6Address;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -080057import org.projectfloodlight.openflow.types.IPv6FlowLabel;
alshabibeec3a062014-09-17 18:01:26 -070058import org.projectfloodlight.openflow.types.IpProtocol;
59import org.projectfloodlight.openflow.types.MacAddress;
60import org.projectfloodlight.openflow.types.Masked;
alshabibeec3a062014-09-17 18:01:26 -070061import org.projectfloodlight.openflow.types.OFPort;
62import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Jonathan Hart34bc6142014-10-17 11:00:43 -070063import org.projectfloodlight.openflow.types.TransportPort;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080064import org.projectfloodlight.openflow.types.U32;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -080065import org.projectfloodlight.openflow.types.U8;
alshabibeec3a062014-09-17 18:01:26 -070066import org.projectfloodlight.openflow.types.VlanPcp;
67import org.projectfloodlight.openflow.types.VlanVid;
68import org.slf4j.Logger;
69
Brian O'Connor427a1762014-11-19 18:40:32 -080070import java.util.Optional;
71
Jonathan Hart6cd2f352015-01-13 17:44:45 -080072import static org.slf4j.LoggerFactory.getLogger;
73
Jonathan Hart86e59352014-10-22 10:42:16 -070074/**
75 * Builder for OpenFlow flow mods based on FlowRules.
76 */
77public abstract class FlowModBuilder {
alshabibeec3a062014-09-17 18:01:26 -070078
79 private final Logger log = getLogger(getClass());
80
81 private final OFFactory factory;
Jonathan Hart86e59352014-10-22 10:42:16 -070082 private final FlowRule flowRule;
alshabibeec3a062014-09-17 18:01:26 -070083 private final TrafficSelector selector;
Brian O'Connor427a1762014-11-19 18:40:32 -080084 protected final Long xid;
alshabibeec3a062014-09-17 18:01:26 -070085
Jonathan Hart86e59352014-10-22 10:42:16 -070086 /**
87 * Creates a new flow mod builder.
88 *
89 * @param flowRule the flow rule to transform into a flow mod
90 * @param factory the OpenFlow factory to use to build the flow mod
Pavlin Radoslavov119fd5c2014-11-25 19:08:19 -080091 * @param xid the transaction ID
Jonathan Hart86e59352014-10-22 10:42:16 -070092 * @return the new flow mod builder
93 */
Brian O'Connor427a1762014-11-19 18:40:32 -080094 public static FlowModBuilder builder(FlowRule flowRule,
95 OFFactory factory, Optional<Long> xid) {
Jonathan Hart86e59352014-10-22 10:42:16 -070096 switch (factory.getVersion()) {
97 case OF_10:
Brian O'Connor427a1762014-11-19 18:40:32 -080098 return new FlowModBuilderVer10(flowRule, factory, xid);
Jonathan Hart86e59352014-10-22 10:42:16 -070099 case OF_13:
Brian O'Connor427a1762014-11-19 18:40:32 -0800100 return new FlowModBuilderVer13(flowRule, factory, xid);
Jonathan Hart86e59352014-10-22 10:42:16 -0700101 default:
102 throw new UnsupportedOperationException(
103 "No flow mod builder for protocol version " + factory.getVersion());
104 }
105 }
alshabibeec3a062014-09-17 18:01:26 -0700106
Jonathan Hart86e59352014-10-22 10:42:16 -0700107 /**
108 * Constructs a flow mod builder.
109 *
110 * @param flowRule the flow rule to transform into a flow mod
111 * @param factory the OpenFlow factory to use to build the flow mod
Pavlin Radoslavov119fd5c2014-11-25 19:08:19 -0800112 * @param xid the transaction ID
Jonathan Hart86e59352014-10-22 10:42:16 -0700113 */
Brian O'Connor427a1762014-11-19 18:40:32 -0800114 protected FlowModBuilder(FlowRule flowRule, OFFactory factory, Optional<Long> xid) {
alshabibeec3a062014-09-17 18:01:26 -0700115 this.factory = factory;
Jonathan Hart86e59352014-10-22 10:42:16 -0700116 this.flowRule = flowRule;
alshabibeec3a062014-09-17 18:01:26 -0700117 this.selector = flowRule.selector();
Brian O'Connor427a1762014-11-19 18:40:32 -0800118 this.xid = xid.orElse((long) 0);
119
alshabibeec3a062014-09-17 18:01:26 -0700120 }
121
Jonathan Hart86e59352014-10-22 10:42:16 -0700122 /**
123 * Builds an ADD flow mod.
124 *
125 * @return the flow mod
126 */
127 public abstract OFFlowAdd buildFlowAdd();
alshabibeec3a062014-09-17 18:01:26 -0700128
Jonathan Hart86e59352014-10-22 10:42:16 -0700129 /**
130 * Builds a MODIFY flow mod.
131 *
132 * @return the flow mod
133 */
134 public abstract OFFlowMod buildFlowMod();
alshabibeec3a062014-09-17 18:01:26 -0700135
Jonathan Hart86e59352014-10-22 10:42:16 -0700136 /**
137 * Builds a DELETE flow mod.
138 *
139 * @return the flow mod
140 */
141 public abstract OFFlowDelete buildFlowDel();
alshabibeec3a062014-09-17 18:01:26 -0700142
Jonathan Hart86e59352014-10-22 10:42:16 -0700143 /**
144 * Builds the match for the flow mod.
145 *
146 * @return the match
147 */
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800148 // CHECKSTYLE IGNORE MethodLength FOR NEXT 300 LINES
Jonathan Hart86e59352014-10-22 10:42:16 -0700149 protected Match buildMatch() {
alshabibeec3a062014-09-17 18:01:26 -0700150 Match.Builder mBuilder = factory.buildMatch();
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800151 Ip6Address ip6Address;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800152 Ip4Prefix ip4Prefix;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +0800153 Ip6Prefix ip6Prefix;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800154 EthCriterion ethCriterion;
155 IPCriterion ipCriterion;
156 TcpPortCriterion tcpPortCriterion;
157 UdpPortCriterion udpPortCriterion;
158 SctpPortCriterion sctpPortCriterion;
159 IPv6NDLinkLayerAddressCriterion llAddressCriterion;
160
alshabibeec3a062014-09-17 18:01:26 -0700161 for (Criterion c : selector.criteria()) {
162 switch (c.type()) {
163 case IN_PORT:
164 PortCriterion inport = (PortCriterion) c;
165 mBuilder.setExact(MatchField.IN_PORT, OFPort.of((int) inport.port().toLong()));
166 break;
alshabibeec3a062014-09-17 18:01:26 -0700167 case ETH_DST:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800168 ethCriterion = (EthCriterion) c;
169 mBuilder.setExact(MatchField.ETH_DST,
170 MacAddress.of(ethCriterion.mac().toLong()));
171 break;
172 case ETH_SRC:
173 ethCriterion = (EthCriterion) c;
174 mBuilder.setExact(MatchField.ETH_SRC,
175 MacAddress.of(ethCriterion.mac().toLong()));
alshabibeec3a062014-09-17 18:01:26 -0700176 break;
177 case ETH_TYPE:
178 EthTypeCriterion ethType = (EthTypeCriterion) c;
179 mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(ethType.ethType()));
180 break;
alshabibeec3a062014-09-17 18:01:26 -0700181 case VLAN_VID:
182 VlanIdCriterion vid = (VlanIdCriterion) c;
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800183
184 if (vid.vlanId().equals(VlanId.ANY)) {
185 mBuilder.setMasked(MatchField.VLAN_VID, OFVlanVidMatch.PRESENT,
186 OFVlanVidMatch.PRESENT);
187 } else {
188 mBuilder.setExact(MatchField.VLAN_VID,
189 OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vid.vlanId().toShort())));
190 }
alshabibeec3a062014-09-17 18:01:26 -0700191 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800192 case VLAN_PCP:
193 VlanPcpCriterion vpcp = (VlanPcpCriterion) c;
194 mBuilder.setExact(MatchField.VLAN_PCP, VlanPcp.of(vpcp.priority()));
195 break;
196 case IP_PROTO:
197 IPProtocolCriterion p = (IPProtocolCriterion) c;
198 mBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(p.protocol()));
199 break;
200 case IPV4_SRC:
201 ipCriterion = (IPCriterion) c;
202 ip4Prefix = ipCriterion.ip().getIp4Prefix();
203 if (ip4Prefix.prefixLength() != Ip4Prefix.MAX_MASK_LENGTH) {
204 Ip4Address maskAddr =
205 Ip4Address.makeMaskPrefix(ip4Prefix.prefixLength());
206 Masked<IPv4Address> maskedIp =
207 Masked.of(IPv4Address.of(ip4Prefix.address().toInt()),
208 IPv4Address.of(maskAddr.toInt()));
209 mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp);
210 } else {
211 mBuilder.setExact(MatchField.IPV4_SRC,
212 IPv4Address.of(ip4Prefix.address().toInt()));
213 }
214 break;
215 case IPV4_DST:
216 ipCriterion = (IPCriterion) c;
217 ip4Prefix = ipCriterion.ip().getIp4Prefix();
218 if (ip4Prefix.prefixLength() != Ip4Prefix.MAX_MASK_LENGTH) {
219 Ip4Address maskAddr =
220 Ip4Address.makeMaskPrefix(ip4Prefix.prefixLength());
221 Masked<IPv4Address> maskedIp =
222 Masked.of(IPv4Address.of(ip4Prefix.address().toInt()),
223 IPv4Address.of(maskAddr.toInt()));
224 mBuilder.setMasked(MatchField.IPV4_DST, maskedIp);
225 } else {
226 mBuilder.setExact(MatchField.IPV4_DST,
227 IPv4Address.of(ip4Prefix.address().toInt()));
228 }
Jonathan Hart34bc6142014-10-17 11:00:43 -0700229 break;
230 case TCP_SRC:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800231 tcpPortCriterion = (TcpPortCriterion) c;
232 mBuilder.setExact(MatchField.TCP_SRC,
233 TransportPort.of(tcpPortCriterion.tcpPort()));
234 break;
235 case TCP_DST:
236 tcpPortCriterion = (TcpPortCriterion) c;
237 mBuilder.setExact(MatchField.TCP_DST,
238 TransportPort.of(tcpPortCriterion.tcpPort()));
239 break;
240 case UDP_SRC:
241 udpPortCriterion = (UdpPortCriterion) c;
242 mBuilder.setExact(MatchField.UDP_SRC,
243 TransportPort.of(udpPortCriterion.udpPort()));
244 break;
245 case UDP_DST:
246 udpPortCriterion = (UdpPortCriterion) c;
247 mBuilder.setExact(MatchField.UDP_DST,
248 TransportPort.of(udpPortCriterion.udpPort()));
249 break;
250 case SCTP_SRC:
251 sctpPortCriterion = (SctpPortCriterion) c;
252 mBuilder.setExact(MatchField.SCTP_SRC,
253 TransportPort.of(sctpPortCriterion.sctpPort()));
254 break;
255 case SCTP_DST:
256 sctpPortCriterion = (SctpPortCriterion) c;
257 mBuilder.setExact(MatchField.SCTP_DST,
258 TransportPort.of(sctpPortCriterion.sctpPort()));
259 break;
260 case ICMPV4_TYPE:
261 IcmpTypeCriterion icmpType = (IcmpTypeCriterion) c;
262 mBuilder.setExact(MatchField.ICMPV4_TYPE,
263 ICMPv4Type.of(icmpType.icmpType()));
264 break;
265 case ICMPV4_CODE:
266 IcmpCodeCriterion icmpCode = (IcmpCodeCriterion) c;
267 mBuilder.setExact(MatchField.ICMPV4_CODE,
268 ICMPv4Code.of(icmpCode.icmpCode()));
269 break;
270 case IPV6_SRC:
271 ipCriterion = (IPCriterion) c;
272 ip6Prefix = ipCriterion.ip().getIp6Prefix();
273 if (ip6Prefix.prefixLength() != Ip6Prefix.MAX_MASK_LENGTH) {
274 Ip6Address maskAddr =
275 Ip6Address.makeMaskPrefix(ip6Prefix.prefixLength());
276 Masked<IPv6Address> maskedIp =
277 Masked.of(IPv6Address.of(ip6Prefix.address().toString()),
278 IPv6Address.of(maskAddr.toString()));
279 mBuilder.setMasked(MatchField.IPV6_SRC, maskedIp);
280 } else {
281 mBuilder.setExact(MatchField.IPV6_SRC,
282 IPv6Address.of(ip6Prefix.address().toString()));
283 }
284 break;
285 case IPV6_DST:
286 ipCriterion = (IPCriterion) c;
287 ip6Prefix = ipCriterion.ip().getIp6Prefix();
288 if (ip6Prefix.prefixLength() != Ip6Prefix.MAX_MASK_LENGTH) {
289 Ip6Address maskAddr =
290 Ip6Address.makeMaskPrefix(ip6Prefix.prefixLength());
291 Masked<IPv6Address> maskedIp =
292 Masked.of(IPv6Address.of(ip6Prefix.address().toString()),
293 IPv6Address.of(maskAddr.toString()));
294 mBuilder.setMasked(MatchField.IPV6_DST, maskedIp);
295 } else {
296 mBuilder.setExact(MatchField.IPV6_DST,
297 IPv6Address.of(ip6Prefix.address().toString()));
298 }
299 break;
300 case IPV6_FLABEL:
301 IPv6FlowLabelCriterion flowLabelCriterion =
302 (IPv6FlowLabelCriterion) c;
303 mBuilder.setExact(MatchField.IPV6_FLABEL,
304 IPv6FlowLabel.of(flowLabelCriterion.flowLabel()));
305 break;
306 case ICMPV6_TYPE:
307 Icmpv6TypeCriterion icmpv6Type = (Icmpv6TypeCriterion) c;
308 mBuilder.setExact(MatchField.ICMPV6_TYPE,
309 U8.of(icmpv6Type.icmpv6Type().byteValue()));
310 break;
311 case ICMPV6_CODE:
312 Icmpv6CodeCriterion icmpv6Code = (Icmpv6CodeCriterion) c;
313 mBuilder.setExact(MatchField.ICMPV6_CODE,
314 U8.of(icmpv6Code.icmpv6Code().byteValue()));
315 break;
316 case IPV6_ND_TARGET:
317 IPv6NDTargetAddressCriterion targetAddressCriterion =
318 (IPv6NDTargetAddressCriterion) c;
319 ip6Address = targetAddressCriterion.targetAddress();
320 mBuilder.setExact(MatchField.IPV6_ND_TARGET,
321 IPv6Address.of(ip6Address.toOctets()));
322 break;
323 case IPV6_ND_SLL:
324 llAddressCriterion =
325 (IPv6NDLinkLayerAddressCriterion) c;
326 mBuilder.setExact(MatchField.IPV6_ND_SLL,
327 MacAddress.of(llAddressCriterion.mac().toLong()));
328 break;
329 case IPV6_ND_TLL:
330 llAddressCriterion =
331 (IPv6NDLinkLayerAddressCriterion) c;
332 mBuilder.setExact(MatchField.IPV6_ND_TLL,
333 MacAddress.of(llAddressCriterion.mac().toLong()));
Jonathan Hart34bc6142014-10-17 11:00:43 -0700334 break;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800335 case MPLS_LABEL:
336 Criteria.MplsCriterion mp = (Criteria.MplsCriterion) c;
337 mBuilder.setExact(MatchField.MPLS_LABEL,
338 U32.of(mp.label().intValue()));
339 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700340 case OCH_SIGID:
341 LambdaCriterion lc = (LambdaCriterion) c;
342 mBuilder.setExact(MatchField.OCH_SIGID,
343 new CircuitSignalID((byte) 1, (byte) 2, lc.lambda(), (short) 1));
344 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800345 case OCH_SIGTYPE:
346 Criteria.OpticalSignalTypeCriterion sc =
347 (Criteria.OpticalSignalTypeCriterion) c;
348 mBuilder.setExact(MatchField.OCH_SIGTYPE,
349 U8.of(sc.signalType()));
350 break;
alshabibeec3a062014-09-17 18:01:26 -0700351 case ARP_OP:
352 case ARP_SHA:
353 case ARP_SPA:
354 case ARP_THA:
355 case ARP_TPA:
alshabibeec3a062014-09-17 18:01:26 -0700356 case IN_PHY_PORT:
alshabibeec3a062014-09-17 18:01:26 -0700357 case IPV6_EXTHDR:
alshabibeec3a062014-09-17 18:01:26 -0700358 case IP_DSCP:
359 case IP_ECN:
360 case METADATA:
361 case MPLS_BOS:
alshabibeec3a062014-09-17 18:01:26 -0700362 case MPLS_TC:
363 case PBB_ISID:
alshabibeec3a062014-09-17 18:01:26 -0700364 case TUNNEL_ID:
alshabibeec3a062014-09-17 18:01:26 -0700365 default:
366 log.warn("Match type {} not yet implemented.", c.type());
367 }
368 }
369 return mBuilder.build();
370 }
371
Jonathan Hart86e59352014-10-22 10:42:16 -0700372 /**
373 * Returns the flow rule for this builder.
374 *
375 * @return the flow rule
376 */
377 protected FlowRule flowRule() {
378 return flowRule;
379 }
alshabib219ebaa2014-09-22 15:41:24 -0700380
Jonathan Hart86e59352014-10-22 10:42:16 -0700381 /**
382 * Returns the factory used for building OpenFlow constructs.
383 *
384 * @return the factory
385 */
386 protected OFFactory factory() {
387 return factory;
388 }
alshabib219ebaa2014-09-22 15:41:24 -0700389
alshabibeec3a062014-09-17 18:01:26 -0700390}