010d7e7cc0da7a0737e465445c2d80405dca19d9
[onosfw.git] /
1 /*
2  * Copyright 2014-2015 Open Networking Laboratory
3  *
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
7  *
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.
15  */
16 package org.onosproject.provider.of.flow.impl;
17
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.Criterion;
28 import org.onosproject.net.flow.criteria.EthCriterion;
29 import org.onosproject.net.flow.criteria.EthTypeCriterion;
30 import org.onosproject.net.flow.criteria.IPCriterion;
31 import org.onosproject.net.flow.criteria.IPDscpCriterion;
32 import org.onosproject.net.flow.criteria.IPEcnCriterion;
33 import org.onosproject.net.flow.criteria.IPProtocolCriterion;
34 import org.onosproject.net.flow.criteria.IPv6ExthdrFlagsCriterion;
35 import org.onosproject.net.flow.criteria.IPv6FlowLabelCriterion;
36 import org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion;
37 import org.onosproject.net.flow.criteria.IPv6NDTargetAddressCriterion;
38 import org.onosproject.net.flow.criteria.IcmpCodeCriterion;
39 import org.onosproject.net.flow.criteria.IcmpTypeCriterion;
40 import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
41 import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
42 import org.onosproject.net.flow.criteria.MetadataCriterion;
43 import org.onosproject.net.flow.criteria.MplsBosCriterion;
44 import org.onosproject.net.flow.criteria.MplsCriterion;
45 import org.onosproject.net.flow.criteria.OchSignalCriterion;
46 import org.onosproject.net.flow.criteria.OchSignalTypeCriterion;
47 import org.onosproject.net.flow.criteria.PortCriterion;
48 import org.onosproject.net.flow.criteria.SctpPortCriterion;
49 import org.onosproject.net.flow.criteria.TcpPortCriterion;
50 import org.onosproject.net.flow.criteria.TunnelIdCriterion;
51 import org.onosproject.net.flow.criteria.UdpPortCriterion;
52 import org.onosproject.net.flow.criteria.VlanIdCriterion;
53 import org.onosproject.net.flow.criteria.VlanPcpCriterion;
54 import org.projectfloodlight.openflow.protocol.OFFactory;
55 import org.projectfloodlight.openflow.protocol.OFFlowAdd;
56 import org.projectfloodlight.openflow.protocol.OFFlowDelete;
57 import org.projectfloodlight.openflow.protocol.OFFlowMod;
58 import org.projectfloodlight.openflow.protocol.match.Match;
59 import org.projectfloodlight.openflow.protocol.match.MatchField;
60 import org.projectfloodlight.openflow.types.CircuitSignalID;
61 import org.projectfloodlight.openflow.types.EthType;
62 import org.projectfloodlight.openflow.types.ICMPv4Code;
63 import org.projectfloodlight.openflow.types.ICMPv4Type;
64 import org.projectfloodlight.openflow.types.IPv4Address;
65 import org.projectfloodlight.openflow.types.IPv6Address;
66 import org.projectfloodlight.openflow.types.IPv6FlowLabel;
67 import org.projectfloodlight.openflow.types.IpDscp;
68 import org.projectfloodlight.openflow.types.IpEcn;
69 import org.projectfloodlight.openflow.types.IpProtocol;
70 import org.projectfloodlight.openflow.types.MacAddress;
71 import org.projectfloodlight.openflow.types.Masked;
72 import org.projectfloodlight.openflow.types.OFBooleanValue;
73 import org.projectfloodlight.openflow.types.OFMetadata;
74 import org.projectfloodlight.openflow.types.OFPort;
75 import org.projectfloodlight.openflow.types.OFVlanVidMatch;
76 import org.projectfloodlight.openflow.types.TransportPort;
77 import org.projectfloodlight.openflow.types.U16;
78 import org.projectfloodlight.openflow.types.U32;
79 import org.projectfloodlight.openflow.types.U64;
80 import org.projectfloodlight.openflow.types.U8;
81 import org.projectfloodlight.openflow.types.VlanPcp;
82 import org.projectfloodlight.openflow.types.VlanVid;
83 import org.slf4j.Logger;
84
85 import java.util.Optional;
86
87 import static org.slf4j.LoggerFactory.getLogger;
88
89 /**
90  * Builder for OpenFlow flow mods based on FlowRules.
91  */
92 public abstract class FlowModBuilder {
93
94     private final Logger log = getLogger(getClass());
95
96     private final OFFactory factory;
97     private final FlowRule flowRule;
98     private final TrafficSelector selector;
99     protected final Long xid;
100     protected final Optional<DriverService> driverService;
101
102     /**
103      * Creates a new flow mod builder.
104      *
105      * @param flowRule the flow rule to transform into a flow mod
106      * @param factory the OpenFlow factory to use to build the flow mod
107      * @param xid the transaction ID
108      * @param driverService the device driver service
109      * @return the new flow mod builder
110      */
111     public static FlowModBuilder builder(FlowRule flowRule,
112                                          OFFactory factory,
113                                          Optional<Long> xid,
114                                          Optional<DriverService> driverService) {
115         switch (factory.getVersion()) {
116         case OF_10:
117             return new FlowModBuilderVer10(flowRule, factory, xid, driverService);
118         case OF_13:
119             return new FlowModBuilderVer13(flowRule, factory, xid, driverService);
120         default:
121             throw new UnsupportedOperationException(
122                     "No flow mod builder for protocol version " + factory.getVersion());
123         }
124     }
125
126     /**
127      * Constructs a flow mod builder.
128      *
129      * @param flowRule the flow rule to transform into a flow mod
130      * @param factory the OpenFlow factory to use to build the flow mod
131      * @param driverService the device driver service
132      * @param xid the transaction ID
133      */
134     protected FlowModBuilder(FlowRule flowRule, OFFactory factory, Optional<Long> xid,
135                              Optional<DriverService> driverService) {
136         this.factory = factory;
137         this.flowRule = flowRule;
138         this.selector = flowRule.selector();
139         this.xid = xid.orElse(0L);
140         this.driverService = driverService;
141     }
142
143     /**
144      * Builds an ADD flow mod.
145      *
146      * @return the flow mod
147      */
148     public abstract OFFlowAdd buildFlowAdd();
149
150     /**
151      * Builds a MODIFY flow mod.
152      *
153      * @return the flow mod
154      */
155     public abstract OFFlowMod buildFlowMod();
156
157     /**
158      * Builds a DELETE flow mod.
159      *
160      * @return the flow mod
161      */
162     public abstract OFFlowDelete buildFlowDel();
163
164     /**
165      * Builds the match for the flow mod.
166      *
167      * @return the match
168      */
169     // CHECKSTYLE IGNORE MethodLength FOR NEXT 300 LINES
170     protected Match buildMatch() {
171         Match.Builder mBuilder = factory.buildMatch();
172         Ip6Address ip6Address;
173         Ip4Prefix ip4Prefix;
174         Ip6Prefix ip6Prefix;
175         EthCriterion ethCriterion;
176         IPCriterion ipCriterion;
177         TcpPortCriterion tcpPortCriterion;
178         UdpPortCriterion udpPortCriterion;
179         SctpPortCriterion sctpPortCriterion;
180         IPv6NDLinkLayerAddressCriterion llAddressCriterion;
181
182         for (Criterion c : selector.criteria()) {
183             switch (c.type()) {
184             case IN_PORT:
185                 PortCriterion inPort = (PortCriterion) c;
186                 mBuilder.setExact(MatchField.IN_PORT,
187                                   OFPort.of((int) inPort.port().toLong()));
188                 break;
189             case IN_PHY_PORT:
190                 PortCriterion inPhyPort = (PortCriterion) c;
191                 mBuilder.setExact(MatchField.IN_PORT,
192                                   OFPort.of((int) inPhyPort.port().toLong()));
193                 break;
194             case METADATA:
195                 MetadataCriterion metadata = (MetadataCriterion) c;
196                 mBuilder.setExact(MatchField.METADATA,
197                                   OFMetadata.ofRaw(metadata.metadata()));
198                 break;
199             case ETH_DST:
200                 ethCriterion = (EthCriterion) c;
201                 mBuilder.setExact(MatchField.ETH_DST,
202                                   MacAddress.of(ethCriterion.mac().toLong()));
203                 break;
204             case ETH_SRC:
205                 ethCriterion = (EthCriterion) c;
206                 mBuilder.setExact(MatchField.ETH_SRC,
207                                   MacAddress.of(ethCriterion.mac().toLong()));
208                 break;
209             case ETH_TYPE:
210                 EthTypeCriterion ethType = (EthTypeCriterion) c;
211                 mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(ethType.ethType().toShort()));
212                 break;
213             case VLAN_VID:
214                 VlanIdCriterion vid = (VlanIdCriterion) c;
215
216                 if (vid.vlanId().equals(VlanId.ANY)) {
217                     mBuilder.setMasked(MatchField.VLAN_VID, OFVlanVidMatch.PRESENT,
218                                        OFVlanVidMatch.PRESENT);
219                 } else if (vid.vlanId().equals(VlanId.NONE)) {
220                     mBuilder.setExact(MatchField.VLAN_VID, OFVlanVidMatch.NONE);
221                 } else {
222                     mBuilder.setExact(MatchField.VLAN_VID,
223                                       OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vid.vlanId().toShort())));
224                 }
225                 break;
226             case VLAN_PCP:
227                 VlanPcpCriterion vpcp = (VlanPcpCriterion) c;
228                 mBuilder.setExact(MatchField.VLAN_PCP, VlanPcp.of(vpcp.priority()));
229                 break;
230             case IP_DSCP:
231                 IPDscpCriterion ipDscpCriterion = (IPDscpCriterion) c;
232                 mBuilder.setExact(MatchField.IP_DSCP,
233                                   IpDscp.of(ipDscpCriterion.ipDscp()));
234                 break;
235             case IP_ECN:
236                 IPEcnCriterion ipEcnCriterion = (IPEcnCriterion) c;
237                 mBuilder.setExact(MatchField.IP_ECN,
238                                   IpEcn.of(ipEcnCriterion.ipEcn()));
239                 break;
240             case IP_PROTO:
241                 IPProtocolCriterion p = (IPProtocolCriterion) c;
242                 mBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(p.protocol()));
243                 break;
244             case IPV4_SRC:
245                 ipCriterion = (IPCriterion) c;
246                 ip4Prefix = ipCriterion.ip().getIp4Prefix();
247                 if (ip4Prefix.prefixLength() != Ip4Prefix.MAX_MASK_LENGTH) {
248                     Ip4Address maskAddr =
249                         Ip4Address.makeMaskPrefix(ip4Prefix.prefixLength());
250                     Masked<IPv4Address> maskedIp =
251                         Masked.of(IPv4Address.of(ip4Prefix.address().toInt()),
252                                   IPv4Address.of(maskAddr.toInt()));
253                     mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp);
254                 } else {
255                     mBuilder.setExact(MatchField.IPV4_SRC,
256                                 IPv4Address.of(ip4Prefix.address().toInt()));
257                 }
258                 break;
259             case IPV4_DST:
260                 ipCriterion = (IPCriterion) c;
261                 ip4Prefix = ipCriterion.ip().getIp4Prefix();
262                 if (ip4Prefix.prefixLength() != Ip4Prefix.MAX_MASK_LENGTH) {
263                     Ip4Address maskAddr =
264                         Ip4Address.makeMaskPrefix(ip4Prefix.prefixLength());
265                     Masked<IPv4Address> maskedIp =
266                         Masked.of(IPv4Address.of(ip4Prefix.address().toInt()),
267                                   IPv4Address.of(maskAddr.toInt()));
268                     mBuilder.setMasked(MatchField.IPV4_DST, maskedIp);
269                 } else {
270                     mBuilder.setExact(MatchField.IPV4_DST,
271                                 IPv4Address.of(ip4Prefix.address().toInt()));
272                 }
273                 break;
274             case TCP_SRC:
275                 tcpPortCriterion = (TcpPortCriterion) c;
276                 mBuilder.setExact(MatchField.TCP_SRC,
277                                   TransportPort.of(tcpPortCriterion.tcpPort().toInt()));
278                 break;
279             case TCP_DST:
280                 tcpPortCriterion = (TcpPortCriterion) c;
281                 mBuilder.setExact(MatchField.TCP_DST,
282                                   TransportPort.of(tcpPortCriterion.tcpPort().toInt()));
283                 break;
284             case UDP_SRC:
285                 udpPortCriterion = (UdpPortCriterion) c;
286                 mBuilder.setExact(MatchField.UDP_SRC,
287                                   TransportPort.of(udpPortCriterion.udpPort().toInt()));
288                 break;
289             case UDP_DST:
290                 udpPortCriterion = (UdpPortCriterion) c;
291                 mBuilder.setExact(MatchField.UDP_DST,
292                                   TransportPort.of(udpPortCriterion.udpPort().toInt()));
293                 break;
294             case SCTP_SRC:
295                 sctpPortCriterion = (SctpPortCriterion) c;
296                 mBuilder.setExact(MatchField.SCTP_SRC,
297                                   TransportPort.of(sctpPortCriterion.sctpPort().toInt()));
298                 break;
299             case SCTP_DST:
300                 sctpPortCriterion = (SctpPortCriterion) c;
301                 mBuilder.setExact(MatchField.SCTP_DST,
302                                   TransportPort.of(sctpPortCriterion.sctpPort().toInt()));
303                 break;
304             case ICMPV4_TYPE:
305                 IcmpTypeCriterion icmpType = (IcmpTypeCriterion) c;
306                 mBuilder.setExact(MatchField.ICMPV4_TYPE,
307                                   ICMPv4Type.of(icmpType.icmpType()));
308                 break;
309             case ICMPV4_CODE:
310                 IcmpCodeCriterion icmpCode = (IcmpCodeCriterion) c;
311                 mBuilder.setExact(MatchField.ICMPV4_CODE,
312                                   ICMPv4Code.of(icmpCode.icmpCode()));
313                 break;
314             case IPV6_SRC:
315                 ipCriterion = (IPCriterion) c;
316                 ip6Prefix = ipCriterion.ip().getIp6Prefix();
317                 if (ip6Prefix.prefixLength() != Ip6Prefix.MAX_MASK_LENGTH) {
318                     Ip6Address maskAddr =
319                             Ip6Address.makeMaskPrefix(ip6Prefix.prefixLength());
320                     Masked<IPv6Address> maskedIp =
321                             Masked.of(IPv6Address.of(ip6Prefix.address().toString()),
322                                     IPv6Address.of(maskAddr.toString()));
323                     mBuilder.setMasked(MatchField.IPV6_SRC, maskedIp);
324                 } else {
325                     mBuilder.setExact(MatchField.IPV6_SRC,
326                             IPv6Address.of(ip6Prefix.address().toString()));
327                 }
328                 break;
329             case IPV6_DST:
330                 ipCriterion = (IPCriterion) c;
331                 ip6Prefix = ipCriterion.ip().getIp6Prefix();
332                 if (ip6Prefix.prefixLength() != Ip6Prefix.MAX_MASK_LENGTH) {
333                     Ip6Address maskAddr =
334                             Ip6Address.makeMaskPrefix(ip6Prefix.prefixLength());
335                     Masked<IPv6Address> maskedIp =
336                             Masked.of(IPv6Address.of(ip6Prefix.address().toString()),
337                                     IPv6Address.of(maskAddr.toString()));
338                     mBuilder.setMasked(MatchField.IPV6_DST, maskedIp);
339                 } else {
340                     mBuilder.setExact(MatchField.IPV6_DST,
341                             IPv6Address.of(ip6Prefix.address().toString()));
342                 }
343                 break;
344             case IPV6_FLABEL:
345                 IPv6FlowLabelCriterion flowLabelCriterion =
346                     (IPv6FlowLabelCriterion) c;
347                 mBuilder.setExact(MatchField.IPV6_FLABEL,
348                                   IPv6FlowLabel.of(flowLabelCriterion.flowLabel()));
349                 break;
350             case ICMPV6_TYPE:
351                 Icmpv6TypeCriterion icmpv6Type = (Icmpv6TypeCriterion) c;
352                 mBuilder.setExact(MatchField.ICMPV6_TYPE,
353                                   U8.of(icmpv6Type.icmpv6Type()));
354                 break;
355             case ICMPV6_CODE:
356                 Icmpv6CodeCriterion icmpv6Code = (Icmpv6CodeCriterion) c;
357                 mBuilder.setExact(MatchField.ICMPV6_CODE,
358                                   U8.of(icmpv6Code.icmpv6Code()));
359                 break;
360             case IPV6_ND_TARGET:
361                 IPv6NDTargetAddressCriterion targetAddressCriterion =
362                     (IPv6NDTargetAddressCriterion) c;
363                 ip6Address = targetAddressCriterion.targetAddress();
364                 mBuilder.setExact(MatchField.IPV6_ND_TARGET,
365                                   IPv6Address.of(ip6Address.toOctets()));
366                 break;
367             case IPV6_ND_SLL:
368                 llAddressCriterion =
369                     (IPv6NDLinkLayerAddressCriterion) c;
370                 mBuilder.setExact(MatchField.IPV6_ND_SLL,
371                         MacAddress.of(llAddressCriterion.mac().toLong()));
372                 break;
373             case IPV6_ND_TLL:
374                 llAddressCriterion =
375                     (IPv6NDLinkLayerAddressCriterion) c;
376                 mBuilder.setExact(MatchField.IPV6_ND_TLL,
377                         MacAddress.of(llAddressCriterion.mac().toLong()));
378                 break;
379             case MPLS_LABEL:
380                 MplsCriterion mp = (MplsCriterion) c;
381                 mBuilder.setExact(MatchField.MPLS_LABEL, U32.of(mp.label().toInt()));
382                 break;
383             case IPV6_EXTHDR:
384                 IPv6ExthdrFlagsCriterion exthdrFlagsCriterion =
385                     (IPv6ExthdrFlagsCriterion) c;
386                 mBuilder.setExact(MatchField.IPV6_EXTHDR,
387                                   U16.of(exthdrFlagsCriterion.exthdrFlags()));
388                 break;
389             case OCH_SIGID:
390                 try {
391                     OchSignalCriterion ochSignalCriterion = (OchSignalCriterion) c;
392                     OchSignal signal = ochSignalCriterion.lambda();
393                     byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
394                     byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
395                     mBuilder.setExact(MatchField.OCH_SIGID,
396                             new CircuitSignalID(gridType, channelSpacing,
397                                     (short) signal.spacingMultiplier(), (short) signal.slotGranularity()));
398                 } catch (NoMappingFoundException e) {
399                     log.warn(e.getMessage());
400                 }
401                 break;
402             case OCH_SIGTYPE:
403                 OchSignalTypeCriterion sc = (OchSignalTypeCriterion) c;
404                 byte signalType = OpenFlowValueMapper.lookupOchSignalType(sc.signalType());
405                 mBuilder.setExact(MatchField.OCH_SIGTYPE, U8.of(signalType));
406                 break;
407             case TUNNEL_ID:
408                 TunnelIdCriterion tunnelId = (TunnelIdCriterion) c;
409                 mBuilder.setExact(MatchField.TUNNEL_ID,
410                                   U64.of(tunnelId.tunnelId()));
411                 break;
412             case MPLS_BOS:
413                 MplsBosCriterion mplsBos = (MplsBosCriterion) c;
414                 mBuilder.setExact(MatchField.MPLS_BOS,
415                                   mplsBos.mplsBos() ? OFBooleanValue.TRUE
416                                                     : OFBooleanValue.FALSE);
417                 break;
418             case ARP_OP:
419             case ARP_SHA:
420             case ARP_SPA:
421             case ARP_THA:
422             case ARP_TPA:
423             case MPLS_TC:
424             case PBB_ISID:
425             default:
426                 log.warn("Match type {} not yet implemented.", c.type());
427             }
428         }
429         return mBuilder.build();
430     }
431
432     /**
433      * Returns the flow rule for this builder.
434      *
435      * @return the flow rule
436      */
437     protected FlowRule flowRule() {
438         return flowRule;
439     }
440
441     /**
442      * Returns the factory used for building OpenFlow constructs.
443      *
444      * @return the factory
445      */
446     protected OFFactory factory() {
447         return factory;
448     }
449
450 }