blob: f0e4f8411f9829b238c82beb82d542ccc6d5fb55 [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;
alshabib6b5cfec2014-09-18 17:42:18 -0700268 case SET_TP_DST:
269 case SET_TP_SRC:
alshabib6b5cfec2014-09-18 17:42:18 -0700270 case POP_PBB:
271 case POP_VLAN:
alshabib6b5cfec2014-09-18 17:42:18 -0700272 case PUSH_PBB:
273 case PUSH_VLAN:
alshabib6b5cfec2014-09-18 17:42:18 -0700274 case SET_MPLS_LABEL:
275 case SET_MPLS_TC:
276 case SET_MPLS_TTL:
277 case SET_NW_ECN:
278 case SET_NW_TOS:
279 case SET_NW_TTL:
280 case SET_QUEUE:
281 case STRIP_VLAN:
alshabib6b5cfec2014-09-18 17:42:18 -0700282 case ENQUEUE:
alshabib6b5cfec2014-09-18 17:42:18 -0700283 default:
284 log.warn("Action type {} not yet implemented.", act.getType());
285 }
286 }
287
288 return builder.build();
289 }
290
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800291 private void handleSetField(TrafficTreatment.Builder builder, OFOxm<?> oxm) {
292 switch (oxm.getMatchField().id) {
293 case VLAN_PCP:
294 @SuppressWarnings("unchecked")
295 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
296 builder.setVlanPcp(vlanpcp.getValue().getValue());
297 break;
298 case VLAN_VID:
299 @SuppressWarnings("unchecked")
300 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
301 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
302 break;
303 case ETH_DST:
304 @SuppressWarnings("unchecked")
305 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
306 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
307 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
308 break;
309 case ETH_SRC:
310 @SuppressWarnings("unchecked")
311 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
312 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
313 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
314 break;
315 case IPV4_DST:
316 @SuppressWarnings("unchecked")
317 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
318 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
319 break;
320 case IPV4_SRC:
321 @SuppressWarnings("unchecked")
322 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
323 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
324 break;
sangho3f97a17d2015-01-29 22:56:29 -0800325 case MPLS_LABEL:
326 @SuppressWarnings("unchecked")
327 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100328 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
sangho3f97a17d2015-01-29 22:56:29 -0800329 break;
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800330 case ARP_OP:
331 case ARP_SHA:
332 case ARP_SPA:
333 case ARP_THA:
334 case ARP_TPA:
335 case BSN_EGR_PORT_GROUP_ID:
336 case BSN_GLOBAL_VRF_ALLOWED:
337 case BSN_IN_PORTS_128:
338 case BSN_L3_DST_CLASS_ID:
339 case BSN_L3_INTERFACE_CLASS_ID:
340 case BSN_L3_SRC_CLASS_ID:
341 case BSN_LAG_ID:
342 case BSN_TCP_FLAGS:
343 case BSN_UDF0:
344 case BSN_UDF1:
345 case BSN_UDF2:
346 case BSN_UDF3:
347 case BSN_UDF4:
348 case BSN_UDF5:
349 case BSN_UDF6:
350 case BSN_UDF7:
351 case BSN_VLAN_XLATE_PORT_GROUP_ID:
352 case BSN_VRF:
353 case ETH_TYPE:
354 case ICMPV4_CODE:
355 case ICMPV4_TYPE:
356 case ICMPV6_CODE:
357 case ICMPV6_TYPE:
358 case IN_PHY_PORT:
359 case IN_PORT:
360 case IPV6_DST:
361 case IPV6_FLABEL:
362 case IPV6_ND_SLL:
363 case IPV6_ND_TARGET:
364 case IPV6_ND_TLL:
365 case IPV6_SRC:
366 case IP_DSCP:
367 case IP_ECN:
368 case IP_PROTO:
369 case METADATA:
Jonathan Hartd0ef7522014-12-11 11:51:18 -0800370 case MPLS_TC:
371 case OCH_SIGID:
372 case OCH_SIGID_BASIC:
373 case OCH_SIGTYPE:
374 case OCH_SIGTYPE_BASIC:
375 case SCTP_DST:
376 case SCTP_SRC:
377 case TCP_DST:
378 case TCP_SRC:
379 case TUNNEL_ID:
380 case UDP_DST:
381 case UDP_SRC:
382 default:
383 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
384 break;
385 }
386 }
387
alshabib6b5cfec2014-09-18 17:42:18 -0700388 private TrafficSelector buildSelector() {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800389 MacAddress mac;
390 Ip4Prefix ip4Prefix;
391 Ip6Address ip6Address;
392 Ip6Prefix ip6Prefix;
393
tom9a693fd2014-10-03 11:32:19 -0700394 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
alshabib6b5cfec2014-09-18 17:42:18 -0700395 for (MatchField<?> field : match.getMatchFields()) {
396 switch (field.id) {
397 case IN_PORT:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800398 builder.matchInPort(PortNumber
alshabib010c31d2014-09-26 10:01:12 -0700399 .portNumber(match.get(MatchField.IN_PORT).getPortNumber()));
alshabib6b5cfec2014-09-18 17:42:18 -0700400 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800401 case IN_PHY_PORT:
402 builder.matchInPhyPort(PortNumber
403 .portNumber(match.get(MatchField.IN_PHY_PORT).getPortNumber()));
404 break;
405 case METADATA:
406 long metadata =
407 match.get(MatchField.METADATA).getValue().getValue();
408 builder.matchMetadata(metadata);
409 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700410 case ETH_DST:
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800411 mac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong());
412 builder.matchEthDst(mac);
413 break;
414 case ETH_SRC:
415 mac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong());
416 builder.matchEthSrc(mac);
alshabib6b5cfec2014-09-18 17:42:18 -0700417 break;
418 case ETH_TYPE:
419 int ethType = match.get(MatchField.ETH_TYPE).getValue();
alshabib010c31d2014-09-26 10:01:12 -0700420 builder.matchEthType((short) ethType);
alshabib6b5cfec2014-09-18 17:42:18 -0700421 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700422 case VLAN_VID:
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800423 VlanId vlanId = null;
424 if (match.isPartiallyMasked(MatchField.VLAN_VID)) {
425 Masked<OFVlanVidMatch> masked = match.getMasked(MatchField.VLAN_VID);
426 if (masked.getValue().equals(OFVlanVidMatch.PRESENT)
427 && masked.getMask().equals(OFVlanVidMatch.PRESENT)) {
428 vlanId = VlanId.ANY;
429 }
430 } else {
431 vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan());
432 }
433 if (vlanId != null) {
434 builder.matchVlanId(vlanId);
435 }
alshabib6b5cfec2014-09-18 17:42:18 -0700436 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800437 case VLAN_PCP:
438 byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue();
439 builder.matchVlanPcp(vlanPcp);
440 break;
Pavlin Radoslavovd0fd8412015-02-04 13:57:00 -0800441 case IP_DSCP:
442 byte ipDscp = match.get(MatchField.IP_DSCP).getDscpValue();
443 builder.matchIPDscp(ipDscp);
444 break;
445 case IP_ECN:
446 byte ipEcn = match.get(MatchField.IP_ECN).getEcnValue();
447 builder.matchIPEcn(ipEcn);
448 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800449 case IP_PROTO:
450 short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber();
451 builder.matchIPProtocol((byte) proto);
452 break;
453 case IPV4_SRC:
454 if (match.isPartiallyMasked(MatchField.IPV4_SRC)) {
455 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800456 ip4Prefix = Ip4Prefix.valueOf(
457 maskedIp.getValue().getInt(),
458 maskedIp.getMask().asCidrMaskLength());
459 } else {
460 ip4Prefix = Ip4Prefix.valueOf(
461 match.get(MatchField.IPV4_SRC).getInt(),
462 Ip4Prefix.MAX_MASK_LENGTH);
463 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800464 builder.matchIPSrc(ip4Prefix);
465 break;
466 case IPV4_DST:
467 if (match.isPartiallyMasked(MatchField.IPV4_DST)) {
468 Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST);
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800469 ip4Prefix = Ip4Prefix.valueOf(
470 maskedIp.getValue().getInt(),
471 maskedIp.getMask().asCidrMaskLength());
472 } else {
473 ip4Prefix = Ip4Prefix.valueOf(
474 match.get(MatchField.IPV4_DST).getInt(),
475 Ip4Prefix.MAX_MASK_LENGTH);
476 }
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800477 builder.matchIPDst(ip4Prefix);
Jonathan Hart34bc6142014-10-17 11:00:43 -0700478 break;
479 case TCP_SRC:
480 builder.matchTcpSrc((short) match.get(MatchField.TCP_SRC).getPort());
481 break;
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800482 case TCP_DST:
483 builder.matchTcpDst((short) match.get(MatchField.TCP_DST).getPort());
484 break;
485 case UDP_SRC:
486 builder.matchUdpSrc((short) match.get(MatchField.UDP_SRC).getPort());
487 break;
488 case UDP_DST:
489 builder.matchUdpDst((short) match.get(MatchField.UDP_DST).getPort());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100490 case MPLS_LABEL:
491 builder.matchMplsLabel(MplsLabel.mplsLabel((int) match.get(MatchField.MPLS_LABEL)
492 .getValue()));
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800493 break;
494 case SCTP_SRC:
495 builder.matchSctpSrc((short) match.get(MatchField.SCTP_SRC).getPort());
496 break;
497 case SCTP_DST:
498 builder.matchSctpDst((short) match.get(MatchField.SCTP_DST).getPort());
499 break;
500 case ICMPV4_TYPE:
501 byte icmpType = (byte) match.get(MatchField.ICMPV4_TYPE).getType();
502 builder.matchIcmpType(icmpType);
503 break;
504 case ICMPV4_CODE:
505 byte icmpCode = (byte) match.get(MatchField.ICMPV4_CODE).getCode();
506 builder.matchIcmpCode(icmpCode);
507 break;
508 case IPV6_SRC:
509 if (match.isPartiallyMasked(MatchField.IPV6_SRC)) {
510 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_SRC);
511 ip6Prefix = Ip6Prefix.valueOf(
512 maskedIp.getValue().getBytes(),
513 maskedIp.getMask().asCidrMaskLength());
514 } else {
515 ip6Prefix = Ip6Prefix.valueOf(
516 match.get(MatchField.IPV6_SRC).getBytes(),
517 Ip6Prefix.MAX_MASK_LENGTH);
518 }
519 builder.matchIPv6Src(ip6Prefix);
520 break;
521 case IPV6_DST:
522 if (match.isPartiallyMasked(MatchField.IPV6_DST)) {
523 Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_DST);
524 ip6Prefix = Ip6Prefix.valueOf(
525 maskedIp.getValue().getBytes(),
526 maskedIp.getMask().asCidrMaskLength());
527 } else {
528 ip6Prefix = Ip6Prefix.valueOf(
529 match.get(MatchField.IPV6_DST).getBytes(),
530 Ip6Prefix.MAX_MASK_LENGTH);
531 }
532 builder.matchIPv6Dst(ip6Prefix);
533 break;
534 case IPV6_FLABEL:
535 int flowLabel =
536 match.get(MatchField.IPV6_FLABEL).getIPv6FlowLabelValue();
537 builder.matchIPv6FlowLabel(flowLabel);
538 break;
539 case ICMPV6_TYPE:
540 byte icmpv6type = (byte) match.get(MatchField.ICMPV6_TYPE).getValue();
541 builder.matchIcmpv6Type(icmpv6type);
542 break;
543 case ICMPV6_CODE:
544 byte icmpv6code = (byte) match.get(MatchField.ICMPV6_CODE).getValue();
545 builder.matchIcmpv6Code(icmpv6code);
546 break;
547 case IPV6_ND_TARGET:
548 ip6Address =
549 Ip6Address.valueOf(match.get(MatchField.IPV6_ND_TARGET).getBytes());
550 builder.matchIPv6NDTargetAddress(ip6Address);
551 break;
552 case IPV6_ND_SLL:
553 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_SLL).getLong());
554 builder.matchIPv6NDSourceLinkLayerAddress(mac);
555 break;
556 case IPV6_ND_TLL:
557 mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_TLL).getLong());
558 builder.matchIPv6NDTargetLinkLayerAddress(mac);
559 break;
Pavlin Radoslavov5e4f7542015-02-06 18:18:21 -0800560 case IPV6_EXTHDR:
561 builder.matchIPv6ExthdrFlags((int) match.get(MatchField.IPV6_EXTHDR)
562 .getValue());
563 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700564 case OCH_SIGID:
565 builder.matchLambda(match.get(MatchField.OCH_SIGID).getChannelNumber());
566 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800567 case OCH_SIGTYPE:
568 builder.matchOpticalSignalType(match.get(MatchField
569 .OCH_SIGTYPE).getValue());
570 break;
alshabib6b5cfec2014-09-18 17:42:18 -0700571 case ARP_OP:
572 case ARP_SHA:
573 case ARP_SPA:
574 case ARP_THA:
575 case ARP_TPA:
alshabib6b5cfec2014-09-18 17:42:18 -0700576 case MPLS_TC:
alshabib6b5cfec2014-09-18 17:42:18 -0700577 case TUNNEL_ID:
alshabib6b5cfec2014-09-18 17:42:18 -0700578 default:
579 log.warn("Match type {} not yet implemented.", field.id);
alshabib6b5cfec2014-09-18 17:42:18 -0700580 }
581 }
582 return builder.build();
583 }
alshabib6b5cfec2014-09-18 17:42:18 -0700584}