BUG-2637: migration consequence - fix unit test
[controller.git] / opendaylight / md-sal / compatibility / sal-compatibility / src / main / java / org / opendaylight / controller / sal / compatibility / ToSalConversionsUtils.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.sal.compatibility;
9
10 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.CRUDP;
11 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.ETHERNET_ARP;
12 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.TCP;
13 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.UDP;
14 import static org.opendaylight.controller.sal.match.MatchType.DL_DST;
15 import static org.opendaylight.controller.sal.match.MatchType.DL_SRC;
16 import static org.opendaylight.controller.sal.match.MatchType.DL_TYPE;
17 import static org.opendaylight.controller.sal.match.MatchType.DL_VLAN;
18 import static org.opendaylight.controller.sal.match.MatchType.DL_VLAN_PR;
19 import static org.opendaylight.controller.sal.match.MatchType.NW_DST;
20 import static org.opendaylight.controller.sal.match.MatchType.NW_PROTO;
21 import static org.opendaylight.controller.sal.match.MatchType.NW_SRC;
22 import static org.opendaylight.controller.sal.match.MatchType.NW_TOS;
23 import static org.opendaylight.controller.sal.match.MatchType.TP_DST;
24 import static org.opendaylight.controller.sal.match.MatchType.TP_SRC;
25
26 import java.net.InetAddress;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30
31 import org.opendaylight.controller.sal.action.Controller;
32 import org.opendaylight.controller.sal.action.Drop;
33 import org.opendaylight.controller.sal.action.Flood;
34 import org.opendaylight.controller.sal.action.FloodAll;
35 import org.opendaylight.controller.sal.action.HwPath;
36 import org.opendaylight.controller.sal.action.Loopback;
37 import org.opendaylight.controller.sal.action.Output;
38 import org.opendaylight.controller.sal.action.PopVlan;
39 import org.opendaylight.controller.sal.action.PushVlan;
40 import org.opendaylight.controller.sal.action.SetDlDst;
41 import org.opendaylight.controller.sal.action.SetDlSrc;
42 import org.opendaylight.controller.sal.action.SetDlType;
43 import org.opendaylight.controller.sal.action.SetNextHop;
44 import org.opendaylight.controller.sal.action.SetNwDst;
45 import org.opendaylight.controller.sal.action.SetNwSrc;
46 import org.opendaylight.controller.sal.action.SetNwTos;
47 import org.opendaylight.controller.sal.action.SetTpDst;
48 import org.opendaylight.controller.sal.action.SetTpSrc;
49 import org.opendaylight.controller.sal.action.SetVlanCfi;
50 import org.opendaylight.controller.sal.action.SetVlanId;
51 import org.opendaylight.controller.sal.action.SetVlanPcp;
52 import org.opendaylight.controller.sal.action.SwPath;
53 import org.opendaylight.controller.sal.core.ConstructionException;
54 import org.opendaylight.controller.sal.core.Node;
55 import org.opendaylight.controller.sal.core.Node.NodeIDType;
56 import org.opendaylight.controller.sal.core.NodeConnector;
57 import org.opendaylight.controller.sal.flowprogrammer.Flow;
58 import org.opendaylight.controller.sal.match.Match;
59 import org.opendaylight.controller.sal.match.MatchType;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
61 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
62 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
63 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
64 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
65 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.VlanCfi;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.ControllerActionCase;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCase;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.FloodActionCase;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.FloodAllActionCase;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.HwPathActionCase;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.LoopbackActionCase;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopMplsActionCase;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCase;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushMplsActionCase;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushPbbActionCase;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCase;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlDstActionCase;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlSrcActionCase;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlTypeActionCase;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetMplsTtlActionCase;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNextHopActionCase;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwDstActionCase;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionCase;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwTosActionCase;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwTtlActionCase;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetQueueActionCase;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpDstActionCase;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpSrcActionCase;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanCfiActionCase;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdActionCase;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanPcpActionCase;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SwPathActionCase;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.Address;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv6;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.GenericFlowAttributes;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.MacAddressFilter;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddress;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddress;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetType;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatch;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatch;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanId;
122 import org.slf4j.Logger;
123 import org.slf4j.LoggerFactory;
124
125 import com.google.common.net.InetAddresses;
126
127 public class ToSalConversionsUtils {
128
129     private static final Logger LOG = LoggerFactory.getLogger(ToSalConversionsUtils.class);
130
131     private ToSalConversionsUtils() {
132         throw new IllegalAccessError("forcing no instance for factory");
133     }
134
135     public static Flow toFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow source, Node node) {
136         final Flow target = new Flow();
137         genericFlowToAdFlow(source, target);
138
139         target.setMatch(toMatch(source.getMatch()));
140
141         List<Action> actions = getAction(source);
142         if (actions != null) {
143             target.setActions(actionFrom(actions, node));
144         }
145
146         return target;
147     }
148
149     /**
150      * @param source notification, missing instructions
151      * @param node corresponding node where the flow change occured
152      * @return ad-sal node, build from given data
153      */
154     public static Flow toFlow(SwitchFlowRemoved source, Node node) {
155         final Flow target = new Flow();
156         genericFlowToAdFlow(source, target);
157
158         target.setMatch(toMatch(source.getMatch()));
159
160         return target;
161     }
162
163     /**
164      * @param source
165      * @param target
166      */
167     private static void genericFlowToAdFlow(GenericFlowAttributes source,
168             final Flow target) {
169         Integer hardTimeout = source.getHardTimeout();
170         if (hardTimeout != null) {
171             target.setHardTimeout(hardTimeout.shortValue());
172         }
173
174         Integer idleTimeout = source.getIdleTimeout();
175         if (idleTimeout != null) {
176             target.setIdleTimeout(idleTimeout.shortValue());
177         }
178
179         Integer priority = source.getPriority();
180         if (priority != null) {
181             target.setPriority(priority.shortValue());
182         }
183         target.setId(source.getCookie().getValue().longValue());
184     }
185
186     public static List<Action> getAction(
187             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow source) {
188         if (source.getInstructions() != null) {
189             for (Instruction instruction : source.getInstructions().getInstruction()) {
190                 if (instruction.getInstruction() instanceof ApplyActionsCase) {
191                     return (((ApplyActionsCase) instruction.getInstruction()).getApplyActions().getAction());
192                 }
193             }
194         }
195         // TODO Auto-generated method stub
196         return Collections.emptyList();
197     }
198
199     public static List<org.opendaylight.controller.sal.action.Action> actionFrom(List<Action> actions, Node node) {
200         List<org.opendaylight.controller.sal.action.Action> targetAction = new ArrayList<>();
201         for (Action action : actions) {
202             org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action sourceAction = action
203                     .getAction();
204
205             if (sourceAction instanceof ControllerActionCase) {
206                 targetAction.add(new Controller());
207             } else if (sourceAction instanceof OutputActionCase) {
208
209                 Uri nodeConnector = ((OutputActionCase) sourceAction).getOutputAction().getOutputNodeConnector();
210                 if (nodeConnector != null) {
211                     // TODO: We should really have a bi-directional map from AD-SAL node types to
212                     //       MD-SAL node types, but lets fix that later.
213                     String type = node.getType();
214                     if( type.equals(NodeIDType.OPENFLOW) ){
215                         type = NodeMapping.OPENFLOW_ID_PREFIX;
216                     }else{
217                         type = type + ":";
218                     }
219                     Uri fullNodeConnector = new Uri(type+node.getID()+":"+nodeConnector.getValue());
220                         targetAction.add(new Output(fromNodeConnectorRef(fullNodeConnector, node)));
221                 }
222             } else if (sourceAction instanceof PopMplsActionCase) {
223                 // TODO: define maping
224             } else if (sourceAction instanceof PushMplsActionCase) {
225                 // TODO: define maping
226             } else if (sourceAction instanceof PushPbbActionCase) {
227                 // TODO: define maping
228             } else if (sourceAction instanceof SetMplsTtlActionCase) {
229                 // TODO: define maping
230                 // targetAction = //no action to map
231             } else if (sourceAction instanceof SetNwTtlActionCase) {
232                 // TODO: define maping
233             } else if (sourceAction instanceof SetQueueActionCase) {
234                 // TODO: define maping
235                 // targetAction = //no action to map
236             } else if (sourceAction instanceof DropActionCase) {
237                 targetAction.add(new Drop());
238             } else if (sourceAction instanceof FloodActionCase) {
239                 targetAction.add(new Flood());
240             } else if (sourceAction instanceof FloodAllActionCase) {
241                 targetAction.add(new FloodAll());
242             } else if (sourceAction instanceof HwPathActionCase) {
243                 targetAction.add(new HwPath());
244             } else if (sourceAction instanceof LoopbackActionCase) {
245                 targetAction.add(new Loopback());
246             } else if (sourceAction instanceof PopVlanActionCase) {
247                 targetAction.add(new PopVlan());
248             } else if (sourceAction instanceof PushVlanActionCase) {
249                 PushVlanActionCase pushVlanAction = (PushVlanActionCase) sourceAction;
250                 PushVlan pushVlan = pushVlanFrom(pushVlanAction.getPushVlanAction());
251                 if (pushVlan != null) {
252                     targetAction.add(pushVlan);
253                 }
254             } else if (sourceAction instanceof SetDlDstActionCase) {
255                 MacAddress addressL2Dest = ((SetDlDstActionCase) sourceAction).getSetDlDstAction().getAddress();
256                 if (addressL2Dest != null) {
257                     targetAction.add(new SetDlDst(bytesFrom(addressL2Dest)));
258                 }
259             } else if (sourceAction instanceof SetDlSrcActionCase) {
260                 MacAddress addressL2Src = ((SetDlSrcActionCase) sourceAction).getSetDlSrcAction().getAddress();
261                 if (addressL2Src != null) {
262                     targetAction.add(new SetDlSrc(bytesFrom(addressL2Src)));
263
264                 }
265             } else if (sourceAction instanceof SetDlTypeActionCase) {
266                 EtherType dlType = ((SetDlTypeActionCase) sourceAction).getSetDlTypeAction().getDlType();
267                 if (dlType != null) {
268                     Long dlTypeValue = dlType.getValue();
269                     if (dlTypeValue != null) {
270                         targetAction.add(new SetDlType(dlTypeValue.intValue()));
271                     }
272                 }
273             } else if (sourceAction instanceof SetNextHopActionCase) {
274                 Address addressL3 = ((SetNextHopActionCase) sourceAction).getSetNextHopAction().getAddress();
275
276                 InetAddress inetAddress = inetAddressFrom(addressL3);
277                 if (inetAddress != null) {
278                     targetAction.add(new SetNextHop(inetAddress));
279                 }
280             } else if (sourceAction instanceof SetNwDstActionCase) {
281                 Address addressL3 = ((SetNwDstActionCase) sourceAction).getSetNwDstAction().getAddress();
282
283                 InetAddress inetAddress = inetAddressFrom(addressL3);
284                 if (inetAddress != null) {
285                     targetAction.add(new SetNwDst(inetAddress));
286                 }
287             } else if (sourceAction instanceof SetNwSrcActionCase) {
288                 Address addressL3 = ((SetNwSrcActionCase) sourceAction).getSetNwSrcAction().getAddress();
289
290                 InetAddress inetAddress = inetAddressFrom(addressL3);
291                 if (inetAddress != null) {
292                     targetAction.add(new SetNwSrc(inetAddress));
293                 }
294             } else if (sourceAction instanceof SetNwTosActionCase) {
295                 Integer tos = ((SetNwTosActionCase) sourceAction).getSetNwTosAction().getTos();
296                 if (tos != null) {
297                     targetAction.add(new SetNwTos(ToSalConversionsUtils.tosToNwDscp(tos)));
298                 }
299             } else if (sourceAction instanceof SetTpDstActionCase) {
300                 PortNumber port = ((SetTpDstActionCase) sourceAction).getSetTpDstAction().getPort();
301                 if (port != null) {
302                     Integer portValue = port.getValue();
303                     if (port.getValue() != null) {
304                         targetAction.add(new SetTpDst(portValue));
305                     }
306                 }
307             } else if (sourceAction instanceof SetTpSrcActionCase) {
308                 PortNumber port = ((SetTpSrcActionCase) sourceAction).getSetTpSrcAction().getPort();
309                 if (port != null) {
310                     Integer portValue = port.getValue();
311                     if (port.getValue() != null) {
312                         targetAction.add(new SetTpSrc(portValue));
313                     }
314                 }
315             } else if (sourceAction instanceof SetVlanCfiActionCase) {
316                 VlanCfi vlanCfi = ((SetVlanCfiActionCase) sourceAction).getSetVlanCfiAction().getVlanCfi();
317                 if (vlanCfi != null) {
318                     Integer vlanCfiValue = vlanCfi.getValue();
319                     if (vlanCfiValue != null) {
320                         targetAction.add(new SetVlanCfi(vlanCfiValue));
321                     }
322                 }
323             } else if (sourceAction instanceof SetVlanIdActionCase) {
324                 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanID = ((SetVlanIdActionCase) sourceAction).getSetVlanIdAction()
325                         .getVlanId();
326                 if (vlanID != null) {
327                     Integer vlanIdValue = vlanID.getValue();
328                     if (vlanIdValue != null) {
329                         targetAction.add(new SetVlanId(vlanIdValue));
330                     }
331                 }
332             } else if (sourceAction instanceof SetVlanPcpActionCase) {
333                 VlanPcp vlanPcp = ((SetVlanPcpActionCase) sourceAction).getSetVlanPcpAction().getVlanPcp();
334                 if (vlanPcp != null) {
335                     Short vlanPcpValue = vlanPcp.getValue();
336                     if (vlanPcpValue != null) {
337                         targetAction.add(new SetVlanPcp(vlanPcpValue));
338                     }
339                 }
340             } else if (sourceAction instanceof SwPathActionCase) {
341                 targetAction.add(new SwPath());
342             }
343         }
344
345         return targetAction;
346     }
347
348     private static InetAddress inetAddressFrom(Address addressL3) {
349         if (addressL3 != null) {
350             if (addressL3 instanceof Ipv4) {
351                 Ipv4Prefix addressL3Ipv4 = ((Ipv4) addressL3).getIpv4Address();
352                 if (addressL3Ipv4 != null) {
353                     return inetAddressFrom(addressL3Ipv4);
354                 }
355             } else if (addressL3 instanceof Ipv6) {
356                 Ipv6Prefix addressL3Ipv6 = ((Ipv6) addressL3).getIpv6Address();
357                 if (addressL3Ipv6 != null) {
358                     return inetAddressFrom(addressL3Ipv6);
359                 }
360             }
361         }
362         return null;
363     }
364
365     private static PushVlan pushVlanFrom(org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.vlan.action._case.PushVlanAction pushVlanAction) {
366         Integer tag = pushVlanAction.getTag();
367         if (tag != null) {
368             return new PushVlan(tag.intValue());
369         }
370         return null;
371     }
372
373     /**
374      * @param openflow nodeConnector uri
375      * @param node
376      * @return assembled nodeConnector
377      */
378     public static NodeConnector fromNodeConnectorRef(Uri uri, Node node) {
379         NodeConnector nodeConnector = null;
380         try {
381             NodeConnectorId nodeConnectorId = new NodeConnectorId(uri.getValue());
382             nodeConnector = NodeMapping.toADNodeConnector(nodeConnectorId, node);
383         } catch (ConstructionException e) {
384             LOG.warn("nodeConnector creation failed at node: {} with nodeConnectorUri: {}",
385                     node, uri.getValue());
386         }
387         return nodeConnector;
388     }
389
390     public static Match toMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match source) {
391         Match target = new Match();
392         if (source != null) {
393             fillFrom(target, source.getVlanMatch());
394             fillFrom(target, source.getEthernetMatch());
395             fillFrom(target, source.getLayer3Match());
396             fillFrom(target, source.getLayer4Match());
397             fillFrom(target, source.getIpMatch());
398             fillFrom(target, source.getInPort());
399         }
400
401         return target;
402     }
403
404     /**
405      * @param target
406      * @param inPort
407      */
408     private static void fillFrom(Match target, NodeConnectorId inPort) {
409         if (inPort != null) {
410             String inPortValue = inPort.getValue();
411             if (inPortValue != null) {
412                 try {
413                     target.setField(MatchType.IN_PORT, NodeMapping.toADNodeConnector(inPort,
414                             NodeMapping.toAdNodeId(inPort)));
415                 } catch (ConstructionException e) {
416                     LOG.warn("nodeConnector construction failed", e);
417                 }
418             }
419         }
420     }
421
422     private static void fillFrom(Match target, VlanMatch vlanMatch) {
423         if (vlanMatch != null) {
424             VlanId vlanId = vlanMatch.getVlanId();
425             if (vlanId != null) {
426                 if (Boolean.TRUE.equals(vlanId.isVlanIdPresent())) {
427                     org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanIdInner = vlanId
428                             .getVlanId();
429                     if (vlanIdInner != null) {
430                         Integer vlanValue = vlanIdInner.getValue();
431                         if (vlanValue != null) {
432                             target.setField(DL_VLAN, vlanValue.shortValue());
433                         }
434                     }
435                 } else {
436                     target.setField(DL_VLAN, MatchType.DL_VLAN_NONE);
437                 }
438             }
439             VlanPcp vlanPcp = vlanMatch.getVlanPcp();
440             if (vlanPcp != null) {
441                 Short vlanPcpValue = vlanPcp.getValue();
442                 if (vlanPcpValue != null) {
443                     target.setField(DL_VLAN_PR, vlanPcpValue.byteValue());
444                 }
445             }
446         }
447     }
448
449     private static void fillFrom(Match target, IpMatch ipMatch) {
450         if (ipMatch != null) {
451             Short ipProtocol = ipMatch.getIpProtocol();
452
453             if (ipProtocol != null && target.getField(NW_PROTO) == null) {
454                 target.setField(NW_PROTO, ipProtocol.byteValue());
455             }
456             Dscp dscp = ipMatch.getIpDscp();
457             if (dscp != null) {
458                 Short dscpValue = dscp.getValue();
459                 if (dscpValue != null) {
460                     target.setField(NW_TOS, dscpValue.byteValue());
461                 }
462             }
463         }
464     }
465
466     private static void fillFrom(Match target, Layer4Match layer4Match) {
467         if (layer4Match == null) {
468             return;
469         }
470         if (layer4Match instanceof SctpMatch) {
471             fillTransportLayer(target, (SctpMatch) layer4Match);
472         } else if (layer4Match instanceof TcpMatch) {
473             fillTransportLayer(target, (TcpMatch) layer4Match);
474         } else if (layer4Match instanceof UdpMatch) {
475             fillTransportLayer(target, (UdpMatch) layer4Match);
476         }
477     }
478
479     private static void fillTransportLayer(Match target, UdpMatch source) {
480         PortNumber udpSourcePort = source.getUdpSourcePort();
481         if (udpSourcePort != null) {
482             Integer udpSourcePortValue = udpSourcePort.getValue();
483             if (udpSourcePortValue != null) {
484                 target.setField(TP_SRC, udpSourcePortValue.shortValue());
485             }
486         }
487
488         PortNumber udpDestPort = source.getUdpDestinationPort();
489         if (udpDestPort != null) {
490             Integer udpDestPortValue = udpDestPort.getValue();
491             if (udpDestPortValue != null) {
492                 target.setField(TP_DST, udpDestPortValue.shortValue());
493             }
494         }
495
496         target.setField(NW_PROTO, UDP);
497     }
498
499     private static void fillTransportLayer(Match target, TcpMatch source) {
500         PortNumber tcpSourcePort = source.getTcpSourcePort();
501         if (tcpSourcePort != null) {
502             Integer tcpSourcePortValue = tcpSourcePort.getValue();
503             if (tcpSourcePortValue != null) {
504                 target.setField(TP_SRC, tcpSourcePortValue.shortValue());
505             }
506         }
507
508         PortNumber tcpDestPort = source.getTcpDestinationPort();
509         if (tcpDestPort != null) {
510             Integer tcpDestPortValue = tcpDestPort.getValue();
511             if (tcpDestPortValue != null) {
512                 target.setField(TP_DST, tcpDestPortValue.shortValue());
513             }
514         }
515
516         target.setField(NW_PROTO, TCP);
517     }
518
519     private static void fillTransportLayer(Match target, SctpMatch source) {
520         PortNumber sctpSourcePort = source.getSctpSourcePort();
521         if (sctpSourcePort != null) {
522             Integer sctpSourcePortValue = sctpSourcePort.getValue();
523             if (sctpSourcePortValue != null) {
524                 target.setField(TP_SRC, sctpSourcePortValue.shortValue());
525             }
526         }
527         PortNumber sctpDestPort = source.getSctpDestinationPort();
528         if (sctpDestPort != null) {
529             Integer sctpDestPortValue = sctpDestPort.getValue();
530             if (sctpDestPortValue != null) {
531                 target.setField(TP_DST, sctpDestPortValue.shortValue());
532             }
533         }
534
535         target.setField(NW_PROTO, CRUDP);
536
537     }
538
539     private static void fillFrom(Match target, Layer3Match source) {
540         if (source == null)
541             return;
542         if (source instanceof Ipv4Match) {
543             fillFromIpv4(target, (Ipv4Match) source);
544         } else if (source instanceof Ipv6Match) {
545             fillFromIpv6(target, (Ipv6Match) source);
546         } else if (source instanceof ArpMatch) {
547             fillFromArp(target, (ArpMatch) source);
548         }
549     }
550
551     private static void fillFromArp(Match target, ArpMatch source) {
552         Ipv4Prefix sourceAddress = source.getArpSourceTransportAddress();
553         if (sourceAddress != null) {
554             target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
555         }
556         Ipv4Prefix destAddress = source.getArpTargetTransportAddress();
557         if (destAddress != null) {
558             target.setField(NW_DST, inetAddressFrom(destAddress), null);
559         }
560         ArpSourceHardwareAddress sourceHwAddress = source.getArpSourceHardwareAddress();
561         if (sourceHwAddress != null) {
562             target.setField(DL_SRC, bytesFrom(sourceHwAddress.getAddress()));
563         }
564         ArpTargetHardwareAddress targetHwAddress = source.getArpTargetHardwareAddress();
565         if (targetHwAddress != null) {
566             target.setField(DL_DST, bytesFrom(targetHwAddress.getAddress()));
567         }
568
569         target.setField(DL_TYPE, new Short(ETHERNET_ARP));
570
571     }
572
573     private static void fillFromIpv6(Match target, Ipv6Match source) {
574         Ipv6Prefix sourceAddress = source.getIpv6Source();
575         if (sourceAddress != null) {
576             target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
577         }
578         Ipv6Prefix destAddress = source.getIpv6Destination();
579         if (destAddress != null) {
580             target.setField(NW_DST, inetAddressFrom(destAddress), null);
581         }
582     }
583
584     private static void fillFromIpv4(Match target, Ipv4Match source) {
585         Ipv4Prefix sourceAddress = source.getIpv4Source();
586         if (sourceAddress != null) {
587             target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
588         }
589         Ipv4Prefix destAddress = source.getIpv4Destination();
590         if (destAddress != null) {
591             target.setField(NW_DST, inetAddressFrom(destAddress), null);
592         }
593     }
594
595     private static InetAddress inetAddressFrom(Ipv4Prefix source) {
596         if (source != null) {
597             String[] parts = source.getValue().split("/");
598             return InetAddresses.forString(parts[0]);
599         }
600         return null;
601     }
602
603     private static InetAddress inetAddressFrom(Ipv6Prefix source) {
604         if (source != null) {
605             String[] parts = source.getValue().split("/");
606             return InetAddresses.forString(parts[0]);
607         }
608         return null;
609     }
610
611     private static void fillFrom(Match target, EthernetMatch source) {
612         if (source == null)
613             return;
614         EthernetType ethType = source.getEthernetType();
615         if (ethType != null) {
616             EtherType ethInnerType = ethType.getType();
617             if (ethInnerType != null && target.getField(DL_TYPE) == null) {
618                 Long value = ethInnerType.getValue();
619                 target.setField(DL_TYPE, value.shortValue());
620             }
621         }
622
623         MacAddressFilter ethSource = source.getEthernetSource();
624         if (ethSource != null) {
625             target.setField(DL_SRC, bytesFrom(ethSource.getAddress()));
626         }
627
628         MacAddressFilter ethDest = source.getEthernetDestination();
629         if (ethDest != null) {
630             target.setField(DL_DST, bytesFrom(ethDest.getAddress()));
631         }
632     }
633
634     public static byte[] bytesFrom(MacAddress address) {
635         String[] mac = address.getValue().split(":");
636         byte[] macAddress = new byte[6]; // mac.length == 6 bytes
637         for (int i = 0; i < mac.length; i++) {
638             macAddress[i] = Integer.decode("0x" + mac[i]).byteValue();
639         }
640         return macAddress;
641     }
642
643     public static byte[] bytesFromDpid(long dpid) {
644         byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
645
646         for (short i = 0; i < 6; i++) {
647             mac[5 - i] = (byte) dpid;
648             dpid >>= 8;
649         }
650
651         return mac;
652     }
653
654     /**
655      * @param nwTos NW-TOS
656      * @return shifted to NW-DSCP
657      */
658     public static int tosToNwDscp(int nwTos) {
659         return (short) (nwTos >>> FromSalConversionsUtils.ENC_FIELD_BIT_SIZE);
660     }
661 }