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