Merge "Add test for generated code checking list of dependencies."
[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.Capabilities;
47 import org.opendaylight.controller.sal.core.NodeConnector;
48 import org.opendaylight.controller.sal.flowprogrammer.Flow;
49 import org.opendaylight.controller.sal.match.Match;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.VlanCfi;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.ControllerActionCase;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCase;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.FloodActionCase;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.FloodAllActionCase;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.HwPathActionCase;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.LoopbackActionCase;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopMplsActionCase;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCase;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushMplsActionCase;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushPbbActionCase;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCase;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlDstActionCase;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlSrcActionCase;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlTypeActionCase;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetMplsTtlActionCase;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNextHopActionCase;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwDstActionCase;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionCase;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwTosActionCase;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwTtlActionCase;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetQueueActionCase;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpDstActionCase;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpSrcActionCase;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanCfiActionCase;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdActionCase;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanPcpActionCase;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SwPathActionCase;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.Address;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv6;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FeatureCapability;
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) {
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));
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) {
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)));
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) {
343         // TODO: Define mapping
344         return null;
345     }
346
347     public static Match toMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match source) {
348         Match target = new Match();
349         if (source != null) {
350             fillFrom(target, source.getVlanMatch());
351             fillFrom(target, source.getEthernetMatch());
352             fillFrom(target, source.getLayer3Match());
353             fillFrom(target, source.getLayer4Match());
354             fillFrom(target, source.getIpMatch());
355         }
356
357         return target;
358     }
359
360     private static void fillFrom(Match target, VlanMatch vlanMatch) {
361         if (vlanMatch != null) {
362             VlanId vlanId = vlanMatch.getVlanId();
363             if (vlanId != null) {
364                 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanIdInner = vlanId
365                         .getVlanId();
366                 if (vlanIdInner != null) {
367                     Integer vlanValue = vlanIdInner.getValue();
368                     if (vlanValue != null) {
369                         target.setField(DL_VLAN, vlanValue.shortValue());
370                     }
371                 }
372             }
373             VlanPcp vlanPcp = vlanMatch.getVlanPcp();
374             if (vlanPcp != null) {
375                 Short vlanPcpValue = vlanPcp.getValue();
376                 if (vlanPcpValue != null) {
377                     target.setField(DL_VLAN_PR, vlanPcpValue.byteValue());
378                 }
379             }
380         }
381     }
382
383     private static void fillFrom(Match target, IpMatch ipMatch) {
384         if (ipMatch != null) {
385             Short ipProtocol = ipMatch.getIpProtocol();
386
387             if (ipProtocol != null && target.getField(NW_PROTO) == null) {
388                 target.setField(NW_PROTO, ipProtocol.byteValue());
389             }
390             Dscp dscp = ipMatch.getIpDscp();
391             if (dscp != null) {
392                 Short dscpValue = dscp.getValue();
393                 if (dscpValue != null) {
394                     target.setField(NW_TOS, dscpValue.byteValue());
395                 }
396             }
397         }
398     }
399
400     private static void fillFrom(Match target, Layer4Match layer4Match) {
401         if (layer4Match == null) {
402             return;
403         }
404         if (layer4Match instanceof SctpMatch) {
405             fillTransportLayer(target, (SctpMatch) layer4Match);
406         } else if (layer4Match instanceof TcpMatch) {
407             fillTransportLayer(target, (TcpMatch) layer4Match);
408         } else if (layer4Match instanceof UdpMatch) {
409             fillTransportLayer(target, (UdpMatch) layer4Match);
410         }
411     }
412
413     private static void fillTransportLayer(Match target, UdpMatch source) {
414         PortNumber udpSourcePort = source.getUdpSourcePort();
415         if (udpSourcePort != null) {
416             Integer udpSourcePortValue = udpSourcePort.getValue();
417             if (udpSourcePortValue != null) {
418                 target.setField(TP_SRC, udpSourcePortValue.shortValue());
419             }
420         }
421
422         PortNumber udpDestPort = source.getUdpDestinationPort();
423         if (udpDestPort != null) {
424             Integer udpDestPortValue = udpDestPort.getValue();
425             if (udpDestPortValue != null) {
426                 target.setField(TP_DST, udpDestPortValue.shortValue());
427             }
428         }
429
430         target.setField(NW_PROTO, UDP);
431     }
432
433     private static void fillTransportLayer(Match target, TcpMatch source) {
434         PortNumber tcpSourcePort = source.getTcpSourcePort();
435         if (tcpSourcePort != null) {
436             Integer tcpSourcePortValue = tcpSourcePort.getValue();
437             if (tcpSourcePortValue != null) {
438                 target.setField(TP_SRC, tcpSourcePortValue.shortValue());
439             }
440         }
441
442         PortNumber tcpDestPort = source.getTcpDestinationPort();
443         if (tcpDestPort != null) {
444             Integer tcpDestPortValue = tcpDestPort.getValue();
445             if (tcpDestPortValue != null) {
446                 target.setField(TP_DST, tcpDestPortValue.shortValue());
447             }
448         }
449
450         target.setField(NW_PROTO, TCP);
451     }
452
453     private static void fillTransportLayer(Match target, SctpMatch source) {
454         PortNumber sctpSourcePort = source.getSctpSourcePort();
455         if (sctpSourcePort != null) {
456             Integer sctpSourcePortValue = sctpSourcePort.getValue();
457             if (sctpSourcePortValue != null) {
458                 target.setField(TP_SRC, sctpSourcePortValue.shortValue());
459             }
460         }
461         PortNumber sctpDestPort = source.getSctpDestinationPort();
462         if (sctpDestPort != null) {
463             Integer sctpDestPortValue = sctpDestPort.getValue();
464             if (sctpDestPortValue != null) {
465                 target.setField(TP_DST, sctpDestPortValue.shortValue());
466             }
467         }
468
469         target.setField(NW_PROTO, CRUDP);
470
471     }
472
473     private static void fillFrom(Match target, Layer3Match source) {
474         if (source == null)
475             return;
476         if (source instanceof Ipv4Match) {
477             fillFromIpv4(target, (Ipv4Match) source);
478         } else if (source instanceof Ipv6Match) {
479             fillFromIpv6(target, (Ipv6Match) source);
480         } else if (source instanceof ArpMatch) {
481             fillFromArp(target, (ArpMatch) source);
482         }
483     }
484
485     private static void fillFromArp(Match target, ArpMatch source) {
486         Ipv4Prefix sourceAddress = source.getArpSourceTransportAddress();
487         if (sourceAddress != null) {
488             target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
489         }
490         Ipv4Prefix destAddress = source.getArpTargetTransportAddress();
491         if (destAddress != null) {
492             target.setField(NW_DST, inetAddressFrom(destAddress), null);
493         }
494         ArpSourceHardwareAddress sourceHwAddress = source.getArpSourceHardwareAddress();
495         if (sourceHwAddress != null) {
496             target.setField(DL_SRC, bytesFrom(sourceHwAddress.getAddress()));
497         }
498         ArpTargetHardwareAddress targetHwAddress = source.getArpTargetHardwareAddress();
499         if (targetHwAddress != null) {
500             target.setField(DL_DST, bytesFrom(targetHwAddress.getAddress()));
501         }
502
503         target.setField(DL_TYPE, new Short(ETHERNET_ARP));
504
505     }
506
507     private static void fillFromIpv6(Match target, Ipv6Match source) {
508         Ipv6Prefix sourceAddress = source.getIpv6Source();
509         if (sourceAddress != null) {
510             target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
511         }
512         Ipv6Prefix destAddress = source.getIpv6Destination();
513         if (destAddress != null) {
514             target.setField(NW_DST, inetAddressFrom(destAddress), null);
515         }
516     }
517
518     private static void fillFromIpv4(Match target, Ipv4Match source) {
519         Ipv4Prefix sourceAddress = source.getIpv4Source();
520         if (sourceAddress != null) {
521             target.setField(NW_SRC, inetAddressFrom(sourceAddress), null);
522         }
523         Ipv4Prefix destAddress = source.getIpv4Destination();
524         if (destAddress != null) {
525             target.setField(NW_DST, inetAddressFrom(destAddress), null);
526         }
527     }
528
529     private static InetAddress inetAddressFrom(Ipv4Prefix source) {
530         if (source != null) {
531             String[] parts = source.getValue().split("/");
532             return InetAddresses.forString(parts[0]);
533         }
534         return null;
535     }
536
537     private static InetAddress inetAddressFrom(Ipv6Prefix source) {
538         if (source != null) {
539             String[] parts = source.getValue().split("/");
540             return InetAddresses.forString(parts[0]);
541         }
542         return null;
543     }
544
545     private static void fillFrom(Match target, EthernetMatch source) {
546         if (source == null)
547             return;
548         EthernetType ethType = source.getEthernetType();
549         if (ethType != null) {
550             EtherType ethInnerType = ethType.getType();
551             if (ethInnerType != null && target.getField(DL_TYPE) == null) {
552                 Long value = ethInnerType.getValue();
553                 target.setField(DL_TYPE, value.shortValue());
554             }
555         }
556
557         MacAddressFilter ethSource = source.getEthernetSource();
558         if (ethSource != null) {
559             target.setField(DL_SRC, bytesFrom(ethSource.getAddress()));
560         }
561
562         MacAddressFilter ethDest = source.getEthernetDestination();
563         if (ethDest != null) {
564             target.setField(DL_DST, bytesFrom(ethDest.getAddress()));
565         }
566     }
567
568     public static byte[] bytesFrom(MacAddress address) {
569         String[] mac = address.getValue().split(":");
570         byte[] macAddress = new byte[6]; // mac.length == 6 bytes
571         for (int i = 0; i < mac.length; i++) {
572             macAddress[i] = Integer.decode("0x" + mac[i]).byteValue();
573         }
574         return macAddress;
575     }
576     
577     public static byte[] bytesFromDpid(long dpid) {
578         byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
579
580         for (short i = 0; i < 6; i++) {
581             mac[5 - i] = (byte) dpid;
582             dpid >>= 8;
583         }
584
585         return mac;
586     }
587 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.