blob: 2c01e96daea5832e30966f99cfa391ac52d3fbe8 [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
Jonathan Hart8cf212a2015-10-29 17:42:03 -070018import com.google.common.collect.Lists;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080019import org.onlab.packet.Ip4Address;
20import org.onlab.packet.Ip4Prefix;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -080021import org.onlab.packet.Ip6Address;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080022import org.onlab.packet.Ip6Prefix;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080023import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010024import org.onlab.packet.MplsLabel;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070025import org.onlab.packet.TpPort;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080026import org.onlab.packet.VlanId;
sangho8995ac52015-02-04 11:29:03 -080027import org.onosproject.core.DefaultGroupId;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.DeviceId;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070029import org.onosproject.net.Lambda;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.PortNumber;
31import org.onosproject.net.flow.DefaultFlowEntry;
32import org.onosproject.net.flow.DefaultFlowRule;
33import org.onosproject.net.flow.DefaultTrafficSelector;
34import org.onosproject.net.flow.DefaultTrafficTreatment;
35import org.onosproject.net.flow.FlowEntry;
36import org.onosproject.net.flow.FlowEntry.FlowEntryState;
37import org.onosproject.net.flow.FlowRule;
38import org.onosproject.net.flow.TrafficSelector;
39import org.onosproject.net.flow.TrafficTreatment;
Sho SHIMIZU9553bb82015-06-30 16:02:45 -070040import org.onosproject.net.flow.instructions.Instructions;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.openflow.controller.Dpid;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080042import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabib6b5cfec2014-09-18 17:42:18 -070043import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
44import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
45import org.projectfloodlight.openflow.protocol.action.OFAction;
Marc De Leenheer49087752014-10-23 13:54:09 -070046import org.projectfloodlight.openflow.protocol.action.OFActionCircuit;
47import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter;
sangho8995ac52015-02-04 11:29:03 -080048import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
alshabib6b5cfec2014-09-18 17:42:18 -070049import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
sangho3f97a17d2015-01-29 22:56:29 -080050import org.projectfloodlight.openflow.protocol.action.OFActionPopMpls;
alshabib6b5cfec2014-09-18 17:42:18 -070051import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst;
52import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080053import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
alshabib6b5cfec2014-09-18 17:42:18 -070054import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst;
55import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc;
Jonathan Hart8cf212a2015-10-29 17:42:03 -070056import org.projectfloodlight.openflow.protocol.action.OFActionSetQueue;
alshabib6b5cfec2014-09-18 17:42:18 -070057import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp;
58import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid;
alshabib19fdc122014-10-03 11:38:19 -070059import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hartcf783202014-11-24 18:55:42 -080060import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions;
alshabibbdcbb102015-04-22 14:16:38 -070061import org.projectfloodlight.openflow.protocol.instruction.OFInstructionGotoTable;
alshabib346b5b32015-03-06 00:42:16 -080062import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteActions;
Saurav Das86af8f12015-05-25 23:55:33 -070063import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteMetadata;
alshabib6b5cfec2014-09-18 17:42:18 -070064import org.projectfloodlight.openflow.protocol.match.Match;
65import org.projectfloodlight.openflow.protocol.match.MatchField;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080066import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
Marc De Leenheer49087752014-10-23 13:54:09 -070067import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigidBasic;
alshabib346b5b32015-03-06 00:42:16 -080068import org.projectfloodlight.openflow.protocol.ver13.OFFactoryVer13;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070069import org.projectfloodlight.openflow.types.CircuitSignalID;
alshabib6b5cfec2014-09-18 17:42:18 -070070import org.projectfloodlight.openflow.types.IPv4Address;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080071import org.projectfloodlight.openflow.types.IPv6Address;
Jonathan Hart0e12fad2014-10-17 14:54:58 -070072import org.projectfloodlight.openflow.types.Masked;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080073import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070074import org.projectfloodlight.openflow.types.TransportPort;
sangho3f97a17d2015-01-29 22:56:29 -080075import org.projectfloodlight.openflow.types.U32;
Hyunsun Moona08c5d02015-07-14 17:53:00 -070076import org.projectfloodlight.openflow.types.U64;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070077import org.projectfloodlight.openflow.types.U8;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080078import org.projectfloodlight.openflow.types.VlanPcp;
alshabib6b5cfec2014-09-18 17:42:18 -070079import org.slf4j.Logger;
80
Jonathan Hart8cf212a2015-10-29 17:42:03 -070081import java.util.List;
82
83import static org.onosproject.net.flow.criteria.Criteria.matchLambda;
84import static org.onosproject.net.flow.criteria.Criteria.matchOchSignalType;
85import static org.onosproject.provider.of.flow.impl.OpenFlowValueMapper.lookupChannelSpacing;
86import static org.onosproject.provider.of.flow.impl.OpenFlowValueMapper.lookupGridType;
87import static org.onosproject.provider.of.flow.impl.OpenFlowValueMapper.lookupOchSignalType;
88import static org.slf4j.LoggerFactory.getLogger;
alshabib19fdc122014-10-03 11:38:19 -070089
alshabib1c319ff2014-10-04 20:29:09 -070090public class FlowEntryBuilder {
alshabib6b5cfec2014-09-18 17:42:18 -070091 private final Logger log = getLogger(getClass());
92
93 private final OFFlowStatsEntry stat;
94 private final OFFlowRemoved removed;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080095 private final OFFlowMod flowMod;
alshabib6b5cfec2014-09-18 17:42:18 -070096
97 private final Match match;
alshabib346b5b32015-03-06 00:42:16 -080098
Jonathan Hart67fc0972015-03-19 15:21:20 -070099 // All actions are contained in an OFInstruction. For OF1.0
100 // the instruction type is apply instruction (immediate set in ONOS speak)
alshabib346b5b32015-03-06 00:42:16 -0800101 private final List<OFInstruction> instructions;
alshabib6b5cfec2014-09-18 17:42:18 -0700102
103 private final Dpid dpid;
104
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800105 public enum FlowType { STAT, REMOVED, MOD }
106
107 private final FlowType type;
alshabib6b5cfec2014-09-18 17:42:18 -0700108
alshabibbdcbb102015-04-22 14:16:38 -0700109 public FlowEntryBuilder(Dpid dpid, OFFlowStatsEntry entry) {
Saurav Dasfa2fa932015-03-03 11:29:48 -0800110 this.stat = entry;
111 this.match = entry.getMatch();
alshabib346b5b32015-03-06 00:42:16 -0800112 this.instructions = getInstructions(entry);
Saurav Dasfa2fa932015-03-03 11:29:48 -0800113 this.dpid = dpid;
114 this.removed = null;
115 this.flowMod = null;
116 this.type = FlowType.STAT;
Saurav Dasfa2fa932015-03-03 11:29:48 -0800117 }
118
alshabibbdcbb102015-04-22 14:16:38 -0700119 public FlowEntryBuilder(Dpid dpid, OFFlowRemoved removed) {
alshabib6b5cfec2014-09-18 17:42:18 -0700120 this.match = removed.getMatch();
121 this.removed = removed;
122
123 this.dpid = dpid;
alshabib346b5b32015-03-06 00:42:16 -0800124 this.instructions = null;
alshabib6b5cfec2014-09-18 17:42:18 -0700125 this.stat = null;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800126 this.flowMod = null;
127 this.type = FlowType.REMOVED;
alshabib6b5cfec2014-09-18 17:42:18 -0700128
129 }
130
alshabibbdcbb102015-04-22 14:16:38 -0700131 public FlowEntryBuilder(Dpid dpid, OFFlowMod fm) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800132 this.match = fm.getMatch();
133 this.dpid = dpid;
alshabib346b5b32015-03-06 00:42:16 -0800134 this.instructions = getInstructions(fm);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800135 this.type = FlowType.MOD;
136 this.flowMod = fm;
137 this.stat = null;
138 this.removed = null;
139 }
alshabib1c319ff2014-10-04 20:29:09 -0700140
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800141 public FlowEntry build(FlowEntryState... state) {
142 FlowRule rule;
143 switch (this.type) {
144 case STAT:
alshabibbdcbb102015-04-22 14:16:38 -0700145 rule = DefaultFlowRule.builder()
146 .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
147 .withSelector(buildSelector())
148 .withTreatment(buildTreatment())
149 .withPriority(stat.getPriority())
150 .makeTemporary(stat.getIdleTimeout())
151 .withCookie(stat.getCookie().getValue())
152 .forTable(stat.getTableId().getValue())
153 .build();
154
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800155 return new DefaultFlowEntry(rule, FlowEntryState.ADDED,
156 stat.getDurationSec(), stat.getPacketCount().getValue(),
157 stat.getByteCount().getValue());
158 case REMOVED:
alshabibbdcbb102015-04-22 14:16:38 -0700159 rule = DefaultFlowRule.builder()
160 .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
161 .withSelector(buildSelector())
162 .withPriority(removed.getPriority())
163 .makeTemporary(removed.getIdleTimeout())
164 .withCookie(removed.getCookie().getValue())
165 .forTable(removed.getTableId().getValue())
166 .build();
167
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800168 return new DefaultFlowEntry(rule, FlowEntryState.REMOVED, removed.getDurationSec(),
169 removed.getPacketCount().getValue(), removed.getByteCount().getValue());
170 case MOD:
171 FlowEntryState flowState = state.length > 0 ? state[0] : FlowEntryState.FAILED;
alshabibbdcbb102015-04-22 14:16:38 -0700172 rule = DefaultFlowRule.builder()
173 .forDevice(DeviceId.deviceId(Dpid.uri(dpid)))
174 .withSelector(buildSelector())
175 .withTreatment(buildTreatment())
176 .withPriority(flowMod.getPriority())
177 .makeTemporary(flowMod.getIdleTimeout())
178 .withCookie(flowMod.getCookie().getValue())
179 .forTable(flowMod.getTableId().getValue())
180 .build();
181
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800182 return new DefaultFlowEntry(rule, flowState, 0, 0, 0);
183 default:
184 log.error("Unknown flow type : {}", this.type);
185 return null;
alshabib6b5cfec2014-09-18 17:42:18 -0700186 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800187
alshabib6b5cfec2014-09-18 17:42:18 -0700188 }
189
alshabib346b5b32015-03-06 00:42:16 -0800190 private List<OFInstruction> getInstructions(OFFlowMod entry) {
alshabib19fdc122014-10-03 11:38:19 -0700191 switch (entry.getVersion()) {
192 case OF_10:
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700193 return Lists.newArrayList(OFFactoryVer13.INSTANCE.instructions()
194 .applyActions(
195 entry.getActions()));
alshabib19fdc122014-10-03 11:38:19 -0700196 case OF_11:
197 case OF_12:
198 case OF_13:
alshabib346b5b32015-03-06 00:42:16 -0800199 return entry.getInstructions();
200 default:
201 log.warn("Unknown OF version {}", entry.getVersion());
202 }
203 return Lists.newLinkedList();
204 }
205
206 private List<OFInstruction> getInstructions(OFFlowStatsEntry entry) {
207 switch (entry.getVersion()) {
208 case OF_10:
209 return Lists.newArrayList(
210 OFFactoryVer13.INSTANCE.instructions().applyActions(entry.getActions()));
211 case OF_11:
212 case OF_12:
213 case OF_13:
214 return entry.getInstructions();
alshabib19fdc122014-10-03 11:38:19 -0700215 default:
216 log.warn("Unknown OF version {}", entry.getVersion());
217 }
218 return Lists.newLinkedList();
219 }
alshabib6b5cfec2014-09-18 17:42:18 -0700220
221 private TrafficTreatment buildTreatment() {
tom9a693fd2014-10-03 11:32:19 -0700222 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabib346b5b32015-03-06 00:42:16 -0800223 for (OFInstruction in : instructions) {
224 switch (in.getType()) {
225 case GOTO_TABLE:
alshabibbdcbb102015-04-22 14:16:38 -0700226 builder.transition(((int) ((OFInstructionGotoTable) in)
227 .getTableId().getValue()));
alshabib346b5b32015-03-06 00:42:16 -0800228 break;
229 case WRITE_METADATA:
Saurav Das86af8f12015-05-25 23:55:33 -0700230 OFInstructionWriteMetadata m = (OFInstructionWriteMetadata) in;
231 builder.writeMetadata(m.getMetadata().getValue(),
232 m.getMetadataMask().getValue());
alshabib346b5b32015-03-06 00:42:16 -0800233 break;
234 case WRITE_ACTIONS:
235 builder.deferred();
236 buildActions(((OFInstructionWriteActions) in).getActions(),
237 builder);
238 break;
239 case APPLY_ACTIONS:
240 builder.immediate();
241 buildActions(((OFInstructionApplyActions) in).getActions(),
242 builder);
243 break;
244 case CLEAR_ACTIONS:
245 builder.wipeDeferred();
246 break;
247 case EXPERIMENTER:
248 break;
249 case METER:
250 break;
251 default:
252 log.warn("Unknown instructions type {}", in.getType());
alshabib6b5cfec2014-09-18 17:42:18 -0700253 }
254 }
255
256 return builder.build();
257 }
258
alshabib346b5b32015-03-06 00:42:16 -0800259 private TrafficTreatment.Builder buildActions(List<OFAction> actions,
260 TrafficTreatment.Builder builder) {
261 for (OFAction act : actions) {
262 switch (act.getType()) {
263 case OUTPUT:
264 OFActionOutput out = (OFActionOutput) act;
265 builder.setOutput(
266 PortNumber.portNumber(out.getPort().getPortNumber()));
267 break;
268 case SET_VLAN_VID:
269 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
270 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
271 break;
272 case SET_VLAN_PCP:
273 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
274 builder.setVlanPcp(pcp.getVlanPcp().getValue());
275 break;
276 case SET_DL_DST:
277 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
278 builder.setEthDst(
279 MacAddress.valueOf(dldst.getDlAddr().getLong()));
280 break;
281 case SET_DL_SRC:
282 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
283 builder.setEthSrc(
284 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
285
286 break;
287 case SET_NW_DST:
288 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
289 IPv4Address di = nwdst.getNwAddr();
290 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
291 break;
292 case SET_NW_SRC:
293 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
294 IPv4Address si = nwsrc.getNwAddr();
295 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
296 break;
297 case EXPERIMENTER:
298 OFActionExperimenter exp = (OFActionExperimenter) act;
299 if (exp.getExperimenter() == 0x80005A06 ||
300 exp.getExperimenter() == 0x748771) {
301 OFActionCircuit ct = (OFActionCircuit) exp;
Sho SHIMIZU9553bb82015-06-30 16:02:45 -0700302 short lambda = ((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber();
303 builder.add(Instructions.modL0Lambda(Lambda.indexedLambda(lambda)));
alshabib346b5b32015-03-06 00:42:16 -0800304 } else {
305 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
306 }
307 break;
308 case SET_FIELD:
309 OFActionSetField setField = (OFActionSetField) act;
310 handleSetField(builder, setField.getField());
311 break;
312 case POP_MPLS:
313 OFActionPopMpls popMpls = (OFActionPopMpls) act;
314 builder.popMpls((short) popMpls.getEthertype().getValue());
315 break;
316 case PUSH_MPLS:
317 builder.pushMpls();
318 break;
319 case COPY_TTL_IN:
320 builder.copyTtlIn();
321 break;
322 case COPY_TTL_OUT:
323 builder.copyTtlOut();
324 break;
325 case DEC_MPLS_TTL:
326 builder.decMplsTtl();
327 break;
328 case DEC_NW_TTL:
329 builder.decNwTtl();
330 break;
331 case GROUP:
332 OFActionGroup group = (OFActionGroup) act;
333 builder.group(new DefaultGroupId(group.getGroup().getGroupNumber()));
334 break;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200335 case SET_QUEUE:
336 OFActionSetQueue setQueue = (OFActionSetQueue) act;
337 builder.setQueue(setQueue.getQueueId());
338 break;
Jonathan Hart67fc0972015-03-19 15:21:20 -0700339 case STRIP_VLAN:
alshabib346b5b32015-03-06 00:42:16 -0800340 case POP_VLAN:
341 builder.popVlan();
342 break;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700343 case PUSH_VLAN:
344 builder.pushVlan();
345 break;
alshabib346b5b32015-03-06 00:42:16 -0800346 case SET_TP_DST:
347 case SET_TP_SRC:
348 case POP_PBB:
349 case PUSH_PBB:
alshabib346b5b32015-03-06 00:42:16 -0800350 case SET_MPLS_LABEL:
351 case SET_MPLS_TC:
352 case SET_MPLS_TTL:
353 case SET_NW_ECN:
354 case SET_NW_TOS:
355 case SET_NW_TTL:
alshabib346b5b32015-03-06 00:42:16 -0800356
357 case ENQUEUE:
358 default:
359 log.warn("Action type {} not yet implemented.", act.getType());
360 }
361 }
362 return builder;
363 }
364
365
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800366 private void handleSetField(TrafficTreatment.Builder builder, OFOxm<?> oxm) {
367 switch (oxm.getMatchField().id) {
368 case VLAN_PCP:
369 @SuppressWarnings("unchecked")
370 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
371 builder.setVlanPcp(vlanpcp.getValue().getValue());
372 break;
373 case VLAN_VID:
374 @SuppressWarnings("unchecked")
375 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
376 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
377 break;
378 case ETH_DST:
379 @SuppressWarnings("unchecked")
380 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
381 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
382 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
383 break;
384 case ETH_SRC:
385 @SuppressWarnings("unchecked")
386 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
387 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
388 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
389 break;
390 case IPV4_DST:
391 @SuppressWarnings("unchecked")
392 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
393 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
394 break;
395 case IPV4_SRC:
396 @SuppressWarnings("unchecked")
397 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
398 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
399 break;
sangho3f97a17d2015-01-29 22:56:29 -0800400 case MPLS_LABEL:
401 @SuppressWarnings("unchecked")
402 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100403 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
sangho3f97a17d2015-01-29 22:56:29 -0800404 break;
Saurav Das73a7dd42015-08-19 22:20:31 -0700405 case MPLS_BOS:
406 @SuppressWarnings("unchecked")
407 OFOxm<U8> mplsBos = (OFOxm<U8>) oxm;
408 builder.setMplsBos(mplsBos.getValue() == U8.ZERO ? false : true);
409 break;
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700410 case TUNNEL_ID:
411 @SuppressWarnings("unchecked")
412 OFOxm<U64> tunnelId = (OFOxm<U64>) oxm;
413 builder.setTunnelId(tunnelId.getValue().getValue());
414 break;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700415 case TCP_DST:
416 @SuppressWarnings("unchecked")
417 OFOxm<TransportPort> tcpdst = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700418 builder.setTcpDst(TpPort.tpPort(tcpdst.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700419 break;
420 case TCP_SRC:
421 @SuppressWarnings("unchecked")
422 OFOxm<TransportPort> tcpsrc = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700423 builder.setTcpSrc(TpPort.tpPort(tcpsrc.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700424 break;
425 case UDP_DST:
426 @SuppressWarnings("unchecked")
427 OFOxm<TransportPort> udpdst = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700428 builder.setUdpDst(TpPort.tpPort(udpdst.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700429 break;
430 case UDP_SRC:
431 @SuppressWarnings("unchecked")
432 OFOxm<TransportPort> udpsrc = (OFOxm<TransportPort>) oxm;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700433 builder.setUdpSrc(TpPort.tpPort(udpsrc.getValue().getPort()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700434 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800435 case ARP_OP:
436 case ARP_SHA:
437 case ARP_SPA:
438 case ARP_THA:
439 case ARP_TPA:
440 case BSN_EGR_PORT_GROUP_ID:
441 case BSN_GLOBAL_VRF_ALLOWED:
442 case BSN_IN_PORTS_128:
443 case BSN_L3_DST_CLASS_ID:
444 case BSN_L3_INTERFACE_CLASS_ID:
445 case BSN_L3_SRC_CLASS_ID:
446 case BSN_LAG_ID:
447 case BSN_TCP_FLAGS:
448 case BSN_UDF0:
449 case BSN_UDF1:
450 case BSN_UDF2:
451 case BSN_UDF3:
452 case BSN_UDF4:
453 case BSN_UDF5:
454 case BSN_UDF6:
455 case BSN_UDF7:
456 case BSN_VLAN_XLATE_PORT_GROUP_ID:
457 case BSN_VRF:
458 case ETH_TYPE:
459 case ICMPV4_CODE:
460 case ICMPV4_TYPE:
461 case ICMPV6_CODE:
462 case ICMPV6_TYPE:
463 case IN_PHY_PORT:
464 case IN_PORT:
465 case IPV6_DST:
466 case IPV6_FLABEL:
467 case IPV6_ND_SLL:
468 case IPV6_ND_TARGET:
469 case IPV6_ND_TLL:
470 case IPV6_SRC:
471 case IP_DSCP:
472 case IP_ECN:
473 case IP_PROTO:
474 case METADATA:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800475 case MPLS_TC:
476 case OCH_SIGID:
477 case OCH_SIGID_BASIC:
478 case OCH_SIGTYPE:
479 case OCH_SIGTYPE_BASIC:
480 case SCTP_DST:
481 case SCTP_SRC:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800482 default:
483 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
484 break;
485 }
486 }
487
Sho SHIMIZU6f1b09e2015-05-05 11:26:22 -0700488 // CHECKSTYLE IGNORE MethodLength FOR NEXT 1 LINES
alshabib6b5cfec2014-09-18 17:42:18 -0700489 private TrafficSelector buildSelector() {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800490 MacAddress mac;
491 Ip4Prefix ip4Prefix;
492 Ip6Address ip6Address;
493 Ip6Prefix ip6Prefix;
494
tom9a693fd2014-10-03 11:32:19 -0700495 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700496 for (MatchField<?> field : match.getMatchFields()) {
497 switch (field.id) {
498 case IN_PORT:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800499 builder.matchInPort(PortNumber
alshabib010c31d2014-09-26 10:01:12 -0700500 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700501 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800502 case IN_PHY_PORT:
503 builder.matchInPhyPort(PortNumber
504 .portNumber(match.get(MatchField.IN_PHY_PORT).getPortNumber()));
505 break;
506 case METADATA:
507 long metadata =
508 match.get(MatchField.METADATA).getValue().getValue();
509 builder.matchMetadata(metadata);
510 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700511 case ETH_DST:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800512 mac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
513 builder.matchEthDst(mac);
514 break;
515 case ETH_SRC:
516 mac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
517 builder.matchEthSrc(mac);
alshabib6b5cfec2014-09-18 17:42:18 -0700518 break;
519 case ETH_TYPE:
520 int ethType = match.get(MatchField.ETH_TYPE).getValue();
Jonathan Hart8cf212a2015-10-29 17:42:03 -0700521 builder.matchEthType((short) ethType);
alshabib6b5cfec2014-09-18 17:42:18 -0700522 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700523 case VLAN_VID:
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800524 VlanId vlanId = null;
525 if (match.isPartiallyMasked(MatchField.VLAN_VID)) {
526 Masked<OFVlanVidMatch> masked = match.getMasked(MatchField.VLAN_VID);
527 if (masked.getValue().equals(OFVlanVidMatch.PRESENT)
528 && masked.getMask().equals(OFVlanVidMatch.PRESENT)) {
529 vlanId = VlanId.ANY;
530 }
531 } else {
Jonathan Hart1468fee2015-07-16 18:50:34 -0700532 if (!match.get(MatchField.VLAN_VID).isPresentBitSet()) {
533 vlanId = VlanId.NONE;
534 } else {
535 vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
536 }
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800537 }
538 if (vlanId != null) {
539 builder.matchVlanId(vlanId);
540 }
alshabib6b5cfec2014-09-18 17:42:18 -0700541 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800542 case VLAN_PCP:
543 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
544 builder.matchVlanPcp(vlanPcp);
545 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800546 case IP_DSCP:
547 byte ipDscp = match.get(MatchField.IP_DSCP).getDscpValue();
548 builder.matchIPDscp(ipDscp);
549 break;
550 case IP_ECN:
551 byte ipEcn = match.get(MatchField.IP_ECN).getEcnValue();
552 builder.matchIPEcn(ipEcn);
553 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800554 case IP_PROTO:
555 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
556 builder.matchIPProtocol((byte) proto);
557 break;
558 case IPV4_SRC:
559 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
560 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800561 ip4Prefix = Ip4Prefix.valueOf(
562 maskedIp.getValue().getInt(),
563 maskedIp.getMask().asCidrMaskLength());
564 } else {
565 ip4Prefix = Ip4Prefix.valueOf(
566 match.get(MatchField.IPV4_SRC).getInt(),
567 Ip4Prefix.MAX_MASK_LENGTH);
568 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800569 builder.matchIPSrc(ip4Prefix);
570 break;
571 case IPV4_DST:
572 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
573 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800574 ip4Prefix = Ip4Prefix.valueOf(
575 maskedIp.getValue().getInt(),
576 maskedIp.getMask().asCidrMaskLength());
577 } else {
578 ip4Prefix = Ip4Prefix.valueOf(
579 match.get(MatchField.IPV4_DST).getInt(),
580 Ip4Prefix.MAX_MASK_LENGTH);
581 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800582 builder.matchIPDst(ip4Prefix);
Jonathan Hart34bc6142014-10-17 11:00:43 -0700583 break;
584 case TCP_SRC:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700585 builder.matchTcpSrc(TpPort.tpPort(match.get(MatchField.TCP_SRC).getPort()));
Jonathan Hart34bc6142014-10-17 11:00:43 -0700586 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800587 case TCP_DST:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700588 builder.matchTcpDst(TpPort.tpPort(match.get(MatchField.TCP_DST).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800589 break;
590 case UDP_SRC:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700591 builder.matchUdpSrc(TpPort.tpPort(match.get(MatchField.UDP_SRC).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800592 break;
593 case UDP_DST:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700594 builder.matchUdpDst(TpPort.tpPort(match.get(MatchField.UDP_DST).getPort()));
Sho SHIMIZU6f1b09e2015-05-05 11:26:22 -0700595 break;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100596 case MPLS_LABEL:
597 builder.matchMplsLabel(MplsLabel.mplsLabel((int) match.get(MatchField.MPLS_LABEL)
598 .getValue()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800599 break;
Saurav Dasffc5bbc2015-08-18 23:30:19 -0700600 case MPLS_BOS:
601 builder.matchMplsBos(match.get(MatchField.MPLS_BOS).getValue());
602 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800603 case SCTP_SRC:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700604 builder.matchSctpSrc(TpPort.tpPort(match.get(MatchField.SCTP_SRC).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800605 break;
606 case SCTP_DST:
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700607 builder.matchSctpDst(TpPort.tpPort(match.get(MatchField.SCTP_DST).getPort()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800608 break;
609 case ICMPV4_TYPE:
610 byte icmpType = (byte) match.get(MatchField.ICMPV4_TYPE).getType();
611 builder.matchIcmpType(icmpType);
612 break;
613 case ICMPV4_CODE:
614 byte icmpCode = (byte) match.get(MatchField.ICMPV4_CODE).getCode();
615 builder.matchIcmpCode(icmpCode);
616 break;
617 case IPV6_SRC:
618 if (match.isPartiallyMasked(MatchField.IPV6_SRC)) {
619 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_SRC);
620 ip6Prefix = Ip6Prefix.valueOf(
621 maskedIp.getValue().getBytes(),
622 maskedIp.getMask().asCidrMaskLength());
623 } else {
624 ip6Prefix = Ip6Prefix.valueOf(
625 match.get(MatchField.IPV6_SRC).getBytes(),
626 Ip6Prefix.MAX_MASK_LENGTH);
627 }
628 builder.matchIPv6Src(ip6Prefix);
629 break;
630 case IPV6_DST:
631 if (match.isPartiallyMasked(MatchField.IPV6_DST)) {
632 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_DST);
633 ip6Prefix = Ip6Prefix.valueOf(
634 maskedIp.getValue().getBytes(),
635 maskedIp.getMask().asCidrMaskLength());
636 } else {
637 ip6Prefix = Ip6Prefix.valueOf(
638 match.get(MatchField.IPV6_DST).getBytes(),
639 Ip6Prefix.MAX_MASK_LENGTH);
640 }
641 builder.matchIPv6Dst(ip6Prefix);
642 break;
643 case IPV6_FLABEL:
644 int flowLabel =
645 match.get(MatchField.IPV6_FLABEL).getIPv6FlowLabelValue();
646 builder.matchIPv6FlowLabel(flowLabel);
647 break;
648 case ICMPV6_TYPE:
649 byte icmpv6type = (byte) match.get(MatchField.ICMPV6_TYPE).getValue();
650 builder.matchIcmpv6Type(icmpv6type);
651 break;
652 case ICMPV6_CODE:
653 byte icmpv6code = (byte) match.get(MatchField.ICMPV6_CODE).getValue();
654 builder.matchIcmpv6Code(icmpv6code);
655 break;
656 case IPV6_ND_TARGET:
657 ip6Address =
658 Ip6Address.valueOf(match.get(MatchField.IPV6_ND_TARGET).getBytes());
659 builder.matchIPv6NDTargetAddress(ip6Address);
660 break;
661 case IPV6_ND_SLL:
662 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_SLL).getLong());
663 builder.matchIPv6NDSourceLinkLayerAddress(mac);
664 break;
665 case IPV6_ND_TLL:
666 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_TLL).getLong());
667 builder.matchIPv6NDTargetLinkLayerAddress(mac);
668 break;
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800669 case IPV6_EXTHDR:
Charles M.C. Chan2184de12015-04-26 02:24:53 +0800670 builder.matchIPv6ExthdrFlags((short) match.get(MatchField.IPV6_EXTHDR)
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700671 .getValue());
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800672 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700673 case OCH_SIGID:
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700674 CircuitSignalID sigId = match.get(MatchField.OCH_SIGID);
675 builder.add(matchLambda(Lambda.ochSignal(
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700676 lookupGridType(sigId.getGridType()), lookupChannelSpacing(sigId.getChannelSpacing()),
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -0700677 sigId.getChannelNumber(), sigId.getSpectralWidth())
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700678 ));
Marc De Leenheer49087752014-10-23 13:54:09 -0700679 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800680 case OCH_SIGTYPE:
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700681 U8 sigType = match.get(MatchField.OCH_SIGTYPE);
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700682 builder.add(matchOchSignalType(lookupOchSignalType((byte) sigType.getValue())));
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800683 break;
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700684 case TUNNEL_ID:
685 long tunnelId = match.get(MatchField.TUNNEL_ID).getValue();
686 builder.matchTunnelId(tunnelId);
687 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700688 case ARP_OP:
689 case ARP_SHA:
690 case ARP_SPA:
691 case ARP_THA:
692 case ARP_TPA:
alshabib6b5cfec2014-09-18 17:42:18 -0700693 case MPLS_TC:
alshabib6b5cfec2014-09-18 17:42:18 -0700694 default:
695 log.warn("Match type {} not yet implemented.", field.id);
alshabib6b5cfec2014-09-18 17:42:18 -0700696 }
697 }
698 return builder.build();
699 }
alshabib6b5cfec2014-09-18 17:42:18 -0700700}