6379b5d74e15980b18bab06d06f1e8793cbcbf0e
[controller.git] / opendaylight / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / internal / FlowConverter.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.protocol_plugin.openflow.internal;
10
11 import java.math.BigInteger;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6FlowMod;
18 import org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match;
19 import org.openflow.protocol.OFFlowMod;
20 import org.openflow.protocol.OFMatch;
21 import org.openflow.protocol.OFMessage;
22 import org.openflow.protocol.OFPacketOut;
23 import org.openflow.protocol.OFPort;
24 import org.openflow.protocol.OFVendor;
25 import org.openflow.protocol.action.OFAction;
26 import org.openflow.protocol.action.OFActionDataLayerDestination;
27 import org.openflow.protocol.action.OFActionDataLayerSource;
28 import org.openflow.protocol.action.OFActionNetworkLayerAddress;
29 import org.openflow.protocol.action.OFActionNetworkLayerDestination;
30 import org.openflow.protocol.action.OFActionNetworkLayerSource;
31 import org.openflow.protocol.action.OFActionNetworkTypeOfService;
32 import org.openflow.protocol.action.OFActionOutput;
33 import org.openflow.protocol.action.OFActionStripVirtualLan;
34 import org.openflow.protocol.action.OFActionTransportLayer;
35 import org.openflow.protocol.action.OFActionTransportLayerDestination;
36 import org.openflow.protocol.action.OFActionTransportLayerSource;
37 import org.openflow.protocol.action.OFActionVirtualLanIdentifier;
38 import org.openflow.protocol.action.OFActionVirtualLanPriorityCodePoint;
39 import org.openflow.util.U16;
40 import org.openflow.util.U32;
41
42 import org.opendaylight.controller.sal.action.Action;
43 import org.opendaylight.controller.sal.action.ActionType;
44 import org.opendaylight.controller.sal.action.Controller;
45 import org.opendaylight.controller.sal.action.Drop;
46 import org.opendaylight.controller.sal.action.Flood;
47 import org.opendaylight.controller.sal.action.FloodAll;
48 import org.opendaylight.controller.sal.action.HwPath;
49 import org.opendaylight.controller.sal.action.Loopback;
50 import org.opendaylight.controller.sal.action.Output;
51 import org.opendaylight.controller.sal.action.PopVlan;
52 import org.opendaylight.controller.sal.action.SetDlDst;
53 import org.opendaylight.controller.sal.action.SetDlSrc;
54 import org.opendaylight.controller.sal.action.SetNwDst;
55 import org.opendaylight.controller.sal.action.SetNwSrc;
56 import org.opendaylight.controller.sal.action.SetNwTos;
57 import org.opendaylight.controller.sal.action.SetTpDst;
58 import org.opendaylight.controller.sal.action.SetTpSrc;
59 import org.opendaylight.controller.sal.action.SetVlanId;
60 import org.opendaylight.controller.sal.action.SetVlanPcp;
61 import org.opendaylight.controller.sal.action.SwPath;
62 import org.opendaylight.controller.sal.core.Node;
63 import org.opendaylight.controller.sal.core.NodeConnector;
64 import org.opendaylight.controller.sal.flowprogrammer.Flow;
65 import org.opendaylight.controller.sal.match.Match;
66 import org.opendaylight.controller.sal.match.MatchField;
67 import org.opendaylight.controller.sal.match.MatchType;
68 import org.opendaylight.controller.sal.utils.NetUtils;
69 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
72
73 /**
74  * Utility class for converting a SAL Flow into the OF flow and vice-versa
75  */
76 public class FlowConverter {
77     protected static final Logger logger = LoggerFactory
78     .getLogger(FlowConverter.class);
79     private Flow flow; // SAL Flow
80     private OFMatch ofMatch; // OF 1.0 match or OF 1.0 + IPv6 extension match
81     private List<OFAction> actionsList; // OF 1.0 actions
82     private int actionsLength;
83     private boolean isIPv6;
84
85     public FlowConverter(OFMatch ofMatch, List<OFAction> actionsList) {
86         this.ofMatch = ofMatch;
87         this.actionsList = actionsList;
88         this.actionsLength = 0;
89         this.flow = null;
90         this.isIPv6 = ofMatch instanceof V6Match;
91     }
92
93     public FlowConverter(Flow flow) {
94         this.ofMatch = null;
95         this.actionsList = null;
96         this.actionsLength = 0;
97         this.flow = flow;
98         this.isIPv6 = flow.isIPv6();
99     }
100
101     /**
102      * Returns the match in OF 1.0 (OFMatch) form or OF 1.0 + IPv6 extensions
103      * form (V6Match)
104      * 
105      * @return
106      */
107     public OFMatch getOFMatch() {
108         if (ofMatch == null) {
109             Match match = flow.getMatch();
110             ofMatch = (isIPv6) ? new V6Match() : new OFMatch();
111
112             int wildcards = OFMatch.OFPFW_ALL;
113             if (match.isPresent(MatchType.IN_PORT)) {
114                 short port = (Short) ((NodeConnector) match.getField(
115                         MatchType.IN_PORT).getValue()).getID();
116                 if (!isIPv6) {
117                     ofMatch.setInputPort(port);
118                     wildcards &= ~OFMatch.OFPFW_IN_PORT;
119                 } else {
120                     ((V6Match) ofMatch).setInputPort(port, (short) 0);
121                 }
122             }
123             if (match.isPresent(MatchType.DL_SRC)) {
124                 byte[] srcMac = (byte[]) match.getField(MatchType.DL_SRC)
125                         .getValue();
126                 if (!isIPv6) {
127                     ofMatch.setDataLayerSource(srcMac.clone());
128                     wildcards &= ~OFMatch.OFPFW_DL_SRC;
129                 } else {
130                     ((V6Match) ofMatch).setDataLayerSource(srcMac, null);
131                 }
132             }
133             if (match.isPresent(MatchType.DL_DST)) {
134                 byte[] dstMac = (byte[]) match.getField(MatchType.DL_DST)
135                         .getValue();
136                 if (!isIPv6) {
137                     ofMatch.setDataLayerDestination(dstMac.clone());
138                     wildcards &= ~OFMatch.OFPFW_DL_DST;
139                 } else {
140                     ((V6Match) ofMatch).setDataLayerDestination(dstMac, null);
141                 }
142             }
143             if (match.isPresent(MatchType.DL_VLAN)) {
144                 short vlan = (Short) match.getField(MatchType.DL_VLAN)
145                         .getValue();
146                 if (!isIPv6) {
147                     ofMatch.setDataLayerVirtualLan(vlan);
148                     wildcards &= ~OFMatch.OFPFW_DL_VLAN;
149                 } else {
150                     ((V6Match) ofMatch).setDataLayerVirtualLan(vlan, (short) 0);
151                 }
152             }
153             if (match.isPresent(MatchType.DL_VLAN_PR)) {
154                 byte vlanPr = (Byte) match.getField(MatchType.DL_VLAN_PR)
155                         .getValue();
156                 if (!isIPv6) {
157                     ofMatch.setDataLayerVirtualLanPriorityCodePoint(vlanPr);
158                     wildcards &= ~OFMatch.OFPFW_DL_VLAN_PCP;
159                 } else {
160                     ((V6Match) ofMatch)
161                             .setDataLayerVirtualLanPriorityCodePoint(vlanPr,
162                                     (byte) 0);
163                 }
164             }
165             if (match.isPresent(MatchType.DL_TYPE)) {
166                 short ethType = (Short) match.getField(MatchType.DL_TYPE)
167                         .getValue();
168                 if (!isIPv6) {
169                     ofMatch.setDataLayerType(ethType);
170                     wildcards &= ~OFMatch.OFPFW_DL_TYPE;
171                 } else {
172                     ((V6Match) ofMatch).setDataLayerType(ethType, (short) 0);
173                 }
174             }
175             if (match.isPresent(MatchType.NW_TOS)) {
176                 /*
177                  * OF 1.0 switch expects the TOS as the 6 msb in the byte. it is
178                  * actually the DSCP field followed by a zero ECN
179                  */
180                 byte tos = (Byte) match.getField(MatchType.NW_TOS).getValue();
181                 byte dscp = (byte) ((int) tos << 2);
182                 if (!isIPv6) {
183                     ofMatch.setNetworkTypeOfService(dscp);
184                     wildcards &= ~OFMatch.OFPFW_NW_TOS;
185                 } else {
186                     ((V6Match) ofMatch).setNetworkTypeOfService(dscp, (byte) 0);
187                 }
188             }
189             if (match.isPresent(MatchType.NW_PROTO)) {
190                 byte proto = (Byte) match.getField(MatchType.NW_PROTO)
191                         .getValue();
192                 if (!isIPv6) {
193                     ofMatch.setNetworkProtocol(proto);
194                     wildcards &= ~OFMatch.OFPFW_NW_PROTO;
195                 } else {
196                     ((V6Match) ofMatch).setNetworkProtocol(proto, (byte) 0);
197                 }
198             }
199             if (match.isPresent(MatchType.NW_SRC)) {
200                 InetAddress address = (InetAddress) match.getField(
201                         MatchType.NW_SRC).getValue();
202                 InetAddress mask = (InetAddress) match.getField(
203                         MatchType.NW_SRC).getMask();
204                 if (!isIPv6) {
205                     ofMatch.setNetworkSource(NetUtils.byteArray4ToInt(address
206                             .getAddress()));
207                     int maskLength = NetUtils
208                             .getSubnetMaskLength((mask == null) ? null : mask
209                                     .getAddress());
210                     wildcards = (wildcards & ~OFMatch.OFPFW_NW_SRC_MASK)
211                             | (maskLength << OFMatch.OFPFW_NW_SRC_SHIFT);
212                 } else {
213                     ((V6Match) ofMatch).setNetworkSource(address, mask);
214                 }
215             }
216             if (match.isPresent(MatchType.NW_DST)) {
217                 InetAddress address = (InetAddress) match.getField(
218                         MatchType.NW_DST).getValue();
219                 InetAddress mask = (InetAddress) match.getField(
220                         MatchType.NW_DST).getMask();
221                 if (!isIPv6) {
222                     ofMatch.setNetworkDestination(NetUtils
223                             .byteArray4ToInt(address.getAddress()));
224                     int maskLength = NetUtils
225                             .getSubnetMaskLength((mask == null) ? null : mask
226                                     .getAddress());
227                     wildcards = (wildcards & ~OFMatch.OFPFW_NW_DST_MASK)
228                             | (maskLength << OFMatch.OFPFW_NW_DST_SHIFT);
229                 } else {
230                     ((V6Match) ofMatch).setNetworkDestination(address, mask);
231                 }
232             }
233             if (match.isPresent(MatchType.TP_SRC)) {
234                 short port = (Short) match.getField(MatchType.TP_SRC)
235                         .getValue();
236                 if (!isIPv6) {
237                     ofMatch.setTransportSource(port);
238                     wildcards &= ~OFMatch.OFPFW_TP_SRC;
239                 } else {
240                     ((V6Match) ofMatch).setTransportSource(port, (short) 0);
241                 }
242             }
243             if (match.isPresent(MatchType.TP_DST)) {
244                 short port = (Short) match.getField(MatchType.TP_DST)
245                         .getValue();
246                 if (!isIPv6) {
247                     ofMatch.setTransportDestination(port);
248                     wildcards &= ~OFMatch.OFPFW_TP_DST;
249                 } else {
250                     ((V6Match) ofMatch)
251                             .setTransportDestination(port, (short) 0);
252                 }
253             }
254
255             if (!isIPv6) {
256                 ofMatch.setWildcards(U32.t(Long.valueOf(wildcards)));
257             }
258         }
259
260         return ofMatch;
261     }
262
263     /**
264      * Returns the list of actions in OF 1.0 form
265      * 
266      * @return
267      */
268     public List<OFAction> getOFActions() {
269         if (this.actionsList == null) {
270             actionsList = new ArrayList<OFAction>();
271             for (Action action : flow.getActions()) {
272                 if (action.getType() == ActionType.OUTPUT) {
273                     Output a = (Output) action;
274                     OFActionOutput ofAction = new OFActionOutput();
275                     ofAction.setMaxLength((short) 0xffff);
276                     ofAction.setPort(PortConverter.toOFPort(a.getPort()));
277                     actionsList.add(ofAction);
278                     actionsLength += OFActionOutput.MINIMUM_LENGTH;
279                     continue;
280                 }
281                 if (action.getType() == ActionType.DROP) {
282                     continue;
283                 }
284                 if (action.getType() == ActionType.LOOPBACK) {
285                     OFActionOutput ofAction = new OFActionOutput();
286                     ofAction.setPort(OFPort.OFPP_IN_PORT.getValue());
287                     actionsList.add(ofAction);
288                     actionsLength += OFActionOutput.MINIMUM_LENGTH;
289                     continue;
290                 }
291                 if (action.getType() == ActionType.FLOOD) {
292                     OFActionOutput ofAction = new OFActionOutput();
293                     ofAction.setPort(OFPort.OFPP_FLOOD.getValue());
294                     actionsList.add(ofAction);
295                     actionsLength += OFActionOutput.MINIMUM_LENGTH;
296                     continue;
297                 }
298                 if (action.getType() == ActionType.FLOOD_ALL) {
299                     OFActionOutput ofAction = new OFActionOutput();
300                     ofAction.setPort(OFPort.OFPP_ALL.getValue());
301                     actionsList.add(ofAction);
302                     actionsLength += OFActionOutput.MINIMUM_LENGTH;
303                     continue;
304                 }
305                 if (action.getType() == ActionType.CONTROLLER) {
306                     OFActionOutput ofAction = new OFActionOutput();
307                     ofAction.setPort(OFPort.OFPP_CONTROLLER.getValue());
308                     // We want the whole frame hitting the match be sent to the
309                     // controller
310                     ofAction.setMaxLength((short) 0xffff);
311                     actionsList.add(ofAction);
312                     actionsLength += OFActionOutput.MINIMUM_LENGTH;
313                     continue;
314                 }
315                 if (action.getType() == ActionType.SW_PATH) {
316                     OFActionOutput ofAction = new OFActionOutput();
317                     ofAction.setPort(OFPort.OFPP_LOCAL.getValue());
318                     actionsList.add(ofAction);
319                     actionsLength += OFActionOutput.MINIMUM_LENGTH;
320                     continue;
321                 }
322                 if (action.getType() == ActionType.HW_PATH) {
323                     OFActionOutput ofAction = new OFActionOutput();
324                     ofAction.setPort(OFPort.OFPP_NORMAL.getValue());
325                     actionsList.add(ofAction);
326                     actionsLength += OFActionOutput.MINIMUM_LENGTH;
327                     continue;
328                 }
329                 if (action.getType() == ActionType.SET_VLAN_ID) {
330                     SetVlanId a = (SetVlanId) action;
331                     OFActionVirtualLanIdentifier ofAction = new OFActionVirtualLanIdentifier();
332                     ofAction.setVirtualLanIdentifier((short) a.getVlanId());
333                     actionsList.add(ofAction);
334                     actionsLength += OFActionVirtualLanIdentifier.MINIMUM_LENGTH;
335                     continue;
336                 }
337                 if (action.getType() == ActionType.SET_VLAN_PCP) {
338                     SetVlanPcp a = (SetVlanPcp) action;
339                     OFActionVirtualLanPriorityCodePoint ofAction = new OFActionVirtualLanPriorityCodePoint();
340                     ofAction.setVirtualLanPriorityCodePoint(Integer.valueOf(
341                             a.getPcp()).byteValue());
342                     actionsList.add(ofAction);
343                     actionsLength += OFActionVirtualLanPriorityCodePoint.MINIMUM_LENGTH;
344                     continue;
345                 }
346                 if (action.getType() == ActionType.POP_VLAN) {
347                     OFActionStripVirtualLan ofAction = new OFActionStripVirtualLan();
348                     actionsList.add(ofAction);
349                     actionsLength += OFActionStripVirtualLan.MINIMUM_LENGTH;
350                     continue;
351                 }
352                 if (action.getType() == ActionType.SET_DL_SRC) {
353                     SetDlSrc a = (SetDlSrc) action;
354                     OFActionDataLayerSource ofAction = new OFActionDataLayerSource();
355                     ofAction.setDataLayerAddress(a.getDlAddress());
356                     actionsList.add(ofAction);
357                     actionsLength += OFActionDataLayerSource.MINIMUM_LENGTH;
358                     continue;
359                 }
360                 if (action.getType() == ActionType.SET_DL_DST) {
361                     SetDlDst a = (SetDlDst) action;
362                     OFActionDataLayerDestination ofAction = new OFActionDataLayerDestination();
363                     ofAction.setDataLayerAddress(a.getDlAddress());
364                     actionsList.add(ofAction);
365                     actionsLength += OFActionDataLayerDestination.MINIMUM_LENGTH;
366                     continue;
367                 }
368                 if (action.getType() == ActionType.SET_NW_SRC) {
369                     SetNwSrc a = (SetNwSrc) action;
370                     OFActionNetworkLayerSource ofAction = new OFActionNetworkLayerSource();
371                     ofAction.setNetworkAddress(NetUtils.byteArray4ToInt(a
372                             .getAddress().getAddress()));
373                     actionsList.add(ofAction);
374                     actionsLength += OFActionNetworkLayerAddress.MINIMUM_LENGTH;
375                     continue;
376                 }
377                 if (action.getType() == ActionType.SET_NW_DST) {
378                     SetNwDst a = (SetNwDst) action;
379                     OFActionNetworkLayerDestination ofAction = new OFActionNetworkLayerDestination();
380                     ofAction.setNetworkAddress(NetUtils.byteArray4ToInt(a
381                             .getAddress().getAddress()));
382                     actionsList.add(ofAction);
383                     actionsLength += OFActionNetworkLayerAddress.MINIMUM_LENGTH;
384                     continue;
385                 }
386                 if (action.getType() == ActionType.SET_NW_TOS) {
387                     SetNwTos a = (SetNwTos) action;
388                     OFActionNetworkTypeOfService ofAction = new OFActionNetworkTypeOfService();
389                     ofAction.setNetworkTypeOfService(Integer.valueOf(
390                             a.getNwTos()).byteValue());
391                     actionsList.add(ofAction);
392                     actionsLength += OFActionNetworkTypeOfService.MINIMUM_LENGTH;
393                     continue;
394                 }
395                 if (action.getType() == ActionType.SET_TP_SRC) {
396                     SetTpSrc a = (SetTpSrc) action;
397                     OFActionTransportLayerSource ofAction = new OFActionTransportLayerSource();
398                     ofAction.setTransportPort(Integer.valueOf(a.getPort())
399                             .shortValue());
400                     actionsList.add(ofAction);
401                     actionsLength += OFActionTransportLayer.MINIMUM_LENGTH;
402                     continue;
403                 }
404                 if (action.getType() == ActionType.SET_TP_DST) {
405                     SetTpDst a = (SetTpDst) action;
406                     OFActionTransportLayerDestination ofAction = new OFActionTransportLayerDestination();
407                     ofAction.setTransportPort(Integer.valueOf(a.getPort())
408                             .shortValue());
409                     actionsList.add(ofAction);
410                     actionsLength += OFActionTransportLayer.MINIMUM_LENGTH;
411                     continue;
412                 }
413                 if (action.getType() == ActionType.SET_NEXT_HOP) {
414                     // TODO
415                     continue;
416                 }
417             }
418         }
419         return actionsList;
420     }
421
422     /**
423      * Utility to convert a SAL flow to an OF 1.0 (OFFlowMod) or to an OF 1.0 +
424      * IPv6 extension (V6FlowMod) Flow modifier Message
425      * 
426      * @param sw
427      * @param command
428      * @param port
429      * @return
430      */
431     public OFMessage getOFFlowMod(short command, OFPort port) {
432         OFMessage fm = (isIPv6) ? new V6FlowMod() : new OFFlowMod();
433         if (this.ofMatch == null) {
434             getOFMatch();
435         }
436         if (this.actionsList == null) {
437             getOFActions();
438         }
439         if (!isIPv6) {
440             ((OFFlowMod) fm).setMatch(this.ofMatch);
441             ((OFFlowMod) fm).setActions(this.actionsList);
442             ((OFFlowMod) fm).setPriority(flow.getPriority());
443             ((OFFlowMod) fm).setCookie(flow.getId());
444             ((OFFlowMod) fm).setBufferId(OFPacketOut.BUFFER_ID_NONE);
445             ((OFFlowMod) fm).setLength(U16.t(OFFlowMod.MINIMUM_LENGTH
446                     + actionsLength));
447             ((OFFlowMod) fm).setIdleTimeout(flow.getIdleTimeout());
448             ((OFFlowMod) fm).setHardTimeout(flow.getHardTimeout());
449             ((OFFlowMod) fm).setCommand(command);
450             if (port != null) {
451                 ((OFFlowMod) fm).setOutPort(port);
452             }
453             if (command == OFFlowMod.OFPFC_ADD
454                     || command == OFFlowMod.OFPFC_MODIFY
455                     || command == OFFlowMod.OFPFC_MODIFY_STRICT) {
456                 if (flow.getIdleTimeout() != 0 || flow.getHardTimeout() != 0) {
457                     // Instruct switch to let controller know when flow expires
458                     ((OFFlowMod) fm).setFlags((short) 1);
459                 }
460             }
461         } else {
462             ((V6FlowMod) fm).setVendor();
463             ((V6FlowMod) fm).setMatch((V6Match) ofMatch);
464             ((V6FlowMod) fm).setActions(this.actionsList);
465             ((V6FlowMod) fm).setPriority(flow.getPriority());
466             ((V6FlowMod) fm).setCookie(flow.getId());
467             ((V6FlowMod) fm).setLength(U16.t(OFVendor.MINIMUM_LENGTH
468                     + ((V6Match) ofMatch).getIPv6ExtMinHdrLen()
469                     + ((V6Match) ofMatch).getIPv6MatchLen()
470                     + ((V6Match) ofMatch).getPadSize() + actionsLength));
471             ((V6FlowMod) fm).setIdleTimeout(flow.getIdleTimeout());
472             ((V6FlowMod) fm).setHardTimeout(flow.getHardTimeout());
473             ((V6FlowMod) fm).setCommand(command);
474             if (port != null) {
475                 ((V6FlowMod) fm).setOutPort(port);
476             }
477             if (command == OFFlowMod.OFPFC_ADD
478                     || command == OFFlowMod.OFPFC_MODIFY
479                     || command == OFFlowMod.OFPFC_MODIFY_STRICT) {
480                 if (flow.getIdleTimeout() != 0 || flow.getHardTimeout() != 0) {
481                     // Instruct switch to let controller know when flow expires
482                     ((V6FlowMod) fm).setFlags((short) 1);
483                 }
484             }
485         }
486         return fm;
487     }
488
489     public Flow getFlow(Node node) {
490         if (this.flow == null) {
491             Match salMatch = new Match();
492
493             /*
494              * Installed flow may not have a Match defined like in case of a
495              * drop all flow
496              */
497             if (ofMatch != null) {
498                 if (!isIPv6) {
499                     // Compute OF1.0 Match
500                     if (ofMatch.getInputPort() != 0) {
501                         salMatch.setField(new MatchField(MatchType.IN_PORT,
502                                 NodeConnectorCreator.createNodeConnector(
503                                         (Short) ofMatch.getInputPort(), node)));
504                     }
505                     if (ofMatch.getDataLayerSource() != null
506                             && !NetUtils
507                                     .isZeroMAC(ofMatch.getDataLayerSource())) {
508                         byte srcMac[] = ofMatch.getDataLayerSource();
509                         salMatch.setField(new MatchField(MatchType.DL_SRC,
510                                 srcMac.clone()));
511                     }
512                     if (ofMatch.getDataLayerDestination() != null
513                             && !NetUtils.isZeroMAC(ofMatch
514                                     .getDataLayerDestination())) {
515                         byte dstMac[] = ofMatch.getDataLayerDestination();
516                         salMatch.setField(new MatchField(MatchType.DL_DST,
517                                 dstMac.clone()));
518                     }
519                     if (ofMatch.getDataLayerType() != 0) {
520                         salMatch.setField(new MatchField(MatchType.DL_TYPE,
521                                 ofMatch.getDataLayerType()));
522                     }
523                     if (ofMatch.getDataLayerVirtualLan() != 0) {
524                         salMatch.setField(new MatchField(MatchType.DL_VLAN,
525                                 ofMatch.getDataLayerVirtualLan()));
526                     }
527                     if (ofMatch.getDataLayerVirtualLanPriorityCodePoint() != 0) {
528                         salMatch.setField(MatchType.DL_VLAN_PR, ofMatch
529                                 .getDataLayerVirtualLanPriorityCodePoint());
530                     }
531                     if (ofMatch.getNetworkSource() != 0) {
532                         salMatch.setField(MatchType.NW_SRC, NetUtils
533                                 .getInetAddress(ofMatch.getNetworkSource()),
534                                 NetUtils.getInetNetworkMask(
535                                         ofMatch.getNetworkSourceMaskLen(),
536                                         false));
537                     }
538                     if (ofMatch.getNetworkDestination() != 0) {
539                         salMatch.setField(MatchType.NW_DST,
540                                 NetUtils.getInetAddress(ofMatch
541                                         .getNetworkDestination()),
542                                 NetUtils.getInetNetworkMask(
543                                         ofMatch.getNetworkDestinationMaskLen(),
544                                         false));
545                     }
546                     if (ofMatch.getNetworkTypeOfService() != 0) {
547                         int dscp = NetUtils.getUnsignedByte(ofMatch
548                                 .getNetworkTypeOfService());
549                         byte tos = (byte) (dscp >> 2);
550                         salMatch.setField(MatchType.NW_TOS, tos);
551                     }
552                     if (ofMatch.getNetworkProtocol() != 0) {
553                         salMatch.setField(MatchType.NW_PROTO,
554                                 ofMatch.getNetworkProtocol());
555                     }
556                     if (ofMatch.getTransportSource() != 0) {
557                         salMatch.setField(MatchType.TP_SRC,
558                                 ((Short) ofMatch.getTransportSource()));
559                     }
560                     if (ofMatch.getTransportDestination() != 0) {
561                         salMatch.setField(MatchType.TP_DST,
562                                 ((Short) ofMatch.getTransportDestination()));
563                     }
564                 } else {
565                     // Compute OF1.0 + IPv6 extensions Match
566                     V6Match v6Match = (V6Match) ofMatch;
567                     if (v6Match.getInputPort() != 0) {
568                         // Mask on input port is not defined
569                         salMatch.setField(new MatchField(MatchType.IN_PORT,
570                                 NodeConnectorCreator.createOFNodeConnector(
571                                         (Short) v6Match.getInputPort(), node)));
572                     }
573                     if (v6Match.getDataLayerSource() != null
574                             && !NetUtils
575                                     .isZeroMAC(ofMatch.getDataLayerSource())) {
576                         byte srcMac[] = v6Match.getDataLayerSource();
577                         salMatch.setField(new MatchField(MatchType.DL_SRC,
578                                 srcMac.clone()));
579                     }
580                     if (v6Match.getDataLayerDestination() != null
581                             && !NetUtils.isZeroMAC(ofMatch
582                                     .getDataLayerDestination())) {
583                         byte dstMac[] = v6Match.getDataLayerDestination();
584                         salMatch.setField(new MatchField(MatchType.DL_DST,
585                                 dstMac.clone()));
586                     }
587                     if (v6Match.getDataLayerType() != 0) {
588                         salMatch.setField(new MatchField(MatchType.DL_TYPE,
589                                 v6Match.getDataLayerType()));
590                     }
591                     if (v6Match.getDataLayerVirtualLan() != 0) {
592                         salMatch.setField(new MatchField(MatchType.DL_VLAN,
593                                 v6Match.getDataLayerVirtualLan()));
594                     }
595                     if (v6Match.getDataLayerVirtualLanPriorityCodePoint() != 0) {
596                         salMatch.setField(MatchType.DL_VLAN_PR, v6Match
597                                 .getDataLayerVirtualLanPriorityCodePoint());
598                     }
599                     if (v6Match.getNetworkSrc() != null) {
600                         salMatch.setField(MatchType.NW_SRC,
601                                 v6Match.getNetworkSrc(),
602                                 v6Match.getNetworkSourceMask());
603                     }
604                     if (v6Match.getNetworkDest() != null) {
605                         salMatch.setField(MatchType.NW_DST,
606                                 v6Match.getNetworkDest(),
607                                 v6Match.getNetworkDestinationMask());
608                     }
609                     if (v6Match.getNetworkTypeOfService() != 0) {
610                         int dscp = NetUtils.getUnsignedByte(v6Match
611                                 .getNetworkTypeOfService());
612                         byte tos = (byte) (dscp >> 2);
613                         salMatch.setField(MatchType.NW_TOS, tos);
614                     }
615                     if (v6Match.getNetworkProtocol() != 0) {
616                         salMatch.setField(MatchType.NW_PROTO,
617                                 v6Match.getNetworkProtocol());
618                     }
619                     if (v6Match.getTransportSource() != 0) {
620                         salMatch.setField(MatchType.TP_SRC,
621                                 ((Short) v6Match.getTransportSource()));
622                     }
623                     if (v6Match.getTransportDestination() != 0) {
624                         salMatch.setField(MatchType.TP_DST,
625                                 ((Short) v6Match.getTransportDestination()));
626                     }
627                 }
628             }
629
630             // Convert actions
631             Action salAction = null;
632             List<Action> salActionList = new ArrayList<Action>();
633             if (actionsList == null) {
634                 salActionList.add(new Drop());
635             } else {
636                 for (OFAction ofAction : actionsList) {
637                     if (ofAction instanceof OFActionOutput) {
638                         short ofPort = ((OFActionOutput) ofAction).getPort();
639                         if (ofPort == OFPort.OFPP_CONTROLLER.getValue()) {
640                             salAction = new Controller();
641                         } else if (ofPort == OFPort.OFPP_NONE.getValue()) {
642                             salAction = new Drop();
643                         } else if (ofPort == OFPort.OFPP_IN_PORT.getValue()) {
644                             salAction = new Loopback();
645                         } else if (ofPort == OFPort.OFPP_FLOOD.getValue()) {
646                             salAction = new Flood();
647                         } else if (ofPort == OFPort.OFPP_ALL.getValue()) {
648                             salAction = new FloodAll();
649                         } else if (ofPort == OFPort.OFPP_LOCAL.getValue()) {
650                             salAction = new SwPath();
651                         } else if (ofPort == OFPort.OFPP_NORMAL.getValue()) {
652                             salAction = new HwPath();
653                         } else if (ofPort == OFPort.OFPP_TABLE.getValue()) {
654                             salAction = new HwPath(); // TODO: we do not handle
655                                                       // table in sal for now
656                         } else {
657                             salAction = new Output(
658                                     NodeConnectorCreator.createOFNodeConnector(
659                                             ofPort, node));
660                         }
661                     } else if (ofAction instanceof OFActionVirtualLanIdentifier) {
662                         salAction = new SetVlanId(
663                                 ((OFActionVirtualLanIdentifier) ofAction)
664                                         .getVirtualLanIdentifier());
665                     } else if (ofAction instanceof OFActionStripVirtualLan) {
666                         salAction = new PopVlan();
667                     } else if (ofAction instanceof OFActionVirtualLanPriorityCodePoint) {
668                         salAction = new SetVlanPcp(
669                                 ((OFActionVirtualLanPriorityCodePoint) ofAction)
670                                         .getVirtualLanPriorityCodePoint());
671                     } else if (ofAction instanceof OFActionDataLayerSource) {
672                         salAction = new SetDlSrc(
673                                 ((OFActionDataLayerSource) ofAction)
674                                         .getDataLayerAddress().clone());
675                     } else if (ofAction instanceof OFActionDataLayerDestination) {
676                         salAction = new SetDlDst(
677                                 ((OFActionDataLayerDestination) ofAction)
678                                         .getDataLayerAddress().clone());
679                     } else if (ofAction instanceof OFActionNetworkLayerSource) {
680                         byte addr[] = BigInteger.valueOf(
681                                 ((OFActionNetworkLayerSource) ofAction)
682                                         .getNetworkAddress()).toByteArray();
683                         InetAddress ip = null;
684                         try {
685                             ip = InetAddress.getByAddress(addr);
686                         } catch (UnknownHostException e) {
687                             logger.error("",e);
688                         }
689                         salAction = new SetNwSrc(ip);
690                     } else if (ofAction instanceof OFActionNetworkLayerDestination) {
691                         byte addr[] = BigInteger.valueOf(
692                                 ((OFActionNetworkLayerDestination) ofAction)
693                                         .getNetworkAddress()).toByteArray();
694                         InetAddress ip = null;
695                         try {
696                             ip = InetAddress.getByAddress(addr);
697                         } catch (UnknownHostException e) {
698                             logger.error("",e);
699                         }
700                         salAction = new SetNwDst(ip);
701                     } else if (ofAction instanceof OFActionNetworkTypeOfService) {
702                         salAction = new SetNwTos(
703                                 ((OFActionNetworkTypeOfService) ofAction)
704                                         .getNetworkTypeOfService());
705                     } else if (ofAction instanceof OFActionTransportLayerSource) {
706                         Short port = ((OFActionTransportLayerSource) ofAction)
707                                 .getTransportPort();
708                         int intPort = NetUtils.getUnsignedShort(port);
709                         salAction = new SetTpSrc(intPort);
710                     } else if (ofAction instanceof OFActionTransportLayerDestination) {
711                         Short port = ((OFActionTransportLayerDestination) ofAction)
712                                 .getTransportPort();
713                         int intPort = NetUtils.getUnsignedShort(port);
714                         salAction = new SetTpDst(intPort);
715                     }
716                     salActionList.add(salAction);
717                 }
718             }
719             // Create Flow
720             flow = new Flow(salMatch, salActionList);
721         }
722         return flow;
723     }
724
725 }