blob: ede50f4084615ae28b5ed096d6611925906d60dd [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:
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700175 return Lists.newArrayList(OFFactoryVer13.INSTANCE.instructions()
176 .applyActions(
177 entry.getActions()));
alshabib19fdc122014-10-03 11:38:19 -0700178 case OF_11:
179 case OF_12:
180 case OF_13:
alshabib346b5b32015-03-06 00:42:16 -0800181 return entry.getInstructions();
182 default:
183 log.warn("Unknown OF version {}", entry.getVersion());
184 }
185 return Lists.newLinkedList();
186 }
187
188 private List<OFInstruction> getInstructions(OFFlowStatsEntry entry) {
189 switch (entry.getVersion()) {
190 case OF_10:
191 return Lists.newArrayList(
192 OFFactoryVer13.INSTANCE.instructions().applyActions(entry.getActions()));
193 case OF_11:
194 case OF_12:
195 case OF_13:
196 return entry.getInstructions();
alshabib19fdc122014-10-03 11:38:19 -0700197 default:
198 log.warn("Unknown OF version {}", entry.getVersion());
199 }
200 return Lists.newLinkedList();
201 }
alshabib6b5cfec2014-09-18 17:42:18 -0700202
203 private TrafficTreatment buildTreatment() {
tom9a693fd2014-10-03 11:32:19 -0700204 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700205 // If this is a drop rule
alshabib346b5b32015-03-06 00:42:16 -0800206 if (instructions.size() == 0) {
alshabib010c31d2014-09-26 10:01:12 -0700207 builder.drop();
alshabib6b5cfec2014-09-18 17:42:18 -0700208 return builder.build();
209 }
alshabib346b5b32015-03-06 00:42:16 -0800210 for (OFInstruction in : instructions) {
211 switch (in.getType()) {
212 case GOTO_TABLE:
213 builder.transition(tableType);
214 break;
215 case WRITE_METADATA:
216 break;
217 case WRITE_ACTIONS:
218 builder.deferred();
219 buildActions(((OFInstructionWriteActions) in).getActions(),
220 builder);
221 break;
222 case APPLY_ACTIONS:
223 builder.immediate();
224 buildActions(((OFInstructionApplyActions) in).getActions(),
225 builder);
226 break;
227 case CLEAR_ACTIONS:
228 builder.wipeDeferred();
229 break;
230 case EXPERIMENTER:
231 break;
232 case METER:
233 break;
234 default:
235 log.warn("Unknown instructions type {}", in.getType());
alshabib6b5cfec2014-09-18 17:42:18 -0700236 }
237 }
238
239 return builder.build();
240 }
241
alshabib346b5b32015-03-06 00:42:16 -0800242 private TrafficTreatment.Builder buildActions(List<OFAction> actions,
243 TrafficTreatment.Builder builder) {
244 for (OFAction act : actions) {
245 switch (act.getType()) {
246 case OUTPUT:
247 OFActionOutput out = (OFActionOutput) act;
248 builder.setOutput(
249 PortNumber.portNumber(out.getPort().getPortNumber()));
250 break;
251 case SET_VLAN_VID:
252 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
253 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
254 break;
255 case SET_VLAN_PCP:
256 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
257 builder.setVlanPcp(pcp.getVlanPcp().getValue());
258 break;
259 case SET_DL_DST:
260 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
261 builder.setEthDst(
262 MacAddress.valueOf(dldst.getDlAddr().getLong()));
263 break;
264 case SET_DL_SRC:
265 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
266 builder.setEthSrc(
267 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
268
269 break;
270 case SET_NW_DST:
271 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
272 IPv4Address di = nwdst.getNwAddr();
273 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
274 break;
275 case SET_NW_SRC:
276 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
277 IPv4Address si = nwsrc.getNwAddr();
278 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
279 break;
280 case EXPERIMENTER:
281 OFActionExperimenter exp = (OFActionExperimenter) act;
282 if (exp.getExperimenter() == 0x80005A06 ||
283 exp.getExperimenter() == 0x748771) {
284 OFActionCircuit ct = (OFActionCircuit) exp;
285 builder.setLambda(((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber());
286 } else {
287 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
288 }
289 break;
290 case SET_FIELD:
291 OFActionSetField setField = (OFActionSetField) act;
292 handleSetField(builder, setField.getField());
293 break;
294 case POP_MPLS:
295 OFActionPopMpls popMpls = (OFActionPopMpls) act;
296 builder.popMpls((short) popMpls.getEthertype().getValue());
297 break;
298 case PUSH_MPLS:
299 builder.pushMpls();
300 break;
301 case COPY_TTL_IN:
302 builder.copyTtlIn();
303 break;
304 case COPY_TTL_OUT:
305 builder.copyTtlOut();
306 break;
307 case DEC_MPLS_TTL:
308 builder.decMplsTtl();
309 break;
310 case DEC_NW_TTL:
311 builder.decNwTtl();
312 break;
313 case GROUP:
314 OFActionGroup group = (OFActionGroup) act;
315 builder.group(new DefaultGroupId(group.getGroup().getGroupNumber()));
316 break;
317 case POP_VLAN:
318 builder.popVlan();
319 break;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700320 case PUSH_VLAN:
321 builder.pushVlan();
322 break;
alshabib346b5b32015-03-06 00:42:16 -0800323 case STRIP_VLAN:
324 builder.stripVlan();
325 break;
326 case SET_TP_DST:
327 case SET_TP_SRC:
328 case POP_PBB:
329 case PUSH_PBB:
alshabib346b5b32015-03-06 00:42:16 -0800330 case SET_MPLS_LABEL:
331 case SET_MPLS_TC:
332 case SET_MPLS_TTL:
333 case SET_NW_ECN:
334 case SET_NW_TOS:
335 case SET_NW_TTL:
336 case SET_QUEUE:
337
338 case ENQUEUE:
339 default:
340 log.warn("Action type {} not yet implemented.", act.getType());
341 }
342 }
343 return builder;
344 }
345
346
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800347 private void handleSetField(TrafficTreatment.Builder builder, OFOxm<?> oxm) {
348 switch (oxm.getMatchField().id) {
349 case VLAN_PCP:
350 @SuppressWarnings("unchecked")
351 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
352 builder.setVlanPcp(vlanpcp.getValue().getValue());
353 break;
354 case VLAN_VID:
355 @SuppressWarnings("unchecked")
356 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
357 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
358 break;
359 case ETH_DST:
360 @SuppressWarnings("unchecked")
361 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
362 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
363 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
364 break;
365 case ETH_SRC:
366 @SuppressWarnings("unchecked")
367 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
368 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
369 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
370 break;
371 case IPV4_DST:
372 @SuppressWarnings("unchecked")
373 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
374 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
375 break;
376 case IPV4_SRC:
377 @SuppressWarnings("unchecked")
378 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
379 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
380 break;
sangho3f97a17d2015-01-29 22:56:29 -0800381 case MPLS_LABEL:
382 @SuppressWarnings("unchecked")
383 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100384 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
sangho3f97a17d2015-01-29 22:56:29 -0800385 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800386 case ARP_OP:
387 case ARP_SHA:
388 case ARP_SPA:
389 case ARP_THA:
390 case ARP_TPA:
391 case BSN_EGR_PORT_GROUP_ID:
392 case BSN_GLOBAL_VRF_ALLOWED:
393 case BSN_IN_PORTS_128:
394 case BSN_L3_DST_CLASS_ID:
395 case BSN_L3_INTERFACE_CLASS_ID:
396 case BSN_L3_SRC_CLASS_ID:
397 case BSN_LAG_ID:
398 case BSN_TCP_FLAGS:
399 case BSN_UDF0:
400 case BSN_UDF1:
401 case BSN_UDF2:
402 case BSN_UDF3:
403 case BSN_UDF4:
404 case BSN_UDF5:
405 case BSN_UDF6:
406 case BSN_UDF7:
407 case BSN_VLAN_XLATE_PORT_GROUP_ID:
408 case BSN_VRF:
409 case ETH_TYPE:
410 case ICMPV4_CODE:
411 case ICMPV4_TYPE:
412 case ICMPV6_CODE:
413 case ICMPV6_TYPE:
414 case IN_PHY_PORT:
415 case IN_PORT:
416 case IPV6_DST:
417 case IPV6_FLABEL:
418 case IPV6_ND_SLL:
419 case IPV6_ND_TARGET:
420 case IPV6_ND_TLL:
421 case IPV6_SRC:
422 case IP_DSCP:
423 case IP_ECN:
424 case IP_PROTO:
425 case METADATA:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800426 case MPLS_TC:
427 case OCH_SIGID:
428 case OCH_SIGID_BASIC:
429 case OCH_SIGTYPE:
430 case OCH_SIGTYPE_BASIC:
431 case SCTP_DST:
432 case SCTP_SRC:
433 case TCP_DST:
434 case TCP_SRC:
435 case TUNNEL_ID:
436 case UDP_DST:
437 case UDP_SRC:
438 default:
439 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
440 break;
441 }
442 }
443
alshabib6b5cfec2014-09-18 17:42:18 -0700444 private TrafficSelector buildSelector() {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800445 MacAddress mac;
446 Ip4Prefix ip4Prefix;
447 Ip6Address ip6Address;
448 Ip6Prefix ip6Prefix;
449
tom9a693fd2014-10-03 11:32:19 -0700450 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700451 for (MatchField<?> field : match.getMatchFields()) {
452 switch (field.id) {
453 case IN_PORT:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800454 builder.matchInPort(PortNumber
alshabib010c31d2014-09-26 10:01:12 -0700455 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700456 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800457 case IN_PHY_PORT:
458 builder.matchInPhyPort(PortNumber
459 .portNumber(match.get(MatchField.IN_PHY_PORT).getPortNumber()));
460 break;
461 case METADATA:
462 long metadata =
463 match.get(MatchField.METADATA).getValue().getValue();
464 builder.matchMetadata(metadata);
465 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700466 case ETH_DST:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800467 mac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
468 builder.matchEthDst(mac);
469 break;
470 case ETH_SRC:
471 mac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
472 builder.matchEthSrc(mac);
alshabib6b5cfec2014-09-18 17:42:18 -0700473 break;
474 case ETH_TYPE:
475 int ethType = match.get(MatchField.ETH_TYPE).getValue();
alshabib010c31d2014-09-26 10:01:12 -0700476 builder.matchEthType((short) ethType);
alshabib6b5cfec2014-09-18 17:42:18 -0700477 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700478 case VLAN_VID:
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800479 VlanId vlanId = null;
480 if (match.isPartiallyMasked(MatchField.VLAN_VID)) {
481 Masked<OFVlanVidMatch> masked = match.getMasked(MatchField.VLAN_VID);
482 if (masked.getValue().equals(OFVlanVidMatch.PRESENT)
483 && masked.getMask().equals(OFVlanVidMatch.PRESENT)) {
484 vlanId = VlanId.ANY;
485 }
486 } else {
487 vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
488 }
489 if (vlanId != null) {
490 builder.matchVlanId(vlanId);
491 }
alshabib6b5cfec2014-09-18 17:42:18 -0700492 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800493 case VLAN_PCP:
494 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
495 builder.matchVlanPcp(vlanPcp);
496 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800497 case IP_DSCP:
498 byte ipDscp = match.get(MatchField.IP_DSCP).getDscpValue();
499 builder.matchIPDscp(ipDscp);
500 break;
501 case IP_ECN:
502 byte ipEcn = match.get(MatchField.IP_ECN).getEcnValue();
503 builder.matchIPEcn(ipEcn);
504 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800505 case IP_PROTO:
506 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
507 builder.matchIPProtocol((byte) proto);
508 break;
509 case IPV4_SRC:
510 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
511 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800512 ip4Prefix = Ip4Prefix.valueOf(
513 maskedIp.getValue().getInt(),
514 maskedIp.getMask().asCidrMaskLength());
515 } else {
516 ip4Prefix = Ip4Prefix.valueOf(
517 match.get(MatchField.IPV4_SRC).getInt(),
518 Ip4Prefix.MAX_MASK_LENGTH);
519 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800520 builder.matchIPSrc(ip4Prefix);
521 break;
522 case IPV4_DST:
523 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
524 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800525 ip4Prefix = Ip4Prefix.valueOf(
526 maskedIp.getValue().getInt(),
527 maskedIp.getMask().asCidrMaskLength());
528 } else {
529 ip4Prefix = Ip4Prefix.valueOf(
530 match.get(MatchField.IPV4_DST).getInt(),
531 Ip4Prefix.MAX_MASK_LENGTH);
532 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800533 builder.matchIPDst(ip4Prefix);
Jonathan Hart34bc6142014-10-17 11:00:43 -0700534 break;
535 case TCP_SRC:
536 builder.matchTcpSrc((short) match.get(MatchField.TCP_SRC).getPort());
537 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800538 case TCP_DST:
539 builder.matchTcpDst((short) match.get(MatchField.TCP_DST).getPort());
540 break;
541 case UDP_SRC:
542 builder.matchUdpSrc((short) match.get(MatchField.UDP_SRC).getPort());
543 break;
544 case UDP_DST:
545 builder.matchUdpDst((short) match.get(MatchField.UDP_DST).getPort());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100546 case MPLS_LABEL:
547 builder.matchMplsLabel(MplsLabel.mplsLabel((int) match.get(MatchField.MPLS_LABEL)
548 .getValue()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800549 break;
550 case SCTP_SRC:
551 builder.matchSctpSrc((short) match.get(MatchField.SCTP_SRC).getPort());
552 break;
553 case SCTP_DST:
554 builder.matchSctpDst((short) match.get(MatchField.SCTP_DST).getPort());
555 break;
556 case ICMPV4_TYPE:
557 byte icmpType = (byte) match.get(MatchField.ICMPV4_TYPE).getType();
558 builder.matchIcmpType(icmpType);
559 break;
560 case ICMPV4_CODE:
561 byte icmpCode = (byte) match.get(MatchField.ICMPV4_CODE).getCode();
562 builder.matchIcmpCode(icmpCode);
563 break;
564 case IPV6_SRC:
565 if (match.isPartiallyMasked(MatchField.IPV6_SRC)) {
566 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_SRC);
567 ip6Prefix = Ip6Prefix.valueOf(
568 maskedIp.getValue().getBytes(),
569 maskedIp.getMask().asCidrMaskLength());
570 } else {
571 ip6Prefix = Ip6Prefix.valueOf(
572 match.get(MatchField.IPV6_SRC).getBytes(),
573 Ip6Prefix.MAX_MASK_LENGTH);
574 }
575 builder.matchIPv6Src(ip6Prefix);
576 break;
577 case IPV6_DST:
578 if (match.isPartiallyMasked(MatchField.IPV6_DST)) {
579 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_DST);
580 ip6Prefix = Ip6Prefix.valueOf(
581 maskedIp.getValue().getBytes(),
582 maskedIp.getMask().asCidrMaskLength());
583 } else {
584 ip6Prefix = Ip6Prefix.valueOf(
585 match.get(MatchField.IPV6_DST).getBytes(),
586 Ip6Prefix.MAX_MASK_LENGTH);
587 }
588 builder.matchIPv6Dst(ip6Prefix);
589 break;
590 case IPV6_FLABEL:
591 int flowLabel =
592 match.get(MatchField.IPV6_FLABEL).getIPv6FlowLabelValue();
593 builder.matchIPv6FlowLabel(flowLabel);
594 break;
595 case ICMPV6_TYPE:
596 byte icmpv6type = (byte) match.get(MatchField.ICMPV6_TYPE).getValue();
597 builder.matchIcmpv6Type(icmpv6type);
598 break;
599 case ICMPV6_CODE:
600 byte icmpv6code = (byte) match.get(MatchField.ICMPV6_CODE).getValue();
601 builder.matchIcmpv6Code(icmpv6code);
602 break;
603 case IPV6_ND_TARGET:
604 ip6Address =
605 Ip6Address.valueOf(match.get(MatchField.IPV6_ND_TARGET).getBytes());
606 builder.matchIPv6NDTargetAddress(ip6Address);
607 break;
608 case IPV6_ND_SLL:
609 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_SLL).getLong());
610 builder.matchIPv6NDSourceLinkLayerAddress(mac);
611 break;
612 case IPV6_ND_TLL:
613 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_TLL).getLong());
614 builder.matchIPv6NDTargetLinkLayerAddress(mac);
615 break;
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800616 case IPV6_EXTHDR:
617 builder.matchIPv6ExthdrFlags((int) match.get(MatchField.IPV6_EXTHDR)
618 .getValue());
619 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700620 case OCH_SIGID:
621 builder.matchLambda(match.get(MatchField.OCH_SIGID).getChannelNumber());
622 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800623 case OCH_SIGTYPE:
624 builder.matchOpticalSignalType(match.get(MatchField
625 .OCH_SIGTYPE).getValue());
626 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700627 case ARP_OP:
628 case ARP_SHA:
629 case ARP_SPA:
630 case ARP_THA:
631 case ARP_TPA:
alshabib6b5cfec2014-09-18 17:42:18 -0700632 case MPLS_TC:
alshabib6b5cfec2014-09-18 17:42:18 -0700633 case TUNNEL_ID:
alshabib6b5cfec2014-09-18 17:42:18 -0700634 default:
635 log.warn("Match type {} not yet implemented.", field.id);
alshabib6b5cfec2014-09-18 17:42:18 -0700636 }
637 }
638 return builder.build();
639 }
alshabib6b5cfec2014-09-18 17:42:18 -0700640}