BUG-1234 nodeType/ID in adaptors
[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.core.NodeConnector.NodeConnectorIDType;
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
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                     //for (Uri uri : nodeConnectors) {
212                         targetAction.add(new Output(fromNodeConnectorRef(nodeConnector, 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             nodeConnector = new NodeConnector(NodeConnectorIDType.OPENFLOW,
389                     Short.valueOf(uri.getValue()), node);
390         } catch (ConstructionException e) {
391             e.printStackTrace();
392         }
393         return nodeConnector;
394     }
395
396     public static Match toMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match source) {
397         Match target = new Match();
398         if (source != null) {
399             fillFrom(target, source.getVlanMatch());
400             fillFrom(target, source.getEthernetMatch());
401             fillFrom(target, source.getLayer3Match());
402             fillFrom(target, source.getLayer4Match());
403             fillFrom(target, source.getIpMatch());
404             fillFrom(target, source.getInPort());
405         }
406
407         return target;
408     }
409
410     /**
411      * @param target
412      * @param inPort
413      */
414     private static void fillFrom(Match target, NodeConnectorId inPort) {
415         if (inPort != null) {
416             String inPortValue = inPort.getValue();
417             if (inPortValue != null) {
418                 try {
419                     target.setField(MatchType.IN_PORT, NodeMapping.toADNodeConnector(inPort,
420                             NodeMapping.toAdNodeId(inPort)));
421                 } catch (ConstructionException e) {
422                     LOG.warn("nodeConnector construction failed", e);
423                 }
424             }
425         }
426     }
427
428     private static void fillFrom(Match target, VlanMatch vlanMatch) {
429         if (vlanMatch != null) {
430             VlanId vlanId = vlanMatch.getVlanId();
431             if (vlanId != null) {
432                 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanIdInner = vlanId
433                         .getVlanId();
434                 if (vlanIdInner != null) {
435                     Integer vlanValue = vlanIdInner.getValue();
436                     if (vlanValue != null) {
437                         target.setField(DL_VLAN, vlanValue.shortValue());
438                     }
439                 }
440             }
441             VlanPcp vlanPcp = vlanMatch.getVlanPcp();
442             if (vlanPcp != null) {
443                 Short vlanPcpValue = vlanPcp.getValue();
444                 if (vlanPcpValue != null) {
445                     target.setField(DL_VLAN_PR, vlanPcpValue.byteValue());
446                 }
447             }
448         }
449     }
450
451     private static void fillFrom(Match target, IpMatch ipMatch) {
452         if (ipMatch != null) {
453             Short ipProtocol = ipMatch.getIpProtocol();
454
455             if (ipProtocol != null && target.getField(NW_PROTO) == null) {
456                 target.setField(NW_PROTO, ipProtocol.byteValue());
457             }
458             Dscp dscp = ipMatch.getIpDscp();
459             if (dscp != null) {
460                 Short dscpValue = dscp.getValue();
461                 if (dscpValue != null) {
462                     target.setField(NW_TOS, dscpValue.byteValue());
463                 }
464             }
465         }
466     }
467
468     private static void fillFrom(Match target, Layer4Match layer4Match) {
469         if (layer4Match == null) {
470             return;
471         }
472         if (layer4Match instanceof SctpMatch) {
473             fillTransportLayer(target, (SctpMatch) layer4Match);
474         } else if (layer4Match instanceof TcpMatch) {
475             fillTransportLayer(target, (TcpMatch) layer4Match);
476         } else if (layer4Match instanceof UdpMatch) {
477             fillTransportLayer(target, (UdpMatch) layer4Match);
478         }
479     }
480
481     private static void fillTransportLayer(Match target, UdpMatch source) {
482         PortNumber udpSourcePort = source.getUdpSourcePort();
483         if (udpSourcePort != null) {
484             Integer udpSourcePortValue = udpSourcePort.getValue();
485             if (udpSourcePortValue != null) {
486                 target.setField(TP_SRC, udpSourcePortValue.shortValue());
487             }
488         }
489
490         PortNumber udpDestPort = source.getUdpDestinationPort();
491         if (udpDestPort != null) {
492             Integer udpDestPortValue = udpDestPort.getValue();
493             if (udpDestPortValue != null) {
494                 target.setField(TP_DST, udpDestPortValue.shortValue());
495             }
496         }
497
498         target.setField(NW_PROTO, UDP);
499     }
500
501     private static void fillTransportLayer(Match target, TcpMatch source) {
502         PortNumber tcpSourcePort = source.getTcpSourcePort();
503         if (tcpSourcePort != null) {
504             Integer tcpSourcePortValue = tcpSourcePort.getValue();
505             if (tcpSourcePortValue != null) {
506                 target.setField(TP_SRC, tcpSourcePortValue.shortValue());
507             }
508         }
509
510         PortNumber tcpDestPort = source.getTcpDestinationPort();
511         if (tcpDestPort != null) {
512             Integer tcpDestPortValue = tcpDestPort.getValue();
513             if (tcpDestPortValue != null) {
514                 target.setField(TP_DST, tcpDestPortValue.shortValue());
515             }
516         }
517
518         target.setField(NW_PROTO, TCP);
519     }
520
521     private static void fillTransportLayer(Match target, SctpMatch source) {
522         PortNumber sctpSourcePort = source.getSctpSourcePort();
523         if (sctpSourcePort != null) {
524             Integer sctpSourcePortValue = sctpSourcePort.getValue();
525             if (sctpSourcePortValue != null) {
526                 target.setField(TP_SRC, sctpSourcePortValue.shortValue());
527             }
528         }
529         PortNumber sctpDestPort = source.getSctpDestinationPort();
530         if (sctpDestPort != null) {
531             Integer sctpDestPortValue = sctpDestPort.getValue();
532             if (sctpDestPortValue != null) {
533                 target.setField(TP_DST, sctpDestPortValue.shortValue());
534             }
535         }
536
537         target.setField(NW_PROTO, CRUDP);
538
539     }
540
541     private static void fillFrom(Match target, Layer3Match source) {
542         if (source == null)
543             return;
544         if (source instanceof Ipv4Match) {
545             fillFromIpv4(target, (Ipv4Match) source);
546         } else if (source instanceof Ipv6Match) {
547             fillFromIpv6(target, (Ipv6Match) source);
548         } else if (source instanceof ArpMatch) {
549             fillFromArp(target, (ArpMatch) source);
550         }
551     }
552
553     private static void fillFromArp(Match target, ArpMatch source) {
554         Ipv4Prefix sourceAddress = source.getArpSourceTransportAddress();
555         if (sourceAddress != null) {
556             target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
557         }
558         Ipv4Prefix destAddress = source.getArpTargetTransportAddress();
559         if (destAddress != null) {
560             target.setField(NW_DST, inetAddressFrom(destAddress), null);
561         }
562         ArpSourceHardwareAddress sourceHwAddress = source.getArpSourceHardwareAddress();
563         if (sourceHwAddress != null) {
564             target.setField(DL_SRC, bytesFrom(sourceHwAddress.getAddress()));
565         }
566         ArpTargetHardwareAddress targetHwAddress = source.getArpTargetHardwareAddress();
567         if (targetHwAddress != null) {
568             target.setField(DL_DST, bytesFrom(targetHwAddress.getAddress()));
569         }
570
571         target.setField(DL_TYPE, new Short(ETHERNET_ARP));
572
573     }
574
575     private static void fillFromIpv6(Match target, Ipv6Match source) {
576         Ipv6Prefix sourceAddress = source.getIpv6Source();
577         if (sourceAddress != null) {
578             target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
579         }
580         Ipv6Prefix destAddress = source.getIpv6Destination();
581         if (destAddress != null) {
582             target.setField(NW_DST, inetAddressFrom(destAddress), null);
583         }
584     }
585
586     private static void fillFromIpv4(Match target, Ipv4Match source) {
587         Ipv4Prefix sourceAddress = source.getIpv4Source();
588         if (sourceAddress != null) {
589             target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
590         }
591         Ipv4Prefix destAddress = source.getIpv4Destination();
592         if (destAddress != null) {
593             target.setField(NW_DST, inetAddressFrom(destAddress), null);
594         }
595     }
596
597     private static InetAddress inetAddressFrom(Ipv4Prefix source) {
598         if (source != null) {
599             String[] parts = source.getValue().split("/");
600             return InetAddresses.forString(parts[0]);
601         }
602         return null;
603     }
604
605     private static InetAddress inetAddressFrom(Ipv6Prefix source) {
606         if (source != null) {
607             String[] parts = source.getValue().split("/");
608             return InetAddresses.forString(parts[0]);
609         }
610         return null;
611     }
612
613     private static void fillFrom(Match target, EthernetMatch source) {
614         if (source == null)
615             return;
616         EthernetType ethType = source.getEthernetType();
617         if (ethType != null) {
618             EtherType ethInnerType = ethType.getType();
619             if (ethInnerType != null && target.getField(DL_TYPE) == null) {
620                 Long value = ethInnerType.getValue();
621                 target.setField(DL_TYPE, value.shortValue());
622             }
623         }
624
625         MacAddressFilter ethSource = source.getEthernetSource();
626         if (ethSource != null) {
627             target.setField(DL_SRC, bytesFrom(ethSource.getAddress()));
628         }
629
630         MacAddressFilter ethDest = source.getEthernetDestination();
631         if (ethDest != null) {
632             target.setField(DL_DST, bytesFrom(ethDest.getAddress()));
633         }
634     }
635
636     public static byte[] bytesFrom(MacAddress address) {
637         String[] mac = address.getValue().split(":");
638         byte[] macAddress = new byte[6]; // mac.length == 6 bytes
639         for (int i = 0; i < mac.length; i++) {
640             macAddress[i] = Integer.decode("0x" + mac[i]).byteValue();
641         }
642         return macAddress;
643     }
644
645     public static byte[] bytesFromDpid(long dpid) {
646         byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
647
648         for (short i = 0; i < 6; i++) {
649             mac[5 - i] = (byte) dpid;
650             dpid >>= 8;
651         }
652
653         return mac;
654     }
655 }