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