blob: 1039d049acab080bb17e63b4ea51a5e6e8c13264 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.of.flow.impl;
alshabib6b5cfec2014-09-18 17:42:18 -070017
Saurav Dasffc5bbc2015-08-18 23:30:19 -070018import static org.onosproject.net.flow.criteria.Criteria.matchLambda;
19import static org.onosproject.net.flow.criteria.Criteria.matchOchSignalType;
20import static org.onosproject.provider.of.flow.impl.OpenFlowValueMapper.lookupChannelSpacing;
21import static org.onosproject.provider.of.flow.impl.OpenFlowValueMapper.lookupGridType;
22import static org.onosproject.provider.of.flow.impl.OpenFlowValueMapper.lookupOchSignalType;
23import static org.slf4j.LoggerFactory.getLogger;
24
25import java.util.List;
Saurav Das86af8f12015-05-25 23:55:33 -070026
Jonathan Hartd0ef7522014-12-11 11:51:18 -080027import org.onlab.packet.Ip4Address;
28import org.onlab.packet.Ip4Prefix;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -080029import org.onlab.packet.Ip6Address;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080030import org.onlab.packet.Ip6Prefix;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080031import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010032import org.onlab.packet.MplsLabel;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070033import org.onlab.packet.TpPort;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080034import org.onlab.packet.VlanId;
sangho8995ac52015-02-04 11:29:03 -080035import org.onosproject.core.DefaultGroupId;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.DeviceId;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070037import org.onosproject.net.Lambda;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.PortNumber;
39import org.onosproject.net.flow.DefaultFlowEntry;
40import org.onosproject.net.flow.DefaultFlowRule;
41import org.onosproject.net.flow.DefaultTrafficSelector;
42import org.onosproject.net.flow.DefaultTrafficTreatment;
43import org.onosproject.net.flow.FlowEntry;
44import org.onosproject.net.flow.FlowEntry.FlowEntryState;
45import org.onosproject.net.flow.FlowRule;
46import org.onosproject.net.flow.TrafficSelector;
47import org.onosproject.net.flow.TrafficTreatment;
Sho SHIMIZU9553bb82015-06-30 16:02:45 -070048import org.onosproject.net.flow.instructions.Instructions;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.openflow.controller.Dpid;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080050import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabib6b5cfec2014-09-18 17:42:18 -070051import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
52import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
53import org.projectfloodlight.openflow.protocol.action.OFAction;
Marc De Leenheer49087752014-10-23 13:54:09 -070054import org.projectfloodlight.openflow.protocol.action.OFActionCircuit;
55import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter;
sangho8995ac52015-02-04 11:29:03 -080056import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
alshabib6b5cfec2014-09-18 17:42:18 -070057import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +020058import org.projectfloodlight.openflow.protocol.action.OFActionSetQueue;
sangho3f97a17d2015-01-29 22:56:29 -080059import org.projectfloodlight.openflow.protocol.action.OFActionPopMpls;
alshabib6b5cfec2014-09-18 17:42:18 -070060import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst;
61import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080062import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
alshabib6b5cfec2014-09-18 17:42:18 -070063import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst;
64import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc;
65import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp;
66import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid;
alshabib19fdc122014-10-03 11:38:19 -070067import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hartcf783202014-11-24 18:55:42 -080068import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions;
alshabibbdcbb102015-04-22 14:16:38 -070069import org.projectfloodlight.openflow.protocol.instruction.OFInstructionGotoTable;
alshabib346b5b32015-03-06 00:42:16 -080070import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteActions;
Saurav Das86af8f12015-05-25 23:55:33 -070071import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteMetadata;
alshabib6b5cfec2014-09-18 17:42:18 -070072import org.projectfloodlight.openflow.protocol.match.Match;
73import org.projectfloodlight.openflow.protocol.match.MatchField;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080074import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
Marc De Leenheer49087752014-10-23 13:54:09 -070075import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigidBasic;
alshabib346b5b32015-03-06 00:42:16 -080076import org.projectfloodlight.openflow.protocol.ver13.OFFactoryVer13;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070077import org.projectfloodlight.openflow.types.CircuitSignalID;
sanghoc0b3c342015-03-12 17:12:10 -070078import org.projectfloodlight.openflow.types.EthType;
alshabib6b5cfec2014-09-18 17:42:18 -070079import org.projectfloodlight.openflow.types.IPv4Address;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080080import org.projectfloodlight.openflow.types.IPv6Address;
Jonathan Hart0e12fad2014-10-17 14:54:58 -070081import org.projectfloodlight.openflow.types.Masked;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080082import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070083import org.projectfloodlight.openflow.types.TransportPort;
sangho3f97a17d2015-01-29 22:56:29 -080084import org.projectfloodlight.openflow.types.U32;
Hyunsun Moona08c5d02015-07-14 17:53:00 -070085import org.projectfloodlight.openflow.types.U64;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070086import org.projectfloodlight.openflow.types.U8;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080087import org.projectfloodlight.openflow.types.VlanPcp;
alshabib6b5cfec2014-09-18 17:42:18 -070088import org.slf4j.Logger;
89
Saurav Dasffc5bbc2015-08-18 23:30:19 -070090import com.google.common.collect.Lists;
alshabib19fdc122014-10-03 11:38:19 -070091
alshabib1c319ff2014-10-04 20:29:09 -070092public class FlowEntryBuilder {
alshabib6b5cfec2014-09-18 17:42:18 -070093 private final Logger log = getLogger(getClass());
94
95 private final OFFlowStatsEntry stat;
96 private final OFFlowRemoved removed;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080097 private final OFFlowMod flowMod;
alshabib6b5cfec2014-09-18 17:42:18 -070098
99 private final Match match;
alshabib346b5b32015-03-06 00:42:16 -0800100
Jonathan Hart67fc0972015-03-19 15:21:20 -0700101 // All actions are contained in an OFInstruction. For OF1.0
102 // the instruction type is apply instruction (immediate set in ONOS speak)
alshabib346b5b32015-03-06 00:42:16 -0800103 private final List<OFInstruction> instructions;
alshabib6b5cfec2014-09-18 17:42:18 -0700104
105 private final Dpid dpid;
106
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800107 public enum FlowType { STAT, REMOVED, MOD }
108
109 private final FlowType type;
alshabib6b5cfec2014-09-18 17:42:18 -0700110
alshabibbdcbb102015-04-22 14:16:38 -0700111 public FlowEntryBuilder(Dpid dpid, OFFlowStatsEntry entry) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800112 this.stat = entry;
113 this.match = entry.getMatch();
alshabib346b5b32015-03-06 00:42:16 -0800114 this.instructions = getInstructions(entry);
Saurav Dasfa2fa932015-03-03 11:29:48 -0800115 this.dpid = dpid;
116 this.removed = null;
117 this.flowMod = null;
118 this.type = FlowType.STAT;
Saurav Dasfa2fa932015-03-03 11:29:48 -0800119 }
120
alshabibbdcbb102015-04-22 14:16:38 -0700121 public FlowEntryBuilder(Dpid dpid, OFFlowRemoved removed) {
alshabib6b5cfec2014-09-18 17:42:18 -0700122 this.match = removed.getMatch();
123 this.removed = removed;
124
125 this.dpid = dpid;
alshabib346b5b32015-03-06 00:42:16 -0800126 this.instructions = null;
alshabib6b5cfec2014-09-18 17:42:18 -0700127 this.stat = null;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800128 this.flowMod = null;
129 this.type = FlowType.REMOVED;
alshabib6b5cfec2014-09-18 17:42:18 -0700130
131 }
132
alshabibbdcbb102015-04-22 14:16:38 -0700133 public FlowEntryBuilder(Dpid dpid, OFFlowMod fm) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800134 this.match = fm.getMatch();
135 this.dpid = dpid;
alshabib346b5b32015-03-06 00:42:16 -0800136 this.instructions = getInstructions(fm);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800137 this.type = FlowType.MOD;
138 this.flowMod = fm;
139 this.stat = null;
140 this.removed = null;
141 }
alshabib1c319ff2014-10-04 20:29:09 -0700142
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800143 public FlowEntry build(FlowEntryState... state) {
144 FlowRule rule;
145 switch (this.type) {
146 case STAT:
alshabibbdcbb102015-04-22 14:16:38 -0700147 rule = DefaultFlowRule.builder()
148 .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
149 .withSelector(buildSelector())
150 .withTreatment(buildTreatment())
151 .withPriority(stat.getPriority())
152 .makeTemporary(stat.getIdleTimeout())
153 .withCookie(stat.getCookie().getValue())
154 .forTable(stat.getTableId().getValue())
155 .build();
156
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800157 return new DefaultFlowEntry(rule, FlowEntryState.ADDED,
158 stat.getDurationSec(), stat.getPacketCount().getValue(),
159 stat.getByteCount().getValue());
160 case REMOVED:
alshabibbdcbb102015-04-22 14:16:38 -0700161 rule = DefaultFlowRule.builder()
162 .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
163 .withSelector(buildSelector())
164 .withPriority(removed.getPriority())
165 .makeTemporary(removed.getIdleTimeout())
166 .withCookie(removed.getCookie().getValue())
167 .forTable(removed.getTableId().getValue())
168 .build();
169
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800170 return new DefaultFlowEntry(rule, FlowEntryState.REMOVED, removed.getDurationSec(),
171 removed.getPacketCount().getValue(), removed.getByteCount().getValue());
172 case MOD:
173 FlowEntryState flowState = state.length > 0 ? state[0] : FlowEntryState.FAILED;
alshabibbdcbb102015-04-22 14:16:38 -0700174 rule = DefaultFlowRule.builder()
175 .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
176 .withSelector(buildSelector())
177 .withTreatment(buildTreatment())
178 .withPriority(flowMod.getPriority())
179 .makeTemporary(flowMod.getIdleTimeout())
180 .withCookie(flowMod.getCookie().getValue())
181 .forTable(flowMod.getTableId().getValue())
182 .build();
183
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800184 return new DefaultFlowEntry(rule, flowState, 0, 0, 0);
185 default:
186 log.error("Unknown flow type : {}", this.type);
187 return null;
alshabib6b5cfec2014-09-18 17:42:18 -0700188 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800189
alshabib6b5cfec2014-09-18 17:42:18 -0700190 }
191
alshabib346b5b32015-03-06 00:42:16 -0800192 private List<OFInstruction> getInstructions(OFFlowMod entry) {
alshabib19fdc122014-10-03 11:38:19 -0700193 switch (entry.getVersion()) {
194 case OF_10:
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700195 return Lists.newArrayList(OFFactoryVer13.INSTANCE.instructions()
196 .applyActions(
197 entry.getActions()));
alshabib19fdc122014-10-03 11:38:19 -0700198 case OF_11:
199 case OF_12:
200 case OF_13:
alshabib346b5b32015-03-06 00:42:16 -0800201 return entry.getInstructions();
202 default:
203 log.warn("Unknown OF version {}", entry.getVersion());
204 }
205 return Lists.newLinkedList();
206 }
207
208 private List<OFInstruction> getInstructions(OFFlowStatsEntry entry) {
209 switch (entry.getVersion()) {
210 case OF_10:
211 return Lists.newArrayList(
212 OFFactoryVer13.INSTANCE.instructions().applyActions(entry.getActions()));
213 case OF_11:
214 case OF_12:
215 case OF_13:
216 return entry.getInstructions();
alshabib19fdc122014-10-03 11:38:19 -0700217 default:
218 log.warn("Unknown OF version {}", entry.getVersion());
219 }
220 return Lists.newLinkedList();
221 }
alshabib6b5cfec2014-09-18 17:42:18 -0700222
223 private TrafficTreatment buildTreatment() {
tom9a693fd2014-10-03 11:32:19 -0700224 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabib346b5b32015-03-06 00:42:16 -0800225 for (OFInstruction in : instructions) {
226 switch (in.getType()) {
227 case GOTO_TABLE:
alshabibbdcbb102015-04-22 14:16:38 -0700228 builder.transition(((int) ((OFInstructionGotoTable) in)
229 .getTableId().getValue()));
alshabib346b5b32015-03-06 00:42:16 -0800230 break;
231 case WRITE_METADATA:
Saurav Das86af8f12015-05-25 23:55:33 -0700232 OFInstructionWriteMetadata m = (OFInstructionWriteMetadata) in;
233 builder.writeMetadata(m.getMetadata().getValue(),
234 m.getMetadataMask().getValue());
alshabib346b5b32015-03-06 00:42:16 -0800235 break;
236 case WRITE_ACTIONS:
237 builder.deferred();
238 buildActions(((OFInstructionWriteActions) in).getActions(),
239 builder);
240 break;
241 case APPLY_ACTIONS:
242 builder.immediate();
243 buildActions(((OFInstructionApplyActions) in).getActions(),
244 builder);
245 break;
246 case CLEAR_ACTIONS:
247 builder.wipeDeferred();
248 break;
249 case EXPERIMENTER:
250 break;
251 case METER:
252 break;
253 default:
254 log.warn("Unknown instructions type {}", in.getType());
alshabib6b5cfec2014-09-18 17:42:18 -0700255 }
256 }
257
258 return builder.build();
259 }
260
alshabib346b5b32015-03-06 00:42:16 -0800261 private TrafficTreatment.Builder buildActions(List<OFAction> actions,
262 TrafficTreatment.Builder builder) {
263 for (OFAction act : actions) {
264 switch (act.getType()) {
265 case OUTPUT:
266 OFActionOutput out = (OFActionOutput) act;
267 builder.setOutput(
268 PortNumber.portNumber(out.getPort().getPortNumber()));
269 break;
270 case SET_VLAN_VID:
271 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
272 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
273 break;
274 case SET_VLAN_PCP:
275 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
276 builder.setVlanPcp(pcp.getVlanPcp().getValue());
277 break;
278 case SET_DL_DST:
279 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
280 builder.setEthDst(
281 MacAddress.valueOf(dldst.getDlAddr().getLong()));
282 break;
283 case SET_DL_SRC:
284 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
285 builder.setEthSrc(
286 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
287
288 break;
289 case SET_NW_DST:
290 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
291 IPv4Address di = nwdst.getNwAddr();
292 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
293 break;
294 case SET_NW_SRC:
295 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
296 IPv4Address si = nwsrc.getNwAddr();
297 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
298 break;
299 case EXPERIMENTER:
300 OFActionExperimenter exp = (OFActionExperimenter) act;
301 if (exp.getExperimenter() == 0x80005A06 ||
302 exp.getExperimenter() == 0x748771) {
303 OFActionCircuit ct = (OFActionCircuit) exp;
Sho SHIMIZU9553bb82015-06-30 16:02:45 -0700304 short lambda = ((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber();
305 builder.add(Instructions.modL0Lambda(Lambda.indexedLambda(lambda)));
alshabib346b5b32015-03-06 00:42:16 -0800306 } else {
307 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
308 }
309 break;
310 case SET_FIELD:
311 OFActionSetField setField = (OFActionSetField) act;
312 handleSetField(builder, setField.getField());
313 break;
314 case POP_MPLS:
315 OFActionPopMpls popMpls = (OFActionPopMpls) act;
316 builder.popMpls((short) popMpls.getEthertype().getValue());
317 break;
318 case PUSH_MPLS:
319 builder.pushMpls();
320 break;
321 case COPY_TTL_IN:
322 builder.copyTtlIn();
323 break;
324 case COPY_TTL_OUT:
325 builder.copyTtlOut();
326 break;
327 case DEC_MPLS_TTL:
328 builder.decMplsTtl();
329 break;
330 case DEC_NW_TTL:
331 builder.decNwTtl();
332 break;
333 case GROUP:
334 OFActionGroup group = (OFActionGroup) act;
335 builder.group(new DefaultGroupId(group.getGroup().getGroupNumber()));
336 break;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200337 case SET_QUEUE:
338 OFActionSetQueue setQueue = (OFActionSetQueue) act;
339 builder.setQueue(setQueue.getQueueId());
340 break;
Jonathan Hart67fc0972015-03-19 15:21:20 -0700341 case STRIP_VLAN:
alshabib346b5b32015-03-06 00:42:16 -0800342 case POP_VLAN:
343 builder.popVlan();
344 break;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700345 case PUSH_VLAN:
346 builder.pushVlan();
347 break;
alshabib346b5b32015-03-06 00:42:16 -0800348 case SET_TP_DST:
349 case SET_TP_SRC:
350 case POP_PBB:
351 case PUSH_PBB:
alshabib346b5b32015-03-06 00:42:16 -0800352 case SET_MPLS_LABEL:
353 case SET_MPLS_TC:
354 case SET_MPLS_TTL:
355 case SET_NW_ECN:
356 case SET_NW_TOS:
357 case SET_NW_TTL:
alshabib346b5b32015-03-06 00:42:16 -0800358
359 case ENQUEUE:
360 default:
361 log.warn("Action type {} not yet implemented.", act.getType());
362 }
363 }
364 return builder;
365 }
366
367
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800368 private void handleSetField(TrafficTreatment.Builder builder, OFOxm<?> oxm) {
369 switch (oxm.getMatchField().id) {
370 case VLAN_PCP:
371 @SuppressWarnings("unchecked")
372 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
373 builder.setVlanPcp(vlanpcp.getValue().getValue());
374 break;
375 case VLAN_VID:
376 @SuppressWarnings("unchecked")
377 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
378 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
379 break;
380 case ETH_DST:
381 @SuppressWarnings("unchecked")
382 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
383 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
384 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
385 break;
386 case ETH_SRC:
387 @SuppressWarnings("unchecked")
388 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
389 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
390 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
391 break;
392 case IPV4_DST:
393 @SuppressWarnings("unchecked")
394 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
395 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
396 break;
397 case IPV4_SRC:
398 @SuppressWarnings("unchecked")
399 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
400 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
401 break;
sangho3f97a17d2015-01-29 22:56:29 -0800402 case MPLS_LABEL:
403 @SuppressWarnings("unchecked")
404 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100405 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
sangho3f97a17d2015-01-29 22:56:29 -0800406 break;
Saurav Das73a7dd42015-08-19 22:20:31 -0700407 case MPLS_BOS:
408 @SuppressWarnings("unchecked")
409 OFOxm<U8> mplsBos = (OFOxm<U8>) oxm;
410 builder.setMplsBos(mplsBos.getValue() == U8.ZERO ? false : true);
411 break;
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700412 case TUNNEL_ID:
413 @SuppressWarnings("unchecked")
414 OFOxm<U64> tunnelId = (OFOxm<U64>) oxm;
415 builder.setTunnelId(tunnelId.getValue().getValue());
416 break;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700417 case TCP_DST:
418 @SuppressWarnings("unchecked")
419 OFOxm<TransportPort> tcpdst = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700420 builder.setTcpDst(TpPort.tpPort(tcpdst.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700421 break;
422 case TCP_SRC:
423 @SuppressWarnings("unchecked")
424 OFOxm<TransportPort> tcpsrc = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700425 builder.setTcpSrc(TpPort.tpPort(tcpsrc.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700426 break;
427 case UDP_DST:
428 @SuppressWarnings("unchecked")
429 OFOxm<TransportPort> udpdst = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700430 builder.setUdpDst(TpPort.tpPort(udpdst.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700431 break;
432 case UDP_SRC:
433 @SuppressWarnings("unchecked")
434 OFOxm<TransportPort> udpsrc = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700435 builder.setUdpSrc(TpPort.tpPort(udpsrc.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700436 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800437 case ARP_OP:
438 case ARP_SHA:
439 case ARP_SPA:
440 case ARP_THA:
441 case ARP_TPA:
442 case BSN_EGR_PORT_GROUP_ID:
443 case BSN_GLOBAL_VRF_ALLOWED:
444 case BSN_IN_PORTS_128:
445 case BSN_L3_DST_CLASS_ID:
446 case BSN_L3_INTERFACE_CLASS_ID:
447 case BSN_L3_SRC_CLASS_ID:
448 case BSN_LAG_ID:
449 case BSN_TCP_FLAGS:
450 case BSN_UDF0:
451 case BSN_UDF1:
452 case BSN_UDF2:
453 case BSN_UDF3:
454 case BSN_UDF4:
455 case BSN_UDF5:
456 case BSN_UDF6:
457 case BSN_UDF7:
458 case BSN_VLAN_XLATE_PORT_GROUP_ID:
459 case BSN_VRF:
460 case ETH_TYPE:
461 case ICMPV4_CODE:
462 case ICMPV4_TYPE:
463 case ICMPV6_CODE:
464 case ICMPV6_TYPE:
465 case IN_PHY_PORT:
466 case IN_PORT:
467 case IPV6_DST:
468 case IPV6_FLABEL:
469 case IPV6_ND_SLL:
470 case IPV6_ND_TARGET:
471 case IPV6_ND_TLL:
472 case IPV6_SRC:
473 case IP_DSCP:
474 case IP_ECN:
475 case IP_PROTO:
476 case METADATA:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800477 case MPLS_TC:
478 case OCH_SIGID:
479 case OCH_SIGID_BASIC:
480 case OCH_SIGTYPE:
481 case OCH_SIGTYPE_BASIC:
482 case SCTP_DST:
483 case SCTP_SRC:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800484 default:
485 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
486 break;
487 }
488 }
489
Sho SHIMIZU6f1b09e2015-05-05 11:26:22 -0700490 // CHECKSTYLE IGNORE MethodLength FOR NEXT 1 LINES
alshabib6b5cfec2014-09-18 17:42:18 -0700491 private TrafficSelector buildSelector() {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800492 MacAddress mac;
493 Ip4Prefix ip4Prefix;
494 Ip6Address ip6Address;
495 Ip6Prefix ip6Prefix;
496
tom9a693fd2014-10-03 11:32:19 -0700497 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700498 for (MatchField<?> field : match.getMatchFields()) {
499 switch (field.id) {
500 case IN_PORT:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800501 builder.matchInPort(PortNumber
alshabib010c31d2014-09-26 10:01:12 -0700502 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700503 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800504 case IN_PHY_PORT:
505 builder.matchInPhyPort(PortNumber
506 .portNumber(match.get(MatchField.IN_PHY_PORT).getPortNumber()));
507 break;
508 case METADATA:
509 long metadata =
510 match.get(MatchField.METADATA).getValue().getValue();
511 builder.matchMetadata(metadata);
512 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700513 case ETH_DST:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800514 mac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
515 builder.matchEthDst(mac);
516 break;
517 case ETH_SRC:
518 mac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
519 builder.matchEthSrc(mac);
alshabib6b5cfec2014-09-18 17:42:18 -0700520 break;
521 case ETH_TYPE:
522 int ethType = match.get(MatchField.ETH_TYPE).getValue();
sanghoc0b3c342015-03-12 17:12:10 -0700523 if (ethType == EthType.VLAN_FRAME.getValue()) {
524 builder.matchVlanId(VlanId.ANY);
525 } else {
526 builder.matchEthType((short) ethType);
527 }
alshabib6b5cfec2014-09-18 17:42:18 -0700528 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700529 case VLAN_VID:
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800530 VlanId vlanId = null;
531 if (match.isPartiallyMasked(MatchField.VLAN_VID)) {
532 Masked<OFVlanVidMatch> masked = match.getMasked(MatchField.VLAN_VID);
533 if (masked.getValue().equals(OFVlanVidMatch.PRESENT)
534 && masked.getMask().equals(OFVlanVidMatch.PRESENT)) {
535 vlanId = VlanId.ANY;
536 }
537 } else {
Jonathan Hart1468fee2015-07-16 18:50:34 -0700538 if (!match.get(MatchField.VLAN_VID).isPresentBitSet()) {
539 vlanId = VlanId.NONE;
540 } else {
541 vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
542 }
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800543 }
544 if (vlanId != null) {
545 builder.matchVlanId(vlanId);
546 }
alshabib6b5cfec2014-09-18 17:42:18 -0700547 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800548 case VLAN_PCP:
549 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
550 builder.matchVlanPcp(vlanPcp);
551 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800552 case IP_DSCP:
553 byte ipDscp = match.get(MatchField.IP_DSCP).getDscpValue();
554 builder.matchIPDscp(ipDscp);
555 break;
556 case IP_ECN:
557 byte ipEcn = match.get(MatchField.IP_ECN).getEcnValue();
558 builder.matchIPEcn(ipEcn);
559 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800560 case IP_PROTO:
561 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
562 builder.matchIPProtocol((byte) proto);
563 break;
564 case IPV4_SRC:
565 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
566 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800567 ip4Prefix = Ip4Prefix.valueOf(
568 maskedIp.getValue().getInt(),
569 maskedIp.getMask().asCidrMaskLength());
570 } else {
571 ip4Prefix = Ip4Prefix.valueOf(
572 match.get(MatchField.IPV4_SRC).getInt(),
573 Ip4Prefix.MAX_MASK_LENGTH);
574 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800575 builder.matchIPSrc(ip4Prefix);
576 break;
577 case IPV4_DST:
578 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
579 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800580 ip4Prefix = Ip4Prefix.valueOf(
581 maskedIp.getValue().getInt(),
582 maskedIp.getMask().asCidrMaskLength());
583 } else {
584 ip4Prefix = Ip4Prefix.valueOf(
585 match.get(MatchField.IPV4_DST).getInt(),
586 Ip4Prefix.MAX_MASK_LENGTH);
587 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800588 builder.matchIPDst(ip4Prefix);
Jonathan Hart34bc6142014-10-17 11:00:43 -0700589 break;
590 case TCP_SRC:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700591 builder.matchTcpSrc(TpPort.tpPort(match.get(MatchField.TCP_SRC).getPort()));
Jonathan Hart34bc6142014-10-17 11:00:43 -0700592 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800593 case TCP_DST:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700594 builder.matchTcpDst(TpPort.tpPort(match.get(MatchField.TCP_DST).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800595 break;
596 case UDP_SRC:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700597 builder.matchUdpSrc(TpPort.tpPort(match.get(MatchField.UDP_SRC).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800598 break;
599 case UDP_DST:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700600 builder.matchUdpDst(TpPort.tpPort(match.get(MatchField.UDP_DST).getPort()));
Sho SHIMIZU6f1b09e2015-05-05 11:26:22 -0700601 break;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100602 case MPLS_LABEL:
603 builder.matchMplsLabel(MplsLabel.mplsLabel((int) match.get(MatchField.MPLS_LABEL)
604 .getValue()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800605 break;
Saurav Dasffc5bbc2015-08-18 23:30:19 -0700606 case MPLS_BOS:
607 builder.matchMplsBos(match.get(MatchField.MPLS_BOS).getValue());
608 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800609 case SCTP_SRC:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700610 builder.matchSctpSrc(TpPort.tpPort(match.get(MatchField.SCTP_SRC).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800611 break;
612 case SCTP_DST:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700613 builder.matchSctpDst(TpPort.tpPort(match.get(MatchField.SCTP_DST).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800614 break;
615 case ICMPV4_TYPE:
616 byte icmpType = (byte) match.get(MatchField.ICMPV4_TYPE).getType();
617 builder.matchIcmpType(icmpType);
618 break;
619 case ICMPV4_CODE:
620 byte icmpCode = (byte) match.get(MatchField.ICMPV4_CODE).getCode();
621 builder.matchIcmpCode(icmpCode);
622 break;
623 case IPV6_SRC:
624 if (match.isPartiallyMasked(MatchField.IPV6_SRC)) {
625 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_SRC);
626 ip6Prefix = Ip6Prefix.valueOf(
627 maskedIp.getValue().getBytes(),
628 maskedIp.getMask().asCidrMaskLength());
629 } else {
630 ip6Prefix = Ip6Prefix.valueOf(
631 match.get(MatchField.IPV6_SRC).getBytes(),
632 Ip6Prefix.MAX_MASK_LENGTH);
633 }
634 builder.matchIPv6Src(ip6Prefix);
635 break;
636 case IPV6_DST:
637 if (match.isPartiallyMasked(MatchField.IPV6_DST)) {
638 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_DST);
639 ip6Prefix = Ip6Prefix.valueOf(
640 maskedIp.getValue().getBytes(),
641 maskedIp.getMask().asCidrMaskLength());
642 } else {
643 ip6Prefix = Ip6Prefix.valueOf(
644 match.get(MatchField.IPV6_DST).getBytes(),
645 Ip6Prefix.MAX_MASK_LENGTH);
646 }
647 builder.matchIPv6Dst(ip6Prefix);
648 break;
649 case IPV6_FLABEL:
650 int flowLabel =
651 match.get(MatchField.IPV6_FLABEL).getIPv6FlowLabelValue();
652 builder.matchIPv6FlowLabel(flowLabel);
653 break;
654 case ICMPV6_TYPE:
655 byte icmpv6type = (byte) match.get(MatchField.ICMPV6_TYPE).getValue();
656 builder.matchIcmpv6Type(icmpv6type);
657 break;
658 case ICMPV6_CODE:
659 byte icmpv6code = (byte) match.get(MatchField.ICMPV6_CODE).getValue();
660 builder.matchIcmpv6Code(icmpv6code);
661 break;
662 case IPV6_ND_TARGET:
663 ip6Address =
664 Ip6Address.valueOf(match.get(MatchField.IPV6_ND_TARGET).getBytes());
665 builder.matchIPv6NDTargetAddress(ip6Address);
666 break;
667 case IPV6_ND_SLL:
668 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_SLL).getLong());
669 builder.matchIPv6NDSourceLinkLayerAddress(mac);
670 break;
671 case IPV6_ND_TLL:
672 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_TLL).getLong());
673 builder.matchIPv6NDTargetLinkLayerAddress(mac);
674 break;
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800675 case IPV6_EXTHDR:
Charles M.C. Chan2184de12015-04-26 02:24:53 +0800676 builder.matchIPv6ExthdrFlags((short) match.get(MatchField.IPV6_EXTHDR)
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700677 .getValue());
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800678 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700679 case OCH_SIGID:
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700680 CircuitSignalID sigId = match.get(MatchField.OCH_SIGID);
681 builder.add(matchLambda(Lambda.ochSignal(
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700682 lookupGridType(sigId.getGridType()), lookupChannelSpacing(sigId.getChannelSpacing()),
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -0700683 sigId.getChannelNumber(), sigId.getSpectralWidth())
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700684 ));
Marc De Leenheer49087752014-10-23 13:54:09 -0700685 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800686 case OCH_SIGTYPE:
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700687 U8 sigType = match.get(MatchField.OCH_SIGTYPE);
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700688 builder.add(matchOchSignalType(lookupOchSignalType((byte) sigType.getValue())));
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800689 break;
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700690 case TUNNEL_ID:
691 long tunnelId = match.get(MatchField.TUNNEL_ID).getValue();
692 builder.matchTunnelId(tunnelId);
693 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700694 case ARP_OP:
695 case ARP_SHA:
696 case ARP_SPA:
697 case ARP_THA:
698 case ARP_TPA:
alshabib6b5cfec2014-09-18 17:42:18 -0700699 case MPLS_TC:
alshabib6b5cfec2014-09-18 17:42:18 -0700700 default:
701 log.warn("Match type {} not yet implemented.", field.id);
alshabib6b5cfec2014-09-18 17:42:18 -0700702 }
703 }
704 return builder.build();
705 }
alshabib6b5cfec2014-09-18 17:42:18 -0700706}