blob: 61f669c6f349c325eecfbde6ef75e7b5b16b5fc8 [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;
Saurav Das86af8f12015-05-25 23:55:33 -070019
Jonathan Hartd0ef7522014-12-11 11:51:18 -080020import org.onlab.packet.Ip4Address;
21import org.onlab.packet.Ip4Prefix;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -080022import org.onlab.packet.Ip6Address;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080023import org.onlab.packet.Ip6Prefix;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080024import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010025import org.onlab.packet.MplsLabel;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080026import org.onlab.packet.VlanId;
sangho8995ac52015-02-04 11:29:03 -080027import org.onosproject.core.DefaultGroupId;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.DeviceId;
29import org.onosproject.net.PortNumber;
30import org.onosproject.net.flow.DefaultFlowEntry;
31import org.onosproject.net.flow.DefaultFlowRule;
32import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
34import org.onosproject.net.flow.FlowEntry;
35import org.onosproject.net.flow.FlowEntry.FlowEntryState;
36import org.onosproject.net.flow.FlowRule;
37import 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;
alshabibbdcbb102015-04-22 14:16:38 -070058import org.projectfloodlight.openflow.protocol.instruction.OFInstructionGotoTable;
alshabib346b5b32015-03-06 00:42:16 -080059import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteActions;
Saurav Das86af8f12015-05-25 23:55:33 -070060import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteMetadata;
alshabib6b5cfec2014-09-18 17:42:18 -070061import org.projectfloodlight.openflow.protocol.match.Match;
62import org.projectfloodlight.openflow.protocol.match.MatchField;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080063import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
Marc De Leenheer49087752014-10-23 13:54:09 -070064import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigidBasic;
alshabib346b5b32015-03-06 00:42:16 -080065import org.projectfloodlight.openflow.protocol.ver13.OFFactoryVer13;
sanghoc0b3c342015-03-12 17:12:10 -070066import org.projectfloodlight.openflow.types.EthType;
alshabib6b5cfec2014-09-18 17:42:18 -070067import org.projectfloodlight.openflow.types.IPv4Address;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080068import org.projectfloodlight.openflow.types.IPv6Address;
Jonathan Hart0e12fad2014-10-17 14:54:58 -070069import org.projectfloodlight.openflow.types.Masked;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080070import org.projectfloodlight.openflow.types.OFVlanVidMatch;
sangho3f97a17d2015-01-29 22:56:29 -080071import org.projectfloodlight.openflow.types.U32;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080072import org.projectfloodlight.openflow.types.VlanPcp;
alshabib6b5cfec2014-09-18 17:42:18 -070073import org.slf4j.Logger;
74
Jonathan Hart6cd2f352015-01-13 17:44:45 -080075import java.util.List;
76
77import static org.slf4j.LoggerFactory.getLogger;
alshabib19fdc122014-10-03 11:38:19 -070078
alshabib1c319ff2014-10-04 20:29:09 -070079public class FlowEntryBuilder {
alshabib6b5cfec2014-09-18 17:42:18 -070080 private final Logger log = getLogger(getClass());
81
82 private final OFFlowStatsEntry stat;
83 private final OFFlowRemoved removed;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080084 private final OFFlowMod flowMod;
alshabib6b5cfec2014-09-18 17:42:18 -070085
86 private final Match match;
alshabib346b5b32015-03-06 00:42:16 -080087
Jonathan Hart67fc0972015-03-19 15:21:20 -070088 // All actions are contained in an OFInstruction. For OF1.0
89 // the instruction type is apply instruction (immediate set in ONOS speak)
alshabib346b5b32015-03-06 00:42:16 -080090 private final List<OFInstruction> instructions;
alshabib6b5cfec2014-09-18 17:42:18 -070091
92 private final Dpid dpid;
93
Brian O'Connor72cb19a2015-01-16 16:14:41 -080094 public enum FlowType { STAT, REMOVED, MOD }
95
96 private final FlowType type;
alshabib6b5cfec2014-09-18 17:42:18 -070097
alshabibbdcbb102015-04-22 14:16:38 -070098 public FlowEntryBuilder(Dpid dpid, OFFlowStatsEntry entry) {
Saurav Dasfa2fa932015-03-03 11:29:48 -080099 this.stat = entry;
100 this.match = entry.getMatch();
alshabib346b5b32015-03-06 00:42:16 -0800101 this.instructions = getInstructions(entry);
Saurav Dasfa2fa932015-03-03 11:29:48 -0800102 this.dpid = dpid;
103 this.removed = null;
104 this.flowMod = null;
105 this.type = FlowType.STAT;
Saurav Dasfa2fa932015-03-03 11:29:48 -0800106 }
107
alshabibbdcbb102015-04-22 14:16:38 -0700108 public FlowEntryBuilder(Dpid dpid, OFFlowRemoved removed) {
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;
alshabib6b5cfec2014-09-18 17:42:18 -0700117
118 }
119
alshabibbdcbb102015-04-22 14:16:38 -0700120 public FlowEntryBuilder(Dpid dpid, OFFlowMod fm) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800121 this.match = fm.getMatch();
122 this.dpid = dpid;
alshabib346b5b32015-03-06 00:42:16 -0800123 this.instructions = getInstructions(fm);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800124 this.type = FlowType.MOD;
125 this.flowMod = fm;
126 this.stat = null;
127 this.removed = null;
128 }
alshabib1c319ff2014-10-04 20:29:09 -0700129
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800130 public FlowEntry build(FlowEntryState... state) {
131 FlowRule rule;
132 switch (this.type) {
133 case STAT:
alshabibbdcbb102015-04-22 14:16:38 -0700134 rule = DefaultFlowRule.builder()
135 .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
136 .withSelector(buildSelector())
137 .withTreatment(buildTreatment())
138 .withPriority(stat.getPriority())
139 .makeTemporary(stat.getIdleTimeout())
140 .withCookie(stat.getCookie().getValue())
141 .forTable(stat.getTableId().getValue())
142 .build();
143
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800144 return new DefaultFlowEntry(rule, FlowEntryState.ADDED,
145 stat.getDurationSec(), stat.getPacketCount().getValue(),
146 stat.getByteCount().getValue());
147 case REMOVED:
alshabibbdcbb102015-04-22 14:16:38 -0700148 rule = DefaultFlowRule.builder()
149 .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
150 .withSelector(buildSelector())
151 .withPriority(removed.getPriority())
152 .makeTemporary(removed.getIdleTimeout())
153 .withCookie(removed.getCookie().getValue())
154 .forTable(removed.getTableId().getValue())
155 .build();
156
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800157 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;
alshabibbdcbb102015-04-22 14:16:38 -0700161 rule = DefaultFlowRule.builder()
162 .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
163 .withSelector(buildSelector())
164 .withTreatment(buildTreatment())
165 .withPriority(flowMod.getPriority())
166 .makeTemporary(flowMod.getIdleTimeout())
167 .withCookie(flowMod.getCookie().getValue())
168 .forTable(flowMod.getTableId().getValue())
169 .build();
170
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800171 return new DefaultFlowEntry(rule, flowState, 0, 0, 0);
172 default:
173 log.error("Unknown flow type : {}", this.type);
174 return null;
alshabib6b5cfec2014-09-18 17:42:18 -0700175 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800176
alshabib6b5cfec2014-09-18 17:42:18 -0700177 }
178
alshabib346b5b32015-03-06 00:42:16 -0800179 private List<OFInstruction> getInstructions(OFFlowMod entry) {
alshabib19fdc122014-10-03 11:38:19 -0700180 switch (entry.getVersion()) {
181 case OF_10:
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700182 return Lists.newArrayList(OFFactoryVer13.INSTANCE.instructions()
183 .applyActions(
184 entry.getActions()));
alshabib19fdc122014-10-03 11:38:19 -0700185 case OF_11:
186 case OF_12:
187 case OF_13:
alshabib346b5b32015-03-06 00:42:16 -0800188 return entry.getInstructions();
189 default:
190 log.warn("Unknown OF version {}", entry.getVersion());
191 }
192 return Lists.newLinkedList();
193 }
194
195 private List<OFInstruction> getInstructions(OFFlowStatsEntry entry) {
196 switch (entry.getVersion()) {
197 case OF_10:
198 return Lists.newArrayList(
199 OFFactoryVer13.INSTANCE.instructions().applyActions(entry.getActions()));
200 case OF_11:
201 case OF_12:
202 case OF_13:
203 return entry.getInstructions();
alshabib19fdc122014-10-03 11:38:19 -0700204 default:
205 log.warn("Unknown OF version {}", entry.getVersion());
206 }
207 return Lists.newLinkedList();
208 }
alshabib6b5cfec2014-09-18 17:42:18 -0700209
210 private TrafficTreatment buildTreatment() {
tom9a693fd2014-10-03 11:32:19 -0700211 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700212 // If this is a drop rule
alshabib346b5b32015-03-06 00:42:16 -0800213 if (instructions.size() == 0) {
alshabib010c31d2014-09-26 10:01:12 -0700214 builder.drop();
alshabib6b5cfec2014-09-18 17:42:18 -0700215 return builder.build();
216 }
alshabib346b5b32015-03-06 00:42:16 -0800217 for (OFInstruction in : instructions) {
218 switch (in.getType()) {
219 case GOTO_TABLE:
alshabibbdcbb102015-04-22 14:16:38 -0700220 builder.transition(((int) ((OFInstructionGotoTable) in)
221 .getTableId().getValue()));
alshabib346b5b32015-03-06 00:42:16 -0800222 break;
223 case WRITE_METADATA:
Saurav Das86af8f12015-05-25 23:55:33 -0700224 OFInstructionWriteMetadata m = (OFInstructionWriteMetadata) in;
225 builder.writeMetadata(m.getMetadata().getValue(),
226 m.getMetadataMask().getValue());
alshabib346b5b32015-03-06 00:42:16 -0800227 break;
228 case WRITE_ACTIONS:
229 builder.deferred();
230 buildActions(((OFInstructionWriteActions) in).getActions(),
231 builder);
232 break;
233 case APPLY_ACTIONS:
234 builder.immediate();
235 buildActions(((OFInstructionApplyActions) in).getActions(),
236 builder);
237 break;
238 case CLEAR_ACTIONS:
239 builder.wipeDeferred();
240 break;
241 case EXPERIMENTER:
242 break;
243 case METER:
244 break;
245 default:
246 log.warn("Unknown instructions type {}", in.getType());
alshabib6b5cfec2014-09-18 17:42:18 -0700247 }
248 }
249
250 return builder.build();
251 }
252
alshabib346b5b32015-03-06 00:42:16 -0800253 private TrafficTreatment.Builder buildActions(List<OFAction> actions,
254 TrafficTreatment.Builder builder) {
255 for (OFAction act : actions) {
256 switch (act.getType()) {
257 case OUTPUT:
258 OFActionOutput out = (OFActionOutput) act;
259 builder.setOutput(
260 PortNumber.portNumber(out.getPort().getPortNumber()));
261 break;
262 case SET_VLAN_VID:
263 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
264 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
265 break;
266 case SET_VLAN_PCP:
267 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
268 builder.setVlanPcp(pcp.getVlanPcp().getValue());
269 break;
270 case SET_DL_DST:
271 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
272 builder.setEthDst(
273 MacAddress.valueOf(dldst.getDlAddr().getLong()));
274 break;
275 case SET_DL_SRC:
276 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
277 builder.setEthSrc(
278 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
279
280 break;
281 case SET_NW_DST:
282 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
283 IPv4Address di = nwdst.getNwAddr();
284 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
285 break;
286 case SET_NW_SRC:
287 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
288 IPv4Address si = nwsrc.getNwAddr();
289 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
290 break;
291 case EXPERIMENTER:
292 OFActionExperimenter exp = (OFActionExperimenter) act;
293 if (exp.getExperimenter() == 0x80005A06 ||
294 exp.getExperimenter() == 0x748771) {
295 OFActionCircuit ct = (OFActionCircuit) exp;
296 builder.setLambda(((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber());
297 } else {
298 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
299 }
300 break;
301 case SET_FIELD:
302 OFActionSetField setField = (OFActionSetField) act;
303 handleSetField(builder, setField.getField());
304 break;
305 case POP_MPLS:
306 OFActionPopMpls popMpls = (OFActionPopMpls) act;
307 builder.popMpls((short) popMpls.getEthertype().getValue());
308 break;
309 case PUSH_MPLS:
310 builder.pushMpls();
311 break;
312 case COPY_TTL_IN:
313 builder.copyTtlIn();
314 break;
315 case COPY_TTL_OUT:
316 builder.copyTtlOut();
317 break;
318 case DEC_MPLS_TTL:
319 builder.decMplsTtl();
320 break;
321 case DEC_NW_TTL:
322 builder.decNwTtl();
323 break;
324 case GROUP:
325 OFActionGroup group = (OFActionGroup) act;
326 builder.group(new DefaultGroupId(group.getGroup().getGroupNumber()));
327 break;
Jonathan Hart67fc0972015-03-19 15:21:20 -0700328 case STRIP_VLAN:
alshabib346b5b32015-03-06 00:42:16 -0800329 case POP_VLAN:
330 builder.popVlan();
331 break;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700332 case PUSH_VLAN:
333 builder.pushVlan();
334 break;
alshabib346b5b32015-03-06 00:42:16 -0800335 case SET_TP_DST:
336 case SET_TP_SRC:
337 case POP_PBB:
338 case PUSH_PBB:
alshabib346b5b32015-03-06 00:42:16 -0800339 case SET_MPLS_LABEL:
340 case SET_MPLS_TC:
341 case SET_MPLS_TTL:
342 case SET_NW_ECN:
343 case SET_NW_TOS:
344 case SET_NW_TTL:
345 case SET_QUEUE:
346
347 case ENQUEUE:
348 default:
349 log.warn("Action type {} not yet implemented.", act.getType());
350 }
351 }
352 return builder;
353 }
354
355
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800356 private void handleSetField(TrafficTreatment.Builder builder, OFOxm<?> oxm) {
357 switch (oxm.getMatchField().id) {
358 case VLAN_PCP:
359 @SuppressWarnings("unchecked")
360 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
361 builder.setVlanPcp(vlanpcp.getValue().getValue());
362 break;
363 case VLAN_VID:
364 @SuppressWarnings("unchecked")
365 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
366 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
367 break;
368 case ETH_DST:
369 @SuppressWarnings("unchecked")
370 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
371 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
372 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
373 break;
374 case ETH_SRC:
375 @SuppressWarnings("unchecked")
376 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
377 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
378 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
379 break;
380 case IPV4_DST:
381 @SuppressWarnings("unchecked")
382 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
383 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
384 break;
385 case IPV4_SRC:
386 @SuppressWarnings("unchecked")
387 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
388 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
389 break;
sangho3f97a17d2015-01-29 22:56:29 -0800390 case MPLS_LABEL:
391 @SuppressWarnings("unchecked")
392 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100393 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
sangho3f97a17d2015-01-29 22:56:29 -0800394 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800395 case ARP_OP:
396 case ARP_SHA:
397 case ARP_SPA:
398 case ARP_THA:
399 case ARP_TPA:
400 case BSN_EGR_PORT_GROUP_ID:
401 case BSN_GLOBAL_VRF_ALLOWED:
402 case BSN_IN_PORTS_128:
403 case BSN_L3_DST_CLASS_ID:
404 case BSN_L3_INTERFACE_CLASS_ID:
405 case BSN_L3_SRC_CLASS_ID:
406 case BSN_LAG_ID:
407 case BSN_TCP_FLAGS:
408 case BSN_UDF0:
409 case BSN_UDF1:
410 case BSN_UDF2:
411 case BSN_UDF3:
412 case BSN_UDF4:
413 case BSN_UDF5:
414 case BSN_UDF6:
415 case BSN_UDF7:
416 case BSN_VLAN_XLATE_PORT_GROUP_ID:
417 case BSN_VRF:
418 case ETH_TYPE:
419 case ICMPV4_CODE:
420 case ICMPV4_TYPE:
421 case ICMPV6_CODE:
422 case ICMPV6_TYPE:
423 case IN_PHY_PORT:
424 case IN_PORT:
425 case IPV6_DST:
426 case IPV6_FLABEL:
427 case IPV6_ND_SLL:
428 case IPV6_ND_TARGET:
429 case IPV6_ND_TLL:
430 case IPV6_SRC:
431 case IP_DSCP:
432 case IP_ECN:
433 case IP_PROTO:
434 case METADATA:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800435 case MPLS_TC:
436 case OCH_SIGID:
437 case OCH_SIGID_BASIC:
438 case OCH_SIGTYPE:
439 case OCH_SIGTYPE_BASIC:
440 case SCTP_DST:
441 case SCTP_SRC:
442 case TCP_DST:
443 case TCP_SRC:
444 case TUNNEL_ID:
445 case UDP_DST:
446 case UDP_SRC:
447 default:
448 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
449 break;
450 }
451 }
452
Sho SHIMIZU6f1b09e2015-05-05 11:26:22 -0700453 // CHECKSTYLE IGNORE MethodLength FOR NEXT 1 LINES
alshabib6b5cfec2014-09-18 17:42:18 -0700454 private TrafficSelector buildSelector() {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800455 MacAddress mac;
456 Ip4Prefix ip4Prefix;
457 Ip6Address ip6Address;
458 Ip6Prefix ip6Prefix;
459
tom9a693fd2014-10-03 11:32:19 -0700460 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700461 for (MatchField<?> field : match.getMatchFields()) {
462 switch (field.id) {
463 case IN_PORT:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800464 builder.matchInPort(PortNumber
alshabib010c31d2014-09-26 10:01:12 -0700465 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700466 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800467 case IN_PHY_PORT:
468 builder.matchInPhyPort(PortNumber
469 .portNumber(match.get(MatchField.IN_PHY_PORT).getPortNumber()));
470 break;
471 case METADATA:
472 long metadata =
473 match.get(MatchField.METADATA).getValue().getValue();
474 builder.matchMetadata(metadata);
475 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700476 case ETH_DST:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800477 mac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
478 builder.matchEthDst(mac);
479 break;
480 case ETH_SRC:
481 mac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
482 builder.matchEthSrc(mac);
alshabib6b5cfec2014-09-18 17:42:18 -0700483 break;
484 case ETH_TYPE:
485 int ethType = match.get(MatchField.ETH_TYPE).getValue();
sanghoc0b3c342015-03-12 17:12:10 -0700486 if (ethType == EthType.VLAN_FRAME.getValue()) {
487 builder.matchVlanId(VlanId.ANY);
488 } else {
489 builder.matchEthType((short) ethType);
490 }
alshabib6b5cfec2014-09-18 17:42:18 -0700491 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700492 case VLAN_VID:
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800493 VlanId vlanId = null;
494 if (match.isPartiallyMasked(MatchField.VLAN_VID)) {
495 Masked<OFVlanVidMatch> masked = match.getMasked(MatchField.VLAN_VID);
496 if (masked.getValue().equals(OFVlanVidMatch.PRESENT)
497 && masked.getMask().equals(OFVlanVidMatch.PRESENT)) {
498 vlanId = VlanId.ANY;
499 }
500 } else {
501 vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
502 }
503 if (vlanId != null) {
504 builder.matchVlanId(vlanId);
505 }
alshabib6b5cfec2014-09-18 17:42:18 -0700506 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800507 case VLAN_PCP:
508 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
509 builder.matchVlanPcp(vlanPcp);
510 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800511 case IP_DSCP:
512 byte ipDscp = match.get(MatchField.IP_DSCP).getDscpValue();
513 builder.matchIPDscp(ipDscp);
514 break;
515 case IP_ECN:
516 byte ipEcn = match.get(MatchField.IP_ECN).getEcnValue();
517 builder.matchIPEcn(ipEcn);
518 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800519 case IP_PROTO:
520 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
521 builder.matchIPProtocol((byte) proto);
522 break;
523 case IPV4_SRC:
524 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
525 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800526 ip4Prefix = Ip4Prefix.valueOf(
527 maskedIp.getValue().getInt(),
528 maskedIp.getMask().asCidrMaskLength());
529 } else {
530 ip4Prefix = Ip4Prefix.valueOf(
531 match.get(MatchField.IPV4_SRC).getInt(),
532 Ip4Prefix.MAX_MASK_LENGTH);
533 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800534 builder.matchIPSrc(ip4Prefix);
535 break;
536 case IPV4_DST:
537 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
538 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800539 ip4Prefix = Ip4Prefix.valueOf(
540 maskedIp.getValue().getInt(),
541 maskedIp.getMask().asCidrMaskLength());
542 } else {
543 ip4Prefix = Ip4Prefix.valueOf(
544 match.get(MatchField.IPV4_DST).getInt(),
545 Ip4Prefix.MAX_MASK_LENGTH);
546 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800547 builder.matchIPDst(ip4Prefix);
Jonathan Hart34bc6142014-10-17 11:00:43 -0700548 break;
549 case TCP_SRC:
550 builder.matchTcpSrc((short) match.get(MatchField.TCP_SRC).getPort());
551 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800552 case TCP_DST:
553 builder.matchTcpDst((short) match.get(MatchField.TCP_DST).getPort());
554 break;
555 case UDP_SRC:
556 builder.matchUdpSrc((short) match.get(MatchField.UDP_SRC).getPort());
557 break;
558 case UDP_DST:
559 builder.matchUdpDst((short) match.get(MatchField.UDP_DST).getPort());
Sho SHIMIZU6f1b09e2015-05-05 11:26:22 -0700560 break;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100561 case MPLS_LABEL:
562 builder.matchMplsLabel(MplsLabel.mplsLabel((int) match.get(MatchField.MPLS_LABEL)
563 .getValue()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800564 break;
565 case SCTP_SRC:
566 builder.matchSctpSrc((short) match.get(MatchField.SCTP_SRC).getPort());
567 break;
568 case SCTP_DST:
569 builder.matchSctpDst((short) match.get(MatchField.SCTP_DST).getPort());
570 break;
571 case ICMPV4_TYPE:
572 byte icmpType = (byte) match.get(MatchField.ICMPV4_TYPE).getType();
573 builder.matchIcmpType(icmpType);
574 break;
575 case ICMPV4_CODE:
576 byte icmpCode = (byte) match.get(MatchField.ICMPV4_CODE).getCode();
577 builder.matchIcmpCode(icmpCode);
578 break;
579 case IPV6_SRC:
580 if (match.isPartiallyMasked(MatchField.IPV6_SRC)) {
581 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_SRC);
582 ip6Prefix = Ip6Prefix.valueOf(
583 maskedIp.getValue().getBytes(),
584 maskedIp.getMask().asCidrMaskLength());
585 } else {
586 ip6Prefix = Ip6Prefix.valueOf(
587 match.get(MatchField.IPV6_SRC).getBytes(),
588 Ip6Prefix.MAX_MASK_LENGTH);
589 }
590 builder.matchIPv6Src(ip6Prefix);
591 break;
592 case IPV6_DST:
593 if (match.isPartiallyMasked(MatchField.IPV6_DST)) {
594 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_DST);
595 ip6Prefix = Ip6Prefix.valueOf(
596 maskedIp.getValue().getBytes(),
597 maskedIp.getMask().asCidrMaskLength());
598 } else {
599 ip6Prefix = Ip6Prefix.valueOf(
600 match.get(MatchField.IPV6_DST).getBytes(),
601 Ip6Prefix.MAX_MASK_LENGTH);
602 }
603 builder.matchIPv6Dst(ip6Prefix);
604 break;
605 case IPV6_FLABEL:
606 int flowLabel =
607 match.get(MatchField.IPV6_FLABEL).getIPv6FlowLabelValue();
608 builder.matchIPv6FlowLabel(flowLabel);
609 break;
610 case ICMPV6_TYPE:
611 byte icmpv6type = (byte) match.get(MatchField.ICMPV6_TYPE).getValue();
612 builder.matchIcmpv6Type(icmpv6type);
613 break;
614 case ICMPV6_CODE:
615 byte icmpv6code = (byte) match.get(MatchField.ICMPV6_CODE).getValue();
616 builder.matchIcmpv6Code(icmpv6code);
617 break;
618 case IPV6_ND_TARGET:
619 ip6Address =
620 Ip6Address.valueOf(match.get(MatchField.IPV6_ND_TARGET).getBytes());
621 builder.matchIPv6NDTargetAddress(ip6Address);
622 break;
623 case IPV6_ND_SLL:
624 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_SLL).getLong());
625 builder.matchIPv6NDSourceLinkLayerAddress(mac);
626 break;
627 case IPV6_ND_TLL:
628 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_TLL).getLong());
629 builder.matchIPv6NDTargetLinkLayerAddress(mac);
630 break;
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800631 case IPV6_EXTHDR:
Charles M.C. Chan2184de12015-04-26 02:24:53 +0800632 builder.matchIPv6ExthdrFlags((short) match.get(MatchField.IPV6_EXTHDR)
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800633 .getValue());
634 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700635 case OCH_SIGID:
636 builder.matchLambda(match.get(MatchField.OCH_SIGID).getChannelNumber());
637 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800638 case OCH_SIGTYPE:
639 builder.matchOpticalSignalType(match.get(MatchField
640 .OCH_SIGTYPE).getValue());
641 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700642 case ARP_OP:
643 case ARP_SHA:
644 case ARP_SPA:
645 case ARP_THA:
646 case ARP_TPA:
alshabib6b5cfec2014-09-18 17:42:18 -0700647 case MPLS_TC:
alshabib6b5cfec2014-09-18 17:42:18 -0700648 case TUNNEL_ID:
alshabib6b5cfec2014-09-18 17:42:18 -0700649 default:
650 log.warn("Match type {} not yet implemented.", field.id);
alshabib6b5cfec2014-09-18 17:42:18 -0700651 }
652 }
653 return builder.build();
654 }
alshabib6b5cfec2014-09-18 17:42:18 -0700655}