blob: 16960a40dc26b7c4fdd00aa95c49e04ebf1e2a7f [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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;
Michele Santuari4b6019e2014-12-19 11:31:45 +010019
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;
29import org.onosproject.net.PortNumber;
30import org.onosproject.net.flow.DefaultFlowEntry;
31import org.onosproject.net.flow.DefaultFlowRule;
32import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
34import org.onosproject.net.flow.FlowEntry;
35import org.onosproject.net.flow.FlowEntry.FlowEntryState;
36import org.onosproject.net.flow.FlowRule;
Saurav Dasfa2fa932015-03-03 11:29:48 -080037import org.onosproject.net.flow.FlowRule.Type;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.flow.TrafficSelector;
39import org.onosproject.net.flow.TrafficTreatment;
40import org.onosproject.openflow.controller.Dpid;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080041import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabib6b5cfec2014-09-18 17:42:18 -070042import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
43import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
alshabib19fdc122014-10-03 11:38:19 -070044import org.projectfloodlight.openflow.protocol.OFInstructionType;
alshabib6b5cfec2014-09-18 17:42:18 -070045import 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;
alshabib6b5cfec2014-09-18 17:42:18 -070060import org.projectfloodlight.openflow.protocol.match.Match;
61import org.projectfloodlight.openflow.protocol.match.MatchField;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080062import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
Marc De Leenheer49087752014-10-23 13:54:09 -070063import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigidBasic;
alshabib6b5cfec2014-09-18 17:42:18 -070064import org.projectfloodlight.openflow.types.IPv4Address;
Charles M.C. Chan52fae7d2015-01-17 00:35:53 +080065import org.projectfloodlight.openflow.types.IPv6Address;
Jonathan Hart0e12fad2014-10-17 14:54:58 -070066import org.projectfloodlight.openflow.types.Masked;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080067import org.projectfloodlight.openflow.types.OFVlanVidMatch;
sangho3f97a17d2015-01-29 22:56:29 -080068import org.projectfloodlight.openflow.types.U32;
Jonathan Hartd0ef7522014-12-11 11:51:18 -080069import org.projectfloodlight.openflow.types.VlanPcp;
alshabib6b5cfec2014-09-18 17:42:18 -070070import org.slf4j.Logger;
71
Jonathan Hart6cd2f352015-01-13 17:44:45 -080072import java.util.List;
73
74import static org.slf4j.LoggerFactory.getLogger;
alshabib19fdc122014-10-03 11:38:19 -070075
alshabib1c319ff2014-10-04 20:29:09 -070076public class FlowEntryBuilder {
alshabib6b5cfec2014-09-18 17:42:18 -070077 private final Logger log = getLogger(getClass());
78
79 private final OFFlowStatsEntry stat;
80 private final OFFlowRemoved removed;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080081 private final OFFlowMod flowMod;
alshabib6b5cfec2014-09-18 17:42:18 -070082
83 private final Match match;
84 private final List<OFAction> actions;
85
86 private final Dpid dpid;
87
Brian O'Connor72cb19a2015-01-16 16:14:41 -080088 public enum FlowType { STAT, REMOVED, MOD }
89
90 private final FlowType type;
Saurav Dasfa2fa932015-03-03 11:29:48 -080091 private Type tableType = FlowRule.Type.DEFAULT;
alshabib6b5cfec2014-09-18 17:42:18 -070092
93
alshabib1c319ff2014-10-04 20:29:09 -070094 public FlowEntryBuilder(Dpid dpid, OFFlowStatsEntry entry) {
alshabib6b5cfec2014-09-18 17:42:18 -070095 this.stat = entry;
96 this.match = entry.getMatch();
alshabib19fdc122014-10-03 11:38:19 -070097 this.actions = getActions(entry);
alshabib6b5cfec2014-09-18 17:42:18 -070098 this.dpid = dpid;
alshabib219ebaa2014-09-22 15:41:24 -070099 this.removed = null;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800100 this.flowMod = null;
101 this.type = FlowType.STAT;
alshabib6b5cfec2014-09-18 17:42:18 -0700102 }
103
Saurav Dasfa2fa932015-03-03 11:29:48 -0800104 public FlowEntryBuilder(Dpid dpid, OFFlowStatsEntry entry, Type tableType) {
105 this.stat = entry;
106 this.match = entry.getMatch();
107 this.actions = getActions(entry);
108 this.dpid = dpid;
109 this.removed = null;
110 this.flowMod = null;
111 this.type = FlowType.STAT;
112 this.tableType = tableType;
113 }
114
alshabib1c319ff2014-10-04 20:29:09 -0700115 public FlowEntryBuilder(Dpid dpid, OFFlowRemoved removed) {
alshabib6b5cfec2014-09-18 17:42:18 -0700116 this.match = removed.getMatch();
117 this.removed = removed;
118
119 this.dpid = dpid;
120 this.actions = null;
121 this.stat = null;
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800122 this.flowMod = null;
123 this.type = FlowType.REMOVED;
alshabib6b5cfec2014-09-18 17:42:18 -0700124
125 }
126
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800127 public FlowEntryBuilder(Dpid dpid, OFFlowMod fm) {
128 this.match = fm.getMatch();
129 this.dpid = dpid;
130 this.actions = fm.getActions();
131 this.type = FlowType.MOD;
132 this.flowMod = fm;
133 this.stat = null;
134 this.removed = null;
135 }
alshabib1c319ff2014-10-04 20:29:09 -0700136
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800137 public FlowEntry build(FlowEntryState... state) {
138 FlowRule rule;
139 switch (this.type) {
140 case STAT:
141 rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
142 buildSelector(), buildTreatment(), stat.getPriority(),
Saurav Dasfa2fa932015-03-03 11:29:48 -0800143 stat.getCookie().getValue(), stat.getIdleTimeout(), false,
144 tableType);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800145 return new DefaultFlowEntry(rule, FlowEntryState.ADDED,
146 stat.getDurationSec(), stat.getPacketCount().getValue(),
147 stat.getByteCount().getValue());
148 case REMOVED:
149 rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
150 buildSelector(), null, removed.getPriority(),
151 removed.getCookie().getValue(), removed.getIdleTimeout(), false);
152 return new DefaultFlowEntry(rule, FlowEntryState.REMOVED, removed.getDurationSec(),
153 removed.getPacketCount().getValue(), removed.getByteCount().getValue());
154 case MOD:
155 FlowEntryState flowState = state.length > 0 ? state[0] : FlowEntryState.FAILED;
156 rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
157 buildSelector(), buildTreatment(), flowMod.getPriority(),
158 flowMod.getCookie().getValue(), flowMod.getIdleTimeout(), false);
159 return new DefaultFlowEntry(rule, flowState, 0, 0, 0);
160 default:
161 log.error("Unknown flow type : {}", this.type);
162 return null;
alshabib6b5cfec2014-09-18 17:42:18 -0700163 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800164
alshabib6b5cfec2014-09-18 17:42:18 -0700165 }
166
alshabib19fdc122014-10-03 11:38:19 -0700167 private List<OFAction> getActions(OFFlowStatsEntry entry) {
168 switch (entry.getVersion()) {
169 case OF_10:
170 return entry.getActions();
171 case OF_11:
172 case OF_12:
173 case OF_13:
174 List<OFInstruction> ins = entry.getInstructions();
175 for (OFInstruction in : ins) {
Jonathan Hartcf783202014-11-24 18:55:42 -0800176 if (in.getType().equals(OFInstructionType.APPLY_ACTIONS)) {
177 OFInstructionApplyActions apply = (OFInstructionApplyActions) in;
alshabib19fdc122014-10-03 11:38:19 -0700178 return apply.getActions();
179 }
180 }
181 return Lists.newLinkedList();
182 default:
183 log.warn("Unknown OF version {}", entry.getVersion());
184 }
185 return Lists.newLinkedList();
186 }
alshabib6b5cfec2014-09-18 17:42:18 -0700187
188 private TrafficTreatment buildTreatment() {
tom9a693fd2014-10-03 11:32:19 -0700189 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700190 // If this is a drop rule
191 if (actions.size() == 0) {
alshabib010c31d2014-09-26 10:01:12 -0700192 builder.drop();
alshabib6b5cfec2014-09-18 17:42:18 -0700193 return builder.build();
194 }
195 for (OFAction act : actions) {
196 switch (act.getType()) {
197 case OUTPUT:
198 OFActionOutput out = (OFActionOutput) act;
alshabib010c31d2014-09-26 10:01:12 -0700199 builder.setOutput(
200 PortNumber.portNumber(out.getPort().getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700201 break;
202 case SET_VLAN_VID:
alshabib010c31d2014-09-26 10:01:12 -0700203 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
204 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
205 break;
206 case SET_VLAN_PCP:
alshabib6b5cfec2014-09-18 17:42:18 -0700207 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800208 builder.setVlanPcp(pcp.getVlanPcp().getValue());
alshabib6b5cfec2014-09-18 17:42:18 -0700209 break;
210 case SET_DL_DST:
211 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
alshabib010c31d2014-09-26 10:01:12 -0700212 builder.setEthDst(
213 MacAddress.valueOf(dldst.getDlAddr().getLong()));
alshabib6b5cfec2014-09-18 17:42:18 -0700214 break;
215 case SET_DL_SRC:
216 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
alshabib010c31d2014-09-26 10:01:12 -0700217 builder.setEthSrc(
218 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
alshabib6b5cfec2014-09-18 17:42:18 -0700219
220 break;
221 case SET_NW_DST:
222 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
223 IPv4Address di = nwdst.getNwAddr();
Pavlin Radoslavovff894a22014-11-05 16:09:35 -0800224 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
alshabib6b5cfec2014-09-18 17:42:18 -0700225 break;
226 case SET_NW_SRC:
227 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
228 IPv4Address si = nwsrc.getNwAddr();
Pavlin Radoslavovff894a22014-11-05 16:09:35 -0800229 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
alshabib6b5cfec2014-09-18 17:42:18 -0700230 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700231 case EXPERIMENTER:
232 OFActionExperimenter exp = (OFActionExperimenter) act;
Praseed Balakrishnandb8a9d22014-12-03 11:47:55 -0800233 if (exp.getExperimenter() == 0x80005A06 ||
234 exp.getExperimenter() == 0x748771) {
Marc De Leenheer49087752014-10-23 13:54:09 -0700235 OFActionCircuit ct = (OFActionCircuit) exp;
236 builder.setLambda(((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber());
237 } else {
238 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
239 }
240 break;
Jonathan Hart1f8e0d72014-11-25 11:13:00 -0800241 case SET_FIELD:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800242 OFActionSetField setField = (OFActionSetField) act;
243 handleSetField(builder, setField.getField());
Jonathan Hart1f8e0d72014-11-25 11:13:00 -0800244 break;
sangho3f97a17d2015-01-29 22:56:29 -0800245 case POP_MPLS:
246 OFActionPopMpls popMpls = (OFActionPopMpls) act;
247 builder.popMpls((short) popMpls.getEthertype().getValue());
248 break;
249 case PUSH_MPLS:
sangho3f97a17d2015-01-29 22:56:29 -0800250 builder.pushMpls();
251 break;
252 case COPY_TTL_IN:
sangho3f97a17d2015-01-29 22:56:29 -0800253 builder.copyTtlIn();
254 break;
255 case COPY_TTL_OUT:
sangho3f97a17d2015-01-29 22:56:29 -0800256 builder.copyTtlOut();
257 break;
258 case DEC_MPLS_TTL:
sangho3f97a17d2015-01-29 22:56:29 -0800259 builder.decMplsTtl();
260 break;
261 case DEC_NW_TTL:
sangho3f97a17d2015-01-29 22:56:29 -0800262 builder.decNwTtl();
263 break;
sangho8995ac52015-02-04 11:29:03 -0800264 case GROUP:
265 OFActionGroup group = (OFActionGroup) act;
266 builder.group(new DefaultGroupId(group.getGroup().getGroupNumber()));
267 break;
Saurav Dasfbe25c52015-03-04 11:12:00 -0800268 case POP_VLAN:
269 builder.popVlan();
270 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700271 case SET_TP_DST:
272 case SET_TP_SRC:
alshabib6b5cfec2014-09-18 17:42:18 -0700273 case POP_PBB:
alshabib6b5cfec2014-09-18 17:42:18 -0700274 case PUSH_PBB:
275 case PUSH_VLAN:
alshabib6b5cfec2014-09-18 17:42:18 -0700276 case SET_MPLS_LABEL:
277 case SET_MPLS_TC:
278 case SET_MPLS_TTL:
279 case SET_NW_ECN:
280 case SET_NW_TOS:
281 case SET_NW_TTL:
282 case SET_QUEUE:
283 case STRIP_VLAN:
alshabib6b5cfec2014-09-18 17:42:18 -0700284 case ENQUEUE:
alshabib6b5cfec2014-09-18 17:42:18 -0700285 default:
286 log.warn("Action type {} not yet implemented.", act.getType());
287 }
288 }
289
290 return builder.build();
291 }
292
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800293 private void handleSetField(TrafficTreatment.Builder builder, OFOxm<?> oxm) {
294 switch (oxm.getMatchField().id) {
295 case VLAN_PCP:
296 @SuppressWarnings("unchecked")
297 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
298 builder.setVlanPcp(vlanpcp.getValue().getValue());
299 break;
300 case VLAN_VID:
301 @SuppressWarnings("unchecked")
302 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
303 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
304 break;
305 case ETH_DST:
306 @SuppressWarnings("unchecked")
307 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
308 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
309 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
310 break;
311 case ETH_SRC:
312 @SuppressWarnings("unchecked")
313 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
314 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
315 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
316 break;
317 case IPV4_DST:
318 @SuppressWarnings("unchecked")
319 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
320 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
321 break;
322 case IPV4_SRC:
323 @SuppressWarnings("unchecked")
324 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
325 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
326 break;
sangho3f97a17d2015-01-29 22:56:29 -0800327 case MPLS_LABEL:
328 @SuppressWarnings("unchecked")
329 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100330 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
sangho3f97a17d2015-01-29 22:56:29 -0800331 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800332 case ARP_OP:
333 case ARP_SHA:
334 case ARP_SPA:
335 case ARP_THA:
336 case ARP_TPA:
337 case BSN_EGR_PORT_GROUP_ID:
338 case BSN_GLOBAL_VRF_ALLOWED:
339 case BSN_IN_PORTS_128:
340 case BSN_L3_DST_CLASS_ID:
341 case BSN_L3_INTERFACE_CLASS_ID:
342 case BSN_L3_SRC_CLASS_ID:
343 case BSN_LAG_ID:
344 case BSN_TCP_FLAGS:
345 case BSN_UDF0:
346 case BSN_UDF1:
347 case BSN_UDF2:
348 case BSN_UDF3:
349 case BSN_UDF4:
350 case BSN_UDF5:
351 case BSN_UDF6:
352 case BSN_UDF7:
353 case BSN_VLAN_XLATE_PORT_GROUP_ID:
354 case BSN_VRF:
355 case ETH_TYPE:
356 case ICMPV4_CODE:
357 case ICMPV4_TYPE:
358 case ICMPV6_CODE:
359 case ICMPV6_TYPE:
360 case IN_PHY_PORT:
361 case IN_PORT:
362 case IPV6_DST:
363 case IPV6_FLABEL:
364 case IPV6_ND_SLL:
365 case IPV6_ND_TARGET:
366 case IPV6_ND_TLL:
367 case IPV6_SRC:
368 case IP_DSCP:
369 case IP_ECN:
370 case IP_PROTO:
371 case METADATA:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800372 case MPLS_TC:
373 case OCH_SIGID:
374 case OCH_SIGID_BASIC:
375 case OCH_SIGTYPE:
376 case OCH_SIGTYPE_BASIC:
377 case SCTP_DST:
378 case SCTP_SRC:
379 case TCP_DST:
380 case TCP_SRC:
381 case TUNNEL_ID:
382 case UDP_DST:
383 case UDP_SRC:
384 default:
385 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
386 break;
387 }
388 }
389
alshabib6b5cfec2014-09-18 17:42:18 -0700390 private TrafficSelector buildSelector() {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800391 MacAddress mac;
392 Ip4Prefix ip4Prefix;
393 Ip6Address ip6Address;
394 Ip6Prefix ip6Prefix;
395
tom9a693fd2014-10-03 11:32:19 -0700396 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700397 for (MatchField<?> field : match.getMatchFields()) {
398 switch (field.id) {
399 case IN_PORT:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800400 builder.matchInPort(PortNumber
alshabib010c31d2014-09-26 10:01:12 -0700401 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700402 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800403 case IN_PHY_PORT:
404 builder.matchInPhyPort(PortNumber
405 .portNumber(match.get(MatchField.IN_PHY_PORT).getPortNumber()));
406 break;
407 case METADATA:
408 long metadata =
409 match.get(MatchField.METADATA).getValue().getValue();
410 builder.matchMetadata(metadata);
411 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700412 case ETH_DST:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800413 mac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
414 builder.matchEthDst(mac);
415 break;
416 case ETH_SRC:
417 mac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
418 builder.matchEthSrc(mac);
alshabib6b5cfec2014-09-18 17:42:18 -0700419 break;
420 case ETH_TYPE:
421 int ethType = match.get(MatchField.ETH_TYPE).getValue();
alshabib010c31d2014-09-26 10:01:12 -0700422 builder.matchEthType((short) ethType);
alshabib6b5cfec2014-09-18 17:42:18 -0700423 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700424 case VLAN_VID:
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800425 VlanId vlanId = null;
426 if (match.isPartiallyMasked(MatchField.VLAN_VID)) {
427 Masked<OFVlanVidMatch> masked = match.getMasked(MatchField.VLAN_VID);
428 if (masked.getValue().equals(OFVlanVidMatch.PRESENT)
429 && masked.getMask().equals(OFVlanVidMatch.PRESENT)) {
430 vlanId = VlanId.ANY;
431 }
432 } else {
433 vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
434 }
435 if (vlanId != null) {
436 builder.matchVlanId(vlanId);
437 }
alshabib6b5cfec2014-09-18 17:42:18 -0700438 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800439 case VLAN_PCP:
440 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
441 builder.matchVlanPcp(vlanPcp);
442 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800443 case IP_DSCP:
444 byte ipDscp = match.get(MatchField.IP_DSCP).getDscpValue();
445 builder.matchIPDscp(ipDscp);
446 break;
447 case IP_ECN:
448 byte ipEcn = match.get(MatchField.IP_ECN).getEcnValue();
449 builder.matchIPEcn(ipEcn);
450 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800451 case IP_PROTO:
452 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
453 builder.matchIPProtocol((byte) proto);
454 break;
455 case IPV4_SRC:
456 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
457 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800458 ip4Prefix = Ip4Prefix.valueOf(
459 maskedIp.getValue().getInt(),
460 maskedIp.getMask().asCidrMaskLength());
461 } else {
462 ip4Prefix = Ip4Prefix.valueOf(
463 match.get(MatchField.IPV4_SRC).getInt(),
464 Ip4Prefix.MAX_MASK_LENGTH);
465 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800466 builder.matchIPSrc(ip4Prefix);
467 break;
468 case IPV4_DST:
469 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
470 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800471 ip4Prefix = Ip4Prefix.valueOf(
472 maskedIp.getValue().getInt(),
473 maskedIp.getMask().asCidrMaskLength());
474 } else {
475 ip4Prefix = Ip4Prefix.valueOf(
476 match.get(MatchField.IPV4_DST).getInt(),
477 Ip4Prefix.MAX_MASK_LENGTH);
478 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800479 builder.matchIPDst(ip4Prefix);
Jonathan Hart34bc6142014-10-17 11:00:43 -0700480 break;
481 case TCP_SRC:
482 builder.matchTcpSrc((short) match.get(MatchField.TCP_SRC).getPort());
483 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800484 case TCP_DST:
485 builder.matchTcpDst((short) match.get(MatchField.TCP_DST).getPort());
486 break;
487 case UDP_SRC:
488 builder.matchUdpSrc((short) match.get(MatchField.UDP_SRC).getPort());
489 break;
490 case UDP_DST:
491 builder.matchUdpDst((short) match.get(MatchField.UDP_DST).getPort());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100492 case MPLS_LABEL:
493 builder.matchMplsLabel(MplsLabel.mplsLabel((int) match.get(MatchField.MPLS_LABEL)
494 .getValue()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800495 break;
496 case SCTP_SRC:
497 builder.matchSctpSrc((short) match.get(MatchField.SCTP_SRC).getPort());
498 break;
499 case SCTP_DST:
500 builder.matchSctpDst((short) match.get(MatchField.SCTP_DST).getPort());
501 break;
502 case ICMPV4_TYPE:
503 byte icmpType = (byte) match.get(MatchField.ICMPV4_TYPE).getType();
504 builder.matchIcmpType(icmpType);
505 break;
506 case ICMPV4_CODE:
507 byte icmpCode = (byte) match.get(MatchField.ICMPV4_CODE).getCode();
508 builder.matchIcmpCode(icmpCode);
509 break;
510 case IPV6_SRC:
511 if (match.isPartiallyMasked(MatchField.IPV6_SRC)) {
512 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_SRC);
513 ip6Prefix = Ip6Prefix.valueOf(
514 maskedIp.getValue().getBytes(),
515 maskedIp.getMask().asCidrMaskLength());
516 } else {
517 ip6Prefix = Ip6Prefix.valueOf(
518 match.get(MatchField.IPV6_SRC).getBytes(),
519 Ip6Prefix.MAX_MASK_LENGTH);
520 }
521 builder.matchIPv6Src(ip6Prefix);
522 break;
523 case IPV6_DST:
524 if (match.isPartiallyMasked(MatchField.IPV6_DST)) {
525 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_DST);
526 ip6Prefix = Ip6Prefix.valueOf(
527 maskedIp.getValue().getBytes(),
528 maskedIp.getMask().asCidrMaskLength());
529 } else {
530 ip6Prefix = Ip6Prefix.valueOf(
531 match.get(MatchField.IPV6_DST).getBytes(),
532 Ip6Prefix.MAX_MASK_LENGTH);
533 }
534 builder.matchIPv6Dst(ip6Prefix);
535 break;
536 case IPV6_FLABEL:
537 int flowLabel =
538 match.get(MatchField.IPV6_FLABEL).getIPv6FlowLabelValue();
539 builder.matchIPv6FlowLabel(flowLabel);
540 break;
541 case ICMPV6_TYPE:
542 byte icmpv6type = (byte) match.get(MatchField.ICMPV6_TYPE).getValue();
543 builder.matchIcmpv6Type(icmpv6type);
544 break;
545 case ICMPV6_CODE:
546 byte icmpv6code = (byte) match.get(MatchField.ICMPV6_CODE).getValue();
547 builder.matchIcmpv6Code(icmpv6code);
548 break;
549 case IPV6_ND_TARGET:
550 ip6Address =
551 Ip6Address.valueOf(match.get(MatchField.IPV6_ND_TARGET).getBytes());
552 builder.matchIPv6NDTargetAddress(ip6Address);
553 break;
554 case IPV6_ND_SLL:
555 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_SLL).getLong());
556 builder.matchIPv6NDSourceLinkLayerAddress(mac);
557 break;
558 case IPV6_ND_TLL:
559 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_TLL).getLong());
560 builder.matchIPv6NDTargetLinkLayerAddress(mac);
561 break;
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800562 case IPV6_EXTHDR:
563 builder.matchIPv6ExthdrFlags((int) match.get(MatchField.IPV6_EXTHDR)
564 .getValue());
565 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700566 case OCH_SIGID:
567 builder.matchLambda(match.get(MatchField.OCH_SIGID).getChannelNumber());
568 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800569 case OCH_SIGTYPE:
570 builder.matchOpticalSignalType(match.get(MatchField
571 .OCH_SIGTYPE).getValue());
572 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700573 case ARP_OP:
574 case ARP_SHA:
575 case ARP_SPA:
576 case ARP_THA:
577 case ARP_TPA:
alshabib6b5cfec2014-09-18 17:42:18 -0700578 case MPLS_TC:
alshabib6b5cfec2014-09-18 17:42:18 -0700579 case TUNNEL_ID:
alshabib6b5cfec2014-09-18 17:42:18 -0700580 default:
581 log.warn("Match type {} not yet implemented.", field.id);
alshabib6b5cfec2014-09-18 17:42:18 -0700582 }
583 }
584 return builder.build();
585 }
alshabib6b5cfec2014-09-18 17:42:18 -0700586}