blob: 793c9f39bf2b11e59eba87de15cbd5cabc049430 [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
Brian O'Connor427a1762014-11-19 18:40:32 -080056import java.util.Optional;
57
Jonathan Hart86e59352014-10-22 10:42:16 -070058/**
59 * Builder for OpenFlow flow mods based on FlowRules.
60 */
61public abstract class FlowModBuilder {
alshabibeec3a062014-09-17 18:01:26 -070062
63 private final Logger log = getLogger(getClass());
64
65 private final OFFactory factory;
Jonathan Hart86e59352014-10-22 10:42:16 -070066 private final FlowRule flowRule;
alshabibeec3a062014-09-17 18:01:26 -070067 private final TrafficSelector selector;
Brian O'Connor427a1762014-11-19 18:40:32 -080068 protected final Long xid;
alshabibeec3a062014-09-17 18:01:26 -070069
Jonathan Hart86e59352014-10-22 10:42:16 -070070 /**
71 * Creates a new flow mod builder.
72 *
73 * @param flowRule the flow rule to transform into a flow mod
74 * @param factory the OpenFlow factory to use to build the flow mod
75 * @return the new flow mod builder
76 */
Brian O'Connor427a1762014-11-19 18:40:32 -080077 public static FlowModBuilder builder(FlowRule flowRule,
78 OFFactory factory, Optional<Long> xid) {
Jonathan Hart86e59352014-10-22 10:42:16 -070079 switch (factory.getVersion()) {
80 case OF_10:
Brian O'Connor427a1762014-11-19 18:40:32 -080081 return new FlowModBuilderVer10(flowRule, factory, xid);
Jonathan Hart86e59352014-10-22 10:42:16 -070082 case OF_13:
Brian O'Connor427a1762014-11-19 18:40:32 -080083 return new FlowModBuilderVer13(flowRule, factory, xid);
Jonathan Hart86e59352014-10-22 10:42:16 -070084 default:
85 throw new UnsupportedOperationException(
86 "No flow mod builder for protocol version " + factory.getVersion());
87 }
88 }
alshabibeec3a062014-09-17 18:01:26 -070089
Jonathan Hart86e59352014-10-22 10:42:16 -070090 /**
91 * Constructs a flow mod builder.
92 *
93 * @param flowRule the flow rule to transform into a flow mod
94 * @param factory the OpenFlow factory to use to build the flow mod
95 */
Brian O'Connor427a1762014-11-19 18:40:32 -080096 protected FlowModBuilder(FlowRule flowRule, OFFactory factory, Optional<Long> xid) {
alshabibeec3a062014-09-17 18:01:26 -070097 this.factory = factory;
Jonathan Hart86e59352014-10-22 10:42:16 -070098 this.flowRule = flowRule;
alshabibeec3a062014-09-17 18:01:26 -070099 this.selector = flowRule.selector();
Brian O'Connor427a1762014-11-19 18:40:32 -0800100 this.xid = xid.orElse((long) 0);
101
alshabibeec3a062014-09-17 18:01:26 -0700102 }
103
Jonathan Hart86e59352014-10-22 10:42:16 -0700104 /**
105 * Builds an ADD flow mod.
106 *
107 * @return the flow mod
108 */
109 public abstract OFFlowAdd buildFlowAdd();
alshabibeec3a062014-09-17 18:01:26 -0700110
Jonathan Hart86e59352014-10-22 10:42:16 -0700111 /**
112 * Builds a MODIFY flow mod.
113 *
114 * @return the flow mod
115 */
116 public abstract OFFlowMod buildFlowMod();
alshabibeec3a062014-09-17 18:01:26 -0700117
Jonathan Hart86e59352014-10-22 10:42:16 -0700118 /**
119 * Builds a DELETE flow mod.
120 *
121 * @return the flow mod
122 */
123 public abstract OFFlowDelete buildFlowDel();
alshabibeec3a062014-09-17 18:01:26 -0700124
Jonathan Hart86e59352014-10-22 10:42:16 -0700125 /**
126 * Builds the match for the flow mod.
127 *
128 * @return the match
129 */
130 protected Match buildMatch() {
alshabibeec3a062014-09-17 18:01:26 -0700131 Match.Builder mBuilder = factory.buildMatch();
132 EthCriterion eth;
133 IPCriterion ip;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800134 Ip4Prefix ip4Prefix;
Jonathan Hart34bc6142014-10-17 11:00:43 -0700135 TcpPortCriterion tp;
alshabibeec3a062014-09-17 18:01:26 -0700136 for (Criterion c : selector.criteria()) {
137 switch (c.type()) {
138 case IN_PORT:
139 PortCriterion inport = (PortCriterion) c;
140 mBuilder.setExact(MatchField.IN_PORT, OFPort.of((int) inport.port().toLong()));
141 break;
142 case ETH_SRC:
143 eth = (EthCriterion) c;
144 mBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(eth.mac().toLong()));
145 break;
146 case ETH_DST:
147 eth = (EthCriterion) c;
148 mBuilder.setExact(MatchField.ETH_DST, MacAddress.of(eth.mac().toLong()));
149 break;
150 case ETH_TYPE:
151 EthTypeCriterion ethType = (EthTypeCriterion) c;
152 mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(ethType.ethType()));
153 break;
154 case IPV4_DST:
155 ip = (IPCriterion) c;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800156 ip4Prefix = ip.ip().getIp4Prefix();
157 if (ip4Prefix.prefixLength() != Ip4Prefix.MAX_MASK_LENGTH) {
158 Ip4Address maskAddr =
159 Ip4Address.makeMaskPrefix(ip4Prefix.prefixLength());
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700160 Masked<IPv4Address> maskedIp =
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800161 Masked.of(IPv4Address.of(ip4Prefix.address().toInt()),
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700162 IPv4Address.of(maskAddr.toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700163 mBuilder.setMasked(MatchField.IPV4_DST, maskedIp);
164 } else {
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700165 mBuilder.setExact(MatchField.IPV4_DST,
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800166 IPv4Address.of(ip4Prefix.address().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700167 }
168 break;
169 case IPV4_SRC:
170 ip = (IPCriterion) c;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800171 ip4Prefix = ip.ip().getIp4Prefix();
172 if (ip4Prefix.prefixLength() != Ip4Prefix.MAX_MASK_LENGTH) {
173 Ip4Address maskAddr =
174 Ip4Address.makeMaskPrefix(ip4Prefix.prefixLength());
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700175 Masked<IPv4Address> maskedIp =
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800176 Masked.of(IPv4Address.of(ip4Prefix.address().toInt()),
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700177 IPv4Address.of(maskAddr.toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700178 mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp);
179 } else {
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700180 mBuilder.setExact(MatchField.IPV4_SRC,
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800181 IPv4Address.of(ip4Prefix.address().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700182 }
183 break;
184 case IP_PROTO:
185 IPProtocolCriterion p = (IPProtocolCriterion) c;
186 mBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(p.protocol()));
187 break;
188 case VLAN_PCP:
189 VlanPcpCriterion vpcp = (VlanPcpCriterion) c;
190 mBuilder.setExact(MatchField.VLAN_PCP, VlanPcp.of(vpcp.priority()));
191 break;
192 case VLAN_VID:
193 VlanIdCriterion vid = (VlanIdCriterion) c;
194 mBuilder.setExact(MatchField.VLAN_VID,
195 OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vid.vlanId().toShort())));
196 break;
Jonathan Hart34bc6142014-10-17 11:00:43 -0700197 case TCP_DST:
198 tp = (TcpPortCriterion) c;
199 mBuilder.setExact(MatchField.TCP_DST, TransportPort.of(tp.tcpPort()));
200 break;
201 case TCP_SRC:
202 tp = (TcpPortCriterion) c;
203 mBuilder.setExact(MatchField.TCP_SRC, TransportPort.of(tp.tcpPort()));
204 break;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800205 case MPLS_LABEL:
206 Criteria.MplsCriterion mp = (Criteria.MplsCriterion) c;
207 mBuilder.setExact(MatchField.MPLS_LABEL,
208 U32.of(mp.label().intValue()));
209 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700210 case OCH_SIGID:
211 LambdaCriterion lc = (LambdaCriterion) c;
212 mBuilder.setExact(MatchField.OCH_SIGID,
213 new CircuitSignalID((byte) 1, (byte) 2, lc.lambda(), (short) 1));
214 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800215 case OCH_SIGTYPE:
216 Criteria.OpticalSignalTypeCriterion sc =
217 (Criteria.OpticalSignalTypeCriterion) c;
218 mBuilder.setExact(MatchField.OCH_SIGTYPE,
219 U8.of(sc.signalType()));
220 break;
alshabibeec3a062014-09-17 18:01:26 -0700221 case ARP_OP:
222 case ARP_SHA:
223 case ARP_SPA:
224 case ARP_THA:
225 case ARP_TPA:
226 case ICMPV4_CODE:
227 case ICMPV4_TYPE:
228 case ICMPV6_CODE:
229 case ICMPV6_TYPE:
230 case IN_PHY_PORT:
231 case IPV6_DST:
232 case IPV6_EXTHDR:
233 case IPV6_FLABEL:
234 case IPV6_ND_SLL:
235 case IPV6_ND_TARGET:
236 case IPV6_ND_TLL:
237 case IPV6_SRC:
238 case IP_DSCP:
239 case IP_ECN:
240 case METADATA:
241 case MPLS_BOS:
alshabibeec3a062014-09-17 18:01:26 -0700242 case MPLS_TC:
243 case PBB_ISID:
244 case SCTP_DST:
245 case SCTP_SRC:
alshabibeec3a062014-09-17 18:01:26 -0700246 case TUNNEL_ID:
247 case UDP_DST:
248 case UDP_SRC:
249 default:
250 log.warn("Match type {} not yet implemented.", c.type());
251 }
252 }
253 return mBuilder.build();
254 }
255
Jonathan Hart86e59352014-10-22 10:42:16 -0700256 /**
257 * Returns the flow rule for this builder.
258 *
259 * @return the flow rule
260 */
261 protected FlowRule flowRule() {
262 return flowRule;
263 }
alshabib219ebaa2014-09-22 15:41:24 -0700264
Jonathan Hart86e59352014-10-22 10:42:16 -0700265 /**
266 * Returns the factory used for building OpenFlow constructs.
267 *
268 * @return the factory
269 */
270 protected OFFactory factory() {
271 return factory;
272 }
alshabib219ebaa2014-09-22 15:41:24 -0700273
alshabibeec3a062014-09-17 18:01:26 -0700274}