blob: e40990e01c5180ff31a9911d1cd39c196ebdd434 [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;
alshabib6b5cfec2014-09-18 17:42:18 -070063import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst;
64import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080065import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
alshabib6b5cfec2014-09-18 17:42:18 -070066import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst;
67import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc;
Jonathan Hart8cf212a2015-10-29 17:42:03 -070068import org.projectfloodlight.openflow.protocol.action.OFActionSetQueue;
alshabib6b5cfec2014-09-18 17:42:18 -070069import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp;
70import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid;
alshabib19fdc122014-10-03 11:38:19 -070071import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hartcf783202014-11-24 18:55:42 -080072import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions;
alshabibbdcbb102015-04-22 14:16:38 -070073import org.projectfloodlight.openflow.protocol.instruction.OFInstructionGotoTable;
alshabib346b5b32015-03-06 00:42:16 -080074import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteActions;
Saurav Das86af8f12015-05-25 23:55:33 -070075import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteMetadata;
alshabib6b5cfec2014-09-18 17:42:18 -070076import org.projectfloodlight.openflow.protocol.match.Match;
77import org.projectfloodlight.openflow.protocol.match.MatchField;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080078import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
Yafit Hadar4cc65f72016-02-10 11:23:06 +020079import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigid;
alshabib346b5b32015-03-06 00:42:16 -080080import org.projectfloodlight.openflow.protocol.ver13.OFFactoryVer13;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070081import org.projectfloodlight.openflow.types.CircuitSignalID;
alshabib6b5cfec2014-09-18 17:42:18 -070082import org.projectfloodlight.openflow.types.IPv4Address;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080083import org.projectfloodlight.openflow.types.IPv6Address;
Jonathan Hart0e12fad2014-10-17 14:54:58 -070084import org.projectfloodlight.openflow.types.Masked;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080085import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Jonathan Harte4e74f02016-03-03 12:57:40 -080086import org.projectfloodlight.openflow.types.OduSignalID;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070087import org.projectfloodlight.openflow.types.TransportPort;
sangho3f97a17d2015-01-29 22:56:29 -080088import org.projectfloodlight.openflow.types.U32;
Hyunsun Moona08c5d02015-07-14 17:53:00 -070089import org.projectfloodlight.openflow.types.U64;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070090import org.projectfloodlight.openflow.types.U8;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080091import org.projectfloodlight.openflow.types.VlanPcp;
alshabib6b5cfec2014-09-18 17:42:18 -070092import org.slf4j.Logger;
Jian Lia0d9a172016-04-01 16:58:06 -070093import org.slf4j.LoggerFactory;
alshabib6b5cfec2014-09-18 17:42:18 -070094
Jonathan Hart8cf212a2015-10-29 17:42:03 -070095import java.util.List;
96
Thiago Santos877914d2016-07-20 18:29:29 -030097import static java.util.concurrent.TimeUnit.NANOSECONDS;
98import static java.util.concurrent.TimeUnit.SECONDS;
alshabib44852fb2016-04-15 15:56:51 -070099import static org.onosproject.net.flow.criteria.Criteria.*;
Yafit Hadar73514612015-11-04 10:14:21 +0200100import static org.onosproject.net.flow.instructions.Instructions.modL0Lambda;
101import static org.onosproject.net.flow.instructions.Instructions.modL1OduSignalId;
alshabib44852fb2016-04-15 15:56:51 -0700102import static org.onosproject.provider.of.flow.util.OpenFlowValueMapper.*;
alshabib19fdc122014-10-03 11:38:19 -0700103
alshabib1c319ff2014-10-04 20:29:09 -0700104public class FlowEntryBuilder {
Jian Lia0d9a172016-04-01 16:58:06 -0700105 private static final Logger log = LoggerFactory.getLogger(FlowEntryBuilder.class);
alshabib6b5cfec2014-09-18 17:42:18 -0700106
107 private final OFFlowStatsEntry stat;
108 private final OFFlowRemoved removed;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800109 private final OFFlowMod flowMod;
alshabib6b5cfec2014-09-18 17:42:18 -0700110
111 private final Match match;
alshabib346b5b32015-03-06 00:42:16 -0800112
Jonathan Hart67fc0972015-03-19 15:21:20 -0700113 // All actions are contained in an OFInstruction. For OF1.0
114 // the instruction type is apply instruction (immediate set in ONOS speak)
alshabib346b5b32015-03-06 00:42:16 -0800115 private final List<OFInstruction> instructions;
alshabib6b5cfec2014-09-18 17:42:18 -0700116
Jonathan Harte4e74f02016-03-03 12:57:40 -0800117 private final DeviceId deviceId;
alshabib6b5cfec2014-09-18 17:42:18 -0700118
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800119 public enum FlowType { STAT, REMOVED, MOD }
120
121 private final FlowType type;
alshabib6b5cfec2014-09-18 17:42:18 -0700122
Jonathan Hart3c259162015-10-21 21:31:19 -0700123 private final DriverService driverService;
124
Jonathan Harte4e74f02016-03-03 12:57:40 -0800125 public FlowEntryBuilder(DeviceId deviceId, OFFlowStatsEntry entry, DriverService driverService) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800126 this.stat = entry;
127 this.match = entry.getMatch();
alshabib346b5b32015-03-06 00:42:16 -0800128 this.instructions = getInstructions(entry);
Jonathan Harte4e74f02016-03-03 12:57:40 -0800129 this.deviceId = deviceId;
Saurav Dasfa2fa932015-03-03 11:29:48 -0800130 this.removed = null;
131 this.flowMod = null;
132 this.type = FlowType.STAT;
Jonathan Hart3c259162015-10-21 21:31:19 -0700133 this.driverService = driverService;
Saurav Dasfa2fa932015-03-03 11:29:48 -0800134 }
135
Jonathan Harte4e74f02016-03-03 12:57:40 -0800136 public FlowEntryBuilder(DeviceId deviceId, OFFlowRemoved removed, DriverService driverService) {
alshabib6b5cfec2014-09-18 17:42:18 -0700137 this.match = removed.getMatch();
138 this.removed = removed;
Jonathan Harte4e74f02016-03-03 12:57:40 -0800139 this.deviceId = deviceId;
alshabib346b5b32015-03-06 00:42:16 -0800140 this.instructions = null;
alshabib6b5cfec2014-09-18 17:42:18 -0700141 this.stat = null;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800142 this.flowMod = null;
143 this.type = FlowType.REMOVED;
Jonathan Hart3c259162015-10-21 21:31:19 -0700144 this.driverService = driverService;
alshabib6b5cfec2014-09-18 17:42:18 -0700145 }
146
Jonathan Harte4e74f02016-03-03 12:57:40 -0800147 public FlowEntryBuilder(DeviceId deviceId, OFFlowMod fm, DriverService driverService) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800148 this.match = fm.getMatch();
Jonathan Harte4e74f02016-03-03 12:57:40 -0800149 this.deviceId = deviceId;
alshabib346b5b32015-03-06 00:42:16 -0800150 this.instructions = getInstructions(fm);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800151 this.type = FlowType.MOD;
152 this.flowMod = fm;
153 this.stat = null;
154 this.removed = null;
Jonathan Hart3c259162015-10-21 21:31:19 -0700155 this.driverService = driverService;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800156 }
alshabib1c319ff2014-10-04 20:29:09 -0700157
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800158 public FlowEntry build(FlowEntryState... state) {
Brian O'Connor21564612015-12-05 19:24:59 -0800159 FlowRule.Builder builder;
160 try {
161 switch (this.type) {
162 case STAT:
163 builder = DefaultFlowRule.builder()
Jonathan Harte4e74f02016-03-03 12:57:40 -0800164 .forDevice(deviceId)
Brian O'Connor21564612015-12-05 19:24:59 -0800165 .withSelector(buildSelector())
166 .withTreatment(buildTreatment())
167 .withPriority(stat.getPriority())
168 .makeTemporary(stat.getIdleTimeout())
Jonathan Hartd1ce4b02016-01-11 13:28:36 -0800169 .withCookie(stat.getCookie().getValue());
170 if (stat.getVersion() != OFVersion.OF_10) {
171 builder.forTable(stat.getTableId().getValue());
172 }
alshabibbdcbb102015-04-22 14:16:38 -0700173
Brian O'Connor21564612015-12-05 19:24:59 -0800174 return new DefaultFlowEntry(builder.build(), FlowEntryState.ADDED,
Thiago Santos877914d2016-07-20 18:29:29 -0300175 SECONDS.toNanos(stat.getDurationSec())
176 + stat.getDurationNsec(), NANOSECONDS,
Brian O'Connor21564612015-12-05 19:24:59 -0800177 stat.getPacketCount().getValue(),
178 stat.getByteCount().getValue());
179 case REMOVED:
180 builder = DefaultFlowRule.builder()
Jonathan Harte4e74f02016-03-03 12:57:40 -0800181 .forDevice(deviceId)
Brian O'Connor21564612015-12-05 19:24:59 -0800182 .withSelector(buildSelector())
183 .withPriority(removed.getPriority())
184 .makeTemporary(removed.getIdleTimeout())
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700185 .withCookie(removed.getCookie().getValue())
186 .withReason(FlowRule.FlowRemoveReason.parseShort(removed.getReason()));
187
Brian O'Connor21564612015-12-05 19:24:59 -0800188 if (removed.getVersion() != OFVersion.OF_10) {
189 builder.forTable(removed.getTableId().getValue());
190 }
alshabibbdcbb102015-04-22 14:16:38 -0700191
Brian O'Connor21564612015-12-05 19:24:59 -0800192 return new DefaultFlowEntry(builder.build(), FlowEntryState.REMOVED,
Thiago Santos877914d2016-07-20 18:29:29 -0300193 SECONDS.toNanos(removed.getDurationSec())
194 + removed.getDurationNsec(), NANOSECONDS,
Brian O'Connor21564612015-12-05 19:24:59 -0800195 removed.getPacketCount().getValue(),
196 removed.getByteCount().getValue());
197 case MOD:
198 FlowEntryState flowState = state.length > 0 ? state[0] : FlowEntryState.FAILED;
199 builder = DefaultFlowRule.builder()
Jonathan Harte4e74f02016-03-03 12:57:40 -0800200 .forDevice(deviceId)
Brian O'Connor21564612015-12-05 19:24:59 -0800201 .withSelector(buildSelector())
202 .withTreatment(buildTreatment())
203 .withPriority(flowMod.getPriority())
204 .makeTemporary(flowMod.getIdleTimeout())
205 .withCookie(flowMod.getCookie().getValue());
206 if (flowMod.getVersion() != OFVersion.OF_10) {
207 builder.forTable(flowMod.getTableId().getValue());
208 }
alshabibbdcbb102015-04-22 14:16:38 -0700209
Brian O'Connor21564612015-12-05 19:24:59 -0800210 return new DefaultFlowEntry(builder.build(), flowState, 0, 0, 0);
211 default:
212 log.error("Unknown flow type : {}", this.type);
213 return null;
214 }
215 } catch (UnsupportedOperationException e) {
216 log.warn("Error building flow entry", e);
217 return null;
alshabib6b5cfec2014-09-18 17:42:18 -0700218 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800219
alshabib6b5cfec2014-09-18 17:42:18 -0700220 }
221
alshabib346b5b32015-03-06 00:42:16 -0800222 private List<OFInstruction> getInstructions(OFFlowMod entry) {
alshabib19fdc122014-10-03 11:38:19 -0700223 switch (entry.getVersion()) {
224 case OF_10:
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700225 return Lists.newArrayList(OFFactoryVer13.INSTANCE.instructions()
226 .applyActions(
227 entry.getActions()));
alshabib19fdc122014-10-03 11:38:19 -0700228 case OF_11:
229 case OF_12:
230 case OF_13:
alshabib346b5b32015-03-06 00:42:16 -0800231 return entry.getInstructions();
232 default:
233 log.warn("Unknown OF version {}", entry.getVersion());
234 }
235 return Lists.newLinkedList();
236 }
237
238 private List<OFInstruction> getInstructions(OFFlowStatsEntry entry) {
239 switch (entry.getVersion()) {
240 case OF_10:
241 return Lists.newArrayList(
242 OFFactoryVer13.INSTANCE.instructions().applyActions(entry.getActions()));
243 case OF_11:
244 case OF_12:
245 case OF_13:
246 return entry.getInstructions();
alshabib19fdc122014-10-03 11:38:19 -0700247 default:
248 log.warn("Unknown OF version {}", entry.getVersion());
249 }
250 return Lists.newLinkedList();
251 }
alshabib6b5cfec2014-09-18 17:42:18 -0700252
253 private TrafficTreatment buildTreatment() {
tom9a693fd2014-10-03 11:32:19 -0700254 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabib346b5b32015-03-06 00:42:16 -0800255 for (OFInstruction in : instructions) {
256 switch (in.getType()) {
257 case GOTO_TABLE:
alshabibbdcbb102015-04-22 14:16:38 -0700258 builder.transition(((int) ((OFInstructionGotoTable) in)
259 .getTableId().getValue()));
alshabib346b5b32015-03-06 00:42:16 -0800260 break;
261 case WRITE_METADATA:
Saurav Das86af8f12015-05-25 23:55:33 -0700262 OFInstructionWriteMetadata m = (OFInstructionWriteMetadata) in;
263 builder.writeMetadata(m.getMetadata().getValue(),
264 m.getMetadataMask().getValue());
alshabib346b5b32015-03-06 00:42:16 -0800265 break;
266 case WRITE_ACTIONS:
267 builder.deferred();
268 buildActions(((OFInstructionWriteActions) in).getActions(),
269 builder);
270 break;
271 case APPLY_ACTIONS:
272 builder.immediate();
273 buildActions(((OFInstructionApplyActions) in).getActions(),
274 builder);
275 break;
276 case CLEAR_ACTIONS:
277 builder.wipeDeferred();
278 break;
279 case EXPERIMENTER:
280 break;
281 case METER:
282 break;
283 default:
284 log.warn("Unknown instructions type {}", in.getType());
alshabib6b5cfec2014-09-18 17:42:18 -0700285 }
286 }
287
288 return builder.build();
289 }
290
Jian Lia0d9a172016-04-01 16:58:06 -0700291 /**
292 * Configures traffic treatment builder with a given collection of actions.
293 *
294 * @param actions a set of OpenFlow actions
295 * @param builder traffic treatment builder
296 * @param driverHandler driver handler
297 * @param deviceId device identifier
298 * @return configured traffic treatment builder
299 */
300 public static TrafficTreatment.Builder configureTreatmentBuilder(List<OFAction> actions,
301 TrafficTreatment.Builder builder,
302 DriverHandler driverHandler,
303 DeviceId deviceId) {
304 ExtensionTreatmentInterpreter interpreter;
Charles Chan14967c22015-12-07 11:11:50 -0800305 if (driverHandler.hasBehaviour(ExtensionTreatmentInterpreter.class)) {
Jian Lia0d9a172016-04-01 16:58:06 -0700306 interpreter = driverHandler.behaviour(ExtensionTreatmentInterpreter.class);
Charles Chan14967c22015-12-07 11:11:50 -0800307 } else {
Jian Lia0d9a172016-04-01 16:58:06 -0700308 interpreter = null;
Charles Chan14967c22015-12-07 11:11:50 -0800309 }
310
alshabib346b5b32015-03-06 00:42:16 -0800311 for (OFAction act : actions) {
312 switch (act.getType()) {
313 case OUTPUT:
314 OFActionOutput out = (OFActionOutput) act;
315 builder.setOutput(
316 PortNumber.portNumber(out.getPort().getPortNumber()));
317 break;
318 case SET_VLAN_VID:
319 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
320 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
321 break;
322 case SET_VLAN_PCP:
323 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
324 builder.setVlanPcp(pcp.getVlanPcp().getValue());
325 break;
326 case SET_DL_DST:
327 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
328 builder.setEthDst(
329 MacAddress.valueOf(dldst.getDlAddr().getLong()));
330 break;
331 case SET_DL_SRC:
332 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
333 builder.setEthSrc(
334 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
alshabib346b5b32015-03-06 00:42:16 -0800335 break;
336 case SET_NW_DST:
337 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
338 IPv4Address di = nwdst.getNwAddr();
339 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
340 break;
341 case SET_NW_SRC:
342 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
343 IPv4Address si = nwsrc.getNwAddr();
344 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
345 break;
346 case EXPERIMENTER:
347 OFActionExperimenter exp = (OFActionExperimenter) act;
348 if (exp.getExperimenter() == 0x80005A06 ||
349 exp.getExperimenter() == 0x748771) {
350 OFActionCircuit ct = (OFActionCircuit) exp;
Yafit Hadar4cc65f72016-02-10 11:23:06 +0200351 CircuitSignalID circuitSignalID = ((OFOxmOchSigid) ct.getField()).getValue();
352 builder.add(Instructions.modL0Lambda(Lambda.ochSignal(
353 lookupGridType(circuitSignalID.getGridType()),
354 lookupChannelSpacing(circuitSignalID.getChannelSpacing()),
355 circuitSignalID.getChannelNumber(), circuitSignalID.getSpectralWidth())));
Jian Lia0d9a172016-04-01 16:58:06 -0700356 } else if (interpreter != null) {
357 builder.extension(interpreter.mapAction(exp), deviceId);
alshabib346b5b32015-03-06 00:42:16 -0800358 } else {
359 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
360 }
361 break;
362 case SET_FIELD:
363 OFActionSetField setField = (OFActionSetField) act;
Jian Lia0d9a172016-04-01 16:58:06 -0700364 handleSetField(builder, setField, driverHandler, deviceId);
alshabib346b5b32015-03-06 00:42:16 -0800365 break;
366 case POP_MPLS:
367 OFActionPopMpls popMpls = (OFActionPopMpls) act;
Ray Milkey84d5a292016-02-22 14:04:01 -0800368 builder.popMpls(new EthType(popMpls.getEthertype().getValue()));
alshabib346b5b32015-03-06 00:42:16 -0800369 break;
370 case PUSH_MPLS:
371 builder.pushMpls();
372 break;
373 case COPY_TTL_IN:
374 builder.copyTtlIn();
375 break;
376 case COPY_TTL_OUT:
377 builder.copyTtlOut();
378 break;
379 case DEC_MPLS_TTL:
380 builder.decMplsTtl();
381 break;
382 case DEC_NW_TTL:
383 builder.decNwTtl();
384 break;
385 case GROUP:
386 OFActionGroup group = (OFActionGroup) act;
387 builder.group(new DefaultGroupId(group.getGroup().getGroupNumber()));
388 break;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200389 case SET_QUEUE:
390 OFActionSetQueue setQueue = (OFActionSetQueue) act;
391 builder.setQueue(setQueue.getQueueId());
392 break;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200393 case ENQUEUE:
394 OFActionEnqueue enqueue = (OFActionEnqueue) act;
Jian Liffef5002016-04-04 23:27:37 -0700395 builder.setQueue(enqueue.getQueueId(),
396 PortNumber.portNumber(enqueue.getPort().getPortNumber()));
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200397 break;
Jonathan Hart67fc0972015-03-19 15:21:20 -0700398 case STRIP_VLAN:
alshabib346b5b32015-03-06 00:42:16 -0800399 case POP_VLAN:
400 builder.popVlan();
401 break;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700402 case PUSH_VLAN:
403 builder.pushVlan();
404 break;
alshabib346b5b32015-03-06 00:42:16 -0800405 case SET_TP_DST:
406 case SET_TP_SRC:
407 case POP_PBB:
408 case PUSH_PBB:
alshabib346b5b32015-03-06 00:42:16 -0800409 case SET_MPLS_LABEL:
410 case SET_MPLS_TC:
411 case SET_MPLS_TTL:
412 case SET_NW_ECN:
413 case SET_NW_TOS:
414 case SET_NW_TTL:
alshabib346b5b32015-03-06 00:42:16 -0800415
alshabib346b5b32015-03-06 00:42:16 -0800416 default:
417 log.warn("Action type {} not yet implemented.", act.getType());
418 }
419 }
420 return builder;
421 }
422
Jian Lia0d9a172016-04-01 16:58:06 -0700423 private TrafficTreatment.Builder buildActions(List<OFAction> actions,
424 TrafficTreatment.Builder builder) {
Jonathan Harte4e74f02016-03-03 12:57:40 -0800425 DriverHandler driverHandler = getDriver(deviceId);
Jian Lia0d9a172016-04-01 16:58:06 -0700426
427 return configureTreatmentBuilder(actions, builder, driverHandler, deviceId);
428 }
429
430
431 private static void handleSetField(TrafficTreatment.Builder builder,
432 OFActionSetField action,
433 DriverHandler driverHandler,
434 DeviceId deviceId) {
Charles Chan14967c22015-12-07 11:11:50 -0800435 ExtensionTreatmentInterpreter treatmentInterpreter;
436 if (driverHandler.hasBehaviour(ExtensionTreatmentInterpreter.class)) {
437 treatmentInterpreter = driverHandler.behaviour(ExtensionTreatmentInterpreter.class);
438 } else {
439 treatmentInterpreter = null;
440 }
441
Jonathan Hart3c259162015-10-21 21:31:19 -0700442 OFOxm<?> oxm = action.getField();
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800443 switch (oxm.getMatchField().id) {
444 case VLAN_PCP:
445 @SuppressWarnings("unchecked")
446 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
447 builder.setVlanPcp(vlanpcp.getValue().getValue());
448 break;
449 case VLAN_VID:
Charles Chan4211baa2016-04-20 17:10:40 -0700450 if (treatmentInterpreter != null) {
451 try {
452 builder.extension(treatmentInterpreter.mapAction(action), deviceId);
453 break;
454 } catch (UnsupportedOperationException e) {
455 log.debug("Unsupported action extension; defaulting to native OF");
456 }
Charles Chan14967c22015-12-07 11:11:50 -0800457 }
alshabib44852fb2016-04-15 15:56:51 -0700458 @SuppressWarnings("unchecked")
459 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
460 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800461 break;
462 case ETH_DST:
463 @SuppressWarnings("unchecked")
464 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
465 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
466 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
467 break;
468 case ETH_SRC:
469 @SuppressWarnings("unchecked")
470 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
471 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
472 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
473 break;
474 case IPV4_DST:
475 @SuppressWarnings("unchecked")
476 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
477 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
478 break;
479 case IPV4_SRC:
480 @SuppressWarnings("unchecked")
481 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
482 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
483 break;
sangho3f97a17d2015-01-29 22:56:29 -0800484 case MPLS_LABEL:
485 @SuppressWarnings("unchecked")
486 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100487 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
sangho3f97a17d2015-01-29 22:56:29 -0800488 break;
Saurav Das73a7dd42015-08-19 22:20:31 -0700489 case MPLS_BOS:
490 @SuppressWarnings("unchecked")
491 OFOxm<U8> mplsBos = (OFOxm<U8>) oxm;
Sho SHIMIZU1375cfe2016-08-18 09:33:37 -0700492 builder.setMplsBos(mplsBos.getValue() != U8.ZERO);
Saurav Das73a7dd42015-08-19 22:20:31 -0700493 break;
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700494 case TUNNEL_ID:
495 @SuppressWarnings("unchecked")
496 OFOxm<U64> tunnelId = (OFOxm<U64>) oxm;
497 builder.setTunnelId(tunnelId.getValue().getValue());
498 break;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700499 case TCP_DST:
500 @SuppressWarnings("unchecked")
501 OFOxm<TransportPort> tcpdst = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700502 builder.setTcpDst(TpPort.tpPort(tcpdst.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700503 break;
504 case TCP_SRC:
505 @SuppressWarnings("unchecked")
506 OFOxm<TransportPort> tcpsrc = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700507 builder.setTcpSrc(TpPort.tpPort(tcpsrc.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700508 break;
509 case UDP_DST:
510 @SuppressWarnings("unchecked")
511 OFOxm<TransportPort> udpdst = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700512 builder.setUdpDst(TpPort.tpPort(udpdst.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700513 break;
514 case UDP_SRC:
515 @SuppressWarnings("unchecked")
516 OFOxm<TransportPort> udpsrc = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700517 builder.setUdpSrc(TpPort.tpPort(udpsrc.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700518 break;
Jonathan Hart3c259162015-10-21 21:31:19 -0700519 case TUNNEL_IPV4_DST:
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530520 case NSP:
521 case NSI:
522 case NSH_C1:
523 case NSH_C2:
524 case NSH_C3:
525 case NSH_C4:
526 case NSH_MDTYPE:
527 case NSH_NP:
528 case ENCAP_ETH_SRC:
529 case ENCAP_ETH_DST:
530 case ENCAP_ETH_TYPE:
531 case TUN_GPE_NP:
Jian Liffef5002016-04-04 23:27:37 -0700532 if (treatmentInterpreter != null) {
533 try {
534 builder.extension(treatmentInterpreter.mapAction(action), deviceId);
535 } catch (UnsupportedOperationException e) {
alshabib44852fb2016-04-15 15:56:51 -0700536 log.debug(e.getMessage());
Jian Liffef5002016-04-04 23:27:37 -0700537 }
Jonathan Hart3c259162015-10-21 21:31:19 -0700538 }
539 break;
Yafit Hadar73514612015-11-04 10:14:21 +0200540 case EXP_ODU_SIG_ID:
541 @SuppressWarnings("unchecked")
542 OFOxm<OduSignalID> oduID = (OFOxm<OduSignalID>) oxm;
543 OduSignalID oduSignalID = oduID.getValue();
544 OduSignalId oduSignalId = OduSignalId.oduSignalId(oduSignalID.getTpn(),
545 oduSignalID.getTslen(),
546 oduSignalID.getTsmap());
547 builder.add(modL1OduSignalId(oduSignalId));
548 break;
549 case EXP_OCH_SIG_ID:
550 try {
551 @SuppressWarnings("unchecked")
552 OFOxm<CircuitSignalID> ochId = (OFOxm<CircuitSignalID>) oxm;
553 CircuitSignalID circuitSignalID = ochId.getValue();
554 builder.add(modL0Lambda(Lambda.ochSignal(
555 lookupGridType(circuitSignalID.getGridType()),
556 lookupChannelSpacing(circuitSignalID.getChannelSpacing()),
557 circuitSignalID.getChannelNumber(), circuitSignalID.getSpectralWidth())));
558 } catch (NoMappingFoundException e) {
559 log.warn(e.getMessage());
560 break;
561 }
562 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800563 case ARP_OP:
lishuai67574ce2016-05-12 16:39:59 +0800564 @SuppressWarnings("unchecked")
565 OFOxm<org.projectfloodlight.openflow.types.ArpOpcode> arpop =
566 (OFOxm<org.projectfloodlight.openflow.types.ArpOpcode>) oxm;
567 builder.setArpOp((short) arpop.getValue().getOpcode());
568 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800569 case ARP_SHA:
lishuai67574ce2016-05-12 16:39:59 +0800570 @SuppressWarnings("unchecked")
571 OFOxm<org.projectfloodlight.openflow.types.MacAddress> arpsha =
572 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
573 builder.setArpSha(MacAddress.valueOf(arpsha.getValue().getLong()));
574 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800575 case ARP_SPA:
lishuai67574ce2016-05-12 16:39:59 +0800576 @SuppressWarnings("unchecked")
577 OFOxm<IPv4Address> arpspa = (OFOxm<IPv4Address>) oxm;
578 builder.setArpSpa(Ip4Address.valueOf(arpspa.getValue().getInt()));
579 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800580 case ARP_THA:
581 case ARP_TPA:
582 case BSN_EGR_PORT_GROUP_ID:
583 case BSN_GLOBAL_VRF_ALLOWED:
584 case BSN_IN_PORTS_128:
585 case BSN_L3_DST_CLASS_ID:
586 case BSN_L3_INTERFACE_CLASS_ID:
587 case BSN_L3_SRC_CLASS_ID:
588 case BSN_LAG_ID:
589 case BSN_TCP_FLAGS:
590 case BSN_UDF0:
591 case BSN_UDF1:
592 case BSN_UDF2:
593 case BSN_UDF3:
594 case BSN_UDF4:
595 case BSN_UDF5:
596 case BSN_UDF6:
597 case BSN_UDF7:
598 case BSN_VLAN_XLATE_PORT_GROUP_ID:
599 case BSN_VRF:
600 case ETH_TYPE:
601 case ICMPV4_CODE:
602 case ICMPV4_TYPE:
603 case ICMPV6_CODE:
604 case ICMPV6_TYPE:
605 case IN_PHY_PORT:
606 case IN_PORT:
607 case IPV6_DST:
608 case IPV6_FLABEL:
609 case IPV6_ND_SLL:
610 case IPV6_ND_TARGET:
611 case IPV6_ND_TLL:
612 case IPV6_SRC:
613 case IP_DSCP:
614 case IP_ECN:
615 case IP_PROTO:
616 case METADATA:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800617 case MPLS_TC:
618 case OCH_SIGID:
619 case OCH_SIGID_BASIC:
620 case OCH_SIGTYPE:
621 case OCH_SIGTYPE_BASIC:
622 case SCTP_DST:
623 case SCTP_SRC:
Yafit Hadar73514612015-11-04 10:14:21 +0200624 case EXP_ODU_SIGTYPE:
625 case EXP_OCH_SIGTYPE:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800626 default:
627 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
628 break;
629 }
630 }
631
Sho SHIMIZU6f1b09e2015-05-05 11:26:22 -0700632 // CHECKSTYLE IGNORE MethodLength FOR NEXT 1 LINES
alshabib6b5cfec2014-09-18 17:42:18 -0700633 private TrafficSelector buildSelector() {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800634 MacAddress mac;
635 Ip4Prefix ip4Prefix;
636 Ip6Address ip6Address;
637 Ip6Prefix ip6Prefix;
BitOhenryc1e5fcc2015-11-23 20:47:53 +0800638 Ip4Address ip;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800639
Jonathan Harte4e74f02016-03-03 12:57:40 -0800640 DriverHandler driverHandler = getDriver(deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800641 ExtensionSelectorInterpreter selectorInterpreter;
642 if (driverHandler.hasBehaviour(ExtensionSelectorInterpreter.class)) {
643 selectorInterpreter = driverHandler.behaviour(ExtensionSelectorInterpreter.class);
644 } else {
645 selectorInterpreter = null;
646 }
647
tom9a693fd2014-10-03 11:32:19 -0700648 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700649 for (MatchField<?> field : match.getMatchFields()) {
650 switch (field.id) {
651 case IN_PORT:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800652 builder.matchInPort(PortNumber
alshabib010c31d2014-09-26 10:01:12 -0700653 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700654 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800655 case IN_PHY_PORT:
656 builder.matchInPhyPort(PortNumber
657 .portNumber(match.get(MatchField.IN_PHY_PORT).getPortNumber()));
658 break;
659 case METADATA:
660 long metadata =
661 match.get(MatchField.METADATA).getValue().getValue();
662 builder.matchMetadata(metadata);
663 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700664 case ETH_DST:
Saurav Das9d6c86b2016-02-19 09:01:07 -0800665 if (match.isPartiallyMasked(MatchField.ETH_DST)) {
666 Masked<org.projectfloodlight.openflow.types.MacAddress> maskedMac =
667 match.getMasked(MatchField.ETH_DST);
668 builder.matchEthDstMasked(MacAddress.valueOf(maskedMac.getValue().getLong()),
669 MacAddress.valueOf(maskedMac.getMask().getLong()));
670 } else {
671 mac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
672 builder.matchEthDst(mac);
673 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800674 break;
675 case ETH_SRC:
Saurav Das9d6c86b2016-02-19 09:01:07 -0800676 if (match.isPartiallyMasked(MatchField.ETH_SRC)) {
677 Masked<org.projectfloodlight.openflow.types.MacAddress> maskedMac =
678 match.getMasked(MatchField.ETH_SRC);
679 builder.matchEthSrcMasked(MacAddress.valueOf(maskedMac.getValue().getLong()),
680 MacAddress.valueOf(maskedMac.getMask().getLong()));
681 } else {
682 mac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
683 builder.matchEthSrc(mac);
684 }
alshabib6b5cfec2014-09-18 17:42:18 -0700685 break;
686 case ETH_TYPE:
687 int ethType = match.get(MatchField.ETH_TYPE).getValue();
Jonathan Hart8cf212a2015-10-29 17:42:03 -0700688 builder.matchEthType((short) ethType);
alshabib6b5cfec2014-09-18 17:42:18 -0700689 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700690 case VLAN_VID:
Charles Chan14967c22015-12-07 11:11:50 -0800691 if (selectorInterpreter != null &&
692 selectorInterpreter.supported(ExtensionSelectorTypes.OFDPA_MATCH_VLAN_VID.type())) {
693 if (match.getVersion().equals(OFVersion.OF_13)) {
694 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.VLAN_VID);
695 builder.extension(selectorInterpreter.mapOxm(oxm),
Jonathan Harte4e74f02016-03-03 12:57:40 -0800696 deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800697 } else {
698 break;
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800699 }
700 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800701 VlanId vlanId = null;
702 if (match.isPartiallyMasked(MatchField.VLAN_VID)) {
703 Masked<OFVlanVidMatch> masked = match.getMasked(MatchField.VLAN_VID);
704 if (masked.getValue().equals(OFVlanVidMatch.PRESENT)
705 && masked.getMask().equals(OFVlanVidMatch.PRESENT)) {
706 vlanId = VlanId.ANY;
707 }
Jonathan Hart1468fee2015-07-16 18:50:34 -0700708 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800709 if (!match.get(MatchField.VLAN_VID).isPresentBitSet()) {
710 vlanId = VlanId.NONE;
711 } else {
712 vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
713 }
Jonathan Hart1468fee2015-07-16 18:50:34 -0700714 }
Charles Chan14967c22015-12-07 11:11:50 -0800715 if (vlanId != null) {
716 builder.matchVlanId(vlanId);
717 }
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800718 }
alshabib6b5cfec2014-09-18 17:42:18 -0700719 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800720 case VLAN_PCP:
721 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
722 builder.matchVlanPcp(vlanPcp);
723 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800724 case IP_DSCP:
725 byte ipDscp = match.get(MatchField.IP_DSCP).getDscpValue();
726 builder.matchIPDscp(ipDscp);
727 break;
728 case IP_ECN:
729 byte ipEcn = match.get(MatchField.IP_ECN).getEcnValue();
730 builder.matchIPEcn(ipEcn);
731 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800732 case IP_PROTO:
733 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
734 builder.matchIPProtocol((byte) proto);
735 break;
736 case IPV4_SRC:
737 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
738 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800739 ip4Prefix = Ip4Prefix.valueOf(
740 maskedIp.getValue().getInt(),
741 maskedIp.getMask().asCidrMaskLength());
742 } else {
743 ip4Prefix = Ip4Prefix.valueOf(
744 match.get(MatchField.IPV4_SRC).getInt(),
745 Ip4Prefix.MAX_MASK_LENGTH);
746 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800747 builder.matchIPSrc(ip4Prefix);
748 break;
749 case IPV4_DST:
750 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
751 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800752 ip4Prefix = Ip4Prefix.valueOf(
753 maskedIp.getValue().getInt(),
754 maskedIp.getMask().asCidrMaskLength());
755 } else {
756 ip4Prefix = Ip4Prefix.valueOf(
757 match.get(MatchField.IPV4_DST).getInt(),
758 Ip4Prefix.MAX_MASK_LENGTH);
759 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800760 builder.matchIPDst(ip4Prefix);
Jonathan Hart34bc6142014-10-17 11:00:43 -0700761 break;
762 case TCP_SRC:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700763 builder.matchTcpSrc(TpPort.tpPort(match.get(MatchField.TCP_SRC).getPort()));
Jonathan Hart34bc6142014-10-17 11:00:43 -0700764 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800765 case TCP_DST:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700766 builder.matchTcpDst(TpPort.tpPort(match.get(MatchField.TCP_DST).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800767 break;
768 case UDP_SRC:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700769 builder.matchUdpSrc(TpPort.tpPort(match.get(MatchField.UDP_SRC).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800770 break;
771 case UDP_DST:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700772 builder.matchUdpDst(TpPort.tpPort(match.get(MatchField.UDP_DST).getPort()));
Sho SHIMIZU6f1b09e2015-05-05 11:26:22 -0700773 break;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100774 case MPLS_LABEL:
775 builder.matchMplsLabel(MplsLabel.mplsLabel((int) match.get(MatchField.MPLS_LABEL)
776 .getValue()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800777 break;
Saurav Dasffc5bbc2015-08-18 23:30:19 -0700778 case MPLS_BOS:
779 builder.matchMplsBos(match.get(MatchField.MPLS_BOS).getValue());
780 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800781 case SCTP_SRC:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700782 builder.matchSctpSrc(TpPort.tpPort(match.get(MatchField.SCTP_SRC).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800783 break;
784 case SCTP_DST:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700785 builder.matchSctpDst(TpPort.tpPort(match.get(MatchField.SCTP_DST).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800786 break;
787 case ICMPV4_TYPE:
788 byte icmpType = (byte) match.get(MatchField.ICMPV4_TYPE).getType();
789 builder.matchIcmpType(icmpType);
790 break;
791 case ICMPV4_CODE:
792 byte icmpCode = (byte) match.get(MatchField.ICMPV4_CODE).getCode();
793 builder.matchIcmpCode(icmpCode);
794 break;
795 case IPV6_SRC:
796 if (match.isPartiallyMasked(MatchField.IPV6_SRC)) {
797 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_SRC);
798 ip6Prefix = Ip6Prefix.valueOf(
799 maskedIp.getValue().getBytes(),
800 maskedIp.getMask().asCidrMaskLength());
801 } else {
802 ip6Prefix = Ip6Prefix.valueOf(
803 match.get(MatchField.IPV6_SRC).getBytes(),
804 Ip6Prefix.MAX_MASK_LENGTH);
805 }
806 builder.matchIPv6Src(ip6Prefix);
807 break;
808 case IPV6_DST:
809 if (match.isPartiallyMasked(MatchField.IPV6_DST)) {
810 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_DST);
811 ip6Prefix = Ip6Prefix.valueOf(
812 maskedIp.getValue().getBytes(),
813 maskedIp.getMask().asCidrMaskLength());
814 } else {
815 ip6Prefix = Ip6Prefix.valueOf(
816 match.get(MatchField.IPV6_DST).getBytes(),
817 Ip6Prefix.MAX_MASK_LENGTH);
818 }
819 builder.matchIPv6Dst(ip6Prefix);
820 break;
821 case IPV6_FLABEL:
822 int flowLabel =
823 match.get(MatchField.IPV6_FLABEL).getIPv6FlowLabelValue();
824 builder.matchIPv6FlowLabel(flowLabel);
825 break;
826 case ICMPV6_TYPE:
827 byte icmpv6type = (byte) match.get(MatchField.ICMPV6_TYPE).getValue();
828 builder.matchIcmpv6Type(icmpv6type);
829 break;
830 case ICMPV6_CODE:
831 byte icmpv6code = (byte) match.get(MatchField.ICMPV6_CODE).getValue();
832 builder.matchIcmpv6Code(icmpv6code);
833 break;
834 case IPV6_ND_TARGET:
835 ip6Address =
836 Ip6Address.valueOf(match.get(MatchField.IPV6_ND_TARGET).getBytes());
837 builder.matchIPv6NDTargetAddress(ip6Address);
838 break;
839 case IPV6_ND_SLL:
840 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_SLL).getLong());
841 builder.matchIPv6NDSourceLinkLayerAddress(mac);
842 break;
843 case IPV6_ND_TLL:
844 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_TLL).getLong());
845 builder.matchIPv6NDTargetLinkLayerAddress(mac);
846 break;
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800847 case IPV6_EXTHDR:
Charles M.C. Chan2184de12015-04-26 02:24:53 +0800848 builder.matchIPv6ExthdrFlags((short) match.get(MatchField.IPV6_EXTHDR)
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700849 .getValue());
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800850 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700851 case OCH_SIGID:
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700852 CircuitSignalID sigId = match.get(MatchField.OCH_SIGID);
853 builder.add(matchLambda(Lambda.ochSignal(
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700854 lookupGridType(sigId.getGridType()), lookupChannelSpacing(sigId.getChannelSpacing()),
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -0700855 sigId.getChannelNumber(), sigId.getSpectralWidth())
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700856 ));
Marc De Leenheer49087752014-10-23 13:54:09 -0700857 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800858 case OCH_SIGTYPE:
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700859 U8 sigType = match.get(MatchField.OCH_SIGTYPE);
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700860 builder.add(matchOchSignalType(lookupOchSignalType((byte) sigType.getValue())));
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800861 break;
Yafit Hadar73514612015-11-04 10:14:21 +0200862 case EXP_OCH_SIG_ID:
863 try {
864 CircuitSignalID expSigId = match.get(MatchField.EXP_OCH_SIG_ID);
865 builder.add(matchLambda(Lambda.ochSignal(
866 lookupGridType(expSigId.getGridType()), lookupChannelSpacing(expSigId.getChannelSpacing()),
867 expSigId.getChannelNumber(), expSigId.getSpectralWidth())));
868 } catch (NoMappingFoundException e) {
869 log.warn(e.getMessage());
870 break;
871 }
872 break;
873 case EXP_OCH_SIGTYPE:
874 try {
875 U8 expOchSigType = match.get(MatchField.EXP_OCH_SIGTYPE);
876 builder.add(matchOchSignalType(lookupOchSignalType((byte) expOchSigType.getValue())));
877 } catch (NoMappingFoundException e) {
878 log.warn(e.getMessage());
879 break;
880 }
881 break;
882 case EXP_ODU_SIG_ID:
883 OduSignalId oduSignalId = OduSignalId.oduSignalId(match.get(MatchField.EXP_ODU_SIG_ID).getTpn(),
884 match.get(MatchField.EXP_ODU_SIG_ID).getTslen(),
885 match.get(MatchField.EXP_ODU_SIG_ID).getTsmap());
886 builder.add(matchOduSignalId(oduSignalId));
887 break;
888 case EXP_ODU_SIGTYPE:
889 try {
890 U8 oduSigType = match.get(MatchField.EXP_ODU_SIGTYPE);
891 builder.add(matchOduSignalType(lookupOduSignalType((byte) oduSigType.getValue())));
892 } catch (NoMappingFoundException e) {
893 log.warn(e.getMessage());
894 break;
895 }
896 break;
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700897 case TUNNEL_ID:
898 long tunnelId = match.get(MatchField.TUNNEL_ID).getValue();
899 builder.matchTunnelId(tunnelId);
900 break;
BitOhenryb40129a2015-11-30 12:41:18 +0800901 case ARP_OP:
902 int arpOp = match.get(MatchField.ARP_OP).getOpcode();
903 builder.matchArpOp(arpOp);
904 break;
BitOhenrya331b182015-11-23 08:39:37 +0800905 case ARP_SHA:
906 mac = MacAddress.valueOf(match.get(MatchField.ARP_SHA).getLong());
907 builder.matchArpSha(mac);
908 break;
BitOhenry296b4542015-11-24 08:41:58 +0800909 case ARP_SPA:
910 ip = Ip4Address.valueOf(match.get(MatchField.ARP_SPA).getInt());
911 builder.matchArpSpa(ip);
912 break;
BitOhenry76430852015-11-20 19:04:49 +0800913 case ARP_THA:
914 mac = MacAddress.valueOf(match.get(MatchField.ARP_THA).getLong());
915 builder.matchArpTha(mac);
916 break;
BitOhenryc1e5fcc2015-11-23 20:47:53 +0800917 case ARP_TPA:
918 ip = Ip4Address.valueOf(match.get(MatchField.ARP_TPA).getInt());
919 builder.matchArpTpa(ip);
920 break;
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530921 case NSP:
922 if (selectorInterpreter != null) {
923 try {
924 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.NSP);
925 builder.extension(selectorInterpreter.mapOxm(oxm), deviceId);
926 } catch (UnsupportedOperationException e) {
927 log.debug(e.getMessage());
928 }
929 }
930 break;
931 case NSI:
932 if (selectorInterpreter != null) {
933 try {
934 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.NSI);
935 builder.extension(selectorInterpreter.mapOxm(oxm), deviceId);
936 } catch (UnsupportedOperationException e) {
937 log.debug(e.getMessage());
938 }
939 }
940 break;
941 case ENCAP_ETH_TYPE:
942 if (selectorInterpreter != null) {
943 try {
944 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.ENCAP_ETH_TYPE);
945 builder.extension(selectorInterpreter.mapOxm(oxm), deviceId);
946 } catch (UnsupportedOperationException e) {
947 log.debug(e.getMessage());
948 }
949 }
950 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700951 case MPLS_TC:
alshabib6b5cfec2014-09-18 17:42:18 -0700952 default:
953 log.warn("Match type {} not yet implemented.", field.id);
alshabib6b5cfec2014-09-18 17:42:18 -0700954 }
955 }
956 return builder.build();
957 }
Jonathan Hart3c259162015-10-21 21:31:19 -0700958
Jian Liffef5002016-04-04 23:27:37 -0700959 private DriverHandler getDriver(DeviceId devId) {
960 Driver driver = driverService.getDriver(devId);
961 DriverHandler handler = new DefaultDriverHandler(new DefaultDriverData(driver, devId));
Jonathan Hart3c259162015-10-21 21:31:19 -0700962 return handler;
963 }
alshabib6b5cfec2014-09-18 17:42:18 -0700964}