blob: 4a3872c5b36da1a2e811ec1d10c5d1b4155022f4 [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 Hart6cd2f352015-01-13 17:44:45 -080018import com.google.common.collect.Lists;
Saurav Das86af8f12015-05-25 23:55:33 -070019
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;
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;
56import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp;
57import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid;
alshabib19fdc122014-10-03 11:38:19 -070058import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hartcf783202014-11-24 18:55:42 -080059import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions;
alshabibbdcbb102015-04-22 14:16:38 -070060import org.projectfloodlight.openflow.protocol.instruction.OFInstructionGotoTable;
alshabib346b5b32015-03-06 00:42:16 -080061import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteActions;
Saurav Das86af8f12015-05-25 23:55:33 -070062import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteMetadata;
alshabib6b5cfec2014-09-18 17:42:18 -070063import org.projectfloodlight.openflow.protocol.match.Match;
64import org.projectfloodlight.openflow.protocol.match.MatchField;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080065import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
Marc De Leenheer49087752014-10-23 13:54:09 -070066import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigidBasic;
alshabib346b5b32015-03-06 00:42:16 -080067import org.projectfloodlight.openflow.protocol.ver13.OFFactoryVer13;
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070068import org.projectfloodlight.openflow.types.CircuitSignalID;
sanghoc0b3c342015-03-12 17:12:10 -070069import org.projectfloodlight.openflow.types.EthType;
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 Hart6cd2f352015-01-13 17:44:45 -080081import java.util.List;
82
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070083import static org.onosproject.net.flow.criteria.Criteria.matchLambda;
84import static org.onosproject.net.flow.criteria.Criteria.matchOchSignalType;
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070085import 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;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080088import 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();
alshabib6b5cfec2014-09-18 17:42:18 -0700223 // If this is a drop rule
alshabib346b5b32015-03-06 00:42:16 -0800224 if (instructions.size() == 0) {
alshabib010c31d2014-09-26 10:01:12 -0700225 builder.drop();
alshabib6b5cfec2014-09-18 17:42:18 -0700226 return builder.build();
227 }
alshabib346b5b32015-03-06 00:42:16 -0800228 for (OFInstruction in : instructions) {
229 switch (in.getType()) {
230 case GOTO_TABLE:
alshabibbdcbb102015-04-22 14:16:38 -0700231 builder.transition(((int) ((OFInstructionGotoTable) in)
232 .getTableId().getValue()));
alshabib346b5b32015-03-06 00:42:16 -0800233 break;
234 case WRITE_METADATA:
Saurav Das86af8f12015-05-25 23:55:33 -0700235 OFInstructionWriteMetadata m = (OFInstructionWriteMetadata) in;
236 builder.writeMetadata(m.getMetadata().getValue(),
237 m.getMetadataMask().getValue());
alshabib346b5b32015-03-06 00:42:16 -0800238 break;
239 case WRITE_ACTIONS:
240 builder.deferred();
241 buildActions(((OFInstructionWriteActions) in).getActions(),
242 builder);
243 break;
244 case APPLY_ACTIONS:
245 builder.immediate();
246 buildActions(((OFInstructionApplyActions) in).getActions(),
247 builder);
248 break;
249 case CLEAR_ACTIONS:
250 builder.wipeDeferred();
251 break;
252 case EXPERIMENTER:
253 break;
254 case METER:
255 break;
256 default:
257 log.warn("Unknown instructions type {}", in.getType());
alshabib6b5cfec2014-09-18 17:42:18 -0700258 }
259 }
260
261 return builder.build();
262 }
263
alshabib346b5b32015-03-06 00:42:16 -0800264 private TrafficTreatment.Builder buildActions(List<OFAction> actions,
265 TrafficTreatment.Builder builder) {
266 for (OFAction act : actions) {
267 switch (act.getType()) {
268 case OUTPUT:
269 OFActionOutput out = (OFActionOutput) act;
270 builder.setOutput(
271 PortNumber.portNumber(out.getPort().getPortNumber()));
272 break;
273 case SET_VLAN_VID:
274 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
275 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
276 break;
277 case SET_VLAN_PCP:
278 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
279 builder.setVlanPcp(pcp.getVlanPcp().getValue());
280 break;
281 case SET_DL_DST:
282 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
283 builder.setEthDst(
284 MacAddress.valueOf(dldst.getDlAddr().getLong()));
285 break;
286 case SET_DL_SRC:
287 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
288 builder.setEthSrc(
289 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
290
291 break;
292 case SET_NW_DST:
293 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
294 IPv4Address di = nwdst.getNwAddr();
295 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
296 break;
297 case SET_NW_SRC:
298 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
299 IPv4Address si = nwsrc.getNwAddr();
300 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
301 break;
302 case EXPERIMENTER:
303 OFActionExperimenter exp = (OFActionExperimenter) act;
304 if (exp.getExperimenter() == 0x80005A06 ||
305 exp.getExperimenter() == 0x748771) {
306 OFActionCircuit ct = (OFActionCircuit) exp;
Sho SHIMIZU9553bb82015-06-30 16:02:45 -0700307 short lambda = ((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber();
308 builder.add(Instructions.modL0Lambda(Lambda.indexedLambda(lambda)));
alshabib346b5b32015-03-06 00:42:16 -0800309 } else {
310 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
311 }
312 break;
313 case SET_FIELD:
314 OFActionSetField setField = (OFActionSetField) act;
315 handleSetField(builder, setField.getField());
316 break;
317 case POP_MPLS:
318 OFActionPopMpls popMpls = (OFActionPopMpls) act;
319 builder.popMpls((short) popMpls.getEthertype().getValue());
320 break;
321 case PUSH_MPLS:
322 builder.pushMpls();
323 break;
324 case COPY_TTL_IN:
325 builder.copyTtlIn();
326 break;
327 case COPY_TTL_OUT:
328 builder.copyTtlOut();
329 break;
330 case DEC_MPLS_TTL:
331 builder.decMplsTtl();
332 break;
333 case DEC_NW_TTL:
334 builder.decNwTtl();
335 break;
336 case GROUP:
337 OFActionGroup group = (OFActionGroup) act;
338 builder.group(new DefaultGroupId(group.getGroup().getGroupNumber()));
339 break;
Jonathan Hart67fc0972015-03-19 15:21:20 -0700340 case STRIP_VLAN:
alshabib346b5b32015-03-06 00:42:16 -0800341 case POP_VLAN:
342 builder.popVlan();
343 break;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700344 case PUSH_VLAN:
345 builder.pushVlan();
346 break;
alshabib346b5b32015-03-06 00:42:16 -0800347 case SET_TP_DST:
348 case SET_TP_SRC:
349 case POP_PBB:
350 case PUSH_PBB:
alshabib346b5b32015-03-06 00:42:16 -0800351 case SET_MPLS_LABEL:
352 case SET_MPLS_TC:
353 case SET_MPLS_TTL:
354 case SET_NW_ECN:
355 case SET_NW_TOS:
356 case SET_NW_TTL:
357 case SET_QUEUE:
358
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;
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700407 case TUNNEL_ID:
408 @SuppressWarnings("unchecked")
409 OFOxm<U64> tunnelId = (OFOxm<U64>) oxm;
410 builder.setTunnelId(tunnelId.getValue().getValue());
411 break;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700412 case TCP_DST:
413 @SuppressWarnings("unchecked")
414 OFOxm<TransportPort> tcpdst = (OFOxm<TransportPort>) oxm;
415 builder.setTcpDst((short) tcpdst.getValue().getPort());
416 break;
417 case TCP_SRC:
418 @SuppressWarnings("unchecked")
419 OFOxm<TransportPort> tcpsrc = (OFOxm<TransportPort>) oxm;
420 builder.setTcpSrc((short) tcpsrc.getValue().getPort());
421 break;
422 case UDP_DST:
423 @SuppressWarnings("unchecked")
424 OFOxm<TransportPort> udpdst = (OFOxm<TransportPort>) oxm;
425 builder.setUdpDst((short) udpdst.getValue().getPort());
426 break;
427 case UDP_SRC:
428 @SuppressWarnings("unchecked")
429 OFOxm<TransportPort> udpsrc = (OFOxm<TransportPort>) oxm;
430 builder.setUdpSrc((short) udpsrc.getValue().getPort());
431 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800432 case ARP_OP:
433 case ARP_SHA:
434 case ARP_SPA:
435 case ARP_THA:
436 case ARP_TPA:
437 case BSN_EGR_PORT_GROUP_ID:
438 case BSN_GLOBAL_VRF_ALLOWED:
439 case BSN_IN_PORTS_128:
440 case BSN_L3_DST_CLASS_ID:
441 case BSN_L3_INTERFACE_CLASS_ID:
442 case BSN_L3_SRC_CLASS_ID:
443 case BSN_LAG_ID:
444 case BSN_TCP_FLAGS:
445 case BSN_UDF0:
446 case BSN_UDF1:
447 case BSN_UDF2:
448 case BSN_UDF3:
449 case BSN_UDF4:
450 case BSN_UDF5:
451 case BSN_UDF6:
452 case BSN_UDF7:
453 case BSN_VLAN_XLATE_PORT_GROUP_ID:
454 case BSN_VRF:
455 case ETH_TYPE:
456 case ICMPV4_CODE:
457 case ICMPV4_TYPE:
458 case ICMPV6_CODE:
459 case ICMPV6_TYPE:
460 case IN_PHY_PORT:
461 case IN_PORT:
462 case IPV6_DST:
463 case IPV6_FLABEL:
464 case IPV6_ND_SLL:
465 case IPV6_ND_TARGET:
466 case IPV6_ND_TLL:
467 case IPV6_SRC:
468 case IP_DSCP:
469 case IP_ECN:
470 case IP_PROTO:
471 case METADATA:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800472 case MPLS_TC:
473 case OCH_SIGID:
474 case OCH_SIGID_BASIC:
475 case OCH_SIGTYPE:
476 case OCH_SIGTYPE_BASIC:
477 case SCTP_DST:
478 case SCTP_SRC:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800479 default:
480 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
481 break;
482 }
483 }
484
Sho SHIMIZU6f1b09e2015-05-05 11:26:22 -0700485 // CHECKSTYLE IGNORE MethodLength FOR NEXT 1 LINES
alshabib6b5cfec2014-09-18 17:42:18 -0700486 private TrafficSelector buildSelector() {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800487 MacAddress mac;
488 Ip4Prefix ip4Prefix;
489 Ip6Address ip6Address;
490 Ip6Prefix ip6Prefix;
491
tom9a693fd2014-10-03 11:32:19 -0700492 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700493 for (MatchField<?> field : match.getMatchFields()) {
494 switch (field.id) {
495 case IN_PORT:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800496 builder.matchInPort(PortNumber
alshabib010c31d2014-09-26 10:01:12 -0700497 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700498 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800499 case IN_PHY_PORT:
500 builder.matchInPhyPort(PortNumber
501 .portNumber(match.get(MatchField.IN_PHY_PORT).getPortNumber()));
502 break;
503 case METADATA:
504 long metadata =
505 match.get(MatchField.METADATA).getValue().getValue();
506 builder.matchMetadata(metadata);
507 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700508 case ETH_DST:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800509 mac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
510 builder.matchEthDst(mac);
511 break;
512 case ETH_SRC:
513 mac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
514 builder.matchEthSrc(mac);
alshabib6b5cfec2014-09-18 17:42:18 -0700515 break;
516 case ETH_TYPE:
517 int ethType = match.get(MatchField.ETH_TYPE).getValue();
sanghoc0b3c342015-03-12 17:12:10 -0700518 if (ethType == EthType.VLAN_FRAME.getValue()) {
519 builder.matchVlanId(VlanId.ANY);
520 } else {
521 builder.matchEthType((short) ethType);
522 }
alshabib6b5cfec2014-09-18 17:42:18 -0700523 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700524 case VLAN_VID:
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800525 VlanId vlanId = null;
526 if (match.isPartiallyMasked(MatchField.VLAN_VID)) {
527 Masked<OFVlanVidMatch> masked = match.getMasked(MatchField.VLAN_VID);
528 if (masked.getValue().equals(OFVlanVidMatch.PRESENT)
529 && masked.getMask().equals(OFVlanVidMatch.PRESENT)) {
530 vlanId = VlanId.ANY;
531 }
532 } else {
533 vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
534 }
535 if (vlanId != null) {
536 builder.matchVlanId(vlanId);
537 }
alshabib6b5cfec2014-09-18 17:42:18 -0700538 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800539 case VLAN_PCP:
540 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
541 builder.matchVlanPcp(vlanPcp);
542 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800543 case IP_DSCP:
544 byte ipDscp = match.get(MatchField.IP_DSCP).getDscpValue();
545 builder.matchIPDscp(ipDscp);
546 break;
547 case IP_ECN:
548 byte ipEcn = match.get(MatchField.IP_ECN).getEcnValue();
549 builder.matchIPEcn(ipEcn);
550 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800551 case IP_PROTO:
552 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
553 builder.matchIPProtocol((byte) proto);
554 break;
555 case IPV4_SRC:
556 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
557 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800558 ip4Prefix = Ip4Prefix.valueOf(
559 maskedIp.getValue().getInt(),
560 maskedIp.getMask().asCidrMaskLength());
561 } else {
562 ip4Prefix = Ip4Prefix.valueOf(
563 match.get(MatchField.IPV4_SRC).getInt(),
564 Ip4Prefix.MAX_MASK_LENGTH);
565 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800566 builder.matchIPSrc(ip4Prefix);
567 break;
568 case IPV4_DST:
569 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
570 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800571 ip4Prefix = Ip4Prefix.valueOf(
572 maskedIp.getValue().getInt(),
573 maskedIp.getMask().asCidrMaskLength());
574 } else {
575 ip4Prefix = Ip4Prefix.valueOf(
576 match.get(MatchField.IPV4_DST).getInt(),
577 Ip4Prefix.MAX_MASK_LENGTH);
578 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800579 builder.matchIPDst(ip4Prefix);
Jonathan Hart34bc6142014-10-17 11:00:43 -0700580 break;
581 case TCP_SRC:
582 builder.matchTcpSrc((short) match.get(MatchField.TCP_SRC).getPort());
583 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800584 case TCP_DST:
585 builder.matchTcpDst((short) match.get(MatchField.TCP_DST).getPort());
586 break;
587 case UDP_SRC:
588 builder.matchUdpSrc((short) match.get(MatchField.UDP_SRC).getPort());
589 break;
590 case UDP_DST:
591 builder.matchUdpDst((short) match.get(MatchField.UDP_DST).getPort());
Sho SHIMIZU6f1b09e2015-05-05 11:26:22 -0700592 break;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100593 case MPLS_LABEL:
594 builder.matchMplsLabel(MplsLabel.mplsLabel((int) match.get(MatchField.MPLS_LABEL)
595 .getValue()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800596 break;
597 case SCTP_SRC:
598 builder.matchSctpSrc((short) match.get(MatchField.SCTP_SRC).getPort());
599 break;
600 case SCTP_DST:
601 builder.matchSctpDst((short) match.get(MatchField.SCTP_DST).getPort());
602 break;
603 case ICMPV4_TYPE:
604 byte icmpType = (byte) match.get(MatchField.ICMPV4_TYPE).getType();
605 builder.matchIcmpType(icmpType);
606 break;
607 case ICMPV4_CODE:
608 byte icmpCode = (byte) match.get(MatchField.ICMPV4_CODE).getCode();
609 builder.matchIcmpCode(icmpCode);
610 break;
611 case IPV6_SRC:
612 if (match.isPartiallyMasked(MatchField.IPV6_SRC)) {
613 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_SRC);
614 ip6Prefix = Ip6Prefix.valueOf(
615 maskedIp.getValue().getBytes(),
616 maskedIp.getMask().asCidrMaskLength());
617 } else {
618 ip6Prefix = Ip6Prefix.valueOf(
619 match.get(MatchField.IPV6_SRC).getBytes(),
620 Ip6Prefix.MAX_MASK_LENGTH);
621 }
622 builder.matchIPv6Src(ip6Prefix);
623 break;
624 case IPV6_DST:
625 if (match.isPartiallyMasked(MatchField.IPV6_DST)) {
626 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_DST);
627 ip6Prefix = Ip6Prefix.valueOf(
628 maskedIp.getValue().getBytes(),
629 maskedIp.getMask().asCidrMaskLength());
630 } else {
631 ip6Prefix = Ip6Prefix.valueOf(
632 match.get(MatchField.IPV6_DST).getBytes(),
633 Ip6Prefix.MAX_MASK_LENGTH);
634 }
635 builder.matchIPv6Dst(ip6Prefix);
636 break;
637 case IPV6_FLABEL:
638 int flowLabel =
639 match.get(MatchField.IPV6_FLABEL).getIPv6FlowLabelValue();
640 builder.matchIPv6FlowLabel(flowLabel);
641 break;
642 case ICMPV6_TYPE:
643 byte icmpv6type = (byte) match.get(MatchField.ICMPV6_TYPE).getValue();
644 builder.matchIcmpv6Type(icmpv6type);
645 break;
646 case ICMPV6_CODE:
647 byte icmpv6code = (byte) match.get(MatchField.ICMPV6_CODE).getValue();
648 builder.matchIcmpv6Code(icmpv6code);
649 break;
650 case IPV6_ND_TARGET:
651 ip6Address =
652 Ip6Address.valueOf(match.get(MatchField.IPV6_ND_TARGET).getBytes());
653 builder.matchIPv6NDTargetAddress(ip6Address);
654 break;
655 case IPV6_ND_SLL:
656 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_SLL).getLong());
657 builder.matchIPv6NDSourceLinkLayerAddress(mac);
658 break;
659 case IPV6_ND_TLL:
660 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_TLL).getLong());
661 builder.matchIPv6NDTargetLinkLayerAddress(mac);
662 break;
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800663 case IPV6_EXTHDR:
Charles M.C. Chan2184de12015-04-26 02:24:53 +0800664 builder.matchIPv6ExthdrFlags((short) match.get(MatchField.IPV6_EXTHDR)
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700665 .getValue());
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800666 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700667 case OCH_SIGID:
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700668 CircuitSignalID sigId = match.get(MatchField.OCH_SIGID);
669 builder.add(matchLambda(Lambda.ochSignal(
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700670 lookupGridType(sigId.getGridType()), lookupChannelSpacing(sigId.getChannelSpacing()),
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700671 sigId.getChannelNumber(), sigId.getChannelSpacing())
672 ));
Marc De Leenheer49087752014-10-23 13:54:09 -0700673 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800674 case OCH_SIGTYPE:
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700675 U8 sigType = match.get(MatchField.OCH_SIGTYPE);
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700676 builder.add(matchOchSignalType(lookupOchSignalType((byte) sigType.getValue())));
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800677 break;
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700678 case TUNNEL_ID:
679 long tunnelId = match.get(MatchField.TUNNEL_ID).getValue();
680 builder.matchTunnelId(tunnelId);
681 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700682 case ARP_OP:
683 case ARP_SHA:
684 case ARP_SPA:
685 case ARP_THA:
686 case ARP_TPA:
alshabib6b5cfec2014-09-18 17:42:18 -0700687 case MPLS_TC:
alshabib6b5cfec2014-09-18 17:42:18 -0700688 default:
689 log.warn("Match type {} not yet implemented.", field.id);
alshabib6b5cfec2014-09-18 17:42:18 -0700690 }
691 }
692 return builder.build();
693 }
alshabib6b5cfec2014-09-18 17:42:18 -0700694}