blob: 79efd7c968f59cd473fc49ab230b4d01323b5c99 [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
98
alshabib1c319ff2014-10-04 20:29:09 -070099 public FlowEntryBuilder(Dpid dpid, OFFlowStatsEntry entry) {
alshabib6b5cfec2014-09-18 17:42:18 -0700100 this.stat = entry;
101 this.match = entry.getMatch();
alshabib346b5b32015-03-06 00:42:16 -0800102 this.instructions = getInstructions(entry);
alshabib6b5cfec2014-09-18 17:42:18 -0700103 this.dpid = dpid;
alshabib219ebaa2014-09-22 15:41:24 -0700104 this.removed = null;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800105 this.flowMod = null;
106 this.type = FlowType.STAT;
alshabib6b5cfec2014-09-18 17:42:18 -0700107 }
108
Saurav Dasfa2fa932015-03-03 11:29:48 -0800109 public FlowEntryBuilder(Dpid dpid, OFFlowStatsEntry entry, Type tableType) {
110 this.stat = entry;
111 this.match = entry.getMatch();
alshabib346b5b32015-03-06 00:42:16 -0800112 this.instructions = getInstructions(entry);
Saurav Dasfa2fa932015-03-03 11:29:48 -0800113 this.dpid = dpid;
114 this.removed = null;
115 this.flowMod = null;
116 this.type = FlowType.STAT;
117 this.tableType = tableType;
118 }
119
alshabib1c319ff2014-10-04 20:29:09 -0700120 public FlowEntryBuilder(Dpid dpid, OFFlowRemoved removed) {
alshabib6b5cfec2014-09-18 17:42:18 -0700121 this.match = removed.getMatch();
122 this.removed = removed;
123
124 this.dpid = dpid;
alshabib346b5b32015-03-06 00:42:16 -0800125 this.instructions = null;
alshabib6b5cfec2014-09-18 17:42:18 -0700126 this.stat = null;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800127 this.flowMod = null;
128 this.type = FlowType.REMOVED;
alshabib6b5cfec2014-09-18 17:42:18 -0700129
130 }
131
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800132 public FlowEntryBuilder(Dpid dpid, OFFlowMod fm) {
133 this.match = fm.getMatch();
134 this.dpid = dpid;
alshabib346b5b32015-03-06 00:42:16 -0800135 this.instructions = getInstructions(fm);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800136 this.type = FlowType.MOD;
137 this.flowMod = fm;
138 this.stat = null;
139 this.removed = null;
140 }
alshabib1c319ff2014-10-04 20:29:09 -0700141
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800142 public FlowEntry build(FlowEntryState... state) {
143 FlowRule rule;
144 switch (this.type) {
145 case STAT:
146 rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
147 buildSelector(), buildTreatment(), stat.getPriority(),
Saurav Dasfa2fa932015-03-03 11:29:48 -0800148 stat.getCookie().getValue(), stat.getIdleTimeout(), false,
149 tableType);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800150 return new DefaultFlowEntry(rule, FlowEntryState.ADDED,
151 stat.getDurationSec(), stat.getPacketCount().getValue(),
152 stat.getByteCount().getValue());
153 case REMOVED:
154 rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
155 buildSelector(), null, removed.getPriority(),
156 removed.getCookie().getValue(), removed.getIdleTimeout(), false);
157 return new DefaultFlowEntry(rule, FlowEntryState.REMOVED, removed.getDurationSec(),
158 removed.getPacketCount().getValue(), removed.getByteCount().getValue());
159 case MOD:
160 FlowEntryState flowState = state.length > 0 ? state[0] : FlowEntryState.FAILED;
161 rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
162 buildSelector(), buildTreatment(), flowMod.getPriority(),
163 flowMod.getCookie().getValue(), flowMod.getIdleTimeout(), false);
164 return new DefaultFlowEntry(rule, flowState, 0, 0, 0);
165 default:
166 log.error("Unknown flow type : {}", this.type);
167 return null;
alshabib6b5cfec2014-09-18 17:42:18 -0700168 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800169
alshabib6b5cfec2014-09-18 17:42:18 -0700170 }
171
alshabib346b5b32015-03-06 00:42:16 -0800172 private List<OFInstruction> getInstructions(OFFlowMod entry) {
alshabib19fdc122014-10-03 11:38:19 -0700173 switch (entry.getVersion()) {
174 case OF_10:
alshabib346b5b32015-03-06 00:42:16 -0800175 return Lists.newArrayList(
176 OFFactoryVer13.INSTANCE.instructions().applyActions(entry.getActions()));
alshabib19fdc122014-10-03 11:38:19 -0700177 case OF_11:
178 case OF_12:
179 case OF_13:
alshabib346b5b32015-03-06 00:42:16 -0800180 return entry.getInstructions();
181 default:
182 log.warn("Unknown OF version {}", entry.getVersion());
183 }
184 return Lists.newLinkedList();
185 }
186
187 private List<OFInstruction> getInstructions(OFFlowStatsEntry entry) {
188 switch (entry.getVersion()) {
189 case OF_10:
190 return Lists.newArrayList(
191 OFFactoryVer13.INSTANCE.instructions().applyActions(entry.getActions()));
192 case OF_11:
193 case OF_12:
194 case OF_13:
195 return entry.getInstructions();
alshabib19fdc122014-10-03 11:38:19 -0700196 default:
197 log.warn("Unknown OF version {}", entry.getVersion());
198 }
199 return Lists.newLinkedList();
200 }
alshabib6b5cfec2014-09-18 17:42:18 -0700201
202 private TrafficTreatment buildTreatment() {
tom9a693fd2014-10-03 11:32:19 -0700203 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700204 // If this is a drop rule
alshabib346b5b32015-03-06 00:42:16 -0800205 if (instructions.size() == 0) {
alshabib010c31d2014-09-26 10:01:12 -0700206 builder.drop();
alshabib6b5cfec2014-09-18 17:42:18 -0700207 return builder.build();
208 }
alshabib346b5b32015-03-06 00:42:16 -0800209 for (OFInstruction in : instructions) {
210 switch (in.getType()) {
211 case GOTO_TABLE:
212 builder.transition(tableType);
213 break;
214 case WRITE_METADATA:
215 break;
216 case WRITE_ACTIONS:
217 builder.deferred();
218 buildActions(((OFInstructionWriteActions) in).getActions(),
219 builder);
220 break;
221 case APPLY_ACTIONS:
222 builder.immediate();
223 buildActions(((OFInstructionApplyActions) in).getActions(),
224 builder);
225 break;
226 case CLEAR_ACTIONS:
227 builder.wipeDeferred();
228 break;
229 case EXPERIMENTER:
230 break;
231 case METER:
232 break;
233 default:
234 log.warn("Unknown instructions type {}", in.getType());
alshabib6b5cfec2014-09-18 17:42:18 -0700235 }
236 }
237
238 return builder.build();
239 }
240
alshabib346b5b32015-03-06 00:42:16 -0800241 private TrafficTreatment.Builder buildActions(List<OFAction> actions,
242 TrafficTreatment.Builder builder) {
243 for (OFAction act : actions) {
244 switch (act.getType()) {
245 case OUTPUT:
246 OFActionOutput out = (OFActionOutput) act;
247 builder.setOutput(
248 PortNumber.portNumber(out.getPort().getPortNumber()));
249 break;
250 case SET_VLAN_VID:
251 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
252 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
253 break;
254 case SET_VLAN_PCP:
255 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
256 builder.setVlanPcp(pcp.getVlanPcp().getValue());
257 break;
258 case SET_DL_DST:
259 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
260 builder.setEthDst(
261 MacAddress.valueOf(dldst.getDlAddr().getLong()));
262 break;
263 case SET_DL_SRC:
264 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
265 builder.setEthSrc(
266 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
267
268 break;
269 case SET_NW_DST:
270 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
271 IPv4Address di = nwdst.getNwAddr();
272 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
273 break;
274 case SET_NW_SRC:
275 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
276 IPv4Address si = nwsrc.getNwAddr();
277 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
278 break;
279 case EXPERIMENTER:
280 OFActionExperimenter exp = (OFActionExperimenter) act;
281 if (exp.getExperimenter() == 0x80005A06 ||
282 exp.getExperimenter() == 0x748771) {
283 OFActionCircuit ct = (OFActionCircuit) exp;
284 builder.setLambda(((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber());
285 } else {
286 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
287 }
288 break;
289 case SET_FIELD:
290 OFActionSetField setField = (OFActionSetField) act;
291 handleSetField(builder, setField.getField());
292 break;
293 case POP_MPLS:
294 OFActionPopMpls popMpls = (OFActionPopMpls) act;
295 builder.popMpls((short) popMpls.getEthertype().getValue());
296 break;
297 case PUSH_MPLS:
298 builder.pushMpls();
299 break;
300 case COPY_TTL_IN:
301 builder.copyTtlIn();
302 break;
303 case COPY_TTL_OUT:
304 builder.copyTtlOut();
305 break;
306 case DEC_MPLS_TTL:
307 builder.decMplsTtl();
308 break;
309 case DEC_NW_TTL:
310 builder.decNwTtl();
311 break;
312 case GROUP:
313 OFActionGroup group = (OFActionGroup) act;
314 builder.group(new DefaultGroupId(group.getGroup().getGroupNumber()));
315 break;
316 case POP_VLAN:
317 builder.popVlan();
318 break;
319 case STRIP_VLAN:
320 builder.stripVlan();
321 break;
322 case SET_TP_DST:
323 case SET_TP_SRC:
324 case POP_PBB:
325 case PUSH_PBB:
326 case PUSH_VLAN:
327 case SET_MPLS_LABEL:
328 case SET_MPLS_TC:
329 case SET_MPLS_TTL:
330 case SET_NW_ECN:
331 case SET_NW_TOS:
332 case SET_NW_TTL:
333 case SET_QUEUE:
334
335 case ENQUEUE:
336 default:
337 log.warn("Action type {} not yet implemented.", act.getType());
338 }
339 }
340 return builder;
341 }
342
343
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800344 private void handleSetField(TrafficTreatment.Builder builder, OFOxm<?> oxm) {
345 switch (oxm.getMatchField().id) {
346 case VLAN_PCP:
347 @SuppressWarnings("unchecked")
348 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
349 builder.setVlanPcp(vlanpcp.getValue().getValue());
350 break;
351 case VLAN_VID:
352 @SuppressWarnings("unchecked")
353 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
354 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
355 break;
356 case ETH_DST:
357 @SuppressWarnings("unchecked")
358 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
359 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
360 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
361 break;
362 case ETH_SRC:
363 @SuppressWarnings("unchecked")
364 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
365 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
366 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
367 break;
368 case IPV4_DST:
369 @SuppressWarnings("unchecked")
370 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
371 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
372 break;
373 case IPV4_SRC:
374 @SuppressWarnings("unchecked")
375 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
376 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
377 break;
sangho3f97a17d2015-01-29 22:56:29 -0800378 case MPLS_LABEL:
379 @SuppressWarnings("unchecked")
380 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100381 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
sangho3f97a17d2015-01-29 22:56:29 -0800382 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800383 case ARP_OP:
384 case ARP_SHA:
385 case ARP_SPA:
386 case ARP_THA:
387 case ARP_TPA:
388 case BSN_EGR_PORT_GROUP_ID:
389 case BSN_GLOBAL_VRF_ALLOWED:
390 case BSN_IN_PORTS_128:
391 case BSN_L3_DST_CLASS_ID:
392 case BSN_L3_INTERFACE_CLASS_ID:
393 case BSN_L3_SRC_CLASS_ID:
394 case BSN_LAG_ID:
395 case BSN_TCP_FLAGS:
396 case BSN_UDF0:
397 case BSN_UDF1:
398 case BSN_UDF2:
399 case BSN_UDF3:
400 case BSN_UDF4:
401 case BSN_UDF5:
402 case BSN_UDF6:
403 case BSN_UDF7:
404 case BSN_VLAN_XLATE_PORT_GROUP_ID:
405 case BSN_VRF:
406 case ETH_TYPE:
407 case ICMPV4_CODE:
408 case ICMPV4_TYPE:
409 case ICMPV6_CODE:
410 case ICMPV6_TYPE:
411 case IN_PHY_PORT:
412 case IN_PORT:
413 case IPV6_DST:
414 case IPV6_FLABEL:
415 case IPV6_ND_SLL:
416 case IPV6_ND_TARGET:
417 case IPV6_ND_TLL:
418 case IPV6_SRC:
419 case IP_DSCP:
420 case IP_ECN:
421 case IP_PROTO:
422 case METADATA:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800423 case MPLS_TC:
424 case OCH_SIGID:
425 case OCH_SIGID_BASIC:
426 case OCH_SIGTYPE:
427 case OCH_SIGTYPE_BASIC:
428 case SCTP_DST:
429 case SCTP_SRC:
430 case TCP_DST:
431 case TCP_SRC:
432 case TUNNEL_ID:
433 case UDP_DST:
434 case UDP_SRC:
435 default:
436 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
437 break;
438 }
439 }
440
alshabib6b5cfec2014-09-18 17:42:18 -0700441 private TrafficSelector buildSelector() {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800442 MacAddress mac;
443 Ip4Prefix ip4Prefix;
444 Ip6Address ip6Address;
445 Ip6Prefix ip6Prefix;
446
tom9a693fd2014-10-03 11:32:19 -0700447 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700448 for (MatchField<?> field : match.getMatchFields()) {
449 switch (field.id) {
450 case IN_PORT:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800451 builder.matchInPort(PortNumber
alshabib010c31d2014-09-26 10:01:12 -0700452 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700453 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800454 case IN_PHY_PORT:
455 builder.matchInPhyPort(PortNumber
456 .portNumber(match.get(MatchField.IN_PHY_PORT).getPortNumber()));
457 break;
458 case METADATA:
459 long metadata =
460 match.get(MatchField.METADATA).getValue().getValue();
461 builder.matchMetadata(metadata);
462 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700463 case ETH_DST:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800464 mac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
465 builder.matchEthDst(mac);
466 break;
467 case ETH_SRC:
468 mac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
469 builder.matchEthSrc(mac);
alshabib6b5cfec2014-09-18 17:42:18 -0700470 break;
471 case ETH_TYPE:
472 int ethType = match.get(MatchField.ETH_TYPE).getValue();
alshabib010c31d2014-09-26 10:01:12 -0700473 builder.matchEthType((short) ethType);
alshabib6b5cfec2014-09-18 17:42:18 -0700474 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700475 case VLAN_VID:
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800476 VlanId vlanId = null;
477 if (match.isPartiallyMasked(MatchField.VLAN_VID)) {
478 Masked<OFVlanVidMatch> masked = match.getMasked(MatchField.VLAN_VID);
479 if (masked.getValue().equals(OFVlanVidMatch.PRESENT)
480 && masked.getMask().equals(OFVlanVidMatch.PRESENT)) {
481 vlanId = VlanId.ANY;
482 }
483 } else {
484 vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
485 }
486 if (vlanId != null) {
487 builder.matchVlanId(vlanId);
488 }
alshabib6b5cfec2014-09-18 17:42:18 -0700489 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800490 case VLAN_PCP:
491 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
492 builder.matchVlanPcp(vlanPcp);
493 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800494 case IP_DSCP:
495 byte ipDscp = match.get(MatchField.IP_DSCP).getDscpValue();
496 builder.matchIPDscp(ipDscp);
497 break;
498 case IP_ECN:
499 byte ipEcn = match.get(MatchField.IP_ECN).getEcnValue();
500 builder.matchIPEcn(ipEcn);
501 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800502 case IP_PROTO:
503 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
504 builder.matchIPProtocol((byte) proto);
505 break;
506 case IPV4_SRC:
507 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
508 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800509 ip4Prefix = Ip4Prefix.valueOf(
510 maskedIp.getValue().getInt(),
511 maskedIp.getMask().asCidrMaskLength());
512 } else {
513 ip4Prefix = Ip4Prefix.valueOf(
514 match.get(MatchField.IPV4_SRC).getInt(),
515 Ip4Prefix.MAX_MASK_LENGTH);
516 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800517 builder.matchIPSrc(ip4Prefix);
518 break;
519 case IPV4_DST:
520 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
521 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800522 ip4Prefix = Ip4Prefix.valueOf(
523 maskedIp.getValue().getInt(),
524 maskedIp.getMask().asCidrMaskLength());
525 } else {
526 ip4Prefix = Ip4Prefix.valueOf(
527 match.get(MatchField.IPV4_DST).getInt(),
528 Ip4Prefix.MAX_MASK_LENGTH);
529 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800530 builder.matchIPDst(ip4Prefix);
Jonathan Hart34bc6142014-10-17 11:00:43 -0700531 break;
532 case TCP_SRC:
533 builder.matchTcpSrc((short) match.get(MatchField.TCP_SRC).getPort());
534 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800535 case TCP_DST:
536 builder.matchTcpDst((short) match.get(MatchField.TCP_DST).getPort());
537 break;
538 case UDP_SRC:
539 builder.matchUdpSrc((short) match.get(MatchField.UDP_SRC).getPort());
540 break;
541 case UDP_DST:
542 builder.matchUdpDst((short) match.get(MatchField.UDP_DST).getPort());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100543 case MPLS_LABEL:
544 builder.matchMplsLabel(MplsLabel.mplsLabel((int) match.get(MatchField.MPLS_LABEL)
545 .getValue()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800546 break;
547 case SCTP_SRC:
548 builder.matchSctpSrc((short) match.get(MatchField.SCTP_SRC).getPort());
549 break;
550 case SCTP_DST:
551 builder.matchSctpDst((short) match.get(MatchField.SCTP_DST).getPort());
552 break;
553 case ICMPV4_TYPE:
554 byte icmpType = (byte) match.get(MatchField.ICMPV4_TYPE).getType();
555 builder.matchIcmpType(icmpType);
556 break;
557 case ICMPV4_CODE:
558 byte icmpCode = (byte) match.get(MatchField.ICMPV4_CODE).getCode();
559 builder.matchIcmpCode(icmpCode);
560 break;
561 case IPV6_SRC:
562 if (match.isPartiallyMasked(MatchField.IPV6_SRC)) {
563 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_SRC);
564 ip6Prefix = Ip6Prefix.valueOf(
565 maskedIp.getValue().getBytes(),
566 maskedIp.getMask().asCidrMaskLength());
567 } else {
568 ip6Prefix = Ip6Prefix.valueOf(
569 match.get(MatchField.IPV6_SRC).getBytes(),
570 Ip6Prefix.MAX_MASK_LENGTH);
571 }
572 builder.matchIPv6Src(ip6Prefix);
573 break;
574 case IPV6_DST:
575 if (match.isPartiallyMasked(MatchField.IPV6_DST)) {
576 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_DST);
577 ip6Prefix = Ip6Prefix.valueOf(
578 maskedIp.getValue().getBytes(),
579 maskedIp.getMask().asCidrMaskLength());
580 } else {
581 ip6Prefix = Ip6Prefix.valueOf(
582 match.get(MatchField.IPV6_DST).getBytes(),
583 Ip6Prefix.MAX_MASK_LENGTH);
584 }
585 builder.matchIPv6Dst(ip6Prefix);
586 break;
587 case IPV6_FLABEL:
588 int flowLabel =
589 match.get(MatchField.IPV6_FLABEL).getIPv6FlowLabelValue();
590 builder.matchIPv6FlowLabel(flowLabel);
591 break;
592 case ICMPV6_TYPE:
593 byte icmpv6type = (byte) match.get(MatchField.ICMPV6_TYPE).getValue();
594 builder.matchIcmpv6Type(icmpv6type);
595 break;
596 case ICMPV6_CODE:
597 byte icmpv6code = (byte) match.get(MatchField.ICMPV6_CODE).getValue();
598 builder.matchIcmpv6Code(icmpv6code);
599 break;
600 case IPV6_ND_TARGET:
601 ip6Address =
602 Ip6Address.valueOf(match.get(MatchField.IPV6_ND_TARGET).getBytes());
603 builder.matchIPv6NDTargetAddress(ip6Address);
604 break;
605 case IPV6_ND_SLL:
606 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_SLL).getLong());
607 builder.matchIPv6NDSourceLinkLayerAddress(mac);
608 break;
609 case IPV6_ND_TLL:
610 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_TLL).getLong());
611 builder.matchIPv6NDTargetLinkLayerAddress(mac);
612 break;
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800613 case IPV6_EXTHDR:
614 builder.matchIPv6ExthdrFlags((int) match.get(MatchField.IPV6_EXTHDR)
615 .getValue());
616 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700617 case OCH_SIGID:
618 builder.matchLambda(match.get(MatchField.OCH_SIGID).getChannelNumber());
619 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800620 case OCH_SIGTYPE:
621 builder.matchOpticalSignalType(match.get(MatchField
622 .OCH_SIGTYPE).getValue());
623 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700624 case ARP_OP:
625 case ARP_SHA:
626 case ARP_SPA:
627 case ARP_THA:
628 case ARP_TPA:
alshabib6b5cfec2014-09-18 17:42:18 -0700629 case MPLS_TC:
alshabib6b5cfec2014-09-18 17:42:18 -0700630 case TUNNEL_ID:
alshabib6b5cfec2014-09-18 17:42:18 -0700631 default:
632 log.warn("Match type {} not yet implemented.", field.id);
alshabib6b5cfec2014-09-18 17:42:18 -0700633 }
634 }
635 return builder.build();
636 }
alshabib6b5cfec2014-09-18 17:42:18 -0700637}