2 * Copyright 2014-2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.provider.of.flow.impl;
18 import org.onlab.packet.Ip4Address;
19 import org.onlab.packet.Ip4Prefix;
20 import org.onlab.packet.Ip6Address;
21 import org.onlab.packet.Ip6Prefix;
22 import org.onlab.packet.VlanId;
23 import org.onosproject.net.OchSignal;
24 import org.onosproject.net.driver.DriverService;
25 import org.onosproject.net.flow.FlowRule;
26 import org.onosproject.net.flow.TrafficSelector;
27 import org.onosproject.net.flow.criteria.ArpHaCriterion;
28 import org.onosproject.net.flow.criteria.Criterion;
29 import org.onosproject.net.flow.criteria.EthCriterion;
30 import org.onosproject.net.flow.criteria.EthTypeCriterion;
31 import org.onosproject.net.flow.criteria.IPCriterion;
32 import org.onosproject.net.flow.criteria.IPDscpCriterion;
33 import org.onosproject.net.flow.criteria.IPEcnCriterion;
34 import org.onosproject.net.flow.criteria.IPProtocolCriterion;
35 import org.onosproject.net.flow.criteria.IPv6ExthdrFlagsCriterion;
36 import org.onosproject.net.flow.criteria.IPv6FlowLabelCriterion;
37 import org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion;
38 import org.onosproject.net.flow.criteria.IPv6NDTargetAddressCriterion;
39 import org.onosproject.net.flow.criteria.IcmpCodeCriterion;
40 import org.onosproject.net.flow.criteria.IcmpTypeCriterion;
41 import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
42 import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
43 import org.onosproject.net.flow.criteria.MetadataCriterion;
44 import org.onosproject.net.flow.criteria.MplsBosCriterion;
45 import org.onosproject.net.flow.criteria.MplsCriterion;
46 import org.onosproject.net.flow.criteria.OchSignalCriterion;
47 import org.onosproject.net.flow.criteria.OchSignalTypeCriterion;
48 import org.onosproject.net.flow.criteria.PortCriterion;
49 import org.onosproject.net.flow.criteria.SctpPortCriterion;
50 import org.onosproject.net.flow.criteria.TcpPortCriterion;
51 import org.onosproject.net.flow.criteria.TunnelIdCriterion;
52 import org.onosproject.net.flow.criteria.UdpPortCriterion;
53 import org.onosproject.net.flow.criteria.VlanIdCriterion;
54 import org.onosproject.net.flow.criteria.VlanPcpCriterion;
55 import org.projectfloodlight.openflow.protocol.OFFactory;
56 import org.projectfloodlight.openflow.protocol.OFFlowAdd;
57 import org.projectfloodlight.openflow.protocol.OFFlowDelete;
58 import org.projectfloodlight.openflow.protocol.OFFlowMod;
59 import org.projectfloodlight.openflow.protocol.match.Match;
60 import org.projectfloodlight.openflow.protocol.match.MatchField;
61 import org.projectfloodlight.openflow.types.CircuitSignalID;
62 import org.projectfloodlight.openflow.types.EthType;
63 import org.projectfloodlight.openflow.types.ICMPv4Code;
64 import org.projectfloodlight.openflow.types.ICMPv4Type;
65 import org.projectfloodlight.openflow.types.IPv4Address;
66 import org.projectfloodlight.openflow.types.IPv6Address;
67 import org.projectfloodlight.openflow.types.IPv6FlowLabel;
68 import org.projectfloodlight.openflow.types.IpDscp;
69 import org.projectfloodlight.openflow.types.IpEcn;
70 import org.projectfloodlight.openflow.types.IpProtocol;
71 import org.projectfloodlight.openflow.types.MacAddress;
72 import org.projectfloodlight.openflow.types.Masked;
73 import org.projectfloodlight.openflow.types.OFBooleanValue;
74 import org.projectfloodlight.openflow.types.OFMetadata;
75 import org.projectfloodlight.openflow.types.OFPort;
76 import org.projectfloodlight.openflow.types.OFVlanVidMatch;
77 import org.projectfloodlight.openflow.types.TransportPort;
78 import org.projectfloodlight.openflow.types.U16;
79 import org.projectfloodlight.openflow.types.U32;
80 import org.projectfloodlight.openflow.types.U64;
81 import org.projectfloodlight.openflow.types.U8;
82 import org.projectfloodlight.openflow.types.VlanPcp;
83 import org.projectfloodlight.openflow.types.VlanVid;
84 import org.slf4j.Logger;
86 import java.util.Optional;
88 import static org.slf4j.LoggerFactory.getLogger;
91 * Builder for OpenFlow flow mods based on FlowRules.
93 public abstract class FlowModBuilder {
95 private final Logger log = getLogger(getClass());
97 private final OFFactory factory;
98 private final FlowRule flowRule;
99 private final TrafficSelector selector;
100 protected final Long xid;
101 protected final Optional<DriverService> driverService;
104 * Creates a new flow mod builder.
106 * @param flowRule the flow rule to transform into a flow mod
107 * @param factory the OpenFlow factory to use to build the flow mod
108 * @param xid the transaction ID
109 * @param driverService the device driver service
110 * @return the new flow mod builder
112 public static FlowModBuilder builder(FlowRule flowRule,
115 Optional<DriverService> driverService) {
116 switch (factory.getVersion()) {
118 return new FlowModBuilderVer10(flowRule, factory, xid, driverService);
120 return new FlowModBuilderVer13(flowRule, factory, xid, driverService);
122 throw new UnsupportedOperationException(
123 "No flow mod builder for protocol version " + factory.getVersion());
128 * Constructs a flow mod builder.
130 * @param flowRule the flow rule to transform into a flow mod
131 * @param factory the OpenFlow factory to use to build the flow mod
132 * @param driverService the device driver service
133 * @param xid the transaction ID
135 protected FlowModBuilder(FlowRule flowRule, OFFactory factory, Optional<Long> xid,
136 Optional<DriverService> driverService) {
137 this.factory = factory;
138 this.flowRule = flowRule;
139 this.selector = flowRule.selector();
140 this.xid = xid.orElse(0L);
141 this.driverService = driverService;
145 * Builds an ADD flow mod.
147 * @return the flow mod
149 public abstract OFFlowAdd buildFlowAdd();
152 * Builds a MODIFY flow mod.
154 * @return the flow mod
156 public abstract OFFlowMod buildFlowMod();
159 * Builds a DELETE flow mod.
161 * @return the flow mod
163 public abstract OFFlowDelete buildFlowDel();
166 * Builds the match for the flow mod.
170 // CHECKSTYLE IGNORE MethodLength FOR NEXT 300 LINES
171 protected Match buildMatch() {
172 Match.Builder mBuilder = factory.buildMatch();
173 Ip6Address ip6Address;
176 EthCriterion ethCriterion;
177 IPCriterion ipCriterion;
178 TcpPortCriterion tcpPortCriterion;
179 UdpPortCriterion udpPortCriterion;
180 SctpPortCriterion sctpPortCriterion;
181 IPv6NDLinkLayerAddressCriterion llAddressCriterion;
182 ArpHaCriterion arpHaCriterion;
184 for (Criterion c : selector.criteria()) {
187 PortCriterion inPort = (PortCriterion) c;
188 mBuilder.setExact(MatchField.IN_PORT,
189 OFPort.of((int) inPort.port().toLong()));
192 PortCriterion inPhyPort = (PortCriterion) c;
193 mBuilder.setExact(MatchField.IN_PORT,
194 OFPort.of((int) inPhyPort.port().toLong()));
197 MetadataCriterion metadata = (MetadataCriterion) c;
198 mBuilder.setExact(MatchField.METADATA,
199 OFMetadata.ofRaw(metadata.metadata()));
202 ethCriterion = (EthCriterion) c;
203 mBuilder.setExact(MatchField.ETH_DST,
204 MacAddress.of(ethCriterion.mac().toLong()));
207 ethCriterion = (EthCriterion) c;
208 mBuilder.setExact(MatchField.ETH_SRC,
209 MacAddress.of(ethCriterion.mac().toLong()));
212 EthTypeCriterion ethType = (EthTypeCriterion) c;
213 mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(ethType.ethType().toShort()));
216 VlanIdCriterion vid = (VlanIdCriterion) c;
218 if (vid.vlanId().equals(VlanId.ANY)) {
219 mBuilder.setMasked(MatchField.VLAN_VID, OFVlanVidMatch.PRESENT,
220 OFVlanVidMatch.PRESENT);
221 } else if (vid.vlanId().equals(VlanId.NONE)) {
222 mBuilder.setExact(MatchField.VLAN_VID, OFVlanVidMatch.NONE);
224 mBuilder.setExact(MatchField.VLAN_VID,
225 OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vid.vlanId().toShort())));
229 VlanPcpCriterion vpcp = (VlanPcpCriterion) c;
230 mBuilder.setExact(MatchField.VLAN_PCP, VlanPcp.of(vpcp.priority()));
233 IPDscpCriterion ipDscpCriterion = (IPDscpCriterion) c;
234 mBuilder.setExact(MatchField.IP_DSCP,
235 IpDscp.of(ipDscpCriterion.ipDscp()));
238 IPEcnCriterion ipEcnCriterion = (IPEcnCriterion) c;
239 mBuilder.setExact(MatchField.IP_ECN,
240 IpEcn.of(ipEcnCriterion.ipEcn()));
243 IPProtocolCriterion p = (IPProtocolCriterion) c;
244 mBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(p.protocol()));
247 ipCriterion = (IPCriterion) c;
248 ip4Prefix = ipCriterion.ip().getIp4Prefix();
249 if (ip4Prefix.prefixLength() != Ip4Prefix.MAX_MASK_LENGTH) {
250 Ip4Address maskAddr =
251 Ip4Address.makeMaskPrefix(ip4Prefix.prefixLength());
252 Masked<IPv4Address> maskedIp =
253 Masked.of(IPv4Address.of(ip4Prefix.address().toInt()),
254 IPv4Address.of(maskAddr.toInt()));
255 mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp);
257 mBuilder.setExact(MatchField.IPV4_SRC,
258 IPv4Address.of(ip4Prefix.address().toInt()));
262 ipCriterion = (IPCriterion) c;
263 ip4Prefix = ipCriterion.ip().getIp4Prefix();
264 if (ip4Prefix.prefixLength() != Ip4Prefix.MAX_MASK_LENGTH) {
265 Ip4Address maskAddr =
266 Ip4Address.makeMaskPrefix(ip4Prefix.prefixLength());
267 Masked<IPv4Address> maskedIp =
268 Masked.of(IPv4Address.of(ip4Prefix.address().toInt()),
269 IPv4Address.of(maskAddr.toInt()));
270 mBuilder.setMasked(MatchField.IPV4_DST, maskedIp);
272 mBuilder.setExact(MatchField.IPV4_DST,
273 IPv4Address.of(ip4Prefix.address().toInt()));
277 tcpPortCriterion = (TcpPortCriterion) c;
278 mBuilder.setExact(MatchField.TCP_SRC,
279 TransportPort.of(tcpPortCriterion.tcpPort().toInt()));
282 tcpPortCriterion = (TcpPortCriterion) c;
283 mBuilder.setExact(MatchField.TCP_DST,
284 TransportPort.of(tcpPortCriterion.tcpPort().toInt()));
287 udpPortCriterion = (UdpPortCriterion) c;
288 mBuilder.setExact(MatchField.UDP_SRC,
289 TransportPort.of(udpPortCriterion.udpPort().toInt()));
292 udpPortCriterion = (UdpPortCriterion) c;
293 mBuilder.setExact(MatchField.UDP_DST,
294 TransportPort.of(udpPortCriterion.udpPort().toInt()));
297 sctpPortCriterion = (SctpPortCriterion) c;
298 mBuilder.setExact(MatchField.SCTP_SRC,
299 TransportPort.of(sctpPortCriterion.sctpPort().toInt()));
302 sctpPortCriterion = (SctpPortCriterion) c;
303 mBuilder.setExact(MatchField.SCTP_DST,
304 TransportPort.of(sctpPortCriterion.sctpPort().toInt()));
307 IcmpTypeCriterion icmpType = (IcmpTypeCriterion) c;
308 mBuilder.setExact(MatchField.ICMPV4_TYPE,
309 ICMPv4Type.of(icmpType.icmpType()));
312 IcmpCodeCriterion icmpCode = (IcmpCodeCriterion) c;
313 mBuilder.setExact(MatchField.ICMPV4_CODE,
314 ICMPv4Code.of(icmpCode.icmpCode()));
317 ipCriterion = (IPCriterion) c;
318 ip6Prefix = ipCriterion.ip().getIp6Prefix();
319 if (ip6Prefix.prefixLength() != Ip6Prefix.MAX_MASK_LENGTH) {
320 Ip6Address maskAddr =
321 Ip6Address.makeMaskPrefix(ip6Prefix.prefixLength());
322 Masked<IPv6Address> maskedIp =
323 Masked.of(IPv6Address.of(ip6Prefix.address().toString()),
324 IPv6Address.of(maskAddr.toString()));
325 mBuilder.setMasked(MatchField.IPV6_SRC, maskedIp);
327 mBuilder.setExact(MatchField.IPV6_SRC,
328 IPv6Address.of(ip6Prefix.address().toString()));
332 ipCriterion = (IPCriterion) c;
333 ip6Prefix = ipCriterion.ip().getIp6Prefix();
334 if (ip6Prefix.prefixLength() != Ip6Prefix.MAX_MASK_LENGTH) {
335 Ip6Address maskAddr =
336 Ip6Address.makeMaskPrefix(ip6Prefix.prefixLength());
337 Masked<IPv6Address> maskedIp =
338 Masked.of(IPv6Address.of(ip6Prefix.address().toString()),
339 IPv6Address.of(maskAddr.toString()));
340 mBuilder.setMasked(MatchField.IPV6_DST, maskedIp);
342 mBuilder.setExact(MatchField.IPV6_DST,
343 IPv6Address.of(ip6Prefix.address().toString()));
347 IPv6FlowLabelCriterion flowLabelCriterion =
348 (IPv6FlowLabelCriterion) c;
349 mBuilder.setExact(MatchField.IPV6_FLABEL,
350 IPv6FlowLabel.of(flowLabelCriterion.flowLabel()));
353 Icmpv6TypeCriterion icmpv6Type = (Icmpv6TypeCriterion) c;
354 mBuilder.setExact(MatchField.ICMPV6_TYPE,
355 U8.of(icmpv6Type.icmpv6Type()));
358 Icmpv6CodeCriterion icmpv6Code = (Icmpv6CodeCriterion) c;
359 mBuilder.setExact(MatchField.ICMPV6_CODE,
360 U8.of(icmpv6Code.icmpv6Code()));
363 IPv6NDTargetAddressCriterion targetAddressCriterion =
364 (IPv6NDTargetAddressCriterion) c;
365 ip6Address = targetAddressCriterion.targetAddress();
366 mBuilder.setExact(MatchField.IPV6_ND_TARGET,
367 IPv6Address.of(ip6Address.toOctets()));
371 (IPv6NDLinkLayerAddressCriterion) c;
372 mBuilder.setExact(MatchField.IPV6_ND_SLL,
373 MacAddress.of(llAddressCriterion.mac().toLong()));
377 (IPv6NDLinkLayerAddressCriterion) c;
378 mBuilder.setExact(MatchField.IPV6_ND_TLL,
379 MacAddress.of(llAddressCriterion.mac().toLong()));
382 MplsCriterion mp = (MplsCriterion) c;
383 mBuilder.setExact(MatchField.MPLS_LABEL, U32.of(mp.label().toInt()));
386 IPv6ExthdrFlagsCriterion exthdrFlagsCriterion =
387 (IPv6ExthdrFlagsCriterion) c;
388 mBuilder.setExact(MatchField.IPV6_EXTHDR,
389 U16.of(exthdrFlagsCriterion.exthdrFlags()));
393 OchSignalCriterion ochSignalCriterion = (OchSignalCriterion) c;
394 OchSignal signal = ochSignalCriterion.lambda();
395 byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
396 byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
397 mBuilder.setExact(MatchField.OCH_SIGID,
398 new CircuitSignalID(gridType, channelSpacing,
399 (short) signal.spacingMultiplier(), (short) signal.slotGranularity()));
400 } catch (NoMappingFoundException e) {
401 log.warn(e.getMessage());
405 OchSignalTypeCriterion sc = (OchSignalTypeCriterion) c;
406 byte signalType = OpenFlowValueMapper.lookupOchSignalType(sc.signalType());
407 mBuilder.setExact(MatchField.OCH_SIGTYPE, U8.of(signalType));
410 TunnelIdCriterion tunnelId = (TunnelIdCriterion) c;
411 mBuilder.setExact(MatchField.TUNNEL_ID,
412 U64.of(tunnelId.tunnelId()));
415 MplsBosCriterion mplsBos = (MplsBosCriterion) c;
416 mBuilder.setExact(MatchField.MPLS_BOS,
417 mplsBos.mplsBos() ? OFBooleanValue.TRUE
418 : OFBooleanValue.FALSE);
421 arpHaCriterion = (ArpHaCriterion) c;
422 mBuilder.setExact(MatchField.ARP_THA,
423 MacAddress.of(arpHaCriterion.mac().toLong()));
432 log.warn("Match type {} not yet implemented.", c.type());
435 return mBuilder.build();
439 * Returns the flow rule for this builder.
441 * @return the flow rule
443 protected FlowRule flowRule() {
448 * Returns the factory used for building OpenFlow constructs.
450 * @return the factory
452 protected OFFactory factory() {