blob: cc33f2039a52fc7a28b5d2e0fa895ce065dce60e [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;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -080022import org.onlab.onos.net.flow.criteria.Criteria;
alshabibeec3a062014-09-17 18:01:26 -070023import org.onlab.onos.net.flow.criteria.Criteria.EthCriterion;
24import org.onlab.onos.net.flow.criteria.Criteria.EthTypeCriterion;
25import org.onlab.onos.net.flow.criteria.Criteria.IPCriterion;
26import org.onlab.onos.net.flow.criteria.Criteria.IPProtocolCriterion;
Marc De Leenheer49087752014-10-23 13:54:09 -070027import org.onlab.onos.net.flow.criteria.Criteria.LambdaCriterion;
alshabibeec3a062014-09-17 18:01:26 -070028import org.onlab.onos.net.flow.criteria.Criteria.PortCriterion;
Jonathan Hart34bc6142014-10-17 11:00:43 -070029import org.onlab.onos.net.flow.criteria.Criteria.TcpPortCriterion;
alshabibeec3a062014-09-17 18:01:26 -070030import org.onlab.onos.net.flow.criteria.Criteria.VlanIdCriterion;
31import org.onlab.onos.net.flow.criteria.Criteria.VlanPcpCriterion;
32import org.onlab.onos.net.flow.criteria.Criterion;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -080033import org.onlab.packet.Ip4Address;
34import org.onlab.packet.Ip4Prefix;
alshabibeec3a062014-09-17 18:01:26 -070035import org.projectfloodlight.openflow.protocol.OFFactory;
alshabib193525b2014-10-08 18:58:03 -070036import org.projectfloodlight.openflow.protocol.OFFlowAdd;
37import org.projectfloodlight.openflow.protocol.OFFlowDelete;
alshabibeec3a062014-09-17 18:01:26 -070038import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabibeec3a062014-09-17 18:01:26 -070039import org.projectfloodlight.openflow.protocol.match.Match;
40import org.projectfloodlight.openflow.protocol.match.MatchField;
Marc De Leenheer49087752014-10-23 13:54:09 -070041import org.projectfloodlight.openflow.types.CircuitSignalID;
alshabibeec3a062014-09-17 18:01:26 -070042import org.projectfloodlight.openflow.types.EthType;
43import org.projectfloodlight.openflow.types.IPv4Address;
44import org.projectfloodlight.openflow.types.IpProtocol;
45import org.projectfloodlight.openflow.types.MacAddress;
46import org.projectfloodlight.openflow.types.Masked;
alshabibeec3a062014-09-17 18:01:26 -070047import org.projectfloodlight.openflow.types.OFPort;
48import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Jonathan Hart34bc6142014-10-17 11:00:43 -070049import org.projectfloodlight.openflow.types.TransportPort;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080050import org.projectfloodlight.openflow.types.U32;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -080051import org.projectfloodlight.openflow.types.U8;
alshabibeec3a062014-09-17 18:01:26 -070052import org.projectfloodlight.openflow.types.VlanPcp;
53import org.projectfloodlight.openflow.types.VlanVid;
54import org.slf4j.Logger;
55
Jonathan Hart86e59352014-10-22 10:42:16 -070056/**
57 * Builder for OpenFlow flow mods based on FlowRules.
58 */
59public abstract class FlowModBuilder {
alshabibeec3a062014-09-17 18:01:26 -070060
61 private final Logger log = getLogger(getClass());
62
63 private final OFFactory factory;
Jonathan Hart86e59352014-10-22 10:42:16 -070064 private final FlowRule flowRule;
alshabibeec3a062014-09-17 18:01:26 -070065 private final TrafficSelector selector;
66
Jonathan Hart86e59352014-10-22 10:42:16 -070067 /**
68 * Creates a new flow mod builder.
69 *
70 * @param flowRule the flow rule to transform into a flow mod
71 * @param factory the OpenFlow factory to use to build the flow mod
72 * @return the new flow mod builder
73 */
74 public static FlowModBuilder builder(FlowRule flowRule, OFFactory factory) {
75 switch (factory.getVersion()) {
76 case OF_10:
77 return new FlowModBuilderVer10(flowRule, factory);
78 case OF_13:
79 return new FlowModBuilderVer13(flowRule, factory);
80 default:
81 throw new UnsupportedOperationException(
82 "No flow mod builder for protocol version " + factory.getVersion());
83 }
84 }
alshabibeec3a062014-09-17 18:01:26 -070085
Jonathan Hart86e59352014-10-22 10:42:16 -070086 /**
87 * Constructs a 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
91 */
92 protected FlowModBuilder(FlowRule flowRule, OFFactory factory) {
alshabibeec3a062014-09-17 18:01:26 -070093 this.factory = factory;
Jonathan Hart86e59352014-10-22 10:42:16 -070094 this.flowRule = flowRule;
alshabibeec3a062014-09-17 18:01:26 -070095 this.selector = flowRule.selector();
alshabibeec3a062014-09-17 18:01:26 -070096 }
97
Jonathan Hart86e59352014-10-22 10:42:16 -070098 /**
99 * Builds an ADD flow mod.
100 *
101 * @return the flow mod
102 */
103 public abstract OFFlowAdd buildFlowAdd();
alshabibeec3a062014-09-17 18:01:26 -0700104
Jonathan Hart86e59352014-10-22 10:42:16 -0700105 /**
106 * Builds a MODIFY flow mod.
107 *
108 * @return the flow mod
109 */
110 public abstract OFFlowMod buildFlowMod();
alshabibeec3a062014-09-17 18:01:26 -0700111
Jonathan Hart86e59352014-10-22 10:42:16 -0700112 /**
113 * Builds a DELETE flow mod.
114 *
115 * @return the flow mod
116 */
117 public abstract OFFlowDelete buildFlowDel();
alshabibeec3a062014-09-17 18:01:26 -0700118
Jonathan Hart86e59352014-10-22 10:42:16 -0700119 /**
120 * Builds the match for the flow mod.
121 *
122 * @return the match
123 */
124 protected Match buildMatch() {
alshabibeec3a062014-09-17 18:01:26 -0700125 Match.Builder mBuilder = factory.buildMatch();
126 EthCriterion eth;
127 IPCriterion ip;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800128 Ip4Prefix ip4Prefix;
Jonathan Hart34bc6142014-10-17 11:00:43 -0700129 TcpPortCriterion tp;
alshabibeec3a062014-09-17 18:01:26 -0700130 for (Criterion c : selector.criteria()) {
131 switch (c.type()) {
132 case IN_PORT:
133 PortCriterion inport = (PortCriterion) c;
134 mBuilder.setExact(MatchField.IN_PORT, OFPort.of((int) inport.port().toLong()));
135 break;
136 case ETH_SRC:
137 eth = (EthCriterion) c;
138 mBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(eth.mac().toLong()));
139 break;
140 case ETH_DST:
141 eth = (EthCriterion) c;
142 mBuilder.setExact(MatchField.ETH_DST, MacAddress.of(eth.mac().toLong()));
143 break;
144 case ETH_TYPE:
145 EthTypeCriterion ethType = (EthTypeCriterion) c;
146 mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(ethType.ethType()));
147 break;
148 case IPV4_DST:
149 ip = (IPCriterion) c;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800150 ip4Prefix = ip.ip().getIp4Prefix();
151 if (ip4Prefix.prefixLength() != Ip4Prefix.MAX_MASK_LENGTH) {
152 Ip4Address maskAddr =
153 Ip4Address.makeMaskPrefix(ip4Prefix.prefixLength());
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700154 Masked<IPv4Address> maskedIp =
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800155 Masked.of(IPv4Address.of(ip4Prefix.address().toInt()),
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700156 IPv4Address.of(maskAddr.toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700157 mBuilder.setMasked(MatchField.IPV4_DST, maskedIp);
158 } else {
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700159 mBuilder.setExact(MatchField.IPV4_DST,
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800160 IPv4Address.of(ip4Prefix.address().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700161 }
162 break;
163 case IPV4_SRC:
164 ip = (IPCriterion) c;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800165 ip4Prefix = ip.ip().getIp4Prefix();
166 if (ip4Prefix.prefixLength() != Ip4Prefix.MAX_MASK_LENGTH) {
167 Ip4Address maskAddr =
168 Ip4Address.makeMaskPrefix(ip4Prefix.prefixLength());
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700169 Masked<IPv4Address> maskedIp =
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800170 Masked.of(IPv4Address.of(ip4Prefix.address().toInt()),
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700171 IPv4Address.of(maskAddr.toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700172 mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp);
173 } else {
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700174 mBuilder.setExact(MatchField.IPV4_SRC,
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800175 IPv4Address.of(ip4Prefix.address().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700176 }
177 break;
178 case IP_PROTO:
179 IPProtocolCriterion p = (IPProtocolCriterion) c;
180 mBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(p.protocol()));
181 break;
182 case VLAN_PCP:
183 VlanPcpCriterion vpcp = (VlanPcpCriterion) c;
184 mBuilder.setExact(MatchField.VLAN_PCP, VlanPcp.of(vpcp.priority()));
185 break;
186 case VLAN_VID:
187 VlanIdCriterion vid = (VlanIdCriterion) c;
188 mBuilder.setExact(MatchField.VLAN_VID,
189 OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vid.vlanId().toShort())));
190 break;
Jonathan Hart34bc6142014-10-17 11:00:43 -0700191 case TCP_DST:
192 tp = (TcpPortCriterion) c;
193 mBuilder.setExact(MatchField.TCP_DST, TransportPort.of(tp.tcpPort()));
194 break;
195 case TCP_SRC:
196 tp = (TcpPortCriterion) c;
197 mBuilder.setExact(MatchField.TCP_SRC, TransportPort.of(tp.tcpPort()));
198 break;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800199 case MPLS_LABEL:
200 Criteria.MplsCriterion mp = (Criteria.MplsCriterion) c;
201 mBuilder.setExact(MatchField.MPLS_LABEL,
202 U32.of(mp.label().intValue()));
203 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700204 case OCH_SIGID:
205 LambdaCriterion lc = (LambdaCriterion) c;
206 mBuilder.setExact(MatchField.OCH_SIGID,
207 new CircuitSignalID((byte) 1, (byte) 2, lc.lambda(), (short) 1));
208 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800209 case OCH_SIGTYPE:
210 Criteria.OpticalSignalTypeCriterion sc =
211 (Criteria.OpticalSignalTypeCriterion) c;
212 mBuilder.setExact(MatchField.OCH_SIGTYPE,
213 U8.of(sc.signalType()));
214 break;
alshabibeec3a062014-09-17 18:01:26 -0700215 case ARP_OP:
216 case ARP_SHA:
217 case ARP_SPA:
218 case ARP_THA:
219 case ARP_TPA:
220 case ICMPV4_CODE:
221 case ICMPV4_TYPE:
222 case ICMPV6_CODE:
223 case ICMPV6_TYPE:
224 case IN_PHY_PORT:
225 case IPV6_DST:
226 case IPV6_EXTHDR:
227 case IPV6_FLABEL:
228 case IPV6_ND_SLL:
229 case IPV6_ND_TARGET:
230 case IPV6_ND_TLL:
231 case IPV6_SRC:
232 case IP_DSCP:
233 case IP_ECN:
234 case METADATA:
235 case MPLS_BOS:
alshabibeec3a062014-09-17 18:01:26 -0700236 case MPLS_TC:
237 case PBB_ISID:
238 case SCTP_DST:
239 case SCTP_SRC:
alshabibeec3a062014-09-17 18:01:26 -0700240 case TUNNEL_ID:
241 case UDP_DST:
242 case UDP_SRC:
243 default:
244 log.warn("Match type {} not yet implemented.", c.type());
245 }
246 }
247 return mBuilder.build();
248 }
249
Jonathan Hart86e59352014-10-22 10:42:16 -0700250 /**
251 * Returns the flow rule for this builder.
252 *
253 * @return the flow rule
254 */
255 protected FlowRule flowRule() {
256 return flowRule;
257 }
alshabib219ebaa2014-09-22 15:41:24 -0700258
Jonathan Hart86e59352014-10-22 10:42:16 -0700259 /**
260 * Returns the factory used for building OpenFlow constructs.
261 *
262 * @return the factory
263 */
264 protected OFFactory factory() {
265 return factory;
266 }
alshabib219ebaa2014-09-22 15:41:24 -0700267
alshabibeec3a062014-09-17 18:01:26 -0700268}