Updated Flow Services Adapter,started inventory adapter.
[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.core.Node;
18
19
20 import org.opendaylight.controller.sal.flowprogrammer.Flow;
21 import org.opendaylight.controller.sal.match.Match;
22 import org.opendaylight.controller.sal.match.MatchField;
23 import org.opendaylight.controller.sal.match.MatchType;
24 import org.opendaylight.controller.sal.utils.NetUtils;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.*;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAddedBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.*;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.VlanCfi;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.*;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.Address;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.address.Ipv4Builder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.address.Ipv6Builder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Action;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.ActionBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.arp.match.fields.ArpSourceHardwareAddressBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.arp.match.fields.ArpTargetHardwareAddressBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetDestinationBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetSourceBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetTypeBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.*;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.ArpMatchBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv4MatchBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv6MatchBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.SctpMatchBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.TcpMatchBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.UdpMatchBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.vlan.match.fields.VlanIdBuilder;
61
62 import com.google.common.net.InetAddresses;
63
64
65
66
67 import static org.opendaylight.controller.sal.compability.ProtocolConstants.*;
68 import static org.opendaylight.controller.sal.compability.NodeInventoryAdapter.*;
69
70 public class FromSalConversionsUtils {
71
72     private FromSalConversionsUtils() {
73
74     }
75
76     public static FlowAdded flowAdded(Flow sourceFlow) {
77         if (sourceFlow == null)
78             throw new IllegalArgumentException();
79         final FlowAddedBuilder targetFlow = new FlowAddedBuilder();
80         targetFlow.setHardTimeout((int) sourceFlow.getHardTimeout());
81         targetFlow.setIdleTimeout((int) sourceFlow.getIdleTimeout());
82         targetFlow.setPriority((int) sourceFlow.getPriority());
83         targetFlow
84                 .setCookie(new BigInteger(String.valueOf(sourceFlow.getId())));
85
86         List<org.opendaylight.controller.sal.action.Action> sourceActions = sourceFlow
87                 .getActions();
88         List<Action> targetActions = new ArrayList<>();
89         for (org.opendaylight.controller.sal.action.Action sourceAction : sourceActions) {
90             targetActions.add(action(sourceAction));
91         }
92         targetFlow.setAction(targetActions);
93         targetFlow.setMatch(match(sourceFlow.getMatch()));
94         return targetFlow.build();
95
96     }
97
98     public static GetFlowStatisticsInput flowStatisticsInput(
99             Node node,Flow sourceFlow) {
100         GetFlowStatisticsInputBuilder ret = new GetFlowStatisticsInputBuilder();
101         FlowAdded source = flowAdded(sourceFlow);
102         ret.setAction(source.getAction());
103         ret.setCookie(source.getCookie());
104         ret.setAction(source.getAction());
105         ret.setCookie(source.getCookie());
106         ret.setHardTimeout(source.getHardTimeout());
107         ret.setMatch(source.getMatch());
108         ret.setPriority(source.getPriority());
109         ret.setNode(nodeRef(node));
110         return ret.build();
111     }
112
113     public static RemoveFlowInput removeFlowInput(Node node,Flow sourceFlow) {
114         RemoveFlowInputBuilder ret = new RemoveFlowInputBuilder();
115         FlowAdded source = flowAdded(sourceFlow);
116         ret.setAction(source.getAction());
117         ret.setCookie(source.getCookie());
118         ret.setAction(source.getAction());
119         ret.setCookie(source.getCookie());
120         ret.setHardTimeout(source.getHardTimeout());
121         ret.setMatch(source.getMatch());
122         ret.setPriority(source.getPriority());
123         ret.setNode(nodeRef(node));
124         return ret.build();
125     }
126     
127     public static AddFlowInput addFlowInput(Node node,Flow sourceFlow) {
128         AddFlowInputBuilder ret = new AddFlowInputBuilder();
129         FlowAdded source = flowAdded(sourceFlow);
130         ret.setAction(source.getAction());
131         ret.setCookie(source.getCookie());
132         ret.setAction(source.getAction());
133         ret.setCookie(source.getCookie());
134         ret.setHardTimeout(source.getHardTimeout());
135         ret.setMatch(source.getMatch());
136         ret.setPriority(source.getPriority());
137         ret.setNode(nodeRef(node));
138         return ret.build();
139     }
140
141     public static UpdateFlowInput updateFlowInput(Node node, Flow oldFlow, Flow newFlow){
142         UpdateFlowInputBuilder ret = new UpdateFlowInputBuilder();
143         FlowAdded source = flowAdded(newFlow);
144         ret.setAction(source.getAction());
145         ret.setCookie(source.getCookie());
146         ret.setAction(source.getAction());
147         ret.setCookie(source.getCookie());
148         ret.setHardTimeout(source.getHardTimeout());
149         ret.setMatch(source.getMatch());
150         ret.setPriority(source.getPriority());
151         ret.setNode(nodeRef(node));
152         return ret.build();
153     }
154
155     public static GetNodeConnectorStatisticsInput nodeConnectorStatistics(
156             NodeConnector connector) {
157         GetNodeConnectorStatisticsInputBuilder target = new GetNodeConnectorStatisticsInputBuilder();
158
159         NodeRef nodeRef = nodeRef(connector.getNode());
160         target.setNode(nodeRef);
161
162         NodeConnectorRef nodeConnectorRef = nodeConnectorRef(connector);
163         target.setNodeConnector(nodeConnectorRef);
164
165         return target.build();
166     }
167
168     private static Action action(
169             org.opendaylight.controller.sal.action.Action sourceAction) {
170
171         ActionBuilder targetActionBuilder = new ActionBuilder();
172         org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.Action targetAction = null;
173
174         if (sourceAction instanceof Controller) {
175             targetAction = new ControllerActionBuilder().build();
176         } else if (sourceAction instanceof Drop) {
177             targetAction = new DropActionBuilder().build();
178         } else if (sourceAction instanceof Flood) {
179             targetAction = new FloodActionBuilder().build();
180         } else if (sourceAction instanceof FloodAll) {
181             targetAction = new FloodAllActionBuilder().build();
182         } else if (sourceAction instanceof HwPath) {
183             targetAction = new HwPathActionBuilder().build();
184         } else if (sourceAction instanceof Loopback) {
185             targetAction = new LoopbackActionBuilder().build();
186         } else if (sourceAction instanceof Output) {
187             NodeConnector nodeConnector = ((Output) sourceAction).getPort();
188
189             OutputActionBuilder outputActionBuilder = new OutputActionBuilder();
190             outputActionBuilder
191                     .setOutputNodeConnector(nodeConnectorToUri(nodeConnector));
192             targetAction = outputActionBuilder.build();
193
194         } else if (sourceAction instanceof PopVlan) {
195             targetAction = new PopVlanActionBuilder().build();
196         } else if (sourceAction instanceof PushVlan) {
197             PushVlan pushVlan = (PushVlan) sourceAction;
198             PushVlanActionBuilder pushVlanActionBuilder = new PushVlanActionBuilder();
199
200             pushVlanActionBuilder.setCfi(new VlanCfi(pushVlan.getCfi()));
201             pushVlanActionBuilder.setVlanId(new VlanId(pushVlan.getVlanId()));
202             pushVlanActionBuilder.setPcp(pushVlan.getPcp());
203             pushVlanActionBuilder.setTag(pushVlan.getTag());
204             targetAction = pushVlanActionBuilder.build();
205         } else if (sourceAction instanceof SetDlDst) {
206             SetDlDst setDlDst = (SetDlDst) sourceAction;
207             SetDlDstActionBuilder setDlDstActionBuilder = new SetDlDstActionBuilder();
208
209             setDlDstActionBuilder.setAddress(new MacAddress(new String(setDlDst
210                     .getDlAddress())));
211             targetAction = setDlDstActionBuilder.build();
212         } else if (sourceAction instanceof SetDlSrc) {
213             SetDlSrc setDlSrc = (SetDlSrc) sourceAction;
214             SetDlSrcActionBuilder setDlSrcActionBuilder = new SetDlSrcActionBuilder();
215
216             setDlSrcActionBuilder.setAddress(new MacAddress(new String(setDlSrc
217                     .getDlAddress())));
218             targetAction = setDlSrcActionBuilder.build();
219         } else if (sourceAction instanceof SetDlType) {
220             SetDlType setDlType = (SetDlType) sourceAction;
221             SetDlTypeActionBuilder setDlTypeActionBuilder = new SetDlTypeActionBuilder();
222
223             setDlTypeActionBuilder.setDlType(new EtherType(new Long(setDlType
224                     .getDlType())));
225             targetAction = setDlTypeActionBuilder.build();
226         } else if (sourceAction instanceof SetNextHop) {
227             SetNextHop setNextHop = (SetNextHop) sourceAction;
228             SetNextHopActionBuilder setNextHopActionBuilder = new SetNextHopActionBuilder();
229
230             InetAddress inetAddress = setNextHop.getAddress();
231             setNextHopActionBuilder.setAddress(addressFromAction(inetAddress));
232
233             targetAction = setNextHopActionBuilder.build();
234         } else if (sourceAction instanceof SetNwDst) {
235             SetNwDst setNwDst = (SetNwDst) sourceAction;
236             SetNwDstActionBuilder setNwDstActionBuilder = new SetNwDstActionBuilder();
237
238             InetAddress inetAddress = setNwDst.getAddress();
239             setNwDstActionBuilder.setAddress(addressFromAction(inetAddress));
240
241             targetAction = setNwDstActionBuilder.build();
242         } else if (sourceAction instanceof SetNwSrc) {
243             SetNwSrc setNwSrc = (SetNwSrc) sourceAction;
244             SetNwSrcActionBuilder setNwSrcActionBuilder = new SetNwSrcActionBuilder();
245
246             InetAddress inetAddress = setNwSrc.getAddress();
247             setNwSrcActionBuilder.setAddress(addressFromAction(inetAddress));
248
249             targetAction = setNwSrcActionBuilder.build();
250         } else if (sourceAction instanceof SetNwTos) {
251             SetNwTos setNwTos = (SetNwTos) sourceAction;
252             SetNwTosActionBuilder setNwTosActionBuilder = new SetNwTosActionBuilder();
253
254             setNwTosActionBuilder.setTos(setNwTos.getNwTos());
255             targetAction = setNwTosActionBuilder.build();
256         } else if (sourceAction instanceof SetTpDst) {
257             SetTpDst setTpDst = (SetTpDst) sourceAction;
258             SetTpDstActionBuilder setTpDstActionBuilder = new SetTpDstActionBuilder();
259
260             setTpDstActionBuilder.setPort(new PortNumber(setTpDst.getPort()));
261
262             targetAction = setTpDstActionBuilder.build();
263         } else if (sourceAction instanceof SetTpSrc) {
264             SetTpSrc setTpSrc = (SetTpSrc) sourceAction;
265             SetTpSrcActionBuilder setTpSrcActionBuilder = new SetTpSrcActionBuilder();
266
267             setTpSrcActionBuilder.setPort(new PortNumber(setTpSrc.getPort()));
268
269             targetAction = setTpSrcActionBuilder.build();
270         } else if (sourceAction instanceof SetVlanCfi) {
271             SetVlanCfi setVlanCfi = (SetVlanCfi) sourceAction;
272             SetVlanCfiActionBuilder setVlanCfiActionBuilder = new SetVlanCfiActionBuilder();
273
274             setVlanCfiActionBuilder
275                     .setVlanCfi(new VlanCfi(setVlanCfi.getCfi()));
276
277             targetAction = setVlanCfiActionBuilder.build();
278         } else if (sourceAction instanceof SetVlanId) {
279             SetVlanId setVlanId = (SetVlanId) sourceAction;
280             SetVlanIdActionBuilder setVlanIdActionBuilder = new SetVlanIdActionBuilder();
281
282             setVlanIdActionBuilder.setVlanId(new VlanId(setVlanId.getVlanId()));
283
284             targetAction = setVlanIdActionBuilder.build();
285         } else if (sourceAction instanceof SetVlanPcp) {
286             SetVlanPcp setVlanPcp = (SetVlanPcp) sourceAction;
287             SetVlanPcpActionBuilder setVlanPcpActionBuilder = new SetVlanPcpActionBuilder();
288
289             setVlanPcpActionBuilder.setVlanPcp(new VlanPcp((short) setVlanPcp
290                     .getPcp()));
291
292             targetAction = setVlanPcpActionBuilder.build();
293         } else if (sourceAction instanceof SwPath) {
294             targetAction = new SwPathActionBuilder().build();
295         }
296
297         targetActionBuilder.setAction(targetAction);
298
299         return targetActionBuilder.build();
300     }
301
302     private static Address addressFromAction(InetAddress inetAddress) {
303         String strInetAddresss = InetAddresses.toAddrString(inetAddress);
304         if (inetAddress instanceof Inet4Address) {
305             Ipv4Builder ipv4Builder = new Ipv4Builder();
306             ipv4Builder.setIpv4Address(new Ipv4Prefix(strInetAddresss));
307             return ipv4Builder.build();
308         } else if (inetAddress instanceof Inet6Address) {
309             Ipv6Builder ipv6Builder = new Ipv6Builder();
310             ipv6Builder.setIpv6Address(new Ipv6Prefix(strInetAddresss));
311             return ipv6Builder.build();
312         }
313         return null;
314     }
315
316     private static List<Uri> nodeConnectorToUri(NodeConnector nodeConnector) {
317         // TODO Define mapping
318         return null;
319     }
320
321     private static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Match match(
322             Match sourceMatch) {
323         if (sourceMatch != null) {
324             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();
325
326             targetBuilder.setEthernetMatch(ethernetMatch(sourceMatch));
327             targetBuilder.setIpMatch(ipMatch(sourceMatch));
328             targetBuilder.setVlanMatch(vlanMatch(sourceMatch));
329             targetBuilder.setLayer3Match(layer3Match(sourceMatch));
330             targetBuilder.setLayer4Match(layer4Match(sourceMatch));
331
332             return targetBuilder.build();
333         }
334         return null;
335
336     }
337
338     private static Layer4Match layer4Match(final Match sourceMatch) {
339         MatchField nwProto = sourceMatch.getField(MatchType.NW_PROTO);
340         Short nwProtocolSource = null;
341         if (nwProto != null && nwProto.getValue() != null) {
342             nwProtocolSource = (short) ((byte) nwProto.getValue());
343             switch (nwProtocolSource) {
344             case TCP:
345                 return Layer4MatchAsTcp(sourceMatch);
346             case UDP:
347                 return Layer4MatchAsUdp(sourceMatch);
348             case SCTP:
349                 return Layer4MatchAsSctp(sourceMatch);
350             }
351         }
352         return null;
353     }
354
355     private static Layer4Match Layer4MatchAsSctp(final Match sourceMatch) {
356         SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
357
358         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
359         Integer destinationPort = transportPort(sourceMatch,
360                 MatchType.TP_DST);
361
362         if (sourcePort != null) {
363             sctpMatchBuilder.setSctpSourcePort(new PortNumber(sourcePort));
364         }
365         if (destinationPort != null) {
366             sctpMatchBuilder.setSctpDestinationPort(new PortNumber(
367                     destinationPort));
368         }
369
370         return sctpMatchBuilder.build();
371     }
372
373     private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
374         UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
375
376         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
377         Integer destinationPort = transportPort(sourceMatch,
378                 MatchType.TP_DST);
379
380         if (sourcePort != null) {
381             udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
382         }
383
384         if (destinationPort != null) {
385             udpMatchBuilder.setUdpDestinationPort(new PortNumber(
386                     destinationPort));
387         }
388
389         return udpMatchBuilder.build();
390     }
391
392     private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
393         TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
394
395         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
396         Integer destinationPort = transportPort(sourceMatch,
397                 MatchType.TP_DST);
398
399         if (sourcePort != null) {
400             tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
401         }
402         if (destinationPort != null) {
403             tcpMatchBuilder.setTcpDestinationPort(new PortNumber(
404                     destinationPort));
405         }
406
407         return tcpMatchBuilder.build();
408     }
409
410     private static Integer transportPort(final Match sourceMatch,
411             final MatchType matchType) {
412         MatchField transportPort = sourceMatch.getField(matchType);
413         if (transportPort != null && transportPort.getValue() != null
414                 && transportPort.getValue().getClass().equals(Short.class)) {
415             return new Integer(NetUtils.getUnsignedShort((short) transportPort
416                     .getValue()));
417         }
418         return null;
419     }
420
421     private static VlanMatch vlanMatch(final Match sourceMatch) {
422         VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder();
423
424         MatchField vlan = sourceMatch.getField(MatchType.DL_VLAN);
425         if (vlan != null && vlan.getValue() != null) {
426             VlanIdBuilder vlanIDBuilder = new VlanIdBuilder();
427             vlanIDBuilder.setVlanId(new VlanId((int) (NetUtils
428                     .getUnsignedShort((short) vlan.getValue()))));
429             vlanMatchBuild.setVlanId(vlanIDBuilder.build());
430         }
431
432         MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR);
433         if (vlanPriority != null && vlanPriority.getValue() != null) {
434             vlanMatchBuild.setVlanPcp(new VlanPcp((short) ((byte) vlanPriority
435                     .getValue())));
436         }
437
438         return vlanMatchBuild.build();
439     }
440
441     private static IpMatch ipMatch(final Match sourceMatch) {
442         IpMatchBuilder targetIpMatchBuild = new IpMatchBuilder();
443         MatchField networkTos = sourceMatch.getField(MatchType.NW_TOS);
444         if (networkTos != null && networkTos.getValue() != null) {
445             Dscp dscp = new Dscp(
446                     (short) (NetUtils.getUnsignedByte((Byte) networkTos
447                             .getValue())));
448             targetIpMatchBuild.setIpDscp(dscp);
449         }
450
451         MatchField protocol = sourceMatch.getField(MatchType.NW_PROTO);
452         if (protocol != null && protocol.getValue() != null) {
453             targetIpMatchBuild.setIpProtocol((short) ((byte) protocol
454                     .getValue()));
455         }
456
457         return targetIpMatchBuild.build();
458
459     }
460
461     private static EthernetMatch ethernetMatch(final Match sourceMatch) {
462         final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder();
463
464         EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder()
465                 .setAddress(ethernetSourceAddress(sourceMatch));
466         targetEthMatchBuild.setEthernetSource(ethSourBuild.build());
467
468         EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder()
469                 .setAddress(ethernetDestAddress(sourceMatch));
470         targetEthMatchBuild.setEthernetDestination(ethDestBuild.build());
471
472         final MatchField dataLinkType = sourceMatch.getField(MatchType.DL_TYPE);
473         if (dataLinkType != null && dataLinkType.getValue() != null) {
474             EtherType etherType = new EtherType(new Long(
475                     NetUtils.getUnsignedShort((Short) dataLinkType.getValue())));
476             EthernetTypeBuilder ethType = new EthernetTypeBuilder()
477                     .setType(etherType);
478             targetEthMatchBuild.setEthernetType(ethType.build());
479         }
480         return targetEthMatchBuild.build();
481     }
482
483     private static MacAddress ethernetSourceAddress(final Match sourceMatch) {
484         final MatchField dataLinkSource = sourceMatch.getField(DL_SRC);
485         if (dataLinkSource != null && dataLinkSource.getValue() != null) {
486             return new MacAddress(new MacAddress(new String(
487                     (byte[]) dataLinkSource.getValue())));
488         }
489         return null;
490
491     }
492
493     private static Layer3Match layer3Match(final Match sourceMatch) {
494         InetAddress inetSourceAddress = null;
495         MatchField netSource = sourceMatch.getField(MatchType.NW_SRC);
496         if (netSource != null && netSource.getValue() != null) {
497             inetSourceAddress = (InetAddress) (netSource.getValue());
498         }
499
500         InetAddress inetDestAddress = null;
501         MatchField netDest = sourceMatch.getField(MatchType.NW_DST);
502         if (netSource != null && netSource.getValue() != null) {
503             inetDestAddress = (InetAddress) (netDest.getValue());
504         }
505
506         if ((inetSourceAddress instanceof Inet4Address)
507                 && (inetDestAddress instanceof Inet4Address)) {
508             MatchField dataLinkType = sourceMatch.getField(DL_TYPE);
509             Short dLType = null;
510             if (dataLinkType != null && dataLinkType.getValue() != null) {
511                 dLType = (Short) (dataLinkType.getValue());
512             }
513             if (dLType != null && dLType.equals(ETHERNET_ARP)) {
514                 return setLayer3MatchAsArp(sourceMatch,
515                         (Inet4Address) inetSourceAddress,
516                         (Inet4Address) inetDestAddress);
517             } else {
518                 return setLayer3MatchAsIpv4((Inet4Address) inetSourceAddress,
519                         (Inet4Address) inetDestAddress);
520             }
521         } else if ((inetSourceAddress instanceof Inet6Address)
522                 && (inetDestAddress instanceof Inet6Address)) {
523             return setLayer3MatchAsIpv6((Inet6Address) inetSourceAddress,
524                     (Inet6Address) inetDestAddress);
525         }
526
527         return null;
528
529     }
530
531     private static Layer3Match setLayer3MatchAsArp(final Match sourceMatch,
532             final Inet4Address inetSourceAddress,
533             final Inet4Address inetDestAddress) {
534         String inetSourceAddressStr = InetAddresses
535                 .toAddrString(inetSourceAddress);
536         Ipv4Prefix ipv4SourcePrefix = new Ipv4Prefix(inetSourceAddressStr);
537
538         String inetDestAddressValue = InetAddresses
539                 .toAddrString(inetDestAddress);
540         Ipv4Prefix ipv4DestPrefix = new Ipv4Prefix(inetDestAddressValue);
541
542         ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
543
544         arpMatchBuilder.setArpSourceTransportAddress(ipv4SourcePrefix);
545         arpMatchBuilder.setArpTargetTransportAddress(ipv4DestPrefix);
546
547         ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
548         arpSourceHardwareAddressBuilder
549                 .setAddress(ethernetSourceAddress(sourceMatch));
550         arpMatchBuilder
551                 .setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder
552                         .build());
553
554         ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
555         arpTargetHardwareAddressBuilder
556                 .setAddress(ethernetDestAddress(sourceMatch));
557         arpMatchBuilder
558                 .setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder
559                         .build());
560
561         return arpMatchBuilder.build();
562
563     }
564
565     private static MacAddress ethernetDestAddress(final Match sourceMatch) {
566         final MatchField dataLinkDest = sourceMatch.getField(DL_DST);
567         if (dataLinkDest != null && dataLinkDest.getValue() != null) {
568             return new MacAddress(
569                     new String((byte[]) (dataLinkDest.getValue())));
570         }
571         return null;
572     }
573
574     private static Layer3Match setLayer3MatchAsIpv4(
575             final Inet4Address inetSourceAddress,
576             final Inet4Address inetDestAddress) {
577         String inetSrcAddressString = InetAddresses
578                 .toAddrString(inetSourceAddress);
579         String inetDstAddressString = InetAddresses
580                 .toAddrString(inetDestAddress);
581
582         Ipv4MatchBuilder layer4MatchBuild = new Ipv4MatchBuilder();
583         layer4MatchBuild.setIpv4Source(new Ipv4Prefix(inetSrcAddressString));
584         layer4MatchBuild
585                 .setIpv4Destination(new Ipv4Prefix(inetDstAddressString));
586         return layer4MatchBuild.build();
587
588     }
589
590     private static Layer3Match setLayer3MatchAsIpv6(
591             final Inet6Address inetSourceAddress,
592             final Inet6Address inetDestAddress) {
593         String inetSrcAddressString = InetAddresses
594                 .toAddrString(inetSourceAddress);
595         String inetDstAddressString = InetAddresses
596                 .toAddrString(inetDestAddress);
597         Ipv6MatchBuilder layer6MatchBuild = new Ipv6MatchBuilder();
598
599         layer6MatchBuild.setIpv6Source(new Ipv6Prefix(inetSrcAddressString));
600         layer6MatchBuild
601                 .setIpv6Destination(new Ipv6Prefix(inetDstAddressString));
602         return layer6MatchBuild.build();
603     }
604
605 }