blob: 2256a7fca1674950e65ea1ea1e77754d6eac6f21 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present 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 */
Thomas Vachuska95caba32016-04-04 10:42:05 -070016package org.onosproject.provider.of.flow.util;
alshabib6b5cfec2014-09-18 17:42:18 -070017
Jonathan Hart8cf212a2015-10-29 17:42:03 -070018import com.google.common.collect.Lists;
Ray Milkey84d5a292016-02-22 14:04:01 -080019import org.onlab.packet.EthType;
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;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070026import org.onlab.packet.TpPort;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080027import org.onlab.packet.VlanId;
sangho8995ac52015-02-04 11:29:03 -080028import org.onosproject.core.DefaultGroupId;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.DeviceId;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070030import org.onosproject.net.Lambda;
Yafit Hadar73514612015-11-04 10:14:21 +020031import org.onosproject.net.OduSignalId;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.PortNumber;
Jonathan Hart3c259162015-10-21 21:31:19 -070033import org.onosproject.net.driver.DefaultDriverData;
34import org.onosproject.net.driver.DefaultDriverHandler;
35import org.onosproject.net.driver.Driver;
36import org.onosproject.net.driver.DriverHandler;
37import org.onosproject.net.driver.DriverService;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.flow.DefaultFlowEntry;
39import org.onosproject.net.flow.DefaultFlowRule;
40import org.onosproject.net.flow.DefaultTrafficSelector;
41import org.onosproject.net.flow.DefaultTrafficTreatment;
42import org.onosproject.net.flow.FlowEntry;
43import org.onosproject.net.flow.FlowEntry.FlowEntryState;
44import org.onosproject.net.flow.FlowRule;
45import org.onosproject.net.flow.TrafficSelector;
46import org.onosproject.net.flow.TrafficTreatment;
Charles Chan14967c22015-12-07 11:11:50 -080047import org.onosproject.net.flow.criteria.ExtensionSelectorType.ExtensionSelectorTypes;
Sho SHIMIZU9553bb82015-06-30 16:02:45 -070048import org.onosproject.net.flow.instructions.Instructions;
Charles Chan14967c22015-12-07 11:11:50 -080049import org.onosproject.openflow.controller.ExtensionSelectorInterpreter;
alshabib880b6442015-11-23 22:13:04 -080050import org.onosproject.openflow.controller.ExtensionTreatmentInterpreter;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080051import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabib6b5cfec2014-09-18 17:42:18 -070052import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
53import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
Charles Chan14967c22015-12-07 11:11:50 -080054import org.projectfloodlight.openflow.protocol.OFMatchV3;
Brian O'Connor21564612015-12-05 19:24:59 -080055import org.projectfloodlight.openflow.protocol.OFVersion;
alshabib6b5cfec2014-09-18 17:42:18 -070056import org.projectfloodlight.openflow.protocol.action.OFAction;
Marc De Leenheer49087752014-10-23 13:54:09 -070057import org.projectfloodlight.openflow.protocol.action.OFActionCircuit;
Steffen Gebertba2d3b72015-10-22 11:14:31 +020058import org.projectfloodlight.openflow.protocol.action.OFActionEnqueue;
Marc De Leenheer49087752014-10-23 13:54:09 -070059import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter;
sangho8995ac52015-02-04 11:29:03 -080060import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
alshabib6b5cfec2014-09-18 17:42:18 -070061import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
sangho3f97a17d2015-01-29 22:56:29 -080062import org.projectfloodlight.openflow.protocol.action.OFActionPopMpls;
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -050063import org.projectfloodlight.openflow.protocol.action.OFActionPushVlan;
alshabib6b5cfec2014-09-18 17:42:18 -070064import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst;
65import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080066import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
alshabib6b5cfec2014-09-18 17:42:18 -070067import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst;
68import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc;
Jonathan Hart8cf212a2015-10-29 17:42:03 -070069import org.projectfloodlight.openflow.protocol.action.OFActionSetQueue;
alshabib6b5cfec2014-09-18 17:42:18 -070070import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp;
71import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid;
alshabib19fdc122014-10-03 11:38:19 -070072import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hartcf783202014-11-24 18:55:42 -080073import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions;
alshabibbdcbb102015-04-22 14:16:38 -070074import org.projectfloodlight.openflow.protocol.instruction.OFInstructionGotoTable;
alshabib346b5b32015-03-06 00:42:16 -080075import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteActions;
Saurav Das86af8f12015-05-25 23:55:33 -070076import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteMetadata;
alshabib6b5cfec2014-09-18 17:42:18 -070077import org.projectfloodlight.openflow.protocol.match.Match;
78import org.projectfloodlight.openflow.protocol.match.MatchField;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080079import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
Yafit Hadar4cc65f72016-02-10 11:23:06 +020080import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigid;
alshabib346b5b32015-03-06 00:42:16 -080081import org.projectfloodlight.openflow.protocol.ver13.OFFactoryVer13;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070082import org.projectfloodlight.openflow.types.CircuitSignalID;
alshabib6b5cfec2014-09-18 17:42:18 -070083import org.projectfloodlight.openflow.types.IPv4Address;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080084import org.projectfloodlight.openflow.types.IPv6Address;
Jonathan Hart0e12fad2014-10-17 14:54:58 -070085import org.projectfloodlight.openflow.types.Masked;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080086import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Jonathan Harte4e74f02016-03-03 12:57:40 -080087import org.projectfloodlight.openflow.types.OduSignalID;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070088import org.projectfloodlight.openflow.types.TransportPort;
sangho3f97a17d2015-01-29 22:56:29 -080089import org.projectfloodlight.openflow.types.U32;
Hyunsun Moona08c5d02015-07-14 17:53:00 -070090import org.projectfloodlight.openflow.types.U64;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070091import org.projectfloodlight.openflow.types.U8;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080092import org.projectfloodlight.openflow.types.VlanPcp;
alshabib6b5cfec2014-09-18 17:42:18 -070093import org.slf4j.Logger;
Jian Lia0d9a172016-04-01 16:58:06 -070094import org.slf4j.LoggerFactory;
alshabib6b5cfec2014-09-18 17:42:18 -070095
Jonathan Hart8cf212a2015-10-29 17:42:03 -070096import java.util.List;
97
Thiago Santos877914d2016-07-20 18:29:29 -030098import static java.util.concurrent.TimeUnit.NANOSECONDS;
99import static java.util.concurrent.TimeUnit.SECONDS;
alshabib44852fb2016-04-15 15:56:51 -0700100import static org.onosproject.net.flow.criteria.Criteria.*;
Yafit Hadar73514612015-11-04 10:14:21 +0200101import static org.onosproject.net.flow.instructions.Instructions.modL0Lambda;
102import static org.onosproject.net.flow.instructions.Instructions.modL1OduSignalId;
alshabib44852fb2016-04-15 15:56:51 -0700103import static org.onosproject.provider.of.flow.util.OpenFlowValueMapper.*;
alshabib19fdc122014-10-03 11:38:19 -0700104
alshabib1c319ff2014-10-04 20:29:09 -0700105public class FlowEntryBuilder {
Jian Lia0d9a172016-04-01 16:58:06 -0700106 private static final Logger log = LoggerFactory.getLogger(FlowEntryBuilder.class);
alshabib6b5cfec2014-09-18 17:42:18 -0700107
108 private final OFFlowStatsEntry stat;
109 private final OFFlowRemoved removed;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800110 private final OFFlowMod flowMod;
alshabib6b5cfec2014-09-18 17:42:18 -0700111
112 private final Match match;
alshabib346b5b32015-03-06 00:42:16 -0800113
Jonathan Hart67fc0972015-03-19 15:21:20 -0700114 // All actions are contained in an OFInstruction. For OF1.0
115 // the instruction type is apply instruction (immediate set in ONOS speak)
alshabib346b5b32015-03-06 00:42:16 -0800116 private final List<OFInstruction> instructions;
alshabib6b5cfec2014-09-18 17:42:18 -0700117
Jonathan Harte4e74f02016-03-03 12:57:40 -0800118 private final DeviceId deviceId;
alshabib6b5cfec2014-09-18 17:42:18 -0700119
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800120 public enum FlowType { STAT, REMOVED, MOD }
121
122 private final FlowType type;
alshabib6b5cfec2014-09-18 17:42:18 -0700123
Jonathan Hart3c259162015-10-21 21:31:19 -0700124 private final DriverService driverService;
125
Jonathan Harte4e74f02016-03-03 12:57:40 -0800126 public FlowEntryBuilder(DeviceId deviceId, OFFlowStatsEntry entry, DriverService driverService) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800127 this.stat = entry;
128 this.match = entry.getMatch();
alshabib346b5b32015-03-06 00:42:16 -0800129 this.instructions = getInstructions(entry);
Jonathan Harte4e74f02016-03-03 12:57:40 -0800130 this.deviceId = deviceId;
Saurav Dasfa2fa932015-03-03 11:29:48 -0800131 this.removed = null;
132 this.flowMod = null;
133 this.type = FlowType.STAT;
Jonathan Hart3c259162015-10-21 21:31:19 -0700134 this.driverService = driverService;
Saurav Dasfa2fa932015-03-03 11:29:48 -0800135 }
136
Jonathan Harte4e74f02016-03-03 12:57:40 -0800137 public FlowEntryBuilder(DeviceId deviceId, OFFlowRemoved removed, DriverService driverService) {
alshabib6b5cfec2014-09-18 17:42:18 -0700138 this.match = removed.getMatch();
139 this.removed = removed;
Jonathan Harte4e74f02016-03-03 12:57:40 -0800140 this.deviceId = deviceId;
alshabib346b5b32015-03-06 00:42:16 -0800141 this.instructions = null;
alshabib6b5cfec2014-09-18 17:42:18 -0700142 this.stat = null;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800143 this.flowMod = null;
144 this.type = FlowType.REMOVED;
Jonathan Hart3c259162015-10-21 21:31:19 -0700145 this.driverService = driverService;
alshabib6b5cfec2014-09-18 17:42:18 -0700146 }
147
Jonathan Harte4e74f02016-03-03 12:57:40 -0800148 public FlowEntryBuilder(DeviceId deviceId, OFFlowMod fm, DriverService driverService) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800149 this.match = fm.getMatch();
Jonathan Harte4e74f02016-03-03 12:57:40 -0800150 this.deviceId = deviceId;
alshabib346b5b32015-03-06 00:42:16 -0800151 this.instructions = getInstructions(fm);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800152 this.type = FlowType.MOD;
153 this.flowMod = fm;
154 this.stat = null;
155 this.removed = null;
Jonathan Hart3c259162015-10-21 21:31:19 -0700156 this.driverService = driverService;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800157 }
alshabib1c319ff2014-10-04 20:29:09 -0700158
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800159 public FlowEntry build(FlowEntryState... state) {
Brian O'Connor21564612015-12-05 19:24:59 -0800160 FlowRule.Builder builder;
161 try {
162 switch (this.type) {
163 case STAT:
164 builder = DefaultFlowRule.builder()
Jonathan Harte4e74f02016-03-03 12:57:40 -0800165 .forDevice(deviceId)
Brian O'Connor21564612015-12-05 19:24:59 -0800166 .withSelector(buildSelector())
167 .withTreatment(buildTreatment())
168 .withPriority(stat.getPriority())
169 .makeTemporary(stat.getIdleTimeout())
Jonathan Hartd1ce4b02016-01-11 13:28:36 -0800170 .withCookie(stat.getCookie().getValue());
171 if (stat.getVersion() != OFVersion.OF_10) {
172 builder.forTable(stat.getTableId().getValue());
173 }
alshabibbdcbb102015-04-22 14:16:38 -0700174
Brian O'Connor21564612015-12-05 19:24:59 -0800175 return new DefaultFlowEntry(builder.build(), FlowEntryState.ADDED,
Thiago Santos877914d2016-07-20 18:29:29 -0300176 SECONDS.toNanos(stat.getDurationSec())
177 + stat.getDurationNsec(), NANOSECONDS,
Brian O'Connor21564612015-12-05 19:24:59 -0800178 stat.getPacketCount().getValue(),
179 stat.getByteCount().getValue());
180 case REMOVED:
181 builder = DefaultFlowRule.builder()
Jonathan Harte4e74f02016-03-03 12:57:40 -0800182 .forDevice(deviceId)
Brian O'Connor21564612015-12-05 19:24:59 -0800183 .withSelector(buildSelector())
184 .withPriority(removed.getPriority())
185 .makeTemporary(removed.getIdleTimeout())
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700186 .withCookie(removed.getCookie().getValue())
187 .withReason(FlowRule.FlowRemoveReason.parseShort(removed.getReason()));
188
Brian O'Connor21564612015-12-05 19:24:59 -0800189 if (removed.getVersion() != OFVersion.OF_10) {
190 builder.forTable(removed.getTableId().getValue());
191 }
alshabibbdcbb102015-04-22 14:16:38 -0700192
Brian O'Connor21564612015-12-05 19:24:59 -0800193 return new DefaultFlowEntry(builder.build(), FlowEntryState.REMOVED,
Thiago Santos877914d2016-07-20 18:29:29 -0300194 SECONDS.toNanos(removed.getDurationSec())
195 + removed.getDurationNsec(), NANOSECONDS,
Brian O'Connor21564612015-12-05 19:24:59 -0800196 removed.getPacketCount().getValue(),
197 removed.getByteCount().getValue());
198 case MOD:
199 FlowEntryState flowState = state.length > 0 ? state[0] : FlowEntryState.FAILED;
200 builder = DefaultFlowRule.builder()
Jonathan Harte4e74f02016-03-03 12:57:40 -0800201 .forDevice(deviceId)
Brian O'Connor21564612015-12-05 19:24:59 -0800202 .withSelector(buildSelector())
203 .withTreatment(buildTreatment())
204 .withPriority(flowMod.getPriority())
205 .makeTemporary(flowMod.getIdleTimeout())
206 .withCookie(flowMod.getCookie().getValue());
207 if (flowMod.getVersion() != OFVersion.OF_10) {
208 builder.forTable(flowMod.getTableId().getValue());
209 }
alshabibbdcbb102015-04-22 14:16:38 -0700210
Brian O'Connor21564612015-12-05 19:24:59 -0800211 return new DefaultFlowEntry(builder.build(), flowState, 0, 0, 0);
212 default:
213 log.error("Unknown flow type : {}", this.type);
214 return null;
215 }
216 } catch (UnsupportedOperationException e) {
217 log.warn("Error building flow entry", e);
218 return null;
alshabib6b5cfec2014-09-18 17:42:18 -0700219 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800220
alshabib6b5cfec2014-09-18 17:42:18 -0700221 }
222
alshabib346b5b32015-03-06 00:42:16 -0800223 private List<OFInstruction> getInstructions(OFFlowMod entry) {
alshabib19fdc122014-10-03 11:38:19 -0700224 switch (entry.getVersion()) {
225 case OF_10:
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700226 return Lists.newArrayList(OFFactoryVer13.INSTANCE.instructions()
227 .applyActions(
228 entry.getActions()));
alshabib19fdc122014-10-03 11:38:19 -0700229 case OF_11:
230 case OF_12:
231 case OF_13:
alshabib346b5b32015-03-06 00:42:16 -0800232 return entry.getInstructions();
233 default:
234 log.warn("Unknown OF version {}", entry.getVersion());
235 }
236 return Lists.newLinkedList();
237 }
238
239 private List<OFInstruction> getInstructions(OFFlowStatsEntry entry) {
240 switch (entry.getVersion()) {
241 case OF_10:
242 return Lists.newArrayList(
243 OFFactoryVer13.INSTANCE.instructions().applyActions(entry.getActions()));
244 case OF_11:
245 case OF_12:
246 case OF_13:
247 return entry.getInstructions();
alshabib19fdc122014-10-03 11:38:19 -0700248 default:
249 log.warn("Unknown OF version {}", entry.getVersion());
250 }
251 return Lists.newLinkedList();
252 }
alshabib6b5cfec2014-09-18 17:42:18 -0700253
254 private TrafficTreatment buildTreatment() {
tom9a693fd2014-10-03 11:32:19 -0700255 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabib346b5b32015-03-06 00:42:16 -0800256 for (OFInstruction in : instructions) {
257 switch (in.getType()) {
258 case GOTO_TABLE:
alshabibbdcbb102015-04-22 14:16:38 -0700259 builder.transition(((int) ((OFInstructionGotoTable) in)
260 .getTableId().getValue()));
alshabib346b5b32015-03-06 00:42:16 -0800261 break;
262 case WRITE_METADATA:
Saurav Das86af8f12015-05-25 23:55:33 -0700263 OFInstructionWriteMetadata m = (OFInstructionWriteMetadata) in;
264 builder.writeMetadata(m.getMetadata().getValue(),
265 m.getMetadataMask().getValue());
alshabib346b5b32015-03-06 00:42:16 -0800266 break;
267 case WRITE_ACTIONS:
268 builder.deferred();
269 buildActions(((OFInstructionWriteActions) in).getActions(),
270 builder);
271 break;
272 case APPLY_ACTIONS:
273 builder.immediate();
274 buildActions(((OFInstructionApplyActions) in).getActions(),
275 builder);
276 break;
277 case CLEAR_ACTIONS:
278 builder.wipeDeferred();
279 break;
280 case EXPERIMENTER:
281 break;
282 case METER:
283 break;
284 default:
285 log.warn("Unknown instructions type {}", in.getType());
alshabib6b5cfec2014-09-18 17:42:18 -0700286 }
287 }
288
289 return builder.build();
290 }
291
Jian Lia0d9a172016-04-01 16:58:06 -0700292 /**
293 * Configures traffic treatment builder with a given collection of actions.
294 *
295 * @param actions a set of OpenFlow actions
296 * @param builder traffic treatment builder
297 * @param driverHandler driver handler
298 * @param deviceId device identifier
299 * @return configured traffic treatment builder
300 */
301 public static TrafficTreatment.Builder configureTreatmentBuilder(List<OFAction> actions,
302 TrafficTreatment.Builder builder,
303 DriverHandler driverHandler,
304 DeviceId deviceId) {
305 ExtensionTreatmentInterpreter interpreter;
Charles Chan14967c22015-12-07 11:11:50 -0800306 if (driverHandler.hasBehaviour(ExtensionTreatmentInterpreter.class)) {
Jian Lia0d9a172016-04-01 16:58:06 -0700307 interpreter = driverHandler.behaviour(ExtensionTreatmentInterpreter.class);
Charles Chan14967c22015-12-07 11:11:50 -0800308 } else {
Jian Lia0d9a172016-04-01 16:58:06 -0700309 interpreter = null;
Charles Chan14967c22015-12-07 11:11:50 -0800310 }
311
alshabib346b5b32015-03-06 00:42:16 -0800312 for (OFAction act : actions) {
313 switch (act.getType()) {
314 case OUTPUT:
315 OFActionOutput out = (OFActionOutput) act;
316 builder.setOutput(
317 PortNumber.portNumber(out.getPort().getPortNumber()));
318 break;
319 case SET_VLAN_VID:
320 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
321 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
322 break;
323 case SET_VLAN_PCP:
324 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
325 builder.setVlanPcp(pcp.getVlanPcp().getValue());
326 break;
327 case SET_DL_DST:
328 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
329 builder.setEthDst(
330 MacAddress.valueOf(dldst.getDlAddr().getLong()));
331 break;
332 case SET_DL_SRC:
333 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
334 builder.setEthSrc(
335 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
alshabib346b5b32015-03-06 00:42:16 -0800336 break;
337 case SET_NW_DST:
338 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
339 IPv4Address di = nwdst.getNwAddr();
340 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
341 break;
342 case SET_NW_SRC:
343 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
344 IPv4Address si = nwsrc.getNwAddr();
345 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
346 break;
347 case EXPERIMENTER:
348 OFActionExperimenter exp = (OFActionExperimenter) act;
349 if (exp.getExperimenter() == 0x80005A06 ||
350 exp.getExperimenter() == 0x748771) {
351 OFActionCircuit ct = (OFActionCircuit) exp;
Yafit Hadar4cc65f72016-02-10 11:23:06 +0200352 CircuitSignalID circuitSignalID = ((OFOxmOchSigid) ct.getField()).getValue();
353 builder.add(Instructions.modL0Lambda(Lambda.ochSignal(
354 lookupGridType(circuitSignalID.getGridType()),
355 lookupChannelSpacing(circuitSignalID.getChannelSpacing()),
356 circuitSignalID.getChannelNumber(), circuitSignalID.getSpectralWidth())));
Jian Lia0d9a172016-04-01 16:58:06 -0700357 } else if (interpreter != null) {
358 builder.extension(interpreter.mapAction(exp), deviceId);
alshabib346b5b32015-03-06 00:42:16 -0800359 } else {
360 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
361 }
362 break;
363 case SET_FIELD:
364 OFActionSetField setField = (OFActionSetField) act;
Jian Lia0d9a172016-04-01 16:58:06 -0700365 handleSetField(builder, setField, driverHandler, deviceId);
alshabib346b5b32015-03-06 00:42:16 -0800366 break;
367 case POP_MPLS:
368 OFActionPopMpls popMpls = (OFActionPopMpls) act;
Ray Milkey84d5a292016-02-22 14:04:01 -0800369 builder.popMpls(new EthType(popMpls.getEthertype().getValue()));
alshabib346b5b32015-03-06 00:42:16 -0800370 break;
371 case PUSH_MPLS:
372 builder.pushMpls();
373 break;
374 case COPY_TTL_IN:
375 builder.copyTtlIn();
376 break;
377 case COPY_TTL_OUT:
378 builder.copyTtlOut();
379 break;
380 case DEC_MPLS_TTL:
381 builder.decMplsTtl();
382 break;
383 case DEC_NW_TTL:
384 builder.decNwTtl();
385 break;
386 case GROUP:
387 OFActionGroup group = (OFActionGroup) act;
388 builder.group(new DefaultGroupId(group.getGroup().getGroupNumber()));
389 break;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200390 case SET_QUEUE:
391 OFActionSetQueue setQueue = (OFActionSetQueue) act;
392 builder.setQueue(setQueue.getQueueId());
393 break;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200394 case ENQUEUE:
395 OFActionEnqueue enqueue = (OFActionEnqueue) act;
Jian Liffef5002016-04-04 23:27:37 -0700396 builder.setQueue(enqueue.getQueueId(),
397 PortNumber.portNumber(enqueue.getPort().getPortNumber()));
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200398 break;
Jonathan Hart67fc0972015-03-19 15:21:20 -0700399 case STRIP_VLAN:
alshabib346b5b32015-03-06 00:42:16 -0800400 case POP_VLAN:
401 builder.popVlan();
402 break;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700403 case PUSH_VLAN:
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -0500404 OFActionPushVlan pushVlan = (OFActionPushVlan) act;
405 builder.pushVlan(new EthType((short) pushVlan.getEthertype().getValue()));
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700406 break;
alshabib346b5b32015-03-06 00:42:16 -0800407 case SET_TP_DST:
408 case SET_TP_SRC:
409 case POP_PBB:
410 case PUSH_PBB:
alshabib346b5b32015-03-06 00:42:16 -0800411 case SET_MPLS_LABEL:
412 case SET_MPLS_TC:
413 case SET_MPLS_TTL:
414 case SET_NW_ECN:
415 case SET_NW_TOS:
416 case SET_NW_TTL:
alshabib346b5b32015-03-06 00:42:16 -0800417
alshabib346b5b32015-03-06 00:42:16 -0800418 default:
419 log.warn("Action type {} not yet implemented.", act.getType());
420 }
421 }
422 return builder;
423 }
424
Jian Lia0d9a172016-04-01 16:58:06 -0700425 private TrafficTreatment.Builder buildActions(List<OFAction> actions,
426 TrafficTreatment.Builder builder) {
Jonathan Harte4e74f02016-03-03 12:57:40 -0800427 DriverHandler driverHandler = getDriver(deviceId);
Jian Lia0d9a172016-04-01 16:58:06 -0700428
429 return configureTreatmentBuilder(actions, builder, driverHandler, deviceId);
430 }
431
Charles Chancad338a2016-09-16 18:03:11 -0700432 // CHECKSTYLE IGNORE MethodLength FOR NEXT 1 LINES
Jian Lia0d9a172016-04-01 16:58:06 -0700433 private static void handleSetField(TrafficTreatment.Builder builder,
434 OFActionSetField action,
435 DriverHandler driverHandler,
436 DeviceId deviceId) {
Charles Chan14967c22015-12-07 11:11:50 -0800437 ExtensionTreatmentInterpreter treatmentInterpreter;
438 if (driverHandler.hasBehaviour(ExtensionTreatmentInterpreter.class)) {
439 treatmentInterpreter = driverHandler.behaviour(ExtensionTreatmentInterpreter.class);
440 } else {
441 treatmentInterpreter = null;
442 }
Jonathan Hart3c259162015-10-21 21:31:19 -0700443 OFOxm<?> oxm = action.getField();
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800444 switch (oxm.getMatchField().id) {
445 case VLAN_PCP:
446 @SuppressWarnings("unchecked")
447 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
448 builder.setVlanPcp(vlanpcp.getValue().getValue());
449 break;
450 case VLAN_VID:
Charles Chan4211baa2016-04-20 17:10:40 -0700451 if (treatmentInterpreter != null) {
452 try {
453 builder.extension(treatmentInterpreter.mapAction(action), deviceId);
454 break;
455 } catch (UnsupportedOperationException e) {
456 log.debug("Unsupported action extension; defaulting to native OF");
457 }
Charles Chan14967c22015-12-07 11:11:50 -0800458 }
alshabib44852fb2016-04-15 15:56:51 -0700459 @SuppressWarnings("unchecked")
460 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
461 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800462 break;
463 case ETH_DST:
464 @SuppressWarnings("unchecked")
465 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
466 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
467 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
468 break;
469 case ETH_SRC:
470 @SuppressWarnings("unchecked")
471 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
472 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
473 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
474 break;
475 case IPV4_DST:
476 @SuppressWarnings("unchecked")
477 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
478 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
479 break;
480 case IPV4_SRC:
481 @SuppressWarnings("unchecked")
482 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
483 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
484 break;
sangho3f97a17d2015-01-29 22:56:29 -0800485 case MPLS_LABEL:
486 @SuppressWarnings("unchecked")
487 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100488 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
sangho3f97a17d2015-01-29 22:56:29 -0800489 break;
Saurav Das73a7dd42015-08-19 22:20:31 -0700490 case MPLS_BOS:
491 @SuppressWarnings("unchecked")
492 OFOxm<U8> mplsBos = (OFOxm<U8>) oxm;
Sho SHIMIZU1375cfe2016-08-18 09:33:37 -0700493 builder.setMplsBos(mplsBos.getValue() != U8.ZERO);
Saurav Das73a7dd42015-08-19 22:20:31 -0700494 break;
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700495 case TUNNEL_ID:
496 @SuppressWarnings("unchecked")
497 OFOxm<U64> tunnelId = (OFOxm<U64>) oxm;
498 builder.setTunnelId(tunnelId.getValue().getValue());
499 break;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700500 case TCP_DST:
501 @SuppressWarnings("unchecked")
502 OFOxm<TransportPort> tcpdst = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700503 builder.setTcpDst(TpPort.tpPort(tcpdst.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700504 break;
505 case TCP_SRC:
506 @SuppressWarnings("unchecked")
507 OFOxm<TransportPort> tcpsrc = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700508 builder.setTcpSrc(TpPort.tpPort(tcpsrc.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700509 break;
510 case UDP_DST:
511 @SuppressWarnings("unchecked")
512 OFOxm<TransportPort> udpdst = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700513 builder.setUdpDst(TpPort.tpPort(udpdst.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700514 break;
515 case UDP_SRC:
516 @SuppressWarnings("unchecked")
517 OFOxm<TransportPort> udpsrc = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700518 builder.setUdpSrc(TpPort.tpPort(udpsrc.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700519 break;
Jonathan Hart3c259162015-10-21 21:31:19 -0700520 case TUNNEL_IPV4_DST:
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530521 case NSP:
522 case NSI:
523 case NSH_C1:
524 case NSH_C2:
525 case NSH_C3:
526 case NSH_C4:
527 case NSH_MDTYPE:
528 case NSH_NP:
529 case ENCAP_ETH_SRC:
530 case ENCAP_ETH_DST:
531 case ENCAP_ETH_TYPE:
532 case TUN_GPE_NP:
Jian Liffef5002016-04-04 23:27:37 -0700533 if (treatmentInterpreter != null) {
534 try {
535 builder.extension(treatmentInterpreter.mapAction(action), deviceId);
536 } catch (UnsupportedOperationException e) {
alshabib44852fb2016-04-15 15:56:51 -0700537 log.debug(e.getMessage());
Jian Liffef5002016-04-04 23:27:37 -0700538 }
Jonathan Hart3c259162015-10-21 21:31:19 -0700539 }
540 break;
Yafit Hadar73514612015-11-04 10:14:21 +0200541 case EXP_ODU_SIG_ID:
542 @SuppressWarnings("unchecked")
543 OFOxm<OduSignalID> oduID = (OFOxm<OduSignalID>) oxm;
544 OduSignalID oduSignalID = oduID.getValue();
545 OduSignalId oduSignalId = OduSignalId.oduSignalId(oduSignalID.getTpn(),
546 oduSignalID.getTslen(),
547 oduSignalID.getTsmap());
548 builder.add(modL1OduSignalId(oduSignalId));
549 break;
550 case EXP_OCH_SIG_ID:
551 try {
552 @SuppressWarnings("unchecked")
553 OFOxm<CircuitSignalID> ochId = (OFOxm<CircuitSignalID>) oxm;
554 CircuitSignalID circuitSignalID = ochId.getValue();
555 builder.add(modL0Lambda(Lambda.ochSignal(
556 lookupGridType(circuitSignalID.getGridType()),
557 lookupChannelSpacing(circuitSignalID.getChannelSpacing()),
558 circuitSignalID.getChannelNumber(), circuitSignalID.getSpectralWidth())));
559 } catch (NoMappingFoundException e) {
560 log.warn(e.getMessage());
561 break;
562 }
563 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800564 case ARP_OP:
lishuai67574ce2016-05-12 16:39:59 +0800565 @SuppressWarnings("unchecked")
566 OFOxm<org.projectfloodlight.openflow.types.ArpOpcode> arpop =
567 (OFOxm<org.projectfloodlight.openflow.types.ArpOpcode>) oxm;
568 builder.setArpOp((short) arpop.getValue().getOpcode());
569 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800570 case ARP_SHA:
lishuai67574ce2016-05-12 16:39:59 +0800571 @SuppressWarnings("unchecked")
572 OFOxm<org.projectfloodlight.openflow.types.MacAddress> arpsha =
573 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
574 builder.setArpSha(MacAddress.valueOf(arpsha.getValue().getLong()));
575 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800576 case ARP_SPA:
lishuai67574ce2016-05-12 16:39:59 +0800577 @SuppressWarnings("unchecked")
578 OFOxm<IPv4Address> arpspa = (OFOxm<IPv4Address>) oxm;
579 builder.setArpSpa(Ip4Address.valueOf(arpspa.getValue().getInt()));
580 break;
Charles Chancad338a2016-09-16 18:03:11 -0700581 case OFDPA_MPLS_TYPE:
Pier Ventre6f630052016-10-18 09:58:41 -0700582 case OFDPA_OVID:
Charles Chancad338a2016-09-16 18:03:11 -0700583 if (treatmentInterpreter != null) {
584 try {
585 builder.extension(treatmentInterpreter.mapAction(action), deviceId);
586 break;
587 } catch (UnsupportedOperationException e) {
588 log.warn("Unsupported action extension");
589 }
590 }
591 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800592 case ARP_THA:
593 case ARP_TPA:
594 case BSN_EGR_PORT_GROUP_ID:
595 case BSN_GLOBAL_VRF_ALLOWED:
596 case BSN_IN_PORTS_128:
597 case BSN_L3_DST_CLASS_ID:
598 case BSN_L3_INTERFACE_CLASS_ID:
599 case BSN_L3_SRC_CLASS_ID:
600 case BSN_LAG_ID:
601 case BSN_TCP_FLAGS:
602 case BSN_UDF0:
603 case BSN_UDF1:
604 case BSN_UDF2:
605 case BSN_UDF3:
606 case BSN_UDF4:
607 case BSN_UDF5:
608 case BSN_UDF6:
609 case BSN_UDF7:
610 case BSN_VLAN_XLATE_PORT_GROUP_ID:
611 case BSN_VRF:
612 case ETH_TYPE:
613 case ICMPV4_CODE:
614 case ICMPV4_TYPE:
615 case ICMPV6_CODE:
616 case ICMPV6_TYPE:
617 case IN_PHY_PORT:
618 case IN_PORT:
619 case IPV6_DST:
620 case IPV6_FLABEL:
621 case IPV6_ND_SLL:
622 case IPV6_ND_TARGET:
623 case IPV6_ND_TLL:
624 case IPV6_SRC:
625 case IP_DSCP:
626 case IP_ECN:
627 case IP_PROTO:
628 case METADATA:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800629 case MPLS_TC:
630 case OCH_SIGID:
631 case OCH_SIGID_BASIC:
632 case OCH_SIGTYPE:
633 case OCH_SIGTYPE_BASIC:
634 case SCTP_DST:
635 case SCTP_SRC:
Yafit Hadar73514612015-11-04 10:14:21 +0200636 case EXP_ODU_SIGTYPE:
637 case EXP_OCH_SIGTYPE:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800638 default:
639 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
640 break;
641 }
642 }
643
Sho SHIMIZU6f1b09e2015-05-05 11:26:22 -0700644 // CHECKSTYLE IGNORE MethodLength FOR NEXT 1 LINES
alshabib6b5cfec2014-09-18 17:42:18 -0700645 private TrafficSelector buildSelector() {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800646 MacAddress mac;
647 Ip4Prefix ip4Prefix;
648 Ip6Address ip6Address;
649 Ip6Prefix ip6Prefix;
BitOhenryc1e5fcc2015-11-23 20:47:53 +0800650 Ip4Address ip;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800651
Jonathan Harte4e74f02016-03-03 12:57:40 -0800652 DriverHandler driverHandler = getDriver(deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800653 ExtensionSelectorInterpreter selectorInterpreter;
654 if (driverHandler.hasBehaviour(ExtensionSelectorInterpreter.class)) {
655 selectorInterpreter = driverHandler.behaviour(ExtensionSelectorInterpreter.class);
656 } else {
657 selectorInterpreter = null;
658 }
659
tom9a693fd2014-10-03 11:32:19 -0700660 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700661 for (MatchField<?> field : match.getMatchFields()) {
662 switch (field.id) {
663 case IN_PORT:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800664 builder.matchInPort(PortNumber
alshabib010c31d2014-09-26 10:01:12 -0700665 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700666 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800667 case IN_PHY_PORT:
668 builder.matchInPhyPort(PortNumber
669 .portNumber(match.get(MatchField.IN_PHY_PORT).getPortNumber()));
670 break;
671 case METADATA:
672 long metadata =
673 match.get(MatchField.METADATA).getValue().getValue();
674 builder.matchMetadata(metadata);
675 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700676 case ETH_DST:
Saurav Das9d6c86b2016-02-19 09:01:07 -0800677 if (match.isPartiallyMasked(MatchField.ETH_DST)) {
678 Masked<org.projectfloodlight.openflow.types.MacAddress> maskedMac =
679 match.getMasked(MatchField.ETH_DST);
680 builder.matchEthDstMasked(MacAddress.valueOf(maskedMac.getValue().getLong()),
681 MacAddress.valueOf(maskedMac.getMask().getLong()));
682 } else {
683 mac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
684 builder.matchEthDst(mac);
685 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800686 break;
687 case ETH_SRC:
Saurav Das9d6c86b2016-02-19 09:01:07 -0800688 if (match.isPartiallyMasked(MatchField.ETH_SRC)) {
689 Masked<org.projectfloodlight.openflow.types.MacAddress> maskedMac =
690 match.getMasked(MatchField.ETH_SRC);
691 builder.matchEthSrcMasked(MacAddress.valueOf(maskedMac.getValue().getLong()),
692 MacAddress.valueOf(maskedMac.getMask().getLong()));
693 } else {
694 mac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
695 builder.matchEthSrc(mac);
696 }
alshabib6b5cfec2014-09-18 17:42:18 -0700697 break;
698 case ETH_TYPE:
699 int ethType = match.get(MatchField.ETH_TYPE).getValue();
Jonathan Hart8cf212a2015-10-29 17:42:03 -0700700 builder.matchEthType((short) ethType);
alshabib6b5cfec2014-09-18 17:42:18 -0700701 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700702 case VLAN_VID:
Charles Chan14967c22015-12-07 11:11:50 -0800703 if (selectorInterpreter != null &&
704 selectorInterpreter.supported(ExtensionSelectorTypes.OFDPA_MATCH_VLAN_VID.type())) {
705 if (match.getVersion().equals(OFVersion.OF_13)) {
706 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.VLAN_VID);
707 builder.extension(selectorInterpreter.mapOxm(oxm),
Jonathan Harte4e74f02016-03-03 12:57:40 -0800708 deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800709 } else {
710 break;
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800711 }
712 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800713 VlanId vlanId = null;
714 if (match.isPartiallyMasked(MatchField.VLAN_VID)) {
715 Masked<OFVlanVidMatch> masked = match.getMasked(MatchField.VLAN_VID);
716 if (masked.getValue().equals(OFVlanVidMatch.PRESENT)
717 && masked.getMask().equals(OFVlanVidMatch.PRESENT)) {
718 vlanId = VlanId.ANY;
719 }
Jonathan Hart1468fee2015-07-16 18:50:34 -0700720 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800721 if (!match.get(MatchField.VLAN_VID).isPresentBitSet()) {
722 vlanId = VlanId.NONE;
723 } else {
724 vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
725 }
Jonathan Hart1468fee2015-07-16 18:50:34 -0700726 }
Charles Chan14967c22015-12-07 11:11:50 -0800727 if (vlanId != null) {
728 builder.matchVlanId(vlanId);
729 }
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800730 }
alshabib6b5cfec2014-09-18 17:42:18 -0700731 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800732 case VLAN_PCP:
733 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
734 builder.matchVlanPcp(vlanPcp);
735 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800736 case IP_DSCP:
737 byte ipDscp = match.get(MatchField.IP_DSCP).getDscpValue();
738 builder.matchIPDscp(ipDscp);
739 break;
740 case IP_ECN:
741 byte ipEcn = match.get(MatchField.IP_ECN).getEcnValue();
742 builder.matchIPEcn(ipEcn);
743 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800744 case IP_PROTO:
745 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
746 builder.matchIPProtocol((byte) proto);
747 break;
748 case IPV4_SRC:
749 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
750 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800751 ip4Prefix = Ip4Prefix.valueOf(
752 maskedIp.getValue().getInt(),
753 maskedIp.getMask().asCidrMaskLength());
754 } else {
755 ip4Prefix = Ip4Prefix.valueOf(
756 match.get(MatchField.IPV4_SRC).getInt(),
757 Ip4Prefix.MAX_MASK_LENGTH);
758 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800759 builder.matchIPSrc(ip4Prefix);
760 break;
761 case IPV4_DST:
762 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
763 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800764 ip4Prefix = Ip4Prefix.valueOf(
765 maskedIp.getValue().getInt(),
766 maskedIp.getMask().asCidrMaskLength());
767 } else {
768 ip4Prefix = Ip4Prefix.valueOf(
769 match.get(MatchField.IPV4_DST).getInt(),
770 Ip4Prefix.MAX_MASK_LENGTH);
771 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800772 builder.matchIPDst(ip4Prefix);
Jonathan Hart34bc6142014-10-17 11:00:43 -0700773 break;
774 case TCP_SRC:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700775 builder.matchTcpSrc(TpPort.tpPort(match.get(MatchField.TCP_SRC).getPort()));
Jonathan Hart34bc6142014-10-17 11:00:43 -0700776 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800777 case TCP_DST:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700778 builder.matchTcpDst(TpPort.tpPort(match.get(MatchField.TCP_DST).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800779 break;
780 case UDP_SRC:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700781 builder.matchUdpSrc(TpPort.tpPort(match.get(MatchField.UDP_SRC).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800782 break;
783 case UDP_DST:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700784 builder.matchUdpDst(TpPort.tpPort(match.get(MatchField.UDP_DST).getPort()));
Sho SHIMIZU6f1b09e2015-05-05 11:26:22 -0700785 break;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100786 case MPLS_LABEL:
787 builder.matchMplsLabel(MplsLabel.mplsLabel((int) match.get(MatchField.MPLS_LABEL)
788 .getValue()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800789 break;
Saurav Dasffc5bbc2015-08-18 23:30:19 -0700790 case MPLS_BOS:
791 builder.matchMplsBos(match.get(MatchField.MPLS_BOS).getValue());
792 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800793 case SCTP_SRC:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700794 builder.matchSctpSrc(TpPort.tpPort(match.get(MatchField.SCTP_SRC).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800795 break;
796 case SCTP_DST:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700797 builder.matchSctpDst(TpPort.tpPort(match.get(MatchField.SCTP_DST).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800798 break;
799 case ICMPV4_TYPE:
800 byte icmpType = (byte) match.get(MatchField.ICMPV4_TYPE).getType();
801 builder.matchIcmpType(icmpType);
802 break;
803 case ICMPV4_CODE:
804 byte icmpCode = (byte) match.get(MatchField.ICMPV4_CODE).getCode();
805 builder.matchIcmpCode(icmpCode);
806 break;
807 case IPV6_SRC:
808 if (match.isPartiallyMasked(MatchField.IPV6_SRC)) {
809 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_SRC);
810 ip6Prefix = Ip6Prefix.valueOf(
811 maskedIp.getValue().getBytes(),
812 maskedIp.getMask().asCidrMaskLength());
813 } else {
814 ip6Prefix = Ip6Prefix.valueOf(
815 match.get(MatchField.IPV6_SRC).getBytes(),
816 Ip6Prefix.MAX_MASK_LENGTH);
817 }
818 builder.matchIPv6Src(ip6Prefix);
819 break;
820 case IPV6_DST:
821 if (match.isPartiallyMasked(MatchField.IPV6_DST)) {
822 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_DST);
823 ip6Prefix = Ip6Prefix.valueOf(
824 maskedIp.getValue().getBytes(),
825 maskedIp.getMask().asCidrMaskLength());
826 } else {
827 ip6Prefix = Ip6Prefix.valueOf(
828 match.get(MatchField.IPV6_DST).getBytes(),
829 Ip6Prefix.MAX_MASK_LENGTH);
830 }
831 builder.matchIPv6Dst(ip6Prefix);
832 break;
833 case IPV6_FLABEL:
834 int flowLabel =
835 match.get(MatchField.IPV6_FLABEL).getIPv6FlowLabelValue();
836 builder.matchIPv6FlowLabel(flowLabel);
837 break;
838 case ICMPV6_TYPE:
839 byte icmpv6type = (byte) match.get(MatchField.ICMPV6_TYPE).getValue();
840 builder.matchIcmpv6Type(icmpv6type);
841 break;
842 case ICMPV6_CODE:
843 byte icmpv6code = (byte) match.get(MatchField.ICMPV6_CODE).getValue();
844 builder.matchIcmpv6Code(icmpv6code);
845 break;
846 case IPV6_ND_TARGET:
847 ip6Address =
848 Ip6Address.valueOf(match.get(MatchField.IPV6_ND_TARGET).getBytes());
849 builder.matchIPv6NDTargetAddress(ip6Address);
850 break;
851 case IPV6_ND_SLL:
852 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_SLL).getLong());
853 builder.matchIPv6NDSourceLinkLayerAddress(mac);
854 break;
855 case IPV6_ND_TLL:
856 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_TLL).getLong());
857 builder.matchIPv6NDTargetLinkLayerAddress(mac);
858 break;
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800859 case IPV6_EXTHDR:
Charles M.C. Chan2184de12015-04-26 02:24:53 +0800860 builder.matchIPv6ExthdrFlags((short) match.get(MatchField.IPV6_EXTHDR)
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700861 .getValue());
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800862 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700863 case OCH_SIGID:
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700864 CircuitSignalID sigId = match.get(MatchField.OCH_SIGID);
865 builder.add(matchLambda(Lambda.ochSignal(
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700866 lookupGridType(sigId.getGridType()), lookupChannelSpacing(sigId.getChannelSpacing()),
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -0700867 sigId.getChannelNumber(), sigId.getSpectralWidth())
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700868 ));
Marc De Leenheer49087752014-10-23 13:54:09 -0700869 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800870 case OCH_SIGTYPE:
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700871 U8 sigType = match.get(MatchField.OCH_SIGTYPE);
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700872 builder.add(matchOchSignalType(lookupOchSignalType((byte) sigType.getValue())));
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800873 break;
Yafit Hadar73514612015-11-04 10:14:21 +0200874 case EXP_OCH_SIG_ID:
875 try {
876 CircuitSignalID expSigId = match.get(MatchField.EXP_OCH_SIG_ID);
877 builder.add(matchLambda(Lambda.ochSignal(
878 lookupGridType(expSigId.getGridType()), lookupChannelSpacing(expSigId.getChannelSpacing()),
879 expSigId.getChannelNumber(), expSigId.getSpectralWidth())));
880 } catch (NoMappingFoundException e) {
881 log.warn(e.getMessage());
882 break;
883 }
884 break;
885 case EXP_OCH_SIGTYPE:
886 try {
887 U8 expOchSigType = match.get(MatchField.EXP_OCH_SIGTYPE);
888 builder.add(matchOchSignalType(lookupOchSignalType((byte) expOchSigType.getValue())));
889 } catch (NoMappingFoundException e) {
890 log.warn(e.getMessage());
891 break;
892 }
893 break;
894 case EXP_ODU_SIG_ID:
895 OduSignalId oduSignalId = OduSignalId.oduSignalId(match.get(MatchField.EXP_ODU_SIG_ID).getTpn(),
896 match.get(MatchField.EXP_ODU_SIG_ID).getTslen(),
897 match.get(MatchField.EXP_ODU_SIG_ID).getTsmap());
898 builder.add(matchOduSignalId(oduSignalId));
899 break;
900 case EXP_ODU_SIGTYPE:
901 try {
902 U8 oduSigType = match.get(MatchField.EXP_ODU_SIGTYPE);
903 builder.add(matchOduSignalType(lookupOduSignalType((byte) oduSigType.getValue())));
904 } catch (NoMappingFoundException e) {
905 log.warn(e.getMessage());
906 break;
907 }
908 break;
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700909 case TUNNEL_ID:
910 long tunnelId = match.get(MatchField.TUNNEL_ID).getValue();
911 builder.matchTunnelId(tunnelId);
912 break;
BitOhenryb40129a2015-11-30 12:41:18 +0800913 case ARP_OP:
914 int arpOp = match.get(MatchField.ARP_OP).getOpcode();
915 builder.matchArpOp(arpOp);
916 break;
BitOhenrya331b182015-11-23 08:39:37 +0800917 case ARP_SHA:
918 mac = MacAddress.valueOf(match.get(MatchField.ARP_SHA).getLong());
919 builder.matchArpSha(mac);
920 break;
BitOhenry296b4542015-11-24 08:41:58 +0800921 case ARP_SPA:
922 ip = Ip4Address.valueOf(match.get(MatchField.ARP_SPA).getInt());
923 builder.matchArpSpa(ip);
924 break;
BitOhenry76430852015-11-20 19:04:49 +0800925 case ARP_THA:
926 mac = MacAddress.valueOf(match.get(MatchField.ARP_THA).getLong());
927 builder.matchArpTha(mac);
928 break;
BitOhenryc1e5fcc2015-11-23 20:47:53 +0800929 case ARP_TPA:
930 ip = Ip4Address.valueOf(match.get(MatchField.ARP_TPA).getInt());
931 builder.matchArpTpa(ip);
932 break;
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530933 case NSP:
934 if (selectorInterpreter != null) {
935 try {
936 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.NSP);
937 builder.extension(selectorInterpreter.mapOxm(oxm), deviceId);
938 } catch (UnsupportedOperationException e) {
939 log.debug(e.getMessage());
940 }
941 }
942 break;
943 case NSI:
944 if (selectorInterpreter != null) {
945 try {
946 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.NSI);
947 builder.extension(selectorInterpreter.mapOxm(oxm), deviceId);
948 } catch (UnsupportedOperationException e) {
949 log.debug(e.getMessage());
950 }
951 }
952 break;
953 case ENCAP_ETH_TYPE:
954 if (selectorInterpreter != null) {
955 try {
956 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.ENCAP_ETH_TYPE);
957 builder.extension(selectorInterpreter.mapOxm(oxm), deviceId);
958 } catch (UnsupportedOperationException e) {
959 log.debug(e.getMessage());
960 }
961 }
962 break;
Pier Ventre6f630052016-10-18 09:58:41 -0700963 case OFDPA_OVID:
964 if (selectorInterpreter != null &&
965 selectorInterpreter.supported(ExtensionSelectorTypes.OFDPA_MATCH_OVID.type())) {
966 if (match.getVersion().equals(OFVersion.OF_13)) {
967 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.OFDPA_OVID);
968 builder.extension(selectorInterpreter.mapOxm(oxm),
969 deviceId);
970 } else {
971 break;
972 }
973 }
974 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700975 case MPLS_TC:
alshabib6b5cfec2014-09-18 17:42:18 -0700976 default:
977 log.warn("Match type {} not yet implemented.", field.id);
alshabib6b5cfec2014-09-18 17:42:18 -0700978 }
979 }
980 return builder.build();
981 }
Jonathan Hart3c259162015-10-21 21:31:19 -0700982
Jian Liffef5002016-04-04 23:27:37 -0700983 private DriverHandler getDriver(DeviceId devId) {
984 Driver driver = driverService.getDriver(devId);
985 DriverHandler handler = new DefaultDriverHandler(new DefaultDriverData(driver, devId));
Jonathan Hart3c259162015-10-21 21:31:19 -0700986 return handler;
987 }
alshabib6b5cfec2014-09-18 17:42:18 -0700988}