blob: 17bfc1159c85378c6f9bf2b6bc163ba51fd94272 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.of.flow.impl;
alshabib6b5cfec2014-09-18 17:42:18 -070017
Jonathan Hart6cd2f352015-01-13 17:44:45 -080018import com.google.common.collect.Lists;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080019import org.onlab.packet.Ip4Address;
20import org.onlab.packet.Ip4Prefix;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -080021import org.onlab.packet.Ip6Address;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080022import org.onlab.packet.Ip6Prefix;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080023import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010024import org.onlab.packet.MplsLabel;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080025import org.onlab.packet.VlanId;
sangho8995ac52015-02-04 11:29:03 -080026import org.onosproject.core.DefaultGroupId;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.DeviceId;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.flow.DefaultFlowEntry;
30import org.onosproject.net.flow.DefaultFlowRule;
31import org.onosproject.net.flow.DefaultTrafficSelector;
32import org.onosproject.net.flow.DefaultTrafficTreatment;
33import org.onosproject.net.flow.FlowEntry;
34import org.onosproject.net.flow.FlowEntry.FlowEntryState;
35import org.onosproject.net.flow.FlowRule;
Saurav Dasfa2fa932015-03-03 11:29:48 -080036import org.onosproject.net.flow.FlowRule.Type;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.flow.TrafficSelector;
38import org.onosproject.net.flow.TrafficTreatment;
39import org.onosproject.openflow.controller.Dpid;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080040import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabib6b5cfec2014-09-18 17:42:18 -070041import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
42import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
43import org.projectfloodlight.openflow.protocol.action.OFAction;
Marc De Leenheer49087752014-10-23 13:54:09 -070044import org.projectfloodlight.openflow.protocol.action.OFActionCircuit;
45import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter;
sangho8995ac52015-02-04 11:29:03 -080046import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
alshabib6b5cfec2014-09-18 17:42:18 -070047import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
sangho3f97a17d2015-01-29 22:56:29 -080048import org.projectfloodlight.openflow.protocol.action.OFActionPopMpls;
alshabib6b5cfec2014-09-18 17:42:18 -070049import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst;
50import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080051import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
alshabib6b5cfec2014-09-18 17:42:18 -070052import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst;
53import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc;
54import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp;
55import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid;
alshabib19fdc122014-10-03 11:38:19 -070056import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hartcf783202014-11-24 18:55:42 -080057import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions;
alshabib346b5b32015-03-06 00:42:16 -080058import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteActions;
alshabib6b5cfec2014-09-18 17:42:18 -070059import org.projectfloodlight.openflow.protocol.match.Match;
60import org.projectfloodlight.openflow.protocol.match.MatchField;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080061import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
Marc De Leenheer49087752014-10-23 13:54:09 -070062import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigidBasic;
alshabib346b5b32015-03-06 00:42:16 -080063import org.projectfloodlight.openflow.protocol.ver13.OFFactoryVer13;
alshabib6b5cfec2014-09-18 17:42:18 -070064import org.projectfloodlight.openflow.types.IPv4Address;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080065import org.projectfloodlight.openflow.types.IPv6Address;
Jonathan Hart0e12fad2014-10-17 14:54:58 -070066import org.projectfloodlight.openflow.types.Masked;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080067import org.projectfloodlight.openflow.types.OFVlanVidMatch;
sangho3f97a17d2015-01-29 22:56:29 -080068import org.projectfloodlight.openflow.types.U32;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080069import org.projectfloodlight.openflow.types.VlanPcp;
alshabib6b5cfec2014-09-18 17:42:18 -070070import org.slf4j.Logger;
71
Jonathan Hart6cd2f352015-01-13 17:44:45 -080072import java.util.List;
73
74import static org.slf4j.LoggerFactory.getLogger;
alshabib19fdc122014-10-03 11:38:19 -070075
alshabib1c319ff2014-10-04 20:29:09 -070076public class FlowEntryBuilder {
alshabib6b5cfec2014-09-18 17:42:18 -070077 private final Logger log = getLogger(getClass());
78
79 private final OFFlowStatsEntry stat;
80 private final OFFlowRemoved removed;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080081 private final OFFlowMod flowMod;
alshabib6b5cfec2014-09-18 17:42:18 -070082
83 private final Match match;
alshabib346b5b32015-03-06 00:42:16 -080084
85 /*
86 All actions are contained in an OFInstruction. For OF1.0
87 the instruction type is apply instruction (immediate set in ONOS speak)
88 */
89 private final List<OFInstruction> instructions;
alshabib6b5cfec2014-09-18 17:42:18 -070090
91 private final Dpid dpid;
92
Brian O'Connor72cb19a2015-01-16 16:14:41 -080093 public enum FlowType { STAT, REMOVED, MOD }
94
95 private final FlowType type;
Saurav Dasfa2fa932015-03-03 11:29:48 -080096 private Type tableType = FlowRule.Type.DEFAULT;
alshabib6b5cfec2014-09-18 17:42:18 -070097
Saurav Dasfa2fa932015-03-03 11:29:48 -080098 public FlowEntryBuilder(Dpid dpid, OFFlowStatsEntry entry, Type tableType) {
99 this.stat = entry;
100 this.match = entry.getMatch();
alshabib346b5b32015-03-06 00:42:16 -0800101 this.instructions = getInstructions(entry);
Saurav Dasfa2fa932015-03-03 11:29:48 -0800102 this.dpid = dpid;
103 this.removed = null;
104 this.flowMod = null;
105 this.type = FlowType.STAT;
106 this.tableType = tableType;
107 }
108
alshabibda1644e2015-03-13 14:01:35 -0700109 public FlowEntryBuilder(Dpid dpid, OFFlowRemoved removed, Type tableType) {
alshabib6b5cfec2014-09-18 17:42:18 -0700110 this.match = removed.getMatch();
111 this.removed = removed;
112
113 this.dpid = dpid;
alshabib346b5b32015-03-06 00:42:16 -0800114 this.instructions = null;
alshabib6b5cfec2014-09-18 17:42:18 -0700115 this.stat = null;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800116 this.flowMod = null;
117 this.type = FlowType.REMOVED;
alshabibda1644e2015-03-13 14:01:35 -0700118 this.tableType = tableType;
alshabib6b5cfec2014-09-18 17:42:18 -0700119
120 }
121
alshabibda1644e2015-03-13 14:01:35 -0700122 public FlowEntryBuilder(Dpid dpid, OFFlowMod fm, Type tableType) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800123 this.match = fm.getMatch();
124 this.dpid = dpid;
alshabib346b5b32015-03-06 00:42:16 -0800125 this.instructions = getInstructions(fm);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800126 this.type = FlowType.MOD;
127 this.flowMod = fm;
128 this.stat = null;
129 this.removed = null;
alshabibda1644e2015-03-13 14:01:35 -0700130 this.tableType = tableType;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800131 }
alshabib1c319ff2014-10-04 20:29:09 -0700132
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800133 public FlowEntry build(FlowEntryState... state) {
134 FlowRule rule;
135 switch (this.type) {
136 case STAT:
137 rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
138 buildSelector(), buildTreatment(), stat.getPriority(),
Saurav Dasfa2fa932015-03-03 11:29:48 -0800139 stat.getCookie().getValue(), stat.getIdleTimeout(), false,
140 tableType);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800141 return new DefaultFlowEntry(rule, FlowEntryState.ADDED,
142 stat.getDurationSec(), stat.getPacketCount().getValue(),
143 stat.getByteCount().getValue());
144 case REMOVED:
145 rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
146 buildSelector(), null, removed.getPriority(),
alshabibda1644e2015-03-13 14:01:35 -0700147 removed.getCookie().getValue(), removed.getIdleTimeout(), false,
148 tableType);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800149 return new DefaultFlowEntry(rule, FlowEntryState.REMOVED, removed.getDurationSec(),
150 removed.getPacketCount().getValue(), removed.getByteCount().getValue());
151 case MOD:
152 FlowEntryState flowState = state.length > 0 ? state[0] : FlowEntryState.FAILED;
153 rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
154 buildSelector(), buildTreatment(), flowMod.getPriority(),
alshabibda1644e2015-03-13 14:01:35 -0700155 flowMod.getCookie().getValue(), flowMod.getIdleTimeout(), false,
156 tableType);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800157 return new DefaultFlowEntry(rule, flowState, 0, 0, 0);
158 default:
159 log.error("Unknown flow type : {}", this.type);
160 return null;
alshabib6b5cfec2014-09-18 17:42:18 -0700161 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800162
alshabib6b5cfec2014-09-18 17:42:18 -0700163 }
164
alshabib346b5b32015-03-06 00:42:16 -0800165 private List<OFInstruction> getInstructions(OFFlowMod entry) {
alshabib19fdc122014-10-03 11:38:19 -0700166 switch (entry.getVersion()) {
167 case OF_10:
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700168 return Lists.newArrayList(OFFactoryVer13.INSTANCE.instructions()
169 .applyActions(
170 entry.getActions()));
alshabib19fdc122014-10-03 11:38:19 -0700171 case OF_11:
172 case OF_12:
173 case OF_13:
alshabib346b5b32015-03-06 00:42:16 -0800174 return entry.getInstructions();
175 default:
176 log.warn("Unknown OF version {}", entry.getVersion());
177 }
178 return Lists.newLinkedList();
179 }
180
181 private List<OFInstruction> getInstructions(OFFlowStatsEntry entry) {
182 switch (entry.getVersion()) {
183 case OF_10:
184 return Lists.newArrayList(
185 OFFactoryVer13.INSTANCE.instructions().applyActions(entry.getActions()));
186 case OF_11:
187 case OF_12:
188 case OF_13:
189 return entry.getInstructions();
alshabib19fdc122014-10-03 11:38:19 -0700190 default:
191 log.warn("Unknown OF version {}", entry.getVersion());
192 }
193 return Lists.newLinkedList();
194 }
alshabib6b5cfec2014-09-18 17:42:18 -0700195
196 private TrafficTreatment buildTreatment() {
tom9a693fd2014-10-03 11:32:19 -0700197 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700198 // If this is a drop rule
alshabib346b5b32015-03-06 00:42:16 -0800199 if (instructions.size() == 0) {
alshabib010c31d2014-09-26 10:01:12 -0700200 builder.drop();
alshabib6b5cfec2014-09-18 17:42:18 -0700201 return builder.build();
202 }
alshabib346b5b32015-03-06 00:42:16 -0800203 for (OFInstruction in : instructions) {
204 switch (in.getType()) {
205 case GOTO_TABLE:
206 builder.transition(tableType);
207 break;
208 case WRITE_METADATA:
209 break;
210 case WRITE_ACTIONS:
211 builder.deferred();
212 buildActions(((OFInstructionWriteActions) in).getActions(),
213 builder);
214 break;
215 case APPLY_ACTIONS:
216 builder.immediate();
217 buildActions(((OFInstructionApplyActions) in).getActions(),
218 builder);
219 break;
220 case CLEAR_ACTIONS:
221 builder.wipeDeferred();
222 break;
223 case EXPERIMENTER:
224 break;
225 case METER:
226 break;
227 default:
228 log.warn("Unknown instructions type {}", in.getType());
alshabib6b5cfec2014-09-18 17:42:18 -0700229 }
230 }
231
232 return builder.build();
233 }
234
alshabib346b5b32015-03-06 00:42:16 -0800235 private TrafficTreatment.Builder buildActions(List<OFAction> actions,
236 TrafficTreatment.Builder builder) {
237 for (OFAction act : actions) {
238 switch (act.getType()) {
239 case OUTPUT:
240 OFActionOutput out = (OFActionOutput) act;
241 builder.setOutput(
242 PortNumber.portNumber(out.getPort().getPortNumber()));
243 break;
244 case SET_VLAN_VID:
245 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
246 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
247 break;
248 case SET_VLAN_PCP:
249 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
250 builder.setVlanPcp(pcp.getVlanPcp().getValue());
251 break;
252 case SET_DL_DST:
253 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
254 builder.setEthDst(
255 MacAddress.valueOf(dldst.getDlAddr().getLong()));
256 break;
257 case SET_DL_SRC:
258 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
259 builder.setEthSrc(
260 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
261
262 break;
263 case SET_NW_DST:
264 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
265 IPv4Address di = nwdst.getNwAddr();
266 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
267 break;
268 case SET_NW_SRC:
269 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
270 IPv4Address si = nwsrc.getNwAddr();
271 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
272 break;
273 case EXPERIMENTER:
274 OFActionExperimenter exp = (OFActionExperimenter) act;
275 if (exp.getExperimenter() == 0x80005A06 ||
276 exp.getExperimenter() == 0x748771) {
277 OFActionCircuit ct = (OFActionCircuit) exp;
278 builder.setLambda(((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber());
279 } else {
280 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
281 }
282 break;
283 case SET_FIELD:
284 OFActionSetField setField = (OFActionSetField) act;
285 handleSetField(builder, setField.getField());
286 break;
287 case POP_MPLS:
288 OFActionPopMpls popMpls = (OFActionPopMpls) act;
289 builder.popMpls((short) popMpls.getEthertype().getValue());
290 break;
291 case PUSH_MPLS:
292 builder.pushMpls();
293 break;
294 case COPY_TTL_IN:
295 builder.copyTtlIn();
296 break;
297 case COPY_TTL_OUT:
298 builder.copyTtlOut();
299 break;
300 case DEC_MPLS_TTL:
301 builder.decMplsTtl();
302 break;
303 case DEC_NW_TTL:
304 builder.decNwTtl();
305 break;
306 case GROUP:
307 OFActionGroup group = (OFActionGroup) act;
308 builder.group(new DefaultGroupId(group.getGroup().getGroupNumber()));
309 break;
310 case POP_VLAN:
311 builder.popVlan();
312 break;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700313 case PUSH_VLAN:
314 builder.pushVlan();
315 break;
alshabib346b5b32015-03-06 00:42:16 -0800316 case STRIP_VLAN:
317 builder.stripVlan();
318 break;
319 case SET_TP_DST:
320 case SET_TP_SRC:
321 case POP_PBB:
322 case PUSH_PBB:
alshabib346b5b32015-03-06 00:42:16 -0800323 case SET_MPLS_LABEL:
324 case SET_MPLS_TC:
325 case SET_MPLS_TTL:
326 case SET_NW_ECN:
327 case SET_NW_TOS:
328 case SET_NW_TTL:
329 case SET_QUEUE:
330
331 case ENQUEUE:
332 default:
333 log.warn("Action type {} not yet implemented.", act.getType());
334 }
335 }
336 return builder;
337 }
338
339
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800340 private void handleSetField(TrafficTreatment.Builder builder, OFOxm<?> oxm) {
341 switch (oxm.getMatchField().id) {
342 case VLAN_PCP:
343 @SuppressWarnings("unchecked")
344 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
345 builder.setVlanPcp(vlanpcp.getValue().getValue());
346 break;
347 case VLAN_VID:
348 @SuppressWarnings("unchecked")
349 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
350 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
351 break;
352 case ETH_DST:
353 @SuppressWarnings("unchecked")
354 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
355 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
356 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
357 break;
358 case ETH_SRC:
359 @SuppressWarnings("unchecked")
360 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
361 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
362 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
363 break;
364 case IPV4_DST:
365 @SuppressWarnings("unchecked")
366 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
367 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
368 break;
369 case IPV4_SRC:
370 @SuppressWarnings("unchecked")
371 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
372 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
373 break;
sangho3f97a17d2015-01-29 22:56:29 -0800374 case MPLS_LABEL:
375 @SuppressWarnings("unchecked")
376 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100377 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
sangho3f97a17d2015-01-29 22:56:29 -0800378 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800379 case ARP_OP:
380 case ARP_SHA:
381 case ARP_SPA:
382 case ARP_THA:
383 case ARP_TPA:
384 case BSN_EGR_PORT_GROUP_ID:
385 case BSN_GLOBAL_VRF_ALLOWED:
386 case BSN_IN_PORTS_128:
387 case BSN_L3_DST_CLASS_ID:
388 case BSN_L3_INTERFACE_CLASS_ID:
389 case BSN_L3_SRC_CLASS_ID:
390 case BSN_LAG_ID:
391 case BSN_TCP_FLAGS:
392 case BSN_UDF0:
393 case BSN_UDF1:
394 case BSN_UDF2:
395 case BSN_UDF3:
396 case BSN_UDF4:
397 case BSN_UDF5:
398 case BSN_UDF6:
399 case BSN_UDF7:
400 case BSN_VLAN_XLATE_PORT_GROUP_ID:
401 case BSN_VRF:
402 case ETH_TYPE:
403 case ICMPV4_CODE:
404 case ICMPV4_TYPE:
405 case ICMPV6_CODE:
406 case ICMPV6_TYPE:
407 case IN_PHY_PORT:
408 case IN_PORT:
409 case IPV6_DST:
410 case IPV6_FLABEL:
411 case IPV6_ND_SLL:
412 case IPV6_ND_TARGET:
413 case IPV6_ND_TLL:
414 case IPV6_SRC:
415 case IP_DSCP:
416 case IP_ECN:
417 case IP_PROTO:
418 case METADATA:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800419 case MPLS_TC:
420 case OCH_SIGID:
421 case OCH_SIGID_BASIC:
422 case OCH_SIGTYPE:
423 case OCH_SIGTYPE_BASIC:
424 case SCTP_DST:
425 case SCTP_SRC:
426 case TCP_DST:
427 case TCP_SRC:
428 case TUNNEL_ID:
429 case UDP_DST:
430 case UDP_SRC:
431 default:
432 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
433 break;
434 }
435 }
436
alshabib6b5cfec2014-09-18 17:42:18 -0700437 private TrafficSelector buildSelector() {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800438 MacAddress mac;
439 Ip4Prefix ip4Prefix;
440 Ip6Address ip6Address;
441 Ip6Prefix ip6Prefix;
442
tom9a693fd2014-10-03 11:32:19 -0700443 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700444 for (MatchField<?> field : match.getMatchFields()) {
445 switch (field.id) {
446 case IN_PORT:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800447 builder.matchInPort(PortNumber
alshabib010c31d2014-09-26 10:01:12 -0700448 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700449 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800450 case IN_PHY_PORT:
451 builder.matchInPhyPort(PortNumber
452 .portNumber(match.get(MatchField.IN_PHY_PORT).getPortNumber()));
453 break;
454 case METADATA:
455 long metadata =
456 match.get(MatchField.METADATA).getValue().getValue();
457 builder.matchMetadata(metadata);
458 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700459 case ETH_DST:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800460 mac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
461 builder.matchEthDst(mac);
462 break;
463 case ETH_SRC:
464 mac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
465 builder.matchEthSrc(mac);
alshabib6b5cfec2014-09-18 17:42:18 -0700466 break;
467 case ETH_TYPE:
468 int ethType = match.get(MatchField.ETH_TYPE).getValue();
alshabib010c31d2014-09-26 10:01:12 -0700469 builder.matchEthType((short) ethType);
alshabib6b5cfec2014-09-18 17:42:18 -0700470 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700471 case VLAN_VID:
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800472 VlanId vlanId = null;
473 if (match.isPartiallyMasked(MatchField.VLAN_VID)) {
474 Masked<OFVlanVidMatch> masked = match.getMasked(MatchField.VLAN_VID);
475 if (masked.getValue().equals(OFVlanVidMatch.PRESENT)
476 && masked.getMask().equals(OFVlanVidMatch.PRESENT)) {
477 vlanId = VlanId.ANY;
478 }
479 } else {
480 vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
481 }
482 if (vlanId != null) {
483 builder.matchVlanId(vlanId);
484 }
alshabib6b5cfec2014-09-18 17:42:18 -0700485 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800486 case VLAN_PCP:
487 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
488 builder.matchVlanPcp(vlanPcp);
489 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800490 case IP_DSCP:
491 byte ipDscp = match.get(MatchField.IP_DSCP).getDscpValue();
492 builder.matchIPDscp(ipDscp);
493 break;
494 case IP_ECN:
495 byte ipEcn = match.get(MatchField.IP_ECN).getEcnValue();
496 builder.matchIPEcn(ipEcn);
497 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800498 case IP_PROTO:
499 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
500 builder.matchIPProtocol((byte) proto);
501 break;
502 case IPV4_SRC:
503 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
504 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800505 ip4Prefix = Ip4Prefix.valueOf(
506 maskedIp.getValue().getInt(),
507 maskedIp.getMask().asCidrMaskLength());
508 } else {
509 ip4Prefix = Ip4Prefix.valueOf(
510 match.get(MatchField.IPV4_SRC).getInt(),
511 Ip4Prefix.MAX_MASK_LENGTH);
512 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800513 builder.matchIPSrc(ip4Prefix);
514 break;
515 case IPV4_DST:
516 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
517 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800518 ip4Prefix = Ip4Prefix.valueOf(
519 maskedIp.getValue().getInt(),
520 maskedIp.getMask().asCidrMaskLength());
521 } else {
522 ip4Prefix = Ip4Prefix.valueOf(
523 match.get(MatchField.IPV4_DST).getInt(),
524 Ip4Prefix.MAX_MASK_LENGTH);
525 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800526 builder.matchIPDst(ip4Prefix);
Jonathan Hart34bc6142014-10-17 11:00:43 -0700527 break;
528 case TCP_SRC:
529 builder.matchTcpSrc((short) match.get(MatchField.TCP_SRC).getPort());
530 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800531 case TCP_DST:
532 builder.matchTcpDst((short) match.get(MatchField.TCP_DST).getPort());
533 break;
534 case UDP_SRC:
535 builder.matchUdpSrc((short) match.get(MatchField.UDP_SRC).getPort());
536 break;
537 case UDP_DST:
538 builder.matchUdpDst((short) match.get(MatchField.UDP_DST).getPort());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100539 case MPLS_LABEL:
540 builder.matchMplsLabel(MplsLabel.mplsLabel((int) match.get(MatchField.MPLS_LABEL)
541 .getValue()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800542 break;
543 case SCTP_SRC:
544 builder.matchSctpSrc((short) match.get(MatchField.SCTP_SRC).getPort());
545 break;
546 case SCTP_DST:
547 builder.matchSctpDst((short) match.get(MatchField.SCTP_DST).getPort());
548 break;
549 case ICMPV4_TYPE:
550 byte icmpType = (byte) match.get(MatchField.ICMPV4_TYPE).getType();
551 builder.matchIcmpType(icmpType);
552 break;
553 case ICMPV4_CODE:
554 byte icmpCode = (byte) match.get(MatchField.ICMPV4_CODE).getCode();
555 builder.matchIcmpCode(icmpCode);
556 break;
557 case IPV6_SRC:
558 if (match.isPartiallyMasked(MatchField.IPV6_SRC)) {
559 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_SRC);
560 ip6Prefix = Ip6Prefix.valueOf(
561 maskedIp.getValue().getBytes(),
562 maskedIp.getMask().asCidrMaskLength());
563 } else {
564 ip6Prefix = Ip6Prefix.valueOf(
565 match.get(MatchField.IPV6_SRC).getBytes(),
566 Ip6Prefix.MAX_MASK_LENGTH);
567 }
568 builder.matchIPv6Src(ip6Prefix);
569 break;
570 case IPV6_DST:
571 if (match.isPartiallyMasked(MatchField.IPV6_DST)) {
572 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_DST);
573 ip6Prefix = Ip6Prefix.valueOf(
574 maskedIp.getValue().getBytes(),
575 maskedIp.getMask().asCidrMaskLength());
576 } else {
577 ip6Prefix = Ip6Prefix.valueOf(
578 match.get(MatchField.IPV6_DST).getBytes(),
579 Ip6Prefix.MAX_MASK_LENGTH);
580 }
581 builder.matchIPv6Dst(ip6Prefix);
582 break;
583 case IPV6_FLABEL:
584 int flowLabel =
585 match.get(MatchField.IPV6_FLABEL).getIPv6FlowLabelValue();
586 builder.matchIPv6FlowLabel(flowLabel);
587 break;
588 case ICMPV6_TYPE:
589 byte icmpv6type = (byte) match.get(MatchField.ICMPV6_TYPE).getValue();
590 builder.matchIcmpv6Type(icmpv6type);
591 break;
592 case ICMPV6_CODE:
593 byte icmpv6code = (byte) match.get(MatchField.ICMPV6_CODE).getValue();
594 builder.matchIcmpv6Code(icmpv6code);
595 break;
596 case IPV6_ND_TARGET:
597 ip6Address =
598 Ip6Address.valueOf(match.get(MatchField.IPV6_ND_TARGET).getBytes());
599 builder.matchIPv6NDTargetAddress(ip6Address);
600 break;
601 case IPV6_ND_SLL:
602 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_SLL).getLong());
603 builder.matchIPv6NDSourceLinkLayerAddress(mac);
604 break;
605 case IPV6_ND_TLL:
606 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_TLL).getLong());
607 builder.matchIPv6NDTargetLinkLayerAddress(mac);
608 break;
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800609 case IPV6_EXTHDR:
610 builder.matchIPv6ExthdrFlags((int) match.get(MatchField.IPV6_EXTHDR)
611 .getValue());
612 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700613 case OCH_SIGID:
614 builder.matchLambda(match.get(MatchField.OCH_SIGID).getChannelNumber());
615 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800616 case OCH_SIGTYPE:
617 builder.matchOpticalSignalType(match.get(MatchField
618 .OCH_SIGTYPE).getValue());
619 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700620 case ARP_OP:
621 case ARP_SHA:
622 case ARP_SPA:
623 case ARP_THA:
624 case ARP_TPA:
alshabib6b5cfec2014-09-18 17:42:18 -0700625 case MPLS_TC:
alshabib6b5cfec2014-09-18 17:42:18 -0700626 case TUNNEL_ID:
alshabib6b5cfec2014-09-18 17:42:18 -0700627 default:
628 log.warn("Match type {} not yet implemented.", field.id);
alshabib6b5cfec2014-09-18 17:42:18 -0700629 }
630 }
631 return builder.build();
632 }
alshabib6b5cfec2014-09-18 17:42:18 -0700633}