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