blob: c68c173d049fc108e2ec3daea623cfc638af2e12 [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;
Sangsik Yoonb1b823f2016-05-16 18:55:39 +090051import org.onosproject.provider.of.flow.impl.NewAdaptiveFlowStatsCollector;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080052import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabib6b5cfec2014-09-18 17:42:18 -070053import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
54import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
Charles Chan14967c22015-12-07 11:11:50 -080055import org.projectfloodlight.openflow.protocol.OFMatchV3;
Brian O'Connor21564612015-12-05 19:24:59 -080056import org.projectfloodlight.openflow.protocol.OFVersion;
alshabib6b5cfec2014-09-18 17:42:18 -070057import org.projectfloodlight.openflow.protocol.action.OFAction;
Marc De Leenheer49087752014-10-23 13:54:09 -070058import org.projectfloodlight.openflow.protocol.action.OFActionCircuit;
Steffen Gebertba2d3b72015-10-22 11:14:31 +020059import org.projectfloodlight.openflow.protocol.action.OFActionEnqueue;
Marc De Leenheer49087752014-10-23 13:54:09 -070060import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter;
sangho8995ac52015-02-04 11:29:03 -080061import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
alshabib6b5cfec2014-09-18 17:42:18 -070062import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
sangho3f97a17d2015-01-29 22:56:29 -080063import org.projectfloodlight.openflow.protocol.action.OFActionPopMpls;
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -050064import org.projectfloodlight.openflow.protocol.action.OFActionPushVlan;
alshabib6b5cfec2014-09-18 17:42:18 -070065import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst;
66import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080067import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
alshabib6b5cfec2014-09-18 17:42:18 -070068import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst;
69import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc;
Jonathan Hart8cf212a2015-10-29 17:42:03 -070070import org.projectfloodlight.openflow.protocol.action.OFActionSetQueue;
alshabib6b5cfec2014-09-18 17:42:18 -070071import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp;
72import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid;
alshabib19fdc122014-10-03 11:38:19 -070073import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hartcf783202014-11-24 18:55:42 -080074import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions;
alshabibbdcbb102015-04-22 14:16:38 -070075import org.projectfloodlight.openflow.protocol.instruction.OFInstructionGotoTable;
alshabib346b5b32015-03-06 00:42:16 -080076import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteActions;
Saurav Das86af8f12015-05-25 23:55:33 -070077import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteMetadata;
alshabib6b5cfec2014-09-18 17:42:18 -070078import org.projectfloodlight.openflow.protocol.match.Match;
79import org.projectfloodlight.openflow.protocol.match.MatchField;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080080import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
Yafit Hadar4cc65f72016-02-10 11:23:06 +020081import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigid;
alshabib346b5b32015-03-06 00:42:16 -080082import org.projectfloodlight.openflow.protocol.ver13.OFFactoryVer13;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070083import org.projectfloodlight.openflow.types.CircuitSignalID;
alshabib6b5cfec2014-09-18 17:42:18 -070084import org.projectfloodlight.openflow.types.IPv4Address;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080085import org.projectfloodlight.openflow.types.IPv6Address;
Jonathan Hart0e12fad2014-10-17 14:54:58 -070086import org.projectfloodlight.openflow.types.Masked;
Pier Ventre23f78672016-11-15 08:46:34 -080087import org.projectfloodlight.openflow.types.OFBooleanValue;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080088import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Jonathan Harte4e74f02016-03-03 12:57:40 -080089import org.projectfloodlight.openflow.types.OduSignalID;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070090import org.projectfloodlight.openflow.types.TransportPort;
sangho3f97a17d2015-01-29 22:56:29 -080091import org.projectfloodlight.openflow.types.U32;
Hyunsun Moona08c5d02015-07-14 17:53:00 -070092import org.projectfloodlight.openflow.types.U64;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070093import org.projectfloodlight.openflow.types.U8;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080094import org.projectfloodlight.openflow.types.VlanPcp;
alshabib6b5cfec2014-09-18 17:42:18 -070095import org.slf4j.Logger;
Jian Lia0d9a172016-04-01 16:58:06 -070096import org.slf4j.LoggerFactory;
alshabib6b5cfec2014-09-18 17:42:18 -070097
Jonathan Hart8cf212a2015-10-29 17:42:03 -070098import java.util.List;
99
Thiago Santos877914d2016-07-20 18:29:29 -0300100import static java.util.concurrent.TimeUnit.NANOSECONDS;
101import static java.util.concurrent.TimeUnit.SECONDS;
alshabib44852fb2016-04-15 15:56:51 -0700102import static org.onosproject.net.flow.criteria.Criteria.*;
Yafit Hadar73514612015-11-04 10:14:21 +0200103import static org.onosproject.net.flow.instructions.Instructions.modL0Lambda;
104import static org.onosproject.net.flow.instructions.Instructions.modL1OduSignalId;
alshabib44852fb2016-04-15 15:56:51 -0700105import static org.onosproject.provider.of.flow.util.OpenFlowValueMapper.*;
alshabib19fdc122014-10-03 11:38:19 -0700106
alshabib1c319ff2014-10-04 20:29:09 -0700107public class FlowEntryBuilder {
Jian Lia0d9a172016-04-01 16:58:06 -0700108 private static final Logger log = LoggerFactory.getLogger(FlowEntryBuilder.class);
alshabib6b5cfec2014-09-18 17:42:18 -0700109
110 private final OFFlowStatsEntry stat;
111 private final OFFlowRemoved removed;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800112 private final OFFlowMod flowMod;
alshabib6b5cfec2014-09-18 17:42:18 -0700113
114 private final Match match;
alshabib346b5b32015-03-06 00:42:16 -0800115
Jonathan Hart67fc0972015-03-19 15:21:20 -0700116 // All actions are contained in an OFInstruction. For OF1.0
117 // the instruction type is apply instruction (immediate set in ONOS speak)
alshabib346b5b32015-03-06 00:42:16 -0800118 private final List<OFInstruction> instructions;
alshabib6b5cfec2014-09-18 17:42:18 -0700119
Jonathan Harte4e74f02016-03-03 12:57:40 -0800120 private final DeviceId deviceId;
alshabib6b5cfec2014-09-18 17:42:18 -0700121
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800122 public enum FlowType { STAT, REMOVED, MOD }
123
124 private final FlowType type;
alshabib6b5cfec2014-09-18 17:42:18 -0700125
Jonathan Hart3c259162015-10-21 21:31:19 -0700126 private final DriverService driverService;
127
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900128 // NewAdaptiveFlowStatsCollector for AdaptiveFlowSampling mode,
129 // null is not AFM mode, namely SimpleStatsCollector mode
130 private NewAdaptiveFlowStatsCollector afsc;
131
Jonathan Harte4e74f02016-03-03 12:57:40 -0800132 public FlowEntryBuilder(DeviceId deviceId, OFFlowStatsEntry entry, DriverService driverService) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800133 this.stat = entry;
134 this.match = entry.getMatch();
alshabib346b5b32015-03-06 00:42:16 -0800135 this.instructions = getInstructions(entry);
Jonathan Harte4e74f02016-03-03 12:57:40 -0800136 this.deviceId = deviceId;
Saurav Dasfa2fa932015-03-03 11:29:48 -0800137 this.removed = null;
138 this.flowMod = null;
139 this.type = FlowType.STAT;
Jonathan Hart3c259162015-10-21 21:31:19 -0700140 this.driverService = driverService;
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900141 this.afsc = null;
Saurav Dasfa2fa932015-03-03 11:29:48 -0800142 }
143
Jonathan Harte4e74f02016-03-03 12:57:40 -0800144 public FlowEntryBuilder(DeviceId deviceId, OFFlowRemoved removed, DriverService driverService) {
alshabib6b5cfec2014-09-18 17:42:18 -0700145 this.match = removed.getMatch();
146 this.removed = removed;
Jonathan Harte4e74f02016-03-03 12:57:40 -0800147 this.deviceId = deviceId;
alshabib346b5b32015-03-06 00:42:16 -0800148 this.instructions = null;
alshabib6b5cfec2014-09-18 17:42:18 -0700149 this.stat = null;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800150 this.flowMod = null;
151 this.type = FlowType.REMOVED;
Jonathan Hart3c259162015-10-21 21:31:19 -0700152 this.driverService = driverService;
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900153 this.afsc = null;
alshabib6b5cfec2014-09-18 17:42:18 -0700154 }
155
Jonathan Harte4e74f02016-03-03 12:57:40 -0800156 public FlowEntryBuilder(DeviceId deviceId, OFFlowMod fm, DriverService driverService) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800157 this.match = fm.getMatch();
Jonathan Harte4e74f02016-03-03 12:57:40 -0800158 this.deviceId = deviceId;
alshabib346b5b32015-03-06 00:42:16 -0800159 this.instructions = getInstructions(fm);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800160 this.type = FlowType.MOD;
161 this.flowMod = fm;
162 this.stat = null;
163 this.removed = null;
Jonathan Hart3c259162015-10-21 21:31:19 -0700164 this.driverService = driverService;
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900165 this.afsc = null;
166 }
167
168 public FlowEntryBuilder withSetAfsc(NewAdaptiveFlowStatsCollector afsc) {
169 this.afsc = afsc;
170 return this;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800171 }
alshabib1c319ff2014-10-04 20:29:09 -0700172
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800173 public FlowEntry build(FlowEntryState... state) {
Brian O'Connor21564612015-12-05 19:24:59 -0800174 FlowRule.Builder builder;
175 try {
176 switch (this.type) {
177 case STAT:
178 builder = DefaultFlowRule.builder()
Jonathan Harte4e74f02016-03-03 12:57:40 -0800179 .forDevice(deviceId)
Brian O'Connor21564612015-12-05 19:24:59 -0800180 .withSelector(buildSelector())
181 .withTreatment(buildTreatment())
182 .withPriority(stat.getPriority())
Thiago Santos943cd542017-01-13 13:05:09 -0300183 .withIdleTimeout(stat.getIdleTimeout())
Jonathan Hartd1ce4b02016-01-11 13:28:36 -0800184 .withCookie(stat.getCookie().getValue());
185 if (stat.getVersion() != OFVersion.OF_10) {
186 builder.forTable(stat.getTableId().getValue());
187 }
alshabibbdcbb102015-04-22 14:16:38 -0700188
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900189 if (afsc != null) {
190 FlowEntry.FlowLiveType liveType = afsc.calFlowLiveType(stat.getDurationSec());
191 return new DefaultFlowEntry(builder.build(), FlowEntryState.ADDED,
192 SECONDS.toNanos(stat.getDurationSec())
193 + stat.getDurationNsec(), NANOSECONDS,
194 liveType,
195 stat.getPacketCount().getValue(),
196 stat.getByteCount().getValue());
197 } else {
198 return new DefaultFlowEntry(builder.build(), FlowEntryState.ADDED,
199 stat.getDurationSec(),
200 stat.getPacketCount().getValue(),
201 stat.getByteCount().getValue());
202 }
203
Brian O'Connor21564612015-12-05 19:24:59 -0800204 case REMOVED:
205 builder = DefaultFlowRule.builder()
Jonathan Harte4e74f02016-03-03 12:57:40 -0800206 .forDevice(deviceId)
Brian O'Connor21564612015-12-05 19:24:59 -0800207 .withSelector(buildSelector())
208 .withPriority(removed.getPriority())
Thiago Santos943cd542017-01-13 13:05:09 -0300209 .withIdleTimeout(removed.getIdleTimeout())
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700210 .withCookie(removed.getCookie().getValue())
211 .withReason(FlowRule.FlowRemoveReason.parseShort(removed.getReason()));
212
Brian O'Connor21564612015-12-05 19:24:59 -0800213 if (removed.getVersion() != OFVersion.OF_10) {
214 builder.forTable(removed.getTableId().getValue());
215 }
alshabibbdcbb102015-04-22 14:16:38 -0700216
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900217 if (afsc != null) {
218 FlowEntry.FlowLiveType liveType = afsc.calFlowLiveType(removed.getDurationSec());
219 return new DefaultFlowEntry(builder.build(), FlowEntryState.REMOVED,
220 SECONDS.toNanos(removed.getDurationSec())
221 + removed.getDurationNsec(), NANOSECONDS,
222 liveType,
223 removed.getPacketCount().getValue(),
224 removed.getByteCount().getValue());
225 } else {
226 return new DefaultFlowEntry(builder.build(), FlowEntryState.REMOVED,
227 removed.getDurationSec(),
228 removed.getPacketCount().getValue(),
229 removed.getByteCount().getValue());
230 }
231
Brian O'Connor21564612015-12-05 19:24:59 -0800232 case MOD:
233 FlowEntryState flowState = state.length > 0 ? state[0] : FlowEntryState.FAILED;
234 builder = DefaultFlowRule.builder()
Jonathan Harte4e74f02016-03-03 12:57:40 -0800235 .forDevice(deviceId)
Brian O'Connor21564612015-12-05 19:24:59 -0800236 .withSelector(buildSelector())
237 .withTreatment(buildTreatment())
238 .withPriority(flowMod.getPriority())
Thiago Santos943cd542017-01-13 13:05:09 -0300239 .withIdleTimeout(flowMod.getIdleTimeout())
Brian O'Connor21564612015-12-05 19:24:59 -0800240 .withCookie(flowMod.getCookie().getValue());
241 if (flowMod.getVersion() != OFVersion.OF_10) {
242 builder.forTable(flowMod.getTableId().getValue());
243 }
alshabibbdcbb102015-04-22 14:16:38 -0700244
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900245 if (afsc != null) {
246 FlowEntry.FlowLiveType liveType = FlowEntry.FlowLiveType.IMMEDIATE;
247 return new DefaultFlowEntry(builder.build(), flowState, 0, liveType, 0, 0);
248 } else {
249 return new DefaultFlowEntry(builder.build(), flowState, 0, 0, 0);
250 }
Brian O'Connor21564612015-12-05 19:24:59 -0800251 default:
252 log.error("Unknown flow type : {}", this.type);
253 return null;
254 }
255 } catch (UnsupportedOperationException e) {
256 log.warn("Error building flow entry", e);
257 return null;
alshabib6b5cfec2014-09-18 17:42:18 -0700258 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800259
alshabib6b5cfec2014-09-18 17:42:18 -0700260 }
261
alshabib346b5b32015-03-06 00:42:16 -0800262 private List<OFInstruction> getInstructions(OFFlowMod entry) {
alshabib19fdc122014-10-03 11:38:19 -0700263 switch (entry.getVersion()) {
264 case OF_10:
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700265 return Lists.newArrayList(OFFactoryVer13.INSTANCE.instructions()
266 .applyActions(
267 entry.getActions()));
alshabib19fdc122014-10-03 11:38:19 -0700268 case OF_11:
269 case OF_12:
270 case OF_13:
alshabib346b5b32015-03-06 00:42:16 -0800271 return entry.getInstructions();
272 default:
273 log.warn("Unknown OF version {}", entry.getVersion());
274 }
275 return Lists.newLinkedList();
276 }
277
278 private List<OFInstruction> getInstructions(OFFlowStatsEntry entry) {
279 switch (entry.getVersion()) {
280 case OF_10:
281 return Lists.newArrayList(
282 OFFactoryVer13.INSTANCE.instructions().applyActions(entry.getActions()));
283 case OF_11:
284 case OF_12:
285 case OF_13:
286 return entry.getInstructions();
alshabib19fdc122014-10-03 11:38:19 -0700287 default:
288 log.warn("Unknown OF version {}", entry.getVersion());
289 }
290 return Lists.newLinkedList();
291 }
alshabib6b5cfec2014-09-18 17:42:18 -0700292
293 private TrafficTreatment buildTreatment() {
tom9a693fd2014-10-03 11:32:19 -0700294 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabib346b5b32015-03-06 00:42:16 -0800295 for (OFInstruction in : instructions) {
296 switch (in.getType()) {
297 case GOTO_TABLE:
alshabibbdcbb102015-04-22 14:16:38 -0700298 builder.transition(((int) ((OFInstructionGotoTable) in)
299 .getTableId().getValue()));
alshabib346b5b32015-03-06 00:42:16 -0800300 break;
301 case WRITE_METADATA:
Saurav Das86af8f12015-05-25 23:55:33 -0700302 OFInstructionWriteMetadata m = (OFInstructionWriteMetadata) in;
303 builder.writeMetadata(m.getMetadata().getValue(),
304 m.getMetadataMask().getValue());
alshabib346b5b32015-03-06 00:42:16 -0800305 break;
306 case WRITE_ACTIONS:
307 builder.deferred();
308 buildActions(((OFInstructionWriteActions) in).getActions(),
309 builder);
310 break;
311 case APPLY_ACTIONS:
312 builder.immediate();
313 buildActions(((OFInstructionApplyActions) in).getActions(),
314 builder);
315 break;
316 case CLEAR_ACTIONS:
317 builder.wipeDeferred();
318 break;
319 case EXPERIMENTER:
320 break;
321 case METER:
322 break;
323 default:
324 log.warn("Unknown instructions type {}", in.getType());
alshabib6b5cfec2014-09-18 17:42:18 -0700325 }
326 }
327
328 return builder.build();
329 }
330
Jian Lia0d9a172016-04-01 16:58:06 -0700331 /**
332 * Configures traffic treatment builder with a given collection of actions.
333 *
334 * @param actions a set of OpenFlow actions
335 * @param builder traffic treatment builder
336 * @param driverHandler driver handler
337 * @param deviceId device identifier
338 * @return configured traffic treatment builder
339 */
340 public static TrafficTreatment.Builder configureTreatmentBuilder(List<OFAction> actions,
341 TrafficTreatment.Builder builder,
342 DriverHandler driverHandler,
343 DeviceId deviceId) {
344 ExtensionTreatmentInterpreter interpreter;
Charles Chan14967c22015-12-07 11:11:50 -0800345 if (driverHandler.hasBehaviour(ExtensionTreatmentInterpreter.class)) {
Jian Lia0d9a172016-04-01 16:58:06 -0700346 interpreter = driverHandler.behaviour(ExtensionTreatmentInterpreter.class);
Charles Chan14967c22015-12-07 11:11:50 -0800347 } else {
Jian Lia0d9a172016-04-01 16:58:06 -0700348 interpreter = null;
Charles Chan14967c22015-12-07 11:11:50 -0800349 }
350
alshabib346b5b32015-03-06 00:42:16 -0800351 for (OFAction act : actions) {
352 switch (act.getType()) {
353 case OUTPUT:
354 OFActionOutput out = (OFActionOutput) act;
355 builder.setOutput(
356 PortNumber.portNumber(out.getPort().getPortNumber()));
357 break;
358 case SET_VLAN_VID:
359 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
360 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
361 break;
362 case SET_VLAN_PCP:
363 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
364 builder.setVlanPcp(pcp.getVlanPcp().getValue());
365 break;
366 case SET_DL_DST:
367 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
368 builder.setEthDst(
369 MacAddress.valueOf(dldst.getDlAddr().getLong()));
370 break;
371 case SET_DL_SRC:
372 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
373 builder.setEthSrc(
374 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
alshabib346b5b32015-03-06 00:42:16 -0800375 break;
376 case SET_NW_DST:
377 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
378 IPv4Address di = nwdst.getNwAddr();
379 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
380 break;
381 case SET_NW_SRC:
382 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
383 IPv4Address si = nwsrc.getNwAddr();
384 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
385 break;
386 case EXPERIMENTER:
387 OFActionExperimenter exp = (OFActionExperimenter) act;
388 if (exp.getExperimenter() == 0x80005A06 ||
389 exp.getExperimenter() == 0x748771) {
390 OFActionCircuit ct = (OFActionCircuit) exp;
Yafit Hadar4cc65f72016-02-10 11:23:06 +0200391 CircuitSignalID circuitSignalID = ((OFOxmOchSigid) ct.getField()).getValue();
392 builder.add(Instructions.modL0Lambda(Lambda.ochSignal(
393 lookupGridType(circuitSignalID.getGridType()),
394 lookupChannelSpacing(circuitSignalID.getChannelSpacing()),
395 circuitSignalID.getChannelNumber(), circuitSignalID.getSpectralWidth())));
Jian Lia0d9a172016-04-01 16:58:06 -0700396 } else if (interpreter != null) {
397 builder.extension(interpreter.mapAction(exp), deviceId);
alshabib346b5b32015-03-06 00:42:16 -0800398 } else {
399 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
400 }
401 break;
402 case SET_FIELD:
403 OFActionSetField setField = (OFActionSetField) act;
Jian Lia0d9a172016-04-01 16:58:06 -0700404 handleSetField(builder, setField, driverHandler, deviceId);
alshabib346b5b32015-03-06 00:42:16 -0800405 break;
406 case POP_MPLS:
407 OFActionPopMpls popMpls = (OFActionPopMpls) act;
Ray Milkey84d5a292016-02-22 14:04:01 -0800408 builder.popMpls(new EthType(popMpls.getEthertype().getValue()));
alshabib346b5b32015-03-06 00:42:16 -0800409 break;
410 case PUSH_MPLS:
411 builder.pushMpls();
412 break;
413 case COPY_TTL_IN:
414 builder.copyTtlIn();
415 break;
416 case COPY_TTL_OUT:
417 builder.copyTtlOut();
418 break;
419 case DEC_MPLS_TTL:
420 builder.decMplsTtl();
421 break;
422 case DEC_NW_TTL:
423 builder.decNwTtl();
424 break;
425 case GROUP:
426 OFActionGroup group = (OFActionGroup) act;
427 builder.group(new DefaultGroupId(group.getGroup().getGroupNumber()));
428 break;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200429 case SET_QUEUE:
430 OFActionSetQueue setQueue = (OFActionSetQueue) act;
431 builder.setQueue(setQueue.getQueueId());
432 break;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200433 case ENQUEUE:
434 OFActionEnqueue enqueue = (OFActionEnqueue) act;
Jian Liffef5002016-04-04 23:27:37 -0700435 builder.setQueue(enqueue.getQueueId(),
436 PortNumber.portNumber(enqueue.getPort().getPortNumber()));
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200437 break;
Jonathan Hart67fc0972015-03-19 15:21:20 -0700438 case STRIP_VLAN:
alshabib346b5b32015-03-06 00:42:16 -0800439 case POP_VLAN:
440 builder.popVlan();
441 break;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700442 case PUSH_VLAN:
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -0500443 OFActionPushVlan pushVlan = (OFActionPushVlan) act;
444 builder.pushVlan(new EthType((short) pushVlan.getEthertype().getValue()));
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700445 break;
alshabib346b5b32015-03-06 00:42:16 -0800446 case SET_TP_DST:
447 case SET_TP_SRC:
448 case POP_PBB:
449 case PUSH_PBB:
alshabib346b5b32015-03-06 00:42:16 -0800450 case SET_MPLS_LABEL:
451 case SET_MPLS_TC:
452 case SET_MPLS_TTL:
453 case SET_NW_ECN:
454 case SET_NW_TOS:
455 case SET_NW_TTL:
alshabib346b5b32015-03-06 00:42:16 -0800456
alshabib346b5b32015-03-06 00:42:16 -0800457 default:
458 log.warn("Action type {} not yet implemented.", act.getType());
459 }
460 }
461 return builder;
462 }
463
Jian Lia0d9a172016-04-01 16:58:06 -0700464 private TrafficTreatment.Builder buildActions(List<OFAction> actions,
465 TrafficTreatment.Builder builder) {
Jonathan Harte4e74f02016-03-03 12:57:40 -0800466 DriverHandler driverHandler = getDriver(deviceId);
Jian Lia0d9a172016-04-01 16:58:06 -0700467
468 return configureTreatmentBuilder(actions, builder, driverHandler, deviceId);
469 }
470
Charles Chancad338a2016-09-16 18:03:11 -0700471 // CHECKSTYLE IGNORE MethodLength FOR NEXT 1 LINES
Jian Lia0d9a172016-04-01 16:58:06 -0700472 private static void handleSetField(TrafficTreatment.Builder builder,
473 OFActionSetField action,
474 DriverHandler driverHandler,
475 DeviceId deviceId) {
Charles Chan14967c22015-12-07 11:11:50 -0800476 ExtensionTreatmentInterpreter treatmentInterpreter;
477 if (driverHandler.hasBehaviour(ExtensionTreatmentInterpreter.class)) {
478 treatmentInterpreter = driverHandler.behaviour(ExtensionTreatmentInterpreter.class);
479 } else {
480 treatmentInterpreter = null;
481 }
Jonathan Hart3c259162015-10-21 21:31:19 -0700482 OFOxm<?> oxm = action.getField();
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800483 switch (oxm.getMatchField().id) {
484 case VLAN_PCP:
485 @SuppressWarnings("unchecked")
486 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
487 builder.setVlanPcp(vlanpcp.getValue().getValue());
488 break;
489 case VLAN_VID:
Charles Chan4211baa2016-04-20 17:10:40 -0700490 if (treatmentInterpreter != null) {
491 try {
492 builder.extension(treatmentInterpreter.mapAction(action), deviceId);
493 break;
494 } catch (UnsupportedOperationException e) {
495 log.debug("Unsupported action extension; defaulting to native OF");
496 }
Charles Chan14967c22015-12-07 11:11:50 -0800497 }
alshabib44852fb2016-04-15 15:56:51 -0700498 @SuppressWarnings("unchecked")
499 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
500 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800501 break;
502 case ETH_DST:
503 @SuppressWarnings("unchecked")
504 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
505 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
506 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
507 break;
508 case ETH_SRC:
509 @SuppressWarnings("unchecked")
510 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
511 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
512 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
513 break;
514 case IPV4_DST:
515 @SuppressWarnings("unchecked")
516 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
517 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
518 break;
519 case IPV4_SRC:
520 @SuppressWarnings("unchecked")
521 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
522 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
523 break;
sangho3f97a17d2015-01-29 22:56:29 -0800524 case MPLS_LABEL:
525 @SuppressWarnings("unchecked")
526 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100527 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
sangho3f97a17d2015-01-29 22:56:29 -0800528 break;
Saurav Das73a7dd42015-08-19 22:20:31 -0700529 case MPLS_BOS:
530 @SuppressWarnings("unchecked")
Pier Ventre23f78672016-11-15 08:46:34 -0800531 OFOxm<OFBooleanValue> mplsBos = (OFOxm<OFBooleanValue>) oxm;
532 builder.setMplsBos(mplsBos.getValue().getValue());
Saurav Das73a7dd42015-08-19 22:20:31 -0700533 break;
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700534 case TUNNEL_ID:
535 @SuppressWarnings("unchecked")
536 OFOxm<U64> tunnelId = (OFOxm<U64>) oxm;
537 builder.setTunnelId(tunnelId.getValue().getValue());
538 break;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700539 case TCP_DST:
540 @SuppressWarnings("unchecked")
541 OFOxm<TransportPort> tcpdst = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700542 builder.setTcpDst(TpPort.tpPort(tcpdst.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700543 break;
544 case TCP_SRC:
545 @SuppressWarnings("unchecked")
546 OFOxm<TransportPort> tcpsrc = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700547 builder.setTcpSrc(TpPort.tpPort(tcpsrc.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700548 break;
549 case UDP_DST:
550 @SuppressWarnings("unchecked")
551 OFOxm<TransportPort> udpdst = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700552 builder.setUdpDst(TpPort.tpPort(udpdst.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700553 break;
554 case UDP_SRC:
555 @SuppressWarnings("unchecked")
556 OFOxm<TransportPort> udpsrc = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700557 builder.setUdpSrc(TpPort.tpPort(udpsrc.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700558 break;
Jonathan Hart3c259162015-10-21 21:31:19 -0700559 case TUNNEL_IPV4_DST:
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530560 case NSP:
561 case NSI:
562 case NSH_C1:
563 case NSH_C2:
564 case NSH_C3:
565 case NSH_C4:
566 case NSH_MDTYPE:
567 case NSH_NP:
568 case ENCAP_ETH_SRC:
569 case ENCAP_ETH_DST:
570 case ENCAP_ETH_TYPE:
571 case TUN_GPE_NP:
Jian Liffef5002016-04-04 23:27:37 -0700572 if (treatmentInterpreter != null) {
573 try {
574 builder.extension(treatmentInterpreter.mapAction(action), deviceId);
575 } catch (UnsupportedOperationException e) {
alshabib44852fb2016-04-15 15:56:51 -0700576 log.debug(e.getMessage());
Jian Liffef5002016-04-04 23:27:37 -0700577 }
Jonathan Hart3c259162015-10-21 21:31:19 -0700578 }
579 break;
Yafit Hadar73514612015-11-04 10:14:21 +0200580 case EXP_ODU_SIG_ID:
581 @SuppressWarnings("unchecked")
582 OFOxm<OduSignalID> oduID = (OFOxm<OduSignalID>) oxm;
583 OduSignalID oduSignalID = oduID.getValue();
584 OduSignalId oduSignalId = OduSignalId.oduSignalId(oduSignalID.getTpn(),
585 oduSignalID.getTslen(),
586 oduSignalID.getTsmap());
587 builder.add(modL1OduSignalId(oduSignalId));
588 break;
589 case EXP_OCH_SIG_ID:
590 try {
591 @SuppressWarnings("unchecked")
592 OFOxm<CircuitSignalID> ochId = (OFOxm<CircuitSignalID>) oxm;
593 CircuitSignalID circuitSignalID = ochId.getValue();
594 builder.add(modL0Lambda(Lambda.ochSignal(
595 lookupGridType(circuitSignalID.getGridType()),
596 lookupChannelSpacing(circuitSignalID.getChannelSpacing()),
597 circuitSignalID.getChannelNumber(), circuitSignalID.getSpectralWidth())));
598 } catch (NoMappingFoundException e) {
599 log.warn(e.getMessage());
600 break;
601 }
602 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800603 case ARP_OP:
lishuai67574ce2016-05-12 16:39:59 +0800604 @SuppressWarnings("unchecked")
605 OFOxm<org.projectfloodlight.openflow.types.ArpOpcode> arpop =
606 (OFOxm<org.projectfloodlight.openflow.types.ArpOpcode>) oxm;
607 builder.setArpOp((short) arpop.getValue().getOpcode());
608 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800609 case ARP_SHA:
lishuai67574ce2016-05-12 16:39:59 +0800610 @SuppressWarnings("unchecked")
611 OFOxm<org.projectfloodlight.openflow.types.MacAddress> arpsha =
612 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
613 builder.setArpSha(MacAddress.valueOf(arpsha.getValue().getLong()));
614 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800615 case ARP_SPA:
lishuai67574ce2016-05-12 16:39:59 +0800616 @SuppressWarnings("unchecked")
617 OFOxm<IPv4Address> arpspa = (OFOxm<IPv4Address>) oxm;
618 builder.setArpSpa(Ip4Address.valueOf(arpspa.getValue().getInt()));
619 break;
Charles Chancad338a2016-09-16 18:03:11 -0700620 case OFDPA_MPLS_TYPE:
Pier Ventre6f630052016-10-18 09:58:41 -0700621 case OFDPA_OVID:
Pier Ventre9cf536b2016-10-21 13:30:18 -0700622 case OFDPA_MPLS_L2_PORT:
Pier Ventredb252cc2016-10-21 21:54:26 -0700623 case OFDPA_QOS_INDEX:
Charles Chancad338a2016-09-16 18:03:11 -0700624 if (treatmentInterpreter != null) {
625 try {
626 builder.extension(treatmentInterpreter.mapAction(action), deviceId);
627 break;
628 } catch (UnsupportedOperationException e) {
629 log.warn("Unsupported action extension");
630 }
631 }
632 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800633 case ARP_THA:
634 case ARP_TPA:
635 case BSN_EGR_PORT_GROUP_ID:
636 case BSN_GLOBAL_VRF_ALLOWED:
637 case BSN_IN_PORTS_128:
638 case BSN_L3_DST_CLASS_ID:
639 case BSN_L3_INTERFACE_CLASS_ID:
640 case BSN_L3_SRC_CLASS_ID:
641 case BSN_LAG_ID:
642 case BSN_TCP_FLAGS:
643 case BSN_UDF0:
644 case BSN_UDF1:
645 case BSN_UDF2:
646 case BSN_UDF3:
647 case BSN_UDF4:
648 case BSN_UDF5:
649 case BSN_UDF6:
650 case BSN_UDF7:
651 case BSN_VLAN_XLATE_PORT_GROUP_ID:
652 case BSN_VRF:
653 case ETH_TYPE:
654 case ICMPV4_CODE:
655 case ICMPV4_TYPE:
656 case ICMPV6_CODE:
657 case ICMPV6_TYPE:
658 case IN_PHY_PORT:
659 case IN_PORT:
660 case IPV6_DST:
661 case IPV6_FLABEL:
662 case IPV6_ND_SLL:
663 case IPV6_ND_TARGET:
664 case IPV6_ND_TLL:
665 case IPV6_SRC:
666 case IP_DSCP:
667 case IP_ECN:
668 case IP_PROTO:
669 case METADATA:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800670 case MPLS_TC:
671 case OCH_SIGID:
672 case OCH_SIGID_BASIC:
673 case OCH_SIGTYPE:
674 case OCH_SIGTYPE_BASIC:
675 case SCTP_DST:
676 case SCTP_SRC:
Yafit Hadar73514612015-11-04 10:14:21 +0200677 case EXP_ODU_SIGTYPE:
678 case EXP_OCH_SIGTYPE:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800679 default:
680 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
681 break;
682 }
683 }
684
Sho SHIMIZU6f1b09e2015-05-05 11:26:22 -0700685 // CHECKSTYLE IGNORE MethodLength FOR NEXT 1 LINES
alshabib6b5cfec2014-09-18 17:42:18 -0700686 private TrafficSelector buildSelector() {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800687 MacAddress mac;
688 Ip4Prefix ip4Prefix;
689 Ip6Address ip6Address;
690 Ip6Prefix ip6Prefix;
BitOhenryc1e5fcc2015-11-23 20:47:53 +0800691 Ip4Address ip;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800692
Jonathan Harte4e74f02016-03-03 12:57:40 -0800693 DriverHandler driverHandler = getDriver(deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800694 ExtensionSelectorInterpreter selectorInterpreter;
695 if (driverHandler.hasBehaviour(ExtensionSelectorInterpreter.class)) {
696 selectorInterpreter = driverHandler.behaviour(ExtensionSelectorInterpreter.class);
697 } else {
698 selectorInterpreter = null;
699 }
700
tom9a693fd2014-10-03 11:32:19 -0700701 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700702 for (MatchField<?> field : match.getMatchFields()) {
703 switch (field.id) {
704 case IN_PORT:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800705 builder.matchInPort(PortNumber
alshabib010c31d2014-09-26 10:01:12 -0700706 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700707 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800708 case IN_PHY_PORT:
709 builder.matchInPhyPort(PortNumber
710 .portNumber(match.get(MatchField.IN_PHY_PORT).getPortNumber()));
711 break;
712 case METADATA:
713 long metadata =
714 match.get(MatchField.METADATA).getValue().getValue();
715 builder.matchMetadata(metadata);
716 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700717 case ETH_DST:
Saurav Das9d6c86b2016-02-19 09:01:07 -0800718 if (match.isPartiallyMasked(MatchField.ETH_DST)) {
719 Masked<org.projectfloodlight.openflow.types.MacAddress> maskedMac =
720 match.getMasked(MatchField.ETH_DST);
721 builder.matchEthDstMasked(MacAddress.valueOf(maskedMac.getValue().getLong()),
722 MacAddress.valueOf(maskedMac.getMask().getLong()));
723 } else {
724 mac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
725 builder.matchEthDst(mac);
726 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800727 break;
728 case ETH_SRC:
Saurav Das9d6c86b2016-02-19 09:01:07 -0800729 if (match.isPartiallyMasked(MatchField.ETH_SRC)) {
730 Masked<org.projectfloodlight.openflow.types.MacAddress> maskedMac =
731 match.getMasked(MatchField.ETH_SRC);
732 builder.matchEthSrcMasked(MacAddress.valueOf(maskedMac.getValue().getLong()),
733 MacAddress.valueOf(maskedMac.getMask().getLong()));
734 } else {
735 mac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
736 builder.matchEthSrc(mac);
737 }
alshabib6b5cfec2014-09-18 17:42:18 -0700738 break;
739 case ETH_TYPE:
740 int ethType = match.get(MatchField.ETH_TYPE).getValue();
Jonathan Hart8cf212a2015-10-29 17:42:03 -0700741 builder.matchEthType((short) ethType);
alshabib6b5cfec2014-09-18 17:42:18 -0700742 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700743 case VLAN_VID:
Charles Chan14967c22015-12-07 11:11:50 -0800744 if (selectorInterpreter != null &&
745 selectorInterpreter.supported(ExtensionSelectorTypes.OFDPA_MATCH_VLAN_VID.type())) {
746 if (match.getVersion().equals(OFVersion.OF_13)) {
747 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.VLAN_VID);
748 builder.extension(selectorInterpreter.mapOxm(oxm),
Jonathan Harte4e74f02016-03-03 12:57:40 -0800749 deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800750 } else {
751 break;
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800752 }
753 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800754 VlanId vlanId = null;
755 if (match.isPartiallyMasked(MatchField.VLAN_VID)) {
756 Masked<OFVlanVidMatch> masked = match.getMasked(MatchField.VLAN_VID);
757 if (masked.getValue().equals(OFVlanVidMatch.PRESENT)
758 && masked.getMask().equals(OFVlanVidMatch.PRESENT)) {
759 vlanId = VlanId.ANY;
760 }
Jonathan Hart1468fee2015-07-16 18:50:34 -0700761 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800762 if (!match.get(MatchField.VLAN_VID).isPresentBitSet()) {
763 vlanId = VlanId.NONE;
764 } else {
765 vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
766 }
Jonathan Hart1468fee2015-07-16 18:50:34 -0700767 }
Charles Chan14967c22015-12-07 11:11:50 -0800768 if (vlanId != null) {
769 builder.matchVlanId(vlanId);
770 }
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800771 }
alshabib6b5cfec2014-09-18 17:42:18 -0700772 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800773 case VLAN_PCP:
774 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
775 builder.matchVlanPcp(vlanPcp);
776 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800777 case IP_DSCP:
778 byte ipDscp = match.get(MatchField.IP_DSCP).getDscpValue();
779 builder.matchIPDscp(ipDscp);
780 break;
781 case IP_ECN:
782 byte ipEcn = match.get(MatchField.IP_ECN).getEcnValue();
783 builder.matchIPEcn(ipEcn);
784 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800785 case IP_PROTO:
786 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
787 builder.matchIPProtocol((byte) proto);
788 break;
789 case IPV4_SRC:
790 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
791 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800792 ip4Prefix = Ip4Prefix.valueOf(
793 maskedIp.getValue().getInt(),
794 maskedIp.getMask().asCidrMaskLength());
795 } else {
796 ip4Prefix = Ip4Prefix.valueOf(
797 match.get(MatchField.IPV4_SRC).getInt(),
798 Ip4Prefix.MAX_MASK_LENGTH);
799 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800800 builder.matchIPSrc(ip4Prefix);
801 break;
802 case IPV4_DST:
803 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
804 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800805 ip4Prefix = Ip4Prefix.valueOf(
806 maskedIp.getValue().getInt(),
807 maskedIp.getMask().asCidrMaskLength());
808 } else {
809 ip4Prefix = Ip4Prefix.valueOf(
810 match.get(MatchField.IPV4_DST).getInt(),
811 Ip4Prefix.MAX_MASK_LENGTH);
812 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800813 builder.matchIPDst(ip4Prefix);
Jonathan Hart34bc6142014-10-17 11:00:43 -0700814 break;
815 case TCP_SRC:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700816 builder.matchTcpSrc(TpPort.tpPort(match.get(MatchField.TCP_SRC).getPort()));
Jonathan Hart34bc6142014-10-17 11:00:43 -0700817 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800818 case TCP_DST:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700819 builder.matchTcpDst(TpPort.tpPort(match.get(MatchField.TCP_DST).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800820 break;
821 case UDP_SRC:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700822 builder.matchUdpSrc(TpPort.tpPort(match.get(MatchField.UDP_SRC).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800823 break;
824 case UDP_DST:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700825 builder.matchUdpDst(TpPort.tpPort(match.get(MatchField.UDP_DST).getPort()));
Sho SHIMIZU6f1b09e2015-05-05 11:26:22 -0700826 break;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100827 case MPLS_LABEL:
828 builder.matchMplsLabel(MplsLabel.mplsLabel((int) match.get(MatchField.MPLS_LABEL)
829 .getValue()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800830 break;
Saurav Dasffc5bbc2015-08-18 23:30:19 -0700831 case MPLS_BOS:
832 builder.matchMplsBos(match.get(MatchField.MPLS_BOS).getValue());
833 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800834 case SCTP_SRC:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700835 builder.matchSctpSrc(TpPort.tpPort(match.get(MatchField.SCTP_SRC).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800836 break;
837 case SCTP_DST:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700838 builder.matchSctpDst(TpPort.tpPort(match.get(MatchField.SCTP_DST).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800839 break;
840 case ICMPV4_TYPE:
841 byte icmpType = (byte) match.get(MatchField.ICMPV4_TYPE).getType();
842 builder.matchIcmpType(icmpType);
843 break;
844 case ICMPV4_CODE:
845 byte icmpCode = (byte) match.get(MatchField.ICMPV4_CODE).getCode();
846 builder.matchIcmpCode(icmpCode);
847 break;
848 case IPV6_SRC:
849 if (match.isPartiallyMasked(MatchField.IPV6_SRC)) {
850 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_SRC);
851 ip6Prefix = Ip6Prefix.valueOf(
852 maskedIp.getValue().getBytes(),
853 maskedIp.getMask().asCidrMaskLength());
854 } else {
855 ip6Prefix = Ip6Prefix.valueOf(
856 match.get(MatchField.IPV6_SRC).getBytes(),
857 Ip6Prefix.MAX_MASK_LENGTH);
858 }
859 builder.matchIPv6Src(ip6Prefix);
860 break;
861 case IPV6_DST:
862 if (match.isPartiallyMasked(MatchField.IPV6_DST)) {
863 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_DST);
864 ip6Prefix = Ip6Prefix.valueOf(
865 maskedIp.getValue().getBytes(),
866 maskedIp.getMask().asCidrMaskLength());
867 } else {
868 ip6Prefix = Ip6Prefix.valueOf(
869 match.get(MatchField.IPV6_DST).getBytes(),
870 Ip6Prefix.MAX_MASK_LENGTH);
871 }
872 builder.matchIPv6Dst(ip6Prefix);
873 break;
874 case IPV6_FLABEL:
875 int flowLabel =
876 match.get(MatchField.IPV6_FLABEL).getIPv6FlowLabelValue();
877 builder.matchIPv6FlowLabel(flowLabel);
878 break;
879 case ICMPV6_TYPE:
880 byte icmpv6type = (byte) match.get(MatchField.ICMPV6_TYPE).getValue();
881 builder.matchIcmpv6Type(icmpv6type);
882 break;
883 case ICMPV6_CODE:
884 byte icmpv6code = (byte) match.get(MatchField.ICMPV6_CODE).getValue();
885 builder.matchIcmpv6Code(icmpv6code);
886 break;
887 case IPV6_ND_TARGET:
888 ip6Address =
889 Ip6Address.valueOf(match.get(MatchField.IPV6_ND_TARGET).getBytes());
890 builder.matchIPv6NDTargetAddress(ip6Address);
891 break;
892 case IPV6_ND_SLL:
893 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_SLL).getLong());
894 builder.matchIPv6NDSourceLinkLayerAddress(mac);
895 break;
896 case IPV6_ND_TLL:
897 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_TLL).getLong());
898 builder.matchIPv6NDTargetLinkLayerAddress(mac);
899 break;
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800900 case IPV6_EXTHDR:
Charles M.C. Chan2184de12015-04-26 02:24:53 +0800901 builder.matchIPv6ExthdrFlags((short) match.get(MatchField.IPV6_EXTHDR)
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700902 .getValue());
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800903 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700904 case OCH_SIGID:
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700905 CircuitSignalID sigId = match.get(MatchField.OCH_SIGID);
906 builder.add(matchLambda(Lambda.ochSignal(
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700907 lookupGridType(sigId.getGridType()), lookupChannelSpacing(sigId.getChannelSpacing()),
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -0700908 sigId.getChannelNumber(), sigId.getSpectralWidth())
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700909 ));
Marc De Leenheer49087752014-10-23 13:54:09 -0700910 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800911 case OCH_SIGTYPE:
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700912 U8 sigType = match.get(MatchField.OCH_SIGTYPE);
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700913 builder.add(matchOchSignalType(lookupOchSignalType((byte) sigType.getValue())));
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800914 break;
Yafit Hadar73514612015-11-04 10:14:21 +0200915 case EXP_OCH_SIG_ID:
916 try {
917 CircuitSignalID expSigId = match.get(MatchField.EXP_OCH_SIG_ID);
918 builder.add(matchLambda(Lambda.ochSignal(
919 lookupGridType(expSigId.getGridType()), lookupChannelSpacing(expSigId.getChannelSpacing()),
920 expSigId.getChannelNumber(), expSigId.getSpectralWidth())));
921 } catch (NoMappingFoundException e) {
922 log.warn(e.getMessage());
923 break;
924 }
925 break;
926 case EXP_OCH_SIGTYPE:
927 try {
928 U8 expOchSigType = match.get(MatchField.EXP_OCH_SIGTYPE);
929 builder.add(matchOchSignalType(lookupOchSignalType((byte) expOchSigType.getValue())));
930 } catch (NoMappingFoundException e) {
931 log.warn(e.getMessage());
932 break;
933 }
934 break;
935 case EXP_ODU_SIG_ID:
936 OduSignalId oduSignalId = OduSignalId.oduSignalId(match.get(MatchField.EXP_ODU_SIG_ID).getTpn(),
937 match.get(MatchField.EXP_ODU_SIG_ID).getTslen(),
938 match.get(MatchField.EXP_ODU_SIG_ID).getTsmap());
939 builder.add(matchOduSignalId(oduSignalId));
940 break;
941 case EXP_ODU_SIGTYPE:
942 try {
943 U8 oduSigType = match.get(MatchField.EXP_ODU_SIGTYPE);
944 builder.add(matchOduSignalType(lookupOduSignalType((byte) oduSigType.getValue())));
945 } catch (NoMappingFoundException e) {
946 log.warn(e.getMessage());
947 break;
948 }
949 break;
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700950 case TUNNEL_ID:
951 long tunnelId = match.get(MatchField.TUNNEL_ID).getValue();
952 builder.matchTunnelId(tunnelId);
953 break;
BitOhenryb40129a2015-11-30 12:41:18 +0800954 case ARP_OP:
955 int arpOp = match.get(MatchField.ARP_OP).getOpcode();
956 builder.matchArpOp(arpOp);
957 break;
BitOhenrya331b182015-11-23 08:39:37 +0800958 case ARP_SHA:
959 mac = MacAddress.valueOf(match.get(MatchField.ARP_SHA).getLong());
960 builder.matchArpSha(mac);
961 break;
BitOhenry296b4542015-11-24 08:41:58 +0800962 case ARP_SPA:
963 ip = Ip4Address.valueOf(match.get(MatchField.ARP_SPA).getInt());
964 builder.matchArpSpa(ip);
965 break;
BitOhenry76430852015-11-20 19:04:49 +0800966 case ARP_THA:
967 mac = MacAddress.valueOf(match.get(MatchField.ARP_THA).getLong());
968 builder.matchArpTha(mac);
969 break;
BitOhenryc1e5fcc2015-11-23 20:47:53 +0800970 case ARP_TPA:
971 ip = Ip4Address.valueOf(match.get(MatchField.ARP_TPA).getInt());
972 builder.matchArpTpa(ip);
973 break;
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530974 case NSP:
975 if (selectorInterpreter != null) {
976 try {
977 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.NSP);
978 builder.extension(selectorInterpreter.mapOxm(oxm), deviceId);
979 } catch (UnsupportedOperationException e) {
980 log.debug(e.getMessage());
981 }
982 }
983 break;
984 case NSI:
985 if (selectorInterpreter != null) {
986 try {
987 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.NSI);
988 builder.extension(selectorInterpreter.mapOxm(oxm), deviceId);
989 } catch (UnsupportedOperationException e) {
990 log.debug(e.getMessage());
991 }
992 }
993 break;
994 case ENCAP_ETH_TYPE:
995 if (selectorInterpreter != null) {
996 try {
997 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.ENCAP_ETH_TYPE);
998 builder.extension(selectorInterpreter.mapOxm(oxm), deviceId);
999 } catch (UnsupportedOperationException e) {
1000 log.debug(e.getMessage());
1001 }
1002 }
1003 break;
Pier Ventre6f630052016-10-18 09:58:41 -07001004 case OFDPA_OVID:
1005 if (selectorInterpreter != null &&
1006 selectorInterpreter.supported(ExtensionSelectorTypes.OFDPA_MATCH_OVID.type())) {
1007 if (match.getVersion().equals(OFVersion.OF_13)) {
1008 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.OFDPA_OVID);
1009 builder.extension(selectorInterpreter.mapOxm(oxm),
1010 deviceId);
1011 } else {
1012 break;
1013 }
1014 }
1015 break;
Pier Ventre9cf536b2016-10-21 13:30:18 -07001016 case OFDPA_MPLS_L2_PORT:
1017 if (selectorInterpreter != null &&
1018 selectorInterpreter.supported(ExtensionSelectorTypes.OFDPA_MATCH_MPLS_L2_PORT.type())) {
1019 if (match.getVersion().equals(OFVersion.OF_13)) {
1020 OFOxm oxm = ((OFMatchV3) match).getOxmList().get(MatchField.OFDPA_MPLS_L2_PORT);
1021 builder.extension(selectorInterpreter.mapOxm(oxm),
1022 deviceId);
1023 } else {
1024 break;
1025 }
1026 }
1027 break;
alshabib6b5cfec2014-09-18 17:42:18 -07001028 case MPLS_TC:
alshabib6b5cfec2014-09-18 17:42:18 -07001029 default:
1030 log.warn("Match type {} not yet implemented.", field.id);
alshabib6b5cfec2014-09-18 17:42:18 -07001031 }
1032 }
1033 return builder.build();
1034 }
Jonathan Hart3c259162015-10-21 21:31:19 -07001035
Jian Liffef5002016-04-04 23:27:37 -07001036 private DriverHandler getDriver(DeviceId devId) {
1037 Driver driver = driverService.getDriver(devId);
1038 DriverHandler handler = new DefaultDriverHandler(new DefaultDriverData(driver, devId));
Jonathan Hart3c259162015-10-21 21:31:19 -07001039 return handler;
1040 }
alshabib6b5cfec2014-09-18 17:42:18 -07001041}