blob: f238bdb1705cf5f08bc07a4af0de1b945f230fe7 [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
Saurav Dasffc5bbc2015-08-18 23:30:19 -070018import static org.onosproject.net.flow.criteria.Criteria.matchLambda;
19import static org.onosproject.net.flow.criteria.Criteria.matchOchSignalType;
20import static org.onosproject.provider.of.flow.impl.OpenFlowValueMapper.lookupChannelSpacing;
21import static org.onosproject.provider.of.flow.impl.OpenFlowValueMapper.lookupGridType;
22import static org.onosproject.provider.of.flow.impl.OpenFlowValueMapper.lookupOchSignalType;
23import static org.slf4j.LoggerFactory.getLogger;
24
25import java.util.List;
Saurav Das86af8f12015-05-25 23:55:33 -070026
Jonathan Hartd0ef7522014-12-11 11:51:18 -080027import org.onlab.packet.Ip4Address;
28import org.onlab.packet.Ip4Prefix;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -080029import org.onlab.packet.Ip6Address;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080030import org.onlab.packet.Ip6Prefix;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080031import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010032import org.onlab.packet.MplsLabel;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070033import org.onlab.packet.TpPort;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080034import org.onlab.packet.VlanId;
sangho8995ac52015-02-04 11:29:03 -080035import org.onosproject.core.DefaultGroupId;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.DeviceId;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070037import org.onosproject.net.Lambda;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.PortNumber;
39import org.onosproject.net.flow.DefaultFlowEntry;
40import org.onosproject.net.flow.DefaultFlowRule;
41import org.onosproject.net.flow.DefaultTrafficSelector;
42import org.onosproject.net.flow.DefaultTrafficTreatment;
43import org.onosproject.net.flow.FlowEntry;
44import org.onosproject.net.flow.FlowEntry.FlowEntryState;
45import org.onosproject.net.flow.FlowRule;
46import org.onosproject.net.flow.TrafficSelector;
47import org.onosproject.net.flow.TrafficTreatment;
Sho SHIMIZU9553bb82015-06-30 16:02:45 -070048import org.onosproject.net.flow.instructions.Instructions;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.openflow.controller.Dpid;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080050import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabib6b5cfec2014-09-18 17:42:18 -070051import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
52import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
53import org.projectfloodlight.openflow.protocol.action.OFAction;
Marc De Leenheer49087752014-10-23 13:54:09 -070054import org.projectfloodlight.openflow.protocol.action.OFActionCircuit;
55import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter;
sangho8995ac52015-02-04 11:29:03 -080056import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
alshabib6b5cfec2014-09-18 17:42:18 -070057import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
sangho3f97a17d2015-01-29 22:56:29 -080058import org.projectfloodlight.openflow.protocol.action.OFActionPopMpls;
alshabib6b5cfec2014-09-18 17:42:18 -070059import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst;
60import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080061import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
alshabib6b5cfec2014-09-18 17:42:18 -070062import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst;
63import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc;
64import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp;
65import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid;
alshabib19fdc122014-10-03 11:38:19 -070066import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hartcf783202014-11-24 18:55:42 -080067import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions;
alshabibbdcbb102015-04-22 14:16:38 -070068import org.projectfloodlight.openflow.protocol.instruction.OFInstructionGotoTable;
alshabib346b5b32015-03-06 00:42:16 -080069import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteActions;
Saurav Das86af8f12015-05-25 23:55:33 -070070import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteMetadata;
alshabib6b5cfec2014-09-18 17:42:18 -070071import org.projectfloodlight.openflow.protocol.match.Match;
72import org.projectfloodlight.openflow.protocol.match.MatchField;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080073import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
Marc De Leenheer49087752014-10-23 13:54:09 -070074import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigidBasic;
alshabib346b5b32015-03-06 00:42:16 -080075import org.projectfloodlight.openflow.protocol.ver13.OFFactoryVer13;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070076import org.projectfloodlight.openflow.types.CircuitSignalID;
sanghoc0b3c342015-03-12 17:12:10 -070077import org.projectfloodlight.openflow.types.EthType;
alshabib6b5cfec2014-09-18 17:42:18 -070078import org.projectfloodlight.openflow.types.IPv4Address;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080079import org.projectfloodlight.openflow.types.IPv6Address;
Jonathan Hart0e12fad2014-10-17 14:54:58 -070080import org.projectfloodlight.openflow.types.Masked;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080081import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070082import org.projectfloodlight.openflow.types.TransportPort;
sangho3f97a17d2015-01-29 22:56:29 -080083import org.projectfloodlight.openflow.types.U32;
Hyunsun Moona08c5d02015-07-14 17:53:00 -070084import org.projectfloodlight.openflow.types.U64;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070085import org.projectfloodlight.openflow.types.U8;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080086import org.projectfloodlight.openflow.types.VlanPcp;
alshabib6b5cfec2014-09-18 17:42:18 -070087import org.slf4j.Logger;
88
Saurav Dasffc5bbc2015-08-18 23:30:19 -070089import com.google.common.collect.Lists;
alshabib19fdc122014-10-03 11:38:19 -070090
alshabib1c319ff2014-10-04 20:29:09 -070091public class FlowEntryBuilder {
alshabib6b5cfec2014-09-18 17:42:18 -070092 private final Logger log = getLogger(getClass());
93
94 private final OFFlowStatsEntry stat;
95 private final OFFlowRemoved removed;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080096 private final OFFlowMod flowMod;
alshabib6b5cfec2014-09-18 17:42:18 -070097
98 private final Match match;
alshabib346b5b32015-03-06 00:42:16 -080099
Jonathan Hart67fc0972015-03-19 15:21:20 -0700100 // All actions are contained in an OFInstruction. For OF1.0
101 // the instruction type is apply instruction (immediate set in ONOS speak)
alshabib346b5b32015-03-06 00:42:16 -0800102 private final List<OFInstruction> instructions;
alshabib6b5cfec2014-09-18 17:42:18 -0700103
104 private final Dpid dpid;
105
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800106 public enum FlowType { STAT, REMOVED, MOD }
107
108 private final FlowType type;
alshabib6b5cfec2014-09-18 17:42:18 -0700109
alshabibbdcbb102015-04-22 14:16:38 -0700110 public FlowEntryBuilder(Dpid dpid, OFFlowStatsEntry entry) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800111 this.stat = entry;
112 this.match = entry.getMatch();
alshabib346b5b32015-03-06 00:42:16 -0800113 this.instructions = getInstructions(entry);
Saurav Dasfa2fa932015-03-03 11:29:48 -0800114 this.dpid = dpid;
115 this.removed = null;
116 this.flowMod = null;
117 this.type = FlowType.STAT;
Saurav Dasfa2fa932015-03-03 11:29:48 -0800118 }
119
alshabibbdcbb102015-04-22 14:16:38 -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
alshabibbdcbb102015-04-22 14:16:38 -0700132 public FlowEntryBuilder(Dpid dpid, OFFlowMod fm) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800133 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:
alshabibbdcbb102015-04-22 14:16:38 -0700146 rule = DefaultFlowRule.builder()
147 .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
148 .withSelector(buildSelector())
149 .withTreatment(buildTreatment())
150 .withPriority(stat.getPriority())
151 .makeTemporary(stat.getIdleTimeout())
152 .withCookie(stat.getCookie().getValue())
153 .forTable(stat.getTableId().getValue())
154 .build();
155
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800156 return new DefaultFlowEntry(rule, FlowEntryState.ADDED,
157 stat.getDurationSec(), stat.getPacketCount().getValue(),
158 stat.getByteCount().getValue());
159 case REMOVED:
alshabibbdcbb102015-04-22 14:16:38 -0700160 rule = DefaultFlowRule.builder()
161 .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
162 .withSelector(buildSelector())
163 .withPriority(removed.getPriority())
164 .makeTemporary(removed.getIdleTimeout())
165 .withCookie(removed.getCookie().getValue())
166 .forTable(removed.getTableId().getValue())
167 .build();
168
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800169 return new DefaultFlowEntry(rule, FlowEntryState.REMOVED, removed.getDurationSec(),
170 removed.getPacketCount().getValue(), removed.getByteCount().getValue());
171 case MOD:
172 FlowEntryState flowState = state.length > 0 ? state[0] : FlowEntryState.FAILED;
alshabibbdcbb102015-04-22 14:16:38 -0700173 rule = DefaultFlowRule.builder()
174 .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
175 .withSelector(buildSelector())
176 .withTreatment(buildTreatment())
177 .withPriority(flowMod.getPriority())
178 .makeTemporary(flowMod.getIdleTimeout())
179 .withCookie(flowMod.getCookie().getValue())
180 .forTable(flowMod.getTableId().getValue())
181 .build();
182
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800183 return new DefaultFlowEntry(rule, flowState, 0, 0, 0);
184 default:
185 log.error("Unknown flow type : {}", this.type);
186 return null;
alshabib6b5cfec2014-09-18 17:42:18 -0700187 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800188
alshabib6b5cfec2014-09-18 17:42:18 -0700189 }
190
alshabib346b5b32015-03-06 00:42:16 -0800191 private List<OFInstruction> getInstructions(OFFlowMod entry) {
alshabib19fdc122014-10-03 11:38:19 -0700192 switch (entry.getVersion()) {
193 case OF_10:
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700194 return Lists.newArrayList(OFFactoryVer13.INSTANCE.instructions()
195 .applyActions(
196 entry.getActions()));
alshabib19fdc122014-10-03 11:38:19 -0700197 case OF_11:
198 case OF_12:
199 case OF_13:
alshabib346b5b32015-03-06 00:42:16 -0800200 return entry.getInstructions();
201 default:
202 log.warn("Unknown OF version {}", entry.getVersion());
203 }
204 return Lists.newLinkedList();
205 }
206
207 private List<OFInstruction> getInstructions(OFFlowStatsEntry entry) {
208 switch (entry.getVersion()) {
209 case OF_10:
210 return Lists.newArrayList(
211 OFFactoryVer13.INSTANCE.instructions().applyActions(entry.getActions()));
212 case OF_11:
213 case OF_12:
214 case OF_13:
215 return entry.getInstructions();
alshabib19fdc122014-10-03 11:38:19 -0700216 default:
217 log.warn("Unknown OF version {}", entry.getVersion());
218 }
219 return Lists.newLinkedList();
220 }
alshabib6b5cfec2014-09-18 17:42:18 -0700221
222 private TrafficTreatment buildTreatment() {
tom9a693fd2014-10-03 11:32:19 -0700223 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700224 // If this is a drop rule
alshabib346b5b32015-03-06 00:42:16 -0800225 if (instructions.size() == 0) {
alshabib010c31d2014-09-26 10:01:12 -0700226 builder.drop();
alshabib6b5cfec2014-09-18 17:42:18 -0700227 return builder.build();
228 }
alshabib346b5b32015-03-06 00:42:16 -0800229 for (OFInstruction in : instructions) {
230 switch (in.getType()) {
231 case GOTO_TABLE:
alshabibbdcbb102015-04-22 14:16:38 -0700232 builder.transition(((int) ((OFInstructionGotoTable) in)
233 .getTableId().getValue()));
alshabib346b5b32015-03-06 00:42:16 -0800234 break;
235 case WRITE_METADATA:
Saurav Das86af8f12015-05-25 23:55:33 -0700236 OFInstructionWriteMetadata m = (OFInstructionWriteMetadata) in;
237 builder.writeMetadata(m.getMetadata().getValue(),
238 m.getMetadataMask().getValue());
alshabib346b5b32015-03-06 00:42:16 -0800239 break;
240 case WRITE_ACTIONS:
241 builder.deferred();
242 buildActions(((OFInstructionWriteActions) in).getActions(),
243 builder);
244 break;
245 case APPLY_ACTIONS:
246 builder.immediate();
247 buildActions(((OFInstructionApplyActions) in).getActions(),
248 builder);
249 break;
250 case CLEAR_ACTIONS:
251 builder.wipeDeferred();
252 break;
253 case EXPERIMENTER:
254 break;
255 case METER:
256 break;
257 default:
258 log.warn("Unknown instructions type {}", in.getType());
alshabib6b5cfec2014-09-18 17:42:18 -0700259 }
260 }
261
262 return builder.build();
263 }
264
alshabib346b5b32015-03-06 00:42:16 -0800265 private TrafficTreatment.Builder buildActions(List<OFAction> actions,
266 TrafficTreatment.Builder builder) {
267 for (OFAction act : actions) {
268 switch (act.getType()) {
269 case OUTPUT:
270 OFActionOutput out = (OFActionOutput) act;
271 builder.setOutput(
272 PortNumber.portNumber(out.getPort().getPortNumber()));
273 break;
274 case SET_VLAN_VID:
275 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
276 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
277 break;
278 case SET_VLAN_PCP:
279 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
280 builder.setVlanPcp(pcp.getVlanPcp().getValue());
281 break;
282 case SET_DL_DST:
283 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
284 builder.setEthDst(
285 MacAddress.valueOf(dldst.getDlAddr().getLong()));
286 break;
287 case SET_DL_SRC:
288 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
289 builder.setEthSrc(
290 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
291
292 break;
293 case SET_NW_DST:
294 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
295 IPv4Address di = nwdst.getNwAddr();
296 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
297 break;
298 case SET_NW_SRC:
299 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
300 IPv4Address si = nwsrc.getNwAddr();
301 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
302 break;
303 case EXPERIMENTER:
304 OFActionExperimenter exp = (OFActionExperimenter) act;
305 if (exp.getExperimenter() == 0x80005A06 ||
306 exp.getExperimenter() == 0x748771) {
307 OFActionCircuit ct = (OFActionCircuit) exp;
Sho SHIMIZU9553bb82015-06-30 16:02:45 -0700308 short lambda = ((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber();
309 builder.add(Instructions.modL0Lambda(Lambda.indexedLambda(lambda)));
alshabib346b5b32015-03-06 00:42:16 -0800310 } else {
311 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
312 }
313 break;
314 case SET_FIELD:
315 OFActionSetField setField = (OFActionSetField) act;
316 handleSetField(builder, setField.getField());
317 break;
318 case POP_MPLS:
319 OFActionPopMpls popMpls = (OFActionPopMpls) act;
320 builder.popMpls((short) popMpls.getEthertype().getValue());
321 break;
322 case PUSH_MPLS:
323 builder.pushMpls();
324 break;
325 case COPY_TTL_IN:
326 builder.copyTtlIn();
327 break;
328 case COPY_TTL_OUT:
329 builder.copyTtlOut();
330 break;
331 case DEC_MPLS_TTL:
332 builder.decMplsTtl();
333 break;
334 case DEC_NW_TTL:
335 builder.decNwTtl();
336 break;
337 case GROUP:
338 OFActionGroup group = (OFActionGroup) act;
339 builder.group(new DefaultGroupId(group.getGroup().getGroupNumber()));
340 break;
Jonathan Hart67fc0972015-03-19 15:21:20 -0700341 case STRIP_VLAN:
alshabib346b5b32015-03-06 00:42:16 -0800342 case POP_VLAN:
343 builder.popVlan();
344 break;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700345 case PUSH_VLAN:
346 builder.pushVlan();
347 break;
alshabib346b5b32015-03-06 00:42:16 -0800348 case SET_TP_DST:
349 case SET_TP_SRC:
350 case POP_PBB:
351 case PUSH_PBB:
alshabib346b5b32015-03-06 00:42:16 -0800352 case SET_MPLS_LABEL:
353 case SET_MPLS_TC:
354 case SET_MPLS_TTL:
355 case SET_NW_ECN:
356 case SET_NW_TOS:
357 case SET_NW_TTL:
358 case SET_QUEUE:
359
360 case ENQUEUE:
361 default:
362 log.warn("Action type {} not yet implemented.", act.getType());
363 }
364 }
365 return builder;
366 }
367
368
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800369 private void handleSetField(TrafficTreatment.Builder builder, OFOxm<?> oxm) {
370 switch (oxm.getMatchField().id) {
371 case VLAN_PCP:
372 @SuppressWarnings("unchecked")
373 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
374 builder.setVlanPcp(vlanpcp.getValue().getValue());
375 break;
376 case VLAN_VID:
377 @SuppressWarnings("unchecked")
378 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
379 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
380 break;
381 case ETH_DST:
382 @SuppressWarnings("unchecked")
383 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
384 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
385 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
386 break;
387 case ETH_SRC:
388 @SuppressWarnings("unchecked")
389 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
390 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
391 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
392 break;
393 case IPV4_DST:
394 @SuppressWarnings("unchecked")
395 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
396 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
397 break;
398 case IPV4_SRC:
399 @SuppressWarnings("unchecked")
400 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
401 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
402 break;
sangho3f97a17d2015-01-29 22:56:29 -0800403 case MPLS_LABEL:
404 @SuppressWarnings("unchecked")
405 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100406 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
sangho3f97a17d2015-01-29 22:56:29 -0800407 break;
Saurav Das73a7dd42015-08-19 22:20:31 -0700408 case MPLS_BOS:
409 @SuppressWarnings("unchecked")
410 OFOxm<U8> mplsBos = (OFOxm<U8>) oxm;
411 builder.setMplsBos(mplsBos.getValue() == U8.ZERO ? false : true);
412 break;
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700413 case TUNNEL_ID:
414 @SuppressWarnings("unchecked")
415 OFOxm<U64> tunnelId = (OFOxm<U64>) oxm;
416 builder.setTunnelId(tunnelId.getValue().getValue());
417 break;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700418 case TCP_DST:
419 @SuppressWarnings("unchecked")
420 OFOxm<TransportPort> tcpdst = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700421 builder.setTcpDst(TpPort.tpPort(tcpdst.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700422 break;
423 case TCP_SRC:
424 @SuppressWarnings("unchecked")
425 OFOxm<TransportPort> tcpsrc = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700426 builder.setTcpSrc(TpPort.tpPort(tcpsrc.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700427 break;
428 case UDP_DST:
429 @SuppressWarnings("unchecked")
430 OFOxm<TransportPort> udpdst = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700431 builder.setUdpDst(TpPort.tpPort(udpdst.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700432 break;
433 case UDP_SRC:
434 @SuppressWarnings("unchecked")
435 OFOxm<TransportPort> udpsrc = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700436 builder.setUdpSrc(TpPort.tpPort(udpsrc.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700437 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800438 case ARP_OP:
439 case ARP_SHA:
440 case ARP_SPA:
441 case ARP_THA:
442 case ARP_TPA:
443 case BSN_EGR_PORT_GROUP_ID:
444 case BSN_GLOBAL_VRF_ALLOWED:
445 case BSN_IN_PORTS_128:
446 case BSN_L3_DST_CLASS_ID:
447 case BSN_L3_INTERFACE_CLASS_ID:
448 case BSN_L3_SRC_CLASS_ID:
449 case BSN_LAG_ID:
450 case BSN_TCP_FLAGS:
451 case BSN_UDF0:
452 case BSN_UDF1:
453 case BSN_UDF2:
454 case BSN_UDF3:
455 case BSN_UDF4:
456 case BSN_UDF5:
457 case BSN_UDF6:
458 case BSN_UDF7:
459 case BSN_VLAN_XLATE_PORT_GROUP_ID:
460 case BSN_VRF:
461 case ETH_TYPE:
462 case ICMPV4_CODE:
463 case ICMPV4_TYPE:
464 case ICMPV6_CODE:
465 case ICMPV6_TYPE:
466 case IN_PHY_PORT:
467 case IN_PORT:
468 case IPV6_DST:
469 case IPV6_FLABEL:
470 case IPV6_ND_SLL:
471 case IPV6_ND_TARGET:
472 case IPV6_ND_TLL:
473 case IPV6_SRC:
474 case IP_DSCP:
475 case IP_ECN:
476 case IP_PROTO:
477 case METADATA:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800478 case MPLS_TC:
479 case OCH_SIGID:
480 case OCH_SIGID_BASIC:
481 case OCH_SIGTYPE:
482 case OCH_SIGTYPE_BASIC:
483 case SCTP_DST:
484 case SCTP_SRC:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800485 default:
486 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
487 break;
488 }
489 }
490
Sho SHIMIZU6f1b09e2015-05-05 11:26:22 -0700491 // CHECKSTYLE IGNORE MethodLength FOR NEXT 1 LINES
alshabib6b5cfec2014-09-18 17:42:18 -0700492 private TrafficSelector buildSelector() {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800493 MacAddress mac;
494 Ip4Prefix ip4Prefix;
495 Ip6Address ip6Address;
496 Ip6Prefix ip6Prefix;
497
tom9a693fd2014-10-03 11:32:19 -0700498 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700499 for (MatchField<?> field : match.getMatchFields()) {
500 switch (field.id) {
501 case IN_PORT:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800502 builder.matchInPort(PortNumber
alshabib010c31d2014-09-26 10:01:12 -0700503 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700504 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800505 case IN_PHY_PORT:
506 builder.matchInPhyPort(PortNumber
507 .portNumber(match.get(MatchField.IN_PHY_PORT).getPortNumber()));
508 break;
509 case METADATA:
510 long metadata =
511 match.get(MatchField.METADATA).getValue().getValue();
512 builder.matchMetadata(metadata);
513 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700514 case ETH_DST:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800515 mac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
516 builder.matchEthDst(mac);
517 break;
518 case ETH_SRC:
519 mac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
520 builder.matchEthSrc(mac);
alshabib6b5cfec2014-09-18 17:42:18 -0700521 break;
522 case ETH_TYPE:
523 int ethType = match.get(MatchField.ETH_TYPE).getValue();
sanghoc0b3c342015-03-12 17:12:10 -0700524 if (ethType == EthType.VLAN_FRAME.getValue()) {
525 builder.matchVlanId(VlanId.ANY);
526 } else {
527 builder.matchEthType((short) ethType);
528 }
alshabib6b5cfec2014-09-18 17:42:18 -0700529 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700530 case VLAN_VID:
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800531 VlanId vlanId = null;
532 if (match.isPartiallyMasked(MatchField.VLAN_VID)) {
533 Masked<OFVlanVidMatch> masked = match.getMasked(MatchField.VLAN_VID);
534 if (masked.getValue().equals(OFVlanVidMatch.PRESENT)
535 && masked.getMask().equals(OFVlanVidMatch.PRESENT)) {
536 vlanId = VlanId.ANY;
537 }
538 } else {
Jonathan Hart1468fee2015-07-16 18:50:34 -0700539 if (!match.get(MatchField.VLAN_VID).isPresentBitSet()) {
540 vlanId = VlanId.NONE;
541 } else {
542 vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
543 }
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800544 }
545 if (vlanId != null) {
546 builder.matchVlanId(vlanId);
547 }
alshabib6b5cfec2014-09-18 17:42:18 -0700548 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800549 case VLAN_PCP:
550 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
551 builder.matchVlanPcp(vlanPcp);
552 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800553 case IP_DSCP:
554 byte ipDscp = match.get(MatchField.IP_DSCP).getDscpValue();
555 builder.matchIPDscp(ipDscp);
556 break;
557 case IP_ECN:
558 byte ipEcn = match.get(MatchField.IP_ECN).getEcnValue();
559 builder.matchIPEcn(ipEcn);
560 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800561 case IP_PROTO:
562 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
563 builder.matchIPProtocol((byte) proto);
564 break;
565 case IPV4_SRC:
566 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
567 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800568 ip4Prefix = Ip4Prefix.valueOf(
569 maskedIp.getValue().getInt(),
570 maskedIp.getMask().asCidrMaskLength());
571 } else {
572 ip4Prefix = Ip4Prefix.valueOf(
573 match.get(MatchField.IPV4_SRC).getInt(),
574 Ip4Prefix.MAX_MASK_LENGTH);
575 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800576 builder.matchIPSrc(ip4Prefix);
577 break;
578 case IPV4_DST:
579 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
580 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800581 ip4Prefix = Ip4Prefix.valueOf(
582 maskedIp.getValue().getInt(),
583 maskedIp.getMask().asCidrMaskLength());
584 } else {
585 ip4Prefix = Ip4Prefix.valueOf(
586 match.get(MatchField.IPV4_DST).getInt(),
587 Ip4Prefix.MAX_MASK_LENGTH);
588 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800589 builder.matchIPDst(ip4Prefix);
Jonathan Hart34bc6142014-10-17 11:00:43 -0700590 break;
591 case TCP_SRC:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700592 builder.matchTcpSrc(TpPort.tpPort(match.get(MatchField.TCP_SRC).getPort()));
Jonathan Hart34bc6142014-10-17 11:00:43 -0700593 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800594 case TCP_DST:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700595 builder.matchTcpDst(TpPort.tpPort(match.get(MatchField.TCP_DST).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800596 break;
597 case UDP_SRC:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700598 builder.matchUdpSrc(TpPort.tpPort(match.get(MatchField.UDP_SRC).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800599 break;
600 case UDP_DST:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700601 builder.matchUdpDst(TpPort.tpPort(match.get(MatchField.UDP_DST).getPort()));
Sho SHIMIZU6f1b09e2015-05-05 11:26:22 -0700602 break;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100603 case MPLS_LABEL:
604 builder.matchMplsLabel(MplsLabel.mplsLabel((int) match.get(MatchField.MPLS_LABEL)
605 .getValue()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800606 break;
Saurav Dasffc5bbc2015-08-18 23:30:19 -0700607 case MPLS_BOS:
608 builder.matchMplsBos(match.get(MatchField.MPLS_BOS).getValue());
609 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800610 case SCTP_SRC:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700611 builder.matchSctpSrc(TpPort.tpPort(match.get(MatchField.SCTP_SRC).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800612 break;
613 case SCTP_DST:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700614 builder.matchSctpDst(TpPort.tpPort(match.get(MatchField.SCTP_DST).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800615 break;
616 case ICMPV4_TYPE:
617 byte icmpType = (byte) match.get(MatchField.ICMPV4_TYPE).getType();
618 builder.matchIcmpType(icmpType);
619 break;
620 case ICMPV4_CODE:
621 byte icmpCode = (byte) match.get(MatchField.ICMPV4_CODE).getCode();
622 builder.matchIcmpCode(icmpCode);
623 break;
624 case IPV6_SRC:
625 if (match.isPartiallyMasked(MatchField.IPV6_SRC)) {
626 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_SRC);
627 ip6Prefix = Ip6Prefix.valueOf(
628 maskedIp.getValue().getBytes(),
629 maskedIp.getMask().asCidrMaskLength());
630 } else {
631 ip6Prefix = Ip6Prefix.valueOf(
632 match.get(MatchField.IPV6_SRC).getBytes(),
633 Ip6Prefix.MAX_MASK_LENGTH);
634 }
635 builder.matchIPv6Src(ip6Prefix);
636 break;
637 case IPV6_DST:
638 if (match.isPartiallyMasked(MatchField.IPV6_DST)) {
639 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_DST);
640 ip6Prefix = Ip6Prefix.valueOf(
641 maskedIp.getValue().getBytes(),
642 maskedIp.getMask().asCidrMaskLength());
643 } else {
644 ip6Prefix = Ip6Prefix.valueOf(
645 match.get(MatchField.IPV6_DST).getBytes(),
646 Ip6Prefix.MAX_MASK_LENGTH);
647 }
648 builder.matchIPv6Dst(ip6Prefix);
649 break;
650 case IPV6_FLABEL:
651 int flowLabel =
652 match.get(MatchField.IPV6_FLABEL).getIPv6FlowLabelValue();
653 builder.matchIPv6FlowLabel(flowLabel);
654 break;
655 case ICMPV6_TYPE:
656 byte icmpv6type = (byte) match.get(MatchField.ICMPV6_TYPE).getValue();
657 builder.matchIcmpv6Type(icmpv6type);
658 break;
659 case ICMPV6_CODE:
660 byte icmpv6code = (byte) match.get(MatchField.ICMPV6_CODE).getValue();
661 builder.matchIcmpv6Code(icmpv6code);
662 break;
663 case IPV6_ND_TARGET:
664 ip6Address =
665 Ip6Address.valueOf(match.get(MatchField.IPV6_ND_TARGET).getBytes());
666 builder.matchIPv6NDTargetAddress(ip6Address);
667 break;
668 case IPV6_ND_SLL:
669 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_SLL).getLong());
670 builder.matchIPv6NDSourceLinkLayerAddress(mac);
671 break;
672 case IPV6_ND_TLL:
673 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_TLL).getLong());
674 builder.matchIPv6NDTargetLinkLayerAddress(mac);
675 break;
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800676 case IPV6_EXTHDR:
Charles M.C. Chan2184de12015-04-26 02:24:53 +0800677 builder.matchIPv6ExthdrFlags((short) match.get(MatchField.IPV6_EXTHDR)
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700678 .getValue());
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800679 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700680 case OCH_SIGID:
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700681 CircuitSignalID sigId = match.get(MatchField.OCH_SIGID);
682 builder.add(matchLambda(Lambda.ochSignal(
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700683 lookupGridType(sigId.getGridType()), lookupChannelSpacing(sigId.getChannelSpacing()),
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -0700684 sigId.getChannelNumber(), sigId.getSpectralWidth())
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700685 ));
Marc De Leenheer49087752014-10-23 13:54:09 -0700686 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800687 case OCH_SIGTYPE:
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700688 U8 sigType = match.get(MatchField.OCH_SIGTYPE);
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700689 builder.add(matchOchSignalType(lookupOchSignalType((byte) sigType.getValue())));
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800690 break;
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700691 case TUNNEL_ID:
692 long tunnelId = match.get(MatchField.TUNNEL_ID).getValue();
693 builder.matchTunnelId(tunnelId);
694 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700695 case ARP_OP:
696 case ARP_SHA:
697 case ARP_SPA:
698 case ARP_THA:
699 case ARP_TPA:
alshabib6b5cfec2014-09-18 17:42:18 -0700700 case MPLS_TC:
alshabib6b5cfec2014-09-18 17:42:18 -0700701 default:
702 log.warn("Match type {} not yet implemented.", field.id);
alshabib6b5cfec2014-09-18 17:42:18 -0700703 }
704 }
705 return builder.build();
706 }
alshabib6b5cfec2014-09-18 17:42:18 -0700707}