blob: 05035de7d8fe66f1d92420ad101bcdca8d516490 [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;
sanghoc0b3c342015-03-12 17:12:10 -070064import org.projectfloodlight.openflow.types.EthType;
alshabib6b5cfec2014-09-18 17:42:18 -070065import org.projectfloodlight.openflow.types.IPv4Address;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080066import org.projectfloodlight.openflow.types.IPv6Address;
Jonathan Hart0e12fad2014-10-17 14:54:58 -070067import org.projectfloodlight.openflow.types.Masked;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080068import org.projectfloodlight.openflow.types.OFVlanVidMatch;
sangho3f97a17d2015-01-29 22:56:29 -080069import org.projectfloodlight.openflow.types.U32;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080070import org.projectfloodlight.openflow.types.VlanPcp;
alshabib6b5cfec2014-09-18 17:42:18 -070071import org.slf4j.Logger;
72
Jonathan Hart6cd2f352015-01-13 17:44:45 -080073import java.util.List;
74
75import static org.slf4j.LoggerFactory.getLogger;
alshabib19fdc122014-10-03 11:38:19 -070076
alshabib1c319ff2014-10-04 20:29:09 -070077public class FlowEntryBuilder {
alshabib6b5cfec2014-09-18 17:42:18 -070078 private final Logger log = getLogger(getClass());
79
80 private final OFFlowStatsEntry stat;
81 private final OFFlowRemoved removed;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080082 private final OFFlowMod flowMod;
alshabib6b5cfec2014-09-18 17:42:18 -070083
84 private final Match match;
alshabib346b5b32015-03-06 00:42:16 -080085
Jonathan Hart67fc0972015-03-19 15:21:20 -070086 // All actions are contained in an OFInstruction. For OF1.0
87 // the instruction type is apply instruction (immediate set in ONOS speak)
alshabib346b5b32015-03-06 00:42:16 -080088 private final List<OFInstruction> instructions;
alshabib6b5cfec2014-09-18 17:42:18 -070089
90 private final Dpid dpid;
91
Brian O'Connor72cb19a2015-01-16 16:14:41 -080092 public enum FlowType { STAT, REMOVED, MOD }
93
94 private final FlowType type;
Saurav Dasfa2fa932015-03-03 11:29:48 -080095 private Type tableType = FlowRule.Type.DEFAULT;
alshabib6b5cfec2014-09-18 17:42:18 -070096
Saurav Dasfa2fa932015-03-03 11:29:48 -080097 public FlowEntryBuilder(Dpid dpid, OFFlowStatsEntry entry, Type tableType) {
98 this.stat = entry;
99 this.match = entry.getMatch();
alshabib346b5b32015-03-06 00:42:16 -0800100 this.instructions = getInstructions(entry);
Saurav Dasfa2fa932015-03-03 11:29:48 -0800101 this.dpid = dpid;
102 this.removed = null;
103 this.flowMod = null;
104 this.type = FlowType.STAT;
105 this.tableType = tableType;
106 }
107
alshabibda1644e2015-03-13 14:01:35 -0700108 public FlowEntryBuilder(Dpid dpid, OFFlowRemoved removed, Type tableType) {
alshabib6b5cfec2014-09-18 17:42:18 -0700109 this.match = removed.getMatch();
110 this.removed = removed;
111
112 this.dpid = dpid;
alshabib346b5b32015-03-06 00:42:16 -0800113 this.instructions = null;
alshabib6b5cfec2014-09-18 17:42:18 -0700114 this.stat = null;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800115 this.flowMod = null;
116 this.type = FlowType.REMOVED;
alshabibda1644e2015-03-13 14:01:35 -0700117 this.tableType = tableType;
alshabib6b5cfec2014-09-18 17:42:18 -0700118
119 }
120
alshabibda1644e2015-03-13 14:01:35 -0700121 public FlowEntryBuilder(Dpid dpid, OFFlowMod fm, Type tableType) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800122 this.match = fm.getMatch();
123 this.dpid = dpid;
alshabib346b5b32015-03-06 00:42:16 -0800124 this.instructions = getInstructions(fm);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800125 this.type = FlowType.MOD;
126 this.flowMod = fm;
127 this.stat = null;
128 this.removed = null;
alshabibda1644e2015-03-13 14:01:35 -0700129 this.tableType = tableType;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800130 }
alshabib1c319ff2014-10-04 20:29:09 -0700131
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800132 public FlowEntry build(FlowEntryState... state) {
133 FlowRule rule;
134 switch (this.type) {
135 case STAT:
136 rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
137 buildSelector(), buildTreatment(), stat.getPriority(),
Saurav Dasfa2fa932015-03-03 11:29:48 -0800138 stat.getCookie().getValue(), stat.getIdleTimeout(), false,
139 tableType);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800140 return new DefaultFlowEntry(rule, FlowEntryState.ADDED,
141 stat.getDurationSec(), stat.getPacketCount().getValue(),
142 stat.getByteCount().getValue());
143 case REMOVED:
144 rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
145 buildSelector(), null, removed.getPriority(),
alshabibda1644e2015-03-13 14:01:35 -0700146 removed.getCookie().getValue(), removed.getIdleTimeout(), false,
147 tableType);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800148 return new DefaultFlowEntry(rule, FlowEntryState.REMOVED, removed.getDurationSec(),
149 removed.getPacketCount().getValue(), removed.getByteCount().getValue());
150 case MOD:
151 FlowEntryState flowState = state.length > 0 ? state[0] : FlowEntryState.FAILED;
152 rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
153 buildSelector(), buildTreatment(), flowMod.getPriority(),
alshabibda1644e2015-03-13 14:01:35 -0700154 flowMod.getCookie().getValue(), flowMod.getIdleTimeout(), false,
155 tableType);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800156 return new DefaultFlowEntry(rule, flowState, 0, 0, 0);
157 default:
158 log.error("Unknown flow type : {}", this.type);
159 return null;
alshabib6b5cfec2014-09-18 17:42:18 -0700160 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800161
alshabib6b5cfec2014-09-18 17:42:18 -0700162 }
163
alshabib346b5b32015-03-06 00:42:16 -0800164 private List<OFInstruction> getInstructions(OFFlowMod entry) {
alshabib19fdc122014-10-03 11:38:19 -0700165 switch (entry.getVersion()) {
166 case OF_10:
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700167 return Lists.newArrayList(OFFactoryVer13.INSTANCE.instructions()
168 .applyActions(
169 entry.getActions()));
alshabib19fdc122014-10-03 11:38:19 -0700170 case OF_11:
171 case OF_12:
172 case OF_13:
alshabib346b5b32015-03-06 00:42:16 -0800173 return entry.getInstructions();
174 default:
175 log.warn("Unknown OF version {}", entry.getVersion());
176 }
177 return Lists.newLinkedList();
178 }
179
180 private List<OFInstruction> getInstructions(OFFlowStatsEntry entry) {
181 switch (entry.getVersion()) {
182 case OF_10:
183 return Lists.newArrayList(
184 OFFactoryVer13.INSTANCE.instructions().applyActions(entry.getActions()));
185 case OF_11:
186 case OF_12:
187 case OF_13:
188 return entry.getInstructions();
alshabib19fdc122014-10-03 11:38:19 -0700189 default:
190 log.warn("Unknown OF version {}", entry.getVersion());
191 }
192 return Lists.newLinkedList();
193 }
alshabib6b5cfec2014-09-18 17:42:18 -0700194
195 private TrafficTreatment buildTreatment() {
tom9a693fd2014-10-03 11:32:19 -0700196 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700197 // If this is a drop rule
alshabib346b5b32015-03-06 00:42:16 -0800198 if (instructions.size() == 0) {
alshabib010c31d2014-09-26 10:01:12 -0700199 builder.drop();
alshabib6b5cfec2014-09-18 17:42:18 -0700200 return builder.build();
201 }
alshabib346b5b32015-03-06 00:42:16 -0800202 for (OFInstruction in : instructions) {
203 switch (in.getType()) {
204 case GOTO_TABLE:
205 builder.transition(tableType);
206 break;
207 case WRITE_METADATA:
208 break;
209 case WRITE_ACTIONS:
210 builder.deferred();
211 buildActions(((OFInstructionWriteActions) in).getActions(),
212 builder);
213 break;
214 case APPLY_ACTIONS:
215 builder.immediate();
216 buildActions(((OFInstructionApplyActions) in).getActions(),
217 builder);
218 break;
219 case CLEAR_ACTIONS:
220 builder.wipeDeferred();
221 break;
222 case EXPERIMENTER:
223 break;
224 case METER:
225 break;
226 default:
227 log.warn("Unknown instructions type {}", in.getType());
alshabib6b5cfec2014-09-18 17:42:18 -0700228 }
229 }
230
231 return builder.build();
232 }
233
alshabib346b5b32015-03-06 00:42:16 -0800234 private TrafficTreatment.Builder buildActions(List<OFAction> actions,
235 TrafficTreatment.Builder builder) {
236 for (OFAction act : actions) {
237 switch (act.getType()) {
238 case OUTPUT:
239 OFActionOutput out = (OFActionOutput) act;
240 builder.setOutput(
241 PortNumber.portNumber(out.getPort().getPortNumber()));
242 break;
243 case SET_VLAN_VID:
244 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
245 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
246 break;
247 case SET_VLAN_PCP:
248 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
249 builder.setVlanPcp(pcp.getVlanPcp().getValue());
250 break;
251 case SET_DL_DST:
252 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
253 builder.setEthDst(
254 MacAddress.valueOf(dldst.getDlAddr().getLong()));
255 break;
256 case SET_DL_SRC:
257 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
258 builder.setEthSrc(
259 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
260
261 break;
262 case SET_NW_DST:
263 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
264 IPv4Address di = nwdst.getNwAddr();
265 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
266 break;
267 case SET_NW_SRC:
268 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
269 IPv4Address si = nwsrc.getNwAddr();
270 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
271 break;
272 case EXPERIMENTER:
273 OFActionExperimenter exp = (OFActionExperimenter) act;
274 if (exp.getExperimenter() == 0x80005A06 ||
275 exp.getExperimenter() == 0x748771) {
276 OFActionCircuit ct = (OFActionCircuit) exp;
277 builder.setLambda(((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber());
278 } else {
279 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
280 }
281 break;
282 case SET_FIELD:
283 OFActionSetField setField = (OFActionSetField) act;
284 handleSetField(builder, setField.getField());
285 break;
286 case POP_MPLS:
287 OFActionPopMpls popMpls = (OFActionPopMpls) act;
288 builder.popMpls((short) popMpls.getEthertype().getValue());
289 break;
290 case PUSH_MPLS:
291 builder.pushMpls();
292 break;
293 case COPY_TTL_IN:
294 builder.copyTtlIn();
295 break;
296 case COPY_TTL_OUT:
297 builder.copyTtlOut();
298 break;
299 case DEC_MPLS_TTL:
300 builder.decMplsTtl();
301 break;
302 case DEC_NW_TTL:
303 builder.decNwTtl();
304 break;
305 case GROUP:
306 OFActionGroup group = (OFActionGroup) act;
307 builder.group(new DefaultGroupId(group.getGroup().getGroupNumber()));
308 break;
Jonathan Hart67fc0972015-03-19 15:21:20 -0700309 case STRIP_VLAN:
alshabib346b5b32015-03-06 00:42:16 -0800310 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 SET_TP_DST:
317 case SET_TP_SRC:
318 case POP_PBB:
319 case PUSH_PBB:
alshabib346b5b32015-03-06 00:42:16 -0800320 case SET_MPLS_LABEL:
321 case SET_MPLS_TC:
322 case SET_MPLS_TTL:
323 case SET_NW_ECN:
324 case SET_NW_TOS:
325 case SET_NW_TTL:
326 case SET_QUEUE:
327
328 case ENQUEUE:
329 default:
330 log.warn("Action type {} not yet implemented.", act.getType());
331 }
332 }
333 return builder;
334 }
335
336
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800337 private void handleSetField(TrafficTreatment.Builder builder, OFOxm<?> oxm) {
338 switch (oxm.getMatchField().id) {
339 case VLAN_PCP:
340 @SuppressWarnings("unchecked")
341 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
342 builder.setVlanPcp(vlanpcp.getValue().getValue());
343 break;
344 case VLAN_VID:
345 @SuppressWarnings("unchecked")
346 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
347 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
348 break;
349 case ETH_DST:
350 @SuppressWarnings("unchecked")
351 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
352 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
353 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
354 break;
355 case ETH_SRC:
356 @SuppressWarnings("unchecked")
357 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
358 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
359 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
360 break;
361 case IPV4_DST:
362 @SuppressWarnings("unchecked")
363 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
364 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
365 break;
366 case IPV4_SRC:
367 @SuppressWarnings("unchecked")
368 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
369 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
370 break;
sangho3f97a17d2015-01-29 22:56:29 -0800371 case MPLS_LABEL:
372 @SuppressWarnings("unchecked")
373 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100374 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
sangho3f97a17d2015-01-29 22:56:29 -0800375 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800376 case ARP_OP:
377 case ARP_SHA:
378 case ARP_SPA:
379 case ARP_THA:
380 case ARP_TPA:
381 case BSN_EGR_PORT_GROUP_ID:
382 case BSN_GLOBAL_VRF_ALLOWED:
383 case BSN_IN_PORTS_128:
384 case BSN_L3_DST_CLASS_ID:
385 case BSN_L3_INTERFACE_CLASS_ID:
386 case BSN_L3_SRC_CLASS_ID:
387 case BSN_LAG_ID:
388 case BSN_TCP_FLAGS:
389 case BSN_UDF0:
390 case BSN_UDF1:
391 case BSN_UDF2:
392 case BSN_UDF3:
393 case BSN_UDF4:
394 case BSN_UDF5:
395 case BSN_UDF6:
396 case BSN_UDF7:
397 case BSN_VLAN_XLATE_PORT_GROUP_ID:
398 case BSN_VRF:
399 case ETH_TYPE:
400 case ICMPV4_CODE:
401 case ICMPV4_TYPE:
402 case ICMPV6_CODE:
403 case ICMPV6_TYPE:
404 case IN_PHY_PORT:
405 case IN_PORT:
406 case IPV6_DST:
407 case IPV6_FLABEL:
408 case IPV6_ND_SLL:
409 case IPV6_ND_TARGET:
410 case IPV6_ND_TLL:
411 case IPV6_SRC:
412 case IP_DSCP:
413 case IP_ECN:
414 case IP_PROTO:
415 case METADATA:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800416 case MPLS_TC:
417 case OCH_SIGID:
418 case OCH_SIGID_BASIC:
419 case OCH_SIGTYPE:
420 case OCH_SIGTYPE_BASIC:
421 case SCTP_DST:
422 case SCTP_SRC:
423 case TCP_DST:
424 case TCP_SRC:
425 case TUNNEL_ID:
426 case UDP_DST:
427 case UDP_SRC:
428 default:
429 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
430 break;
431 }
432 }
433
alshabib6b5cfec2014-09-18 17:42:18 -0700434 private TrafficSelector buildSelector() {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800435 MacAddress mac;
436 Ip4Prefix ip4Prefix;
437 Ip6Address ip6Address;
438 Ip6Prefix ip6Prefix;
439
tom9a693fd2014-10-03 11:32:19 -0700440 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700441 for (MatchField<?> field : match.getMatchFields()) {
442 switch (field.id) {
443 case IN_PORT:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800444 builder.matchInPort(PortNumber
alshabib010c31d2014-09-26 10:01:12 -0700445 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700446 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800447 case IN_PHY_PORT:
448 builder.matchInPhyPort(PortNumber
449 .portNumber(match.get(MatchField.IN_PHY_PORT).getPortNumber()));
450 break;
451 case METADATA:
452 long metadata =
453 match.get(MatchField.METADATA).getValue().getValue();
454 builder.matchMetadata(metadata);
455 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700456 case ETH_DST:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800457 mac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
458 builder.matchEthDst(mac);
459 break;
460 case ETH_SRC:
461 mac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
462 builder.matchEthSrc(mac);
alshabib6b5cfec2014-09-18 17:42:18 -0700463 break;
464 case ETH_TYPE:
465 int ethType = match.get(MatchField.ETH_TYPE).getValue();
sanghoc0b3c342015-03-12 17:12:10 -0700466 if (ethType == EthType.VLAN_FRAME.getValue()) {
467 builder.matchVlanId(VlanId.ANY);
468 } else {
469 builder.matchEthType((short) ethType);
470 }
alshabib6b5cfec2014-09-18 17:42:18 -0700471 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700472 case VLAN_VID:
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800473 VlanId vlanId = null;
474 if (match.isPartiallyMasked(MatchField.VLAN_VID)) {
475 Masked<OFVlanVidMatch> masked = match.getMasked(MatchField.VLAN_VID);
476 if (masked.getValue().equals(OFVlanVidMatch.PRESENT)
477 && masked.getMask().equals(OFVlanVidMatch.PRESENT)) {
478 vlanId = VlanId.ANY;
479 }
480 } else {
481 vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
482 }
483 if (vlanId != null) {
484 builder.matchVlanId(vlanId);
485 }
alshabib6b5cfec2014-09-18 17:42:18 -0700486 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800487 case VLAN_PCP:
488 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
489 builder.matchVlanPcp(vlanPcp);
490 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800491 case IP_DSCP:
492 byte ipDscp = match.get(MatchField.IP_DSCP).getDscpValue();
493 builder.matchIPDscp(ipDscp);
494 break;
495 case IP_ECN:
496 byte ipEcn = match.get(MatchField.IP_ECN).getEcnValue();
497 builder.matchIPEcn(ipEcn);
498 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800499 case IP_PROTO:
500 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
501 builder.matchIPProtocol((byte) proto);
502 break;
503 case IPV4_SRC:
504 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
505 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800506 ip4Prefix = Ip4Prefix.valueOf(
507 maskedIp.getValue().getInt(),
508 maskedIp.getMask().asCidrMaskLength());
509 } else {
510 ip4Prefix = Ip4Prefix.valueOf(
511 match.get(MatchField.IPV4_SRC).getInt(),
512 Ip4Prefix.MAX_MASK_LENGTH);
513 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800514 builder.matchIPSrc(ip4Prefix);
515 break;
516 case IPV4_DST:
517 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
518 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800519 ip4Prefix = Ip4Prefix.valueOf(
520 maskedIp.getValue().getInt(),
521 maskedIp.getMask().asCidrMaskLength());
522 } else {
523 ip4Prefix = Ip4Prefix.valueOf(
524 match.get(MatchField.IPV4_DST).getInt(),
525 Ip4Prefix.MAX_MASK_LENGTH);
526 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800527 builder.matchIPDst(ip4Prefix);
Jonathan Hart34bc6142014-10-17 11:00:43 -0700528 break;
529 case TCP_SRC:
530 builder.matchTcpSrc((short) match.get(MatchField.TCP_SRC).getPort());
531 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800532 case TCP_DST:
533 builder.matchTcpDst((short) match.get(MatchField.TCP_DST).getPort());
534 break;
535 case UDP_SRC:
536 builder.matchUdpSrc((short) match.get(MatchField.UDP_SRC).getPort());
537 break;
538 case UDP_DST:
539 builder.matchUdpDst((short) match.get(MatchField.UDP_DST).getPort());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100540 case MPLS_LABEL:
541 builder.matchMplsLabel(MplsLabel.mplsLabel((int) match.get(MatchField.MPLS_LABEL)
542 .getValue()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800543 break;
544 case SCTP_SRC:
545 builder.matchSctpSrc((short) match.get(MatchField.SCTP_SRC).getPort());
546 break;
547 case SCTP_DST:
548 builder.matchSctpDst((short) match.get(MatchField.SCTP_DST).getPort());
549 break;
550 case ICMPV4_TYPE:
551 byte icmpType = (byte) match.get(MatchField.ICMPV4_TYPE).getType();
552 builder.matchIcmpType(icmpType);
553 break;
554 case ICMPV4_CODE:
555 byte icmpCode = (byte) match.get(MatchField.ICMPV4_CODE).getCode();
556 builder.matchIcmpCode(icmpCode);
557 break;
558 case IPV6_SRC:
559 if (match.isPartiallyMasked(MatchField.IPV6_SRC)) {
560 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_SRC);
561 ip6Prefix = Ip6Prefix.valueOf(
562 maskedIp.getValue().getBytes(),
563 maskedIp.getMask().asCidrMaskLength());
564 } else {
565 ip6Prefix = Ip6Prefix.valueOf(
566 match.get(MatchField.IPV6_SRC).getBytes(),
567 Ip6Prefix.MAX_MASK_LENGTH);
568 }
569 builder.matchIPv6Src(ip6Prefix);
570 break;
571 case IPV6_DST:
572 if (match.isPartiallyMasked(MatchField.IPV6_DST)) {
573 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_DST);
574 ip6Prefix = Ip6Prefix.valueOf(
575 maskedIp.getValue().getBytes(),
576 maskedIp.getMask().asCidrMaskLength());
577 } else {
578 ip6Prefix = Ip6Prefix.valueOf(
579 match.get(MatchField.IPV6_DST).getBytes(),
580 Ip6Prefix.MAX_MASK_LENGTH);
581 }
582 builder.matchIPv6Dst(ip6Prefix);
583 break;
584 case IPV6_FLABEL:
585 int flowLabel =
586 match.get(MatchField.IPV6_FLABEL).getIPv6FlowLabelValue();
587 builder.matchIPv6FlowLabel(flowLabel);
588 break;
589 case ICMPV6_TYPE:
590 byte icmpv6type = (byte) match.get(MatchField.ICMPV6_TYPE).getValue();
591 builder.matchIcmpv6Type(icmpv6type);
592 break;
593 case ICMPV6_CODE:
594 byte icmpv6code = (byte) match.get(MatchField.ICMPV6_CODE).getValue();
595 builder.matchIcmpv6Code(icmpv6code);
596 break;
597 case IPV6_ND_TARGET:
598 ip6Address =
599 Ip6Address.valueOf(match.get(MatchField.IPV6_ND_TARGET).getBytes());
600 builder.matchIPv6NDTargetAddress(ip6Address);
601 break;
602 case IPV6_ND_SLL:
603 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_SLL).getLong());
604 builder.matchIPv6NDSourceLinkLayerAddress(mac);
605 break;
606 case IPV6_ND_TLL:
607 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_TLL).getLong());
608 builder.matchIPv6NDTargetLinkLayerAddress(mac);
609 break;
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800610 case IPV6_EXTHDR:
611 builder.matchIPv6ExthdrFlags((int) match.get(MatchField.IPV6_EXTHDR)
612 .getValue());
613 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700614 case OCH_SIGID:
615 builder.matchLambda(match.get(MatchField.OCH_SIGID).getChannelNumber());
616 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800617 case OCH_SIGTYPE:
618 builder.matchOpticalSignalType(match.get(MatchField
619 .OCH_SIGTYPE).getValue());
620 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700621 case ARP_OP:
622 case ARP_SHA:
623 case ARP_SPA:
624 case ARP_THA:
625 case ARP_TPA:
alshabib6b5cfec2014-09-18 17:42:18 -0700626 case MPLS_TC:
alshabib6b5cfec2014-09-18 17:42:18 -0700627 case TUNNEL_ID:
alshabib6b5cfec2014-09-18 17:42:18 -0700628 default:
629 log.warn("Match type {} not yet implemented.", field.id);
alshabib6b5cfec2014-09-18 17:42:18 -0700630 }
631 }
632 return builder.build();
633 }
alshabib6b5cfec2014-09-18 17:42:18 -0700634}