blob: c1253a26c09af296a4ec14b10fd5fd604e8a85d2 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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;
36import org.onosproject.net.flow.TrafficSelector;
37import org.onosproject.net.flow.TrafficTreatment;
38import org.onosproject.openflow.controller.Dpid;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080039import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabib6b5cfec2014-09-18 17:42:18 -070040import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
41import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
42import org.projectfloodlight.openflow.protocol.action.OFAction;
Marc De Leenheer49087752014-10-23 13:54:09 -070043import org.projectfloodlight.openflow.protocol.action.OFActionCircuit;
44import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter;
sangho8995ac52015-02-04 11:29:03 -080045import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
alshabib6b5cfec2014-09-18 17:42:18 -070046import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
sangho3f97a17d2015-01-29 22:56:29 -080047import org.projectfloodlight.openflow.protocol.action.OFActionPopMpls;
alshabib6b5cfec2014-09-18 17:42:18 -070048import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst;
49import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080050import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
alshabib6b5cfec2014-09-18 17:42:18 -070051import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst;
52import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc;
53import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp;
54import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid;
alshabib19fdc122014-10-03 11:38:19 -070055import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hartcf783202014-11-24 18:55:42 -080056import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions;
alshabibbdcbb102015-04-22 14:16:38 -070057import org.projectfloodlight.openflow.protocol.instruction.OFInstructionGotoTable;
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;
alshabib6b5cfec2014-09-18 17:42:18 -070095
alshabibbdcbb102015-04-22 14:16:38 -070096 public FlowEntryBuilder(Dpid dpid, OFFlowStatsEntry entry) {
Saurav Dasfa2fa932015-03-03 11:29:48 -080097 this.stat = entry;
98 this.match = entry.getMatch();
alshabib346b5b32015-03-06 00:42:16 -080099 this.instructions = getInstructions(entry);
Saurav Dasfa2fa932015-03-03 11:29:48 -0800100 this.dpid = dpid;
101 this.removed = null;
102 this.flowMod = null;
103 this.type = FlowType.STAT;
Saurav Dasfa2fa932015-03-03 11:29:48 -0800104 }
105
alshabibbdcbb102015-04-22 14:16:38 -0700106 public FlowEntryBuilder(Dpid dpid, OFFlowRemoved removed) {
alshabib6b5cfec2014-09-18 17:42:18 -0700107 this.match = removed.getMatch();
108 this.removed = removed;
109
110 this.dpid = dpid;
alshabib346b5b32015-03-06 00:42:16 -0800111 this.instructions = null;
alshabib6b5cfec2014-09-18 17:42:18 -0700112 this.stat = null;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800113 this.flowMod = null;
114 this.type = FlowType.REMOVED;
alshabib6b5cfec2014-09-18 17:42:18 -0700115
116 }
117
alshabibbdcbb102015-04-22 14:16:38 -0700118 public FlowEntryBuilder(Dpid dpid, OFFlowMod fm) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800119 this.match = fm.getMatch();
120 this.dpid = dpid;
alshabib346b5b32015-03-06 00:42:16 -0800121 this.instructions = getInstructions(fm);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800122 this.type = FlowType.MOD;
123 this.flowMod = fm;
124 this.stat = null;
125 this.removed = null;
126 }
alshabib1c319ff2014-10-04 20:29:09 -0700127
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800128 public FlowEntry build(FlowEntryState... state) {
129 FlowRule rule;
130 switch (this.type) {
131 case STAT:
alshabibbdcbb102015-04-22 14:16:38 -0700132 rule = DefaultFlowRule.builder()
133 .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
134 .withSelector(buildSelector())
135 .withTreatment(buildTreatment())
136 .withPriority(stat.getPriority())
137 .makeTemporary(stat.getIdleTimeout())
138 .withCookie(stat.getCookie().getValue())
139 .forTable(stat.getTableId().getValue())
140 .build();
141
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800142 return new DefaultFlowEntry(rule, FlowEntryState.ADDED,
143 stat.getDurationSec(), stat.getPacketCount().getValue(),
144 stat.getByteCount().getValue());
145 case REMOVED:
alshabibbdcbb102015-04-22 14:16:38 -0700146 rule = DefaultFlowRule.builder()
147 .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
148 .withSelector(buildSelector())
149 .withPriority(removed.getPriority())
150 .makeTemporary(removed.getIdleTimeout())
151 .withCookie(removed.getCookie().getValue())
152 .forTable(removed.getTableId().getValue())
153 .build();
154
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800155 return new DefaultFlowEntry(rule, FlowEntryState.REMOVED, removed.getDurationSec(),
156 removed.getPacketCount().getValue(), removed.getByteCount().getValue());
157 case MOD:
158 FlowEntryState flowState = state.length > 0 ? state[0] : FlowEntryState.FAILED;
alshabibbdcbb102015-04-22 14:16:38 -0700159 rule = DefaultFlowRule.builder()
160 .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
161 .withSelector(buildSelector())
162 .withTreatment(buildTreatment())
163 .withPriority(flowMod.getPriority())
164 .makeTemporary(flowMod.getIdleTimeout())
165 .withCookie(flowMod.getCookie().getValue())
166 .forTable(flowMod.getTableId().getValue())
167 .build();
168
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800169 return new DefaultFlowEntry(rule, flowState, 0, 0, 0);
170 default:
171 log.error("Unknown flow type : {}", this.type);
172 return null;
alshabib6b5cfec2014-09-18 17:42:18 -0700173 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800174
alshabib6b5cfec2014-09-18 17:42:18 -0700175 }
176
alshabib346b5b32015-03-06 00:42:16 -0800177 private List<OFInstruction> getInstructions(OFFlowMod entry) {
alshabib19fdc122014-10-03 11:38:19 -0700178 switch (entry.getVersion()) {
179 case OF_10:
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700180 return Lists.newArrayList(OFFactoryVer13.INSTANCE.instructions()
181 .applyActions(
182 entry.getActions()));
alshabib19fdc122014-10-03 11:38:19 -0700183 case OF_11:
184 case OF_12:
185 case OF_13:
alshabib346b5b32015-03-06 00:42:16 -0800186 return entry.getInstructions();
187 default:
188 log.warn("Unknown OF version {}", entry.getVersion());
189 }
190 return Lists.newLinkedList();
191 }
192
193 private List<OFInstruction> getInstructions(OFFlowStatsEntry entry) {
194 switch (entry.getVersion()) {
195 case OF_10:
196 return Lists.newArrayList(
197 OFFactoryVer13.INSTANCE.instructions().applyActions(entry.getActions()));
198 case OF_11:
199 case OF_12:
200 case OF_13:
201 return entry.getInstructions();
alshabib19fdc122014-10-03 11:38:19 -0700202 default:
203 log.warn("Unknown OF version {}", entry.getVersion());
204 }
205 return Lists.newLinkedList();
206 }
alshabib6b5cfec2014-09-18 17:42:18 -0700207
208 private TrafficTreatment buildTreatment() {
tom9a693fd2014-10-03 11:32:19 -0700209 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700210 // If this is a drop rule
alshabib346b5b32015-03-06 00:42:16 -0800211 if (instructions.size() == 0) {
alshabib010c31d2014-09-26 10:01:12 -0700212 builder.drop();
alshabib6b5cfec2014-09-18 17:42:18 -0700213 return builder.build();
214 }
alshabib346b5b32015-03-06 00:42:16 -0800215 for (OFInstruction in : instructions) {
216 switch (in.getType()) {
217 case GOTO_TABLE:
alshabibbdcbb102015-04-22 14:16:38 -0700218 builder.transition(((int) ((OFInstructionGotoTable) in)
219 .getTableId().getValue()));
alshabib346b5b32015-03-06 00:42:16 -0800220 break;
221 case WRITE_METADATA:
222 break;
223 case WRITE_ACTIONS:
224 builder.deferred();
225 buildActions(((OFInstructionWriteActions) in).getActions(),
226 builder);
227 break;
228 case APPLY_ACTIONS:
229 builder.immediate();
230 buildActions(((OFInstructionApplyActions) in).getActions(),
231 builder);
232 break;
233 case CLEAR_ACTIONS:
234 builder.wipeDeferred();
235 break;
236 case EXPERIMENTER:
237 break;
238 case METER:
239 break;
240 default:
241 log.warn("Unknown instructions type {}", in.getType());
alshabib6b5cfec2014-09-18 17:42:18 -0700242 }
243 }
244
245 return builder.build();
246 }
247
alshabib346b5b32015-03-06 00:42:16 -0800248 private TrafficTreatment.Builder buildActions(List<OFAction> actions,
249 TrafficTreatment.Builder builder) {
250 for (OFAction act : actions) {
251 switch (act.getType()) {
252 case OUTPUT:
253 OFActionOutput out = (OFActionOutput) act;
254 builder.setOutput(
255 PortNumber.portNumber(out.getPort().getPortNumber()));
256 break;
257 case SET_VLAN_VID:
258 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
259 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
260 break;
261 case SET_VLAN_PCP:
262 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
263 builder.setVlanPcp(pcp.getVlanPcp().getValue());
264 break;
265 case SET_DL_DST:
266 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
267 builder.setEthDst(
268 MacAddress.valueOf(dldst.getDlAddr().getLong()));
269 break;
270 case SET_DL_SRC:
271 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
272 builder.setEthSrc(
273 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
274
275 break;
276 case SET_NW_DST:
277 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
278 IPv4Address di = nwdst.getNwAddr();
279 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
280 break;
281 case SET_NW_SRC:
282 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
283 IPv4Address si = nwsrc.getNwAddr();
284 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
285 break;
286 case EXPERIMENTER:
287 OFActionExperimenter exp = (OFActionExperimenter) act;
288 if (exp.getExperimenter() == 0x80005A06 ||
289 exp.getExperimenter() == 0x748771) {
290 OFActionCircuit ct = (OFActionCircuit) exp;
291 builder.setLambda(((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber());
292 } else {
293 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
294 }
295 break;
296 case SET_FIELD:
297 OFActionSetField setField = (OFActionSetField) act;
298 handleSetField(builder, setField.getField());
299 break;
300 case POP_MPLS:
301 OFActionPopMpls popMpls = (OFActionPopMpls) act;
302 builder.popMpls((short) popMpls.getEthertype().getValue());
303 break;
304 case PUSH_MPLS:
305 builder.pushMpls();
306 break;
307 case COPY_TTL_IN:
308 builder.copyTtlIn();
309 break;
310 case COPY_TTL_OUT:
311 builder.copyTtlOut();
312 break;
313 case DEC_MPLS_TTL:
314 builder.decMplsTtl();
315 break;
316 case DEC_NW_TTL:
317 builder.decNwTtl();
318 break;
319 case GROUP:
320 OFActionGroup group = (OFActionGroup) act;
321 builder.group(new DefaultGroupId(group.getGroup().getGroupNumber()));
322 break;
Jonathan Hart67fc0972015-03-19 15:21:20 -0700323 case STRIP_VLAN:
alshabib346b5b32015-03-06 00:42:16 -0800324 case POP_VLAN:
325 builder.popVlan();
326 break;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700327 case PUSH_VLAN:
328 builder.pushVlan();
329 break;
alshabib346b5b32015-03-06 00:42:16 -0800330 case SET_TP_DST:
331 case SET_TP_SRC:
332 case POP_PBB:
333 case PUSH_PBB:
alshabib346b5b32015-03-06 00:42:16 -0800334 case SET_MPLS_LABEL:
335 case SET_MPLS_TC:
336 case SET_MPLS_TTL:
337 case SET_NW_ECN:
338 case SET_NW_TOS:
339 case SET_NW_TTL:
340 case SET_QUEUE:
341
342 case ENQUEUE:
343 default:
344 log.warn("Action type {} not yet implemented.", act.getType());
345 }
346 }
347 return builder;
348 }
349
350
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800351 private void handleSetField(TrafficTreatment.Builder builder, OFOxm<?> oxm) {
352 switch (oxm.getMatchField().id) {
353 case VLAN_PCP:
354 @SuppressWarnings("unchecked")
355 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
356 builder.setVlanPcp(vlanpcp.getValue().getValue());
357 break;
358 case VLAN_VID:
359 @SuppressWarnings("unchecked")
360 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
361 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
362 break;
363 case ETH_DST:
364 @SuppressWarnings("unchecked")
365 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
366 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
367 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
368 break;
369 case ETH_SRC:
370 @SuppressWarnings("unchecked")
371 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
372 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
373 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
374 break;
375 case IPV4_DST:
376 @SuppressWarnings("unchecked")
377 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
378 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
379 break;
380 case IPV4_SRC:
381 @SuppressWarnings("unchecked")
382 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
383 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
384 break;
sangho3f97a17d2015-01-29 22:56:29 -0800385 case MPLS_LABEL:
386 @SuppressWarnings("unchecked")
387 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100388 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
sangho3f97a17d2015-01-29 22:56:29 -0800389 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800390 case ARP_OP:
391 case ARP_SHA:
392 case ARP_SPA:
393 case ARP_THA:
394 case ARP_TPA:
395 case BSN_EGR_PORT_GROUP_ID:
396 case BSN_GLOBAL_VRF_ALLOWED:
397 case BSN_IN_PORTS_128:
398 case BSN_L3_DST_CLASS_ID:
399 case BSN_L3_INTERFACE_CLASS_ID:
400 case BSN_L3_SRC_CLASS_ID:
401 case BSN_LAG_ID:
402 case BSN_TCP_FLAGS:
403 case BSN_UDF0:
404 case BSN_UDF1:
405 case BSN_UDF2:
406 case BSN_UDF3:
407 case BSN_UDF4:
408 case BSN_UDF5:
409 case BSN_UDF6:
410 case BSN_UDF7:
411 case BSN_VLAN_XLATE_PORT_GROUP_ID:
412 case BSN_VRF:
413 case ETH_TYPE:
414 case ICMPV4_CODE:
415 case ICMPV4_TYPE:
416 case ICMPV6_CODE:
417 case ICMPV6_TYPE:
418 case IN_PHY_PORT:
419 case IN_PORT:
420 case IPV6_DST:
421 case IPV6_FLABEL:
422 case IPV6_ND_SLL:
423 case IPV6_ND_TARGET:
424 case IPV6_ND_TLL:
425 case IPV6_SRC:
426 case IP_DSCP:
427 case IP_ECN:
428 case IP_PROTO:
429 case METADATA:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800430 case MPLS_TC:
431 case OCH_SIGID:
432 case OCH_SIGID_BASIC:
433 case OCH_SIGTYPE:
434 case OCH_SIGTYPE_BASIC:
435 case SCTP_DST:
436 case SCTP_SRC:
437 case TCP_DST:
438 case TCP_SRC:
439 case TUNNEL_ID:
440 case UDP_DST:
441 case UDP_SRC:
442 default:
443 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
444 break;
445 }
446 }
447
alshabib6b5cfec2014-09-18 17:42:18 -0700448 private TrafficSelector buildSelector() {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800449 MacAddress mac;
450 Ip4Prefix ip4Prefix;
451 Ip6Address ip6Address;
452 Ip6Prefix ip6Prefix;
453
tom9a693fd2014-10-03 11:32:19 -0700454 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700455 for (MatchField<?> field : match.getMatchFields()) {
456 switch (field.id) {
457 case IN_PORT:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800458 builder.matchInPort(PortNumber
alshabib010c31d2014-09-26 10:01:12 -0700459 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700460 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800461 case IN_PHY_PORT:
462 builder.matchInPhyPort(PortNumber
463 .portNumber(match.get(MatchField.IN_PHY_PORT).getPortNumber()));
464 break;
465 case METADATA:
466 long metadata =
467 match.get(MatchField.METADATA).getValue().getValue();
468 builder.matchMetadata(metadata);
469 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700470 case ETH_DST:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800471 mac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
472 builder.matchEthDst(mac);
473 break;
474 case ETH_SRC:
475 mac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
476 builder.matchEthSrc(mac);
alshabib6b5cfec2014-09-18 17:42:18 -0700477 break;
478 case ETH_TYPE:
479 int ethType = match.get(MatchField.ETH_TYPE).getValue();
sanghoc0b3c342015-03-12 17:12:10 -0700480 if (ethType == EthType.VLAN_FRAME.getValue()) {
481 builder.matchVlanId(VlanId.ANY);
482 } else {
483 builder.matchEthType((short) ethType);
484 }
alshabib6b5cfec2014-09-18 17:42:18 -0700485 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700486 case VLAN_VID:
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800487 VlanId vlanId = null;
488 if (match.isPartiallyMasked(MatchField.VLAN_VID)) {
489 Masked<OFVlanVidMatch> masked = match.getMasked(MatchField.VLAN_VID);
490 if (masked.getValue().equals(OFVlanVidMatch.PRESENT)
491 && masked.getMask().equals(OFVlanVidMatch.PRESENT)) {
492 vlanId = VlanId.ANY;
493 }
494 } else {
495 vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
496 }
497 if (vlanId != null) {
498 builder.matchVlanId(vlanId);
499 }
alshabib6b5cfec2014-09-18 17:42:18 -0700500 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800501 case VLAN_PCP:
502 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
503 builder.matchVlanPcp(vlanPcp);
504 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800505 case IP_DSCP:
506 byte ipDscp = match.get(MatchField.IP_DSCP).getDscpValue();
507 builder.matchIPDscp(ipDscp);
508 break;
509 case IP_ECN:
510 byte ipEcn = match.get(MatchField.IP_ECN).getEcnValue();
511 builder.matchIPEcn(ipEcn);
512 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800513 case IP_PROTO:
514 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
515 builder.matchIPProtocol((byte) proto);
516 break;
517 case IPV4_SRC:
518 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
519 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800520 ip4Prefix = Ip4Prefix.valueOf(
521 maskedIp.getValue().getInt(),
522 maskedIp.getMask().asCidrMaskLength());
523 } else {
524 ip4Prefix = Ip4Prefix.valueOf(
525 match.get(MatchField.IPV4_SRC).getInt(),
526 Ip4Prefix.MAX_MASK_LENGTH);
527 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800528 builder.matchIPSrc(ip4Prefix);
529 break;
530 case IPV4_DST:
531 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
532 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800533 ip4Prefix = Ip4Prefix.valueOf(
534 maskedIp.getValue().getInt(),
535 maskedIp.getMask().asCidrMaskLength());
536 } else {
537 ip4Prefix = Ip4Prefix.valueOf(
538 match.get(MatchField.IPV4_DST).getInt(),
539 Ip4Prefix.MAX_MASK_LENGTH);
540 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800541 builder.matchIPDst(ip4Prefix);
Jonathan Hart34bc6142014-10-17 11:00:43 -0700542 break;
543 case TCP_SRC:
544 builder.matchTcpSrc((short) match.get(MatchField.TCP_SRC).getPort());
545 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800546 case TCP_DST:
547 builder.matchTcpDst((short) match.get(MatchField.TCP_DST).getPort());
548 break;
549 case UDP_SRC:
550 builder.matchUdpSrc((short) match.get(MatchField.UDP_SRC).getPort());
551 break;
552 case UDP_DST:
553 builder.matchUdpDst((short) match.get(MatchField.UDP_DST).getPort());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100554 case MPLS_LABEL:
555 builder.matchMplsLabel(MplsLabel.mplsLabel((int) match.get(MatchField.MPLS_LABEL)
556 .getValue()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800557 break;
558 case SCTP_SRC:
559 builder.matchSctpSrc((short) match.get(MatchField.SCTP_SRC).getPort());
560 break;
561 case SCTP_DST:
562 builder.matchSctpDst((short) match.get(MatchField.SCTP_DST).getPort());
563 break;
564 case ICMPV4_TYPE:
565 byte icmpType = (byte) match.get(MatchField.ICMPV4_TYPE).getType();
566 builder.matchIcmpType(icmpType);
567 break;
568 case ICMPV4_CODE:
569 byte icmpCode = (byte) match.get(MatchField.ICMPV4_CODE).getCode();
570 builder.matchIcmpCode(icmpCode);
571 break;
572 case IPV6_SRC:
573 if (match.isPartiallyMasked(MatchField.IPV6_SRC)) {
574 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_SRC);
575 ip6Prefix = Ip6Prefix.valueOf(
576 maskedIp.getValue().getBytes(),
577 maskedIp.getMask().asCidrMaskLength());
578 } else {
579 ip6Prefix = Ip6Prefix.valueOf(
580 match.get(MatchField.IPV6_SRC).getBytes(),
581 Ip6Prefix.MAX_MASK_LENGTH);
582 }
583 builder.matchIPv6Src(ip6Prefix);
584 break;
585 case IPV6_DST:
586 if (match.isPartiallyMasked(MatchField.IPV6_DST)) {
587 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_DST);
588 ip6Prefix = Ip6Prefix.valueOf(
589 maskedIp.getValue().getBytes(),
590 maskedIp.getMask().asCidrMaskLength());
591 } else {
592 ip6Prefix = Ip6Prefix.valueOf(
593 match.get(MatchField.IPV6_DST).getBytes(),
594 Ip6Prefix.MAX_MASK_LENGTH);
595 }
596 builder.matchIPv6Dst(ip6Prefix);
597 break;
598 case IPV6_FLABEL:
599 int flowLabel =
600 match.get(MatchField.IPV6_FLABEL).getIPv6FlowLabelValue();
601 builder.matchIPv6FlowLabel(flowLabel);
602 break;
603 case ICMPV6_TYPE:
604 byte icmpv6type = (byte) match.get(MatchField.ICMPV6_TYPE).getValue();
605 builder.matchIcmpv6Type(icmpv6type);
606 break;
607 case ICMPV6_CODE:
608 byte icmpv6code = (byte) match.get(MatchField.ICMPV6_CODE).getValue();
609 builder.matchIcmpv6Code(icmpv6code);
610 break;
611 case IPV6_ND_TARGET:
612 ip6Address =
613 Ip6Address.valueOf(match.get(MatchField.IPV6_ND_TARGET).getBytes());
614 builder.matchIPv6NDTargetAddress(ip6Address);
615 break;
616 case IPV6_ND_SLL:
617 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_SLL).getLong());
618 builder.matchIPv6NDSourceLinkLayerAddress(mac);
619 break;
620 case IPV6_ND_TLL:
621 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_TLL).getLong());
622 builder.matchIPv6NDTargetLinkLayerAddress(mac);
623 break;
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800624 case IPV6_EXTHDR:
625 builder.matchIPv6ExthdrFlags((int) match.get(MatchField.IPV6_EXTHDR)
626 .getValue());
627 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700628 case OCH_SIGID:
629 builder.matchLambda(match.get(MatchField.OCH_SIGID).getChannelNumber());
630 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800631 case OCH_SIGTYPE:
632 builder.matchOpticalSignalType(match.get(MatchField
633 .OCH_SIGTYPE).getValue());
634 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700635 case ARP_OP:
636 case ARP_SHA:
637 case ARP_SPA:
638 case ARP_THA:
639 case ARP_TPA:
alshabib6b5cfec2014-09-18 17:42:18 -0700640 case MPLS_TC:
alshabib6b5cfec2014-09-18 17:42:18 -0700641 case TUNNEL_ID:
alshabib6b5cfec2014-09-18 17:42:18 -0700642 default:
643 log.warn("Match type {} not yet implemented.", field.id);
alshabib6b5cfec2014-09-18 17:42:18 -0700644 }
645 }
646 return builder.build();
647 }
alshabib6b5cfec2014-09-18 17:42:18 -0700648}