4ffa45adf34a13f27cb11e6d8e16ce48ac9336b5
[controller.git] / opendaylight / md-sal / sal-compability / src / main / java / org / opendaylight / controller / sal / compability / FromSalConversionsUtils.java
1 package org.opendaylight.controller.sal.compability;
2
3 import static org.opendaylight.controller.sal.match.MatchType.DL_DST;
4 import static org.opendaylight.controller.sal.match.MatchType.DL_SRC;
5 import static org.opendaylight.controller.sal.match.MatchType.DL_TYPE;
6
7 import java.math.BigInteger;
8 import java.net.Inet4Address;
9 import java.net.Inet6Address;
10 import java.net.InetAddress;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.List;
14
15 import org.opendaylight.controller.sal.action.*;
16 import org.opendaylight.controller.sal.core.NodeConnector;
17 import org.opendaylight.controller.sal.flowprogrammer.Flow;
18 import org.opendaylight.controller.sal.match.Match;
19 import org.opendaylight.controller.sal.match.MatchField;
20 import org.opendaylight.controller.sal.match.MatchType;
21 import org.opendaylight.controller.sal.utils.NetUtils;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.*;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAddedBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.VlanCfi;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.*;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.Address;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.address.Ipv4Builder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.address.Ipv6Builder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Action;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.ActionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.arp.match.fields.ArpSourceHardwareAddressBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.arp.match.fields.ArpTargetHardwareAddressBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetDestinationBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetSourceBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetTypeBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.*;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.ArpMatchBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv4MatchBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv6MatchBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.SctpMatchBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.TcpMatchBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.UdpMatchBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.vlan.match.fields.VlanIdBuilder;
49
50 import com.google.common.net.InetAddresses;
51
52 import static org.opendaylight.controller.sal.compability.ProtocolConstants.ETHERNET_ARP;
53 import static org.opendaylight.controller.sal.compability.ProtocolConstants.SCTP;
54 import static org.opendaylight.controller.sal.compability.ProtocolConstants.TCP;
55 import static org.opendaylight.controller.sal.compability.ProtocolConstants.UDP;
56
57 public class FromSalConversionsUtils {
58
59     private FromSalConversionsUtils() {
60
61     }
62
63     public static FlowAdded flowFrom(Flow sourceFlow) {
64         if (sourceFlow != null) {
65             final FlowAddedBuilder targetFlow = new FlowAddedBuilder();
66
67             targetFlow.setHardTimeout((int) sourceFlow.getHardTimeout());
68             targetFlow.setIdleTimeout((int) sourceFlow.getIdleTimeout());
69             targetFlow.setPriority((int) sourceFlow.getPriority());
70             targetFlow.setCookie(new BigInteger(String.valueOf(sourceFlow.getId())));
71
72             List<org.opendaylight.controller.sal.action.Action> sourceActions = sourceFlow.getActions();
73             List<Action> targetActions = new ArrayList<>();
74             for (org.opendaylight.controller.sal.action.Action sourceAction : sourceActions) {
75                 targetActions.add(actionFrom(sourceAction));
76             }
77             targetFlow.setAction(targetActions);
78
79             targetFlow.setMatch(matchFrom(sourceFlow.getMatch()));
80
81             return targetFlow.build();
82         }
83         return null;
84     }
85
86     private static Action actionFrom(org.opendaylight.controller.sal.action.Action sourceAction) {
87
88         ActionBuilder targetActionBuilder = new ActionBuilder();
89         org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.Action targetAction = null;
90
91         if (sourceAction instanceof Controller) {
92             targetAction = new ControllerActionBuilder().build();
93         } else if (sourceAction instanceof Drop) {
94             targetAction = new DropActionBuilder().build();
95         } else if (sourceAction instanceof Flood) {
96             targetAction = new FloodActionBuilder().build();
97         } else if (sourceAction instanceof FloodAll) {
98             targetAction = new FloodAllActionBuilder().build();
99         } else if (sourceAction instanceof HwPath) {
100             targetAction = new HwPathActionBuilder().build();
101         } else if (sourceAction instanceof Loopback) {
102             targetAction = new LoopbackActionBuilder().build();
103         } else if (sourceAction instanceof Output) {
104             NodeConnector nodeConnector = ((Output) sourceAction).getPort();
105
106             OutputActionBuilder outputActionBuilder = new OutputActionBuilder();
107             outputActionBuilder.setOutputNodeConnector(nodeConnectorToUri(nodeConnector));
108             targetAction = outputActionBuilder.build();
109
110         } else if (sourceAction instanceof PopVlan) {
111             targetAction = new PopVlanActionBuilder().build();
112         } else if (sourceAction instanceof PushVlan) {
113             PushVlan pushVlan = (PushVlan) sourceAction;
114             PushVlanActionBuilder pushVlanActionBuilder = new PushVlanActionBuilder();
115
116             pushVlanActionBuilder.setCfi(new VlanCfi(pushVlan.getCfi()));
117             pushVlanActionBuilder.setVlanId(new VlanId(pushVlan.getVlanId()));
118             pushVlanActionBuilder.setPcp(pushVlan.getPcp());
119             pushVlanActionBuilder.setTag(pushVlan.getTag());
120             targetAction = pushVlanActionBuilder.build();
121         } else if (sourceAction instanceof SetDlDst) {
122             SetDlDst setDlDst = (SetDlDst) sourceAction;
123             SetDlDstActionBuilder setDlDstActionBuilder = new SetDlDstActionBuilder();
124
125             setDlDstActionBuilder.setAddress(new MacAddress(new String(setDlDst.getDlAddress())));
126             targetAction = setDlDstActionBuilder.build();
127         } else if (sourceAction instanceof SetDlSrc) {
128             SetDlSrc setDlSrc = (SetDlSrc) sourceAction;
129             SetDlSrcActionBuilder setDlSrcActionBuilder = new SetDlSrcActionBuilder();
130
131             setDlSrcActionBuilder.setAddress(new MacAddress(new String(setDlSrc.getDlAddress())));
132             targetAction = setDlSrcActionBuilder.build();
133         } else if (sourceAction instanceof SetDlType) {
134             SetDlType setDlType = (SetDlType) sourceAction;
135             SetDlTypeActionBuilder setDlTypeActionBuilder = new SetDlTypeActionBuilder();
136
137             setDlTypeActionBuilder.setDlType(new EtherType(new Long(setDlType.getDlType())));
138             targetAction = setDlTypeActionBuilder.build();
139         } else if (sourceAction instanceof SetNextHop) {
140             SetNextHop setNextHop = (SetNextHop) sourceAction;
141             SetNextHopActionBuilder setNextHopActionBuilder = new SetNextHopActionBuilder();
142
143             InetAddress inetAddress = setNextHop.getAddress();
144             setNextHopActionBuilder.setAddress(addressFromAction(inetAddress));
145
146             targetAction = setNextHopActionBuilder.build();
147         } else if (sourceAction instanceof SetNwDst) {
148             SetNwDst setNwDst = (SetNwDst) sourceAction;
149             SetNwDstActionBuilder setNwDstActionBuilder = new SetNwDstActionBuilder();
150
151             InetAddress inetAddress = setNwDst.getAddress();
152             setNwDstActionBuilder.setAddress(addressFromAction(inetAddress));
153
154             targetAction = setNwDstActionBuilder.build();
155         } else if (sourceAction instanceof SetNwSrc) {
156             SetNwSrc setNwSrc = (SetNwSrc) sourceAction;
157             SetNwSrcActionBuilder setNwSrcActionBuilder = new SetNwSrcActionBuilder();
158
159             InetAddress inetAddress = setNwSrc.getAddress();
160             setNwSrcActionBuilder.setAddress(addressFromAction(inetAddress));
161
162             targetAction = setNwSrcActionBuilder.build();
163         } else if (sourceAction instanceof SetNwTos) {
164             SetNwTos setNwTos = (SetNwTos) sourceAction;
165             SetNwTosActionBuilder setNwTosActionBuilder = new SetNwTosActionBuilder();
166
167             setNwTosActionBuilder.setTos(setNwTos.getNwTos());
168             targetAction = setNwTosActionBuilder.build();
169         } else if (sourceAction instanceof SetTpDst) {
170             SetTpDst setTpDst = (SetTpDst) sourceAction;
171             SetTpDstActionBuilder setTpDstActionBuilder = new SetTpDstActionBuilder();
172
173             setTpDstActionBuilder.setPort(new PortNumber(setTpDst.getPort()));
174
175             targetAction = setTpDstActionBuilder.build();
176         } else if (sourceAction instanceof SetTpSrc) {
177             SetTpSrc setTpSrc = (SetTpSrc) sourceAction;
178             SetTpSrcActionBuilder setTpSrcActionBuilder = new SetTpSrcActionBuilder();
179
180             setTpSrcActionBuilder.setPort(new PortNumber(setTpSrc.getPort()));
181
182             targetAction = setTpSrcActionBuilder.build();
183         } else if (sourceAction instanceof SetVlanCfi) {
184             SetVlanCfi setVlanCfi = (SetVlanCfi) sourceAction;
185             SetVlanCfiActionBuilder setVlanCfiActionBuilder = new SetVlanCfiActionBuilder();
186
187             setVlanCfiActionBuilder.setVlanCfi(new VlanCfi(setVlanCfi.getCfi()));
188
189             targetAction = setVlanCfiActionBuilder.build();
190         } else if (sourceAction instanceof SetVlanId) {
191             SetVlanId setVlanId = (SetVlanId) sourceAction;
192             SetVlanIdActionBuilder setVlanIdActionBuilder = new SetVlanIdActionBuilder();
193
194             setVlanIdActionBuilder.setVlanId(new VlanId(setVlanId.getVlanId()));
195
196             targetAction = setVlanIdActionBuilder.build();
197         } else if (sourceAction instanceof SetVlanPcp) {
198             SetVlanPcp setVlanPcp = (SetVlanPcp) sourceAction;
199             SetVlanPcpActionBuilder setVlanPcpActionBuilder = new SetVlanPcpActionBuilder();
200
201             setVlanPcpActionBuilder.setVlanPcp(new VlanPcp((short) setVlanPcp.getPcp()));
202
203             targetAction = setVlanPcpActionBuilder.build();
204         } else if (sourceAction instanceof SwPath) {
205             targetAction = new SwPathActionBuilder().build();
206         }
207
208         targetActionBuilder.setAction(targetAction);
209
210         return targetActionBuilder.build();
211     }
212
213     private static Address addressFromAction(InetAddress inetAddress) {
214         String strInetAddresss = InetAddresses.toAddrString(inetAddress);
215         if (inetAddress instanceof Inet4Address) {
216             Ipv4Builder ipv4Builder = new Ipv4Builder();
217             ipv4Builder.setIpv4Address(new Ipv4Prefix(strInetAddresss));
218             return ipv4Builder.build();
219         } else if (inetAddress instanceof Inet6Address) {
220             Ipv6Builder ipv6Builder = new Ipv6Builder();
221             ipv6Builder.setIpv6Address(new Ipv6Prefix(strInetAddresss));
222             return ipv6Builder.build();
223         }
224         return null;
225     }
226
227     private static List<Uri> nodeConnectorToUri(NodeConnector nodeConnector) {
228         // TODO Define mapping
229         return null;
230     }
231
232     private static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Match matchFrom(
233             Match sourceMatch) {
234         if (sourceMatch != null) {
235             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.MatchBuilder targetBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.MatchBuilder();
236
237             targetBuilder.setEthernetMatch(ethernetMatchFrom(sourceMatch));
238             targetBuilder.setIpMatch(ipMatchFrom(sourceMatch));
239             targetBuilder.setVlanMatch(vlanMatchFrom(sourceMatch));
240             targetBuilder.setLayer3Match(layer3Match(sourceMatch));
241             targetBuilder.setLayer4Match(layer4Match(sourceMatch));
242
243             return targetBuilder.build();
244         }
245         return null;
246
247     }
248
249     private static Layer4Match layer4Match(final Match sourceMatch) {
250         MatchField nwProto = sourceMatch.getField(MatchType.NW_PROTO);
251         Short nwProtocolSource = null;
252         if (nwProto != null && nwProto.getValue() != null) {
253             nwProtocolSource = (short) ((byte) nwProto.getValue());
254             switch (nwProtocolSource) {
255             case TCP:
256                 return Layer4MatchAsTcp(sourceMatch);
257             case UDP:
258                 return Layer4MatchAsUdp(sourceMatch);
259             case SCTP:
260                 return Layer4MatchAsSctp(sourceMatch);
261             }
262         }
263         return null;
264     }
265
266     private static Layer4Match Layer4MatchAsSctp(final Match sourceMatch) {
267         SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
268
269         Integer sourcePort = transportPortFrom(sourceMatch, MatchType.TP_SRC);
270         Integer destinationPort = transportPortFrom(sourceMatch, MatchType.TP_DST);
271
272         if (sourcePort != null) {
273             sctpMatchBuilder.setSctpSourcePort(new PortNumber(sourcePort));
274         }
275         if (destinationPort != null) {
276             sctpMatchBuilder.setSctpDestinationPort(new PortNumber(destinationPort));
277         }
278
279         return sctpMatchBuilder.build();
280     }
281
282     private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
283         UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
284
285         Integer sourcePort = transportPortFrom(sourceMatch, MatchType.TP_SRC);
286         Integer destinationPort = transportPortFrom(sourceMatch, MatchType.TP_DST);
287
288         if (sourcePort != null) {
289             udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
290         }
291
292         if (destinationPort != null) {
293             udpMatchBuilder.setUdpDestinationPort(new PortNumber(destinationPort));
294         }
295
296         return udpMatchBuilder.build();
297     }
298
299     private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
300         TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
301
302         Integer sourcePort = transportPortFrom(sourceMatch, MatchType.TP_SRC);
303         Integer destinationPort = transportPortFrom(sourceMatch, MatchType.TP_DST);
304
305         if (sourcePort != null) {
306             tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
307         }
308         if (destinationPort != null) {
309             tcpMatchBuilder.setTcpDestinationPort(new PortNumber(destinationPort));
310         }
311
312         return tcpMatchBuilder.build();
313     }
314
315     private static Integer transportPortFrom(final Match sourceMatch, final MatchType matchType) {
316         MatchField transportPort = sourceMatch.getField(matchType);
317         if (transportPort != null && transportPort.getValue() != null
318                 && transportPort.getValue().getClass().equals(Short.class)) {
319             return new Integer(NetUtils.getUnsignedShort((short) transportPort.getValue()));
320         }
321         return null;
322     }
323
324     private static VlanMatch vlanMatchFrom(final Match sourceMatch) {
325         VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder();
326
327         MatchField vlan = sourceMatch.getField(MatchType.DL_VLAN);
328         if (vlan != null && vlan.getValue() != null) {
329             VlanIdBuilder vlanIDBuilder = new VlanIdBuilder();
330             vlanIDBuilder.setVlanId(new VlanId((int) (NetUtils.getUnsignedShort((short) vlan.getValue()))));
331             vlanMatchBuild.setVlanId(vlanIDBuilder.build());
332         }
333
334         MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR);
335         if (vlanPriority != null && vlanPriority.getValue() != null) {
336             vlanMatchBuild.setVlanPcp(new VlanPcp((short) ((byte) vlanPriority.getValue())));
337         }
338
339         return vlanMatchBuild.build();
340     }
341
342     private static IpMatch ipMatchFrom(final Match sourceMatch) {
343         IpMatchBuilder targetIpMatchBuild = new IpMatchBuilder();
344         MatchField networkTos = sourceMatch.getField(MatchType.NW_TOS);
345         if (networkTos != null && networkTos.getValue() != null) {
346             Dscp dscp = new Dscp((short) (NetUtils.getUnsignedByte((Byte) networkTos.getValue())));
347             targetIpMatchBuild.setIpDscp(dscp);
348         }
349
350         MatchField protocol = sourceMatch.getField(MatchType.NW_PROTO);
351         if (protocol != null && protocol.getValue() != null) {
352             targetIpMatchBuild.setIpProtocol((short) ((byte) protocol.getValue()));
353         }
354
355         return targetIpMatchBuild.build();
356
357     }
358
359     private static EthernetMatch ethernetMatchFrom(final Match sourceMatch) {
360         final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder();
361
362         EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder()
363                 .setAddress(ethernetSourceAddressFrom(sourceMatch));
364         targetEthMatchBuild.setEthernetSource(ethSourBuild.build());
365
366         EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder()
367                 .setAddress(ethernetDestAddressFrom(sourceMatch));
368         targetEthMatchBuild.setEthernetDestination(ethDestBuild.build());
369
370         final MatchField dataLinkType = sourceMatch.getField(MatchType.DL_TYPE);
371         if (dataLinkType != null && dataLinkType.getValue() != null) {
372             EtherType etherType = new EtherType(new Long(NetUtils.getUnsignedShort((Short) dataLinkType.getValue())));
373             EthernetTypeBuilder ethType = new EthernetTypeBuilder().setType(etherType);
374             targetEthMatchBuild.setEthernetType(ethType.build());
375         }
376         return targetEthMatchBuild.build();
377     }
378
379     private static MacAddress ethernetSourceAddressFrom(final Match sourceMatch) {
380         final MatchField dataLinkSource = sourceMatch.getField(DL_SRC);
381         if (dataLinkSource != null && dataLinkSource.getValue() != null) {
382             return new MacAddress(new MacAddress(new String((byte[]) dataLinkSource.getValue())));
383         }
384         return null;
385
386     }
387
388     private static Layer3Match layer3Match(final Match sourceMatch) {
389         InetAddress inetSourceAddress = null;
390         MatchField netSource = sourceMatch.getField(MatchType.NW_SRC);
391         if (netSource != null && netSource.getValue() != null) {
392             inetSourceAddress = (InetAddress) (netSource.getValue());
393         }
394
395         InetAddress inetDestAddress = null;
396         MatchField netDest = sourceMatch.getField(MatchType.NW_DST);
397         if (netSource != null && netSource.getValue() != null) {
398             inetDestAddress = (InetAddress) (netDest.getValue());
399         }
400
401         if ((inetSourceAddress instanceof Inet4Address) && (inetDestAddress instanceof Inet4Address)) {
402             MatchField dataLinkType = sourceMatch.getField(DL_TYPE);
403             Short dLType = null;
404             if (dataLinkType != null && dataLinkType.getValue() != null) {
405                 dLType = (Short) (dataLinkType.getValue());
406             }
407             if (dLType != null && dLType.equals(ETHERNET_ARP)) {
408                 return setLayer3MatchAsArp(sourceMatch, (Inet4Address) inetSourceAddress,
409                         (Inet4Address) inetDestAddress);
410             } else {
411                 return setLayer3MatchAsIpv4((Inet4Address) inetSourceAddress, (Inet4Address) inetDestAddress);
412             }
413         } else if ((inetSourceAddress instanceof Inet6Address) && (inetDestAddress instanceof Inet6Address)) {
414             return setLayer3MatchAsIpv6((Inet6Address) inetSourceAddress, (Inet6Address) inetDestAddress);
415         }
416
417         return null;
418
419     }
420
421     private static Layer3Match setLayer3MatchAsArp(final Match sourceMatch, final Inet4Address inetSourceAddress,
422             final Inet4Address inetDestAddress) {
423         String inetSourceAddressStr = InetAddresses.toAddrString(inetSourceAddress);
424         Ipv4Prefix ipv4SourcePrefix = new Ipv4Prefix(inetSourceAddressStr);
425
426         String inetDestAddressValue = InetAddresses.toAddrString(inetDestAddress);
427         Ipv4Prefix ipv4DestPrefix = new Ipv4Prefix(inetDestAddressValue);
428
429         ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
430
431         arpMatchBuilder.setArpSourceTransportAddress(ipv4SourcePrefix);
432         arpMatchBuilder.setArpTargetTransportAddress(ipv4DestPrefix);
433
434         ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
435         arpSourceHardwareAddressBuilder.setAddress(ethernetSourceAddressFrom(sourceMatch));
436         arpMatchBuilder.setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder.build());
437
438         ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
439         arpTargetHardwareAddressBuilder.setAddress(ethernetDestAddressFrom(sourceMatch));
440         arpMatchBuilder.setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder.build());
441
442         return arpMatchBuilder.build();
443
444     }
445
446     private static MacAddress ethernetDestAddressFrom(final Match sourceMatch) {
447         final MatchField dataLinkDest = sourceMatch.getField(DL_DST);
448         if (dataLinkDest != null && dataLinkDest.getValue() != null) {
449             return new MacAddress(new String((byte[]) (dataLinkDest.getValue())));
450         }
451         return null;
452     }
453
454     private static Layer3Match setLayer3MatchAsIpv4(final Inet4Address inetSourceAddress,
455             final Inet4Address inetDestAddress) {
456         String inetSrcAddressString = InetAddresses.toAddrString(inetSourceAddress);
457         String inetDstAddressString = InetAddresses.toAddrString(inetDestAddress);
458
459         Ipv4MatchBuilder layer4MatchBuild = new Ipv4MatchBuilder();
460         layer4MatchBuild.setIpv4Source(new Ipv4Prefix(inetSrcAddressString));
461         layer4MatchBuild.setIpv4Destination(new Ipv4Prefix(inetDstAddressString));
462         return layer4MatchBuild.build();
463
464     }
465
466     private static Layer3Match setLayer3MatchAsIpv6(final Inet6Address inetSourceAddress,
467             final Inet6Address inetDestAddress) {
468         String inetSrcAddressString = InetAddresses.toAddrString(inetSourceAddress);
469         String inetDstAddressString = InetAddresses.toAddrString(inetDestAddress);
470         Ipv6MatchBuilder layer6MatchBuild = new Ipv6MatchBuilder();
471
472         layer6MatchBuild.setIpv6Source(new Ipv6Prefix(inetSrcAddressString));
473         layer6MatchBuild.setIpv6Destination(new Ipv6Prefix(inetDstAddressString));
474         return layer6MatchBuild.build();
475     }
476
477 }