1 package org.opendaylight.controller.sal.compability;
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;
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;
15 import org.opendaylight.controller.sal.action.*;
16 import org.opendaylight.controller.sal.core.NodeConnector;
17 import org.opendaylight.controller.sal.core.Node;
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;
62 import com.google.common.net.InetAddresses;
67 import static org.opendaylight.controller.sal.compability.ProtocolConstants.*;
68 import static org.opendaylight.controller.sal.compability.NodeInventoryAdapter.*;
70 public class FromSalConversionsUtils {
72 private FromSalConversionsUtils() {
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());
84 .setCookie(new BigInteger(String.valueOf(sourceFlow.getId())));
86 List<org.opendaylight.controller.sal.action.Action> sourceActions = sourceFlow
88 List<Action> targetActions = new ArrayList<>();
89 for (org.opendaylight.controller.sal.action.Action sourceAction : sourceActions) {
90 targetActions.add(action(sourceAction));
92 targetFlow.setAction(targetActions);
93 targetFlow.setMatch(match(sourceFlow.getMatch()));
94 return targetFlow.build();
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));
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));
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));
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));
155 public static GetNodeConnectorStatisticsInput nodeConnectorStatistics(
156 NodeConnector connector) {
157 GetNodeConnectorStatisticsInputBuilder target = new GetNodeConnectorStatisticsInputBuilder();
159 NodeRef nodeRef = nodeRef(connector.getNode());
160 target.setNode(nodeRef);
162 NodeConnectorRef nodeConnectorRef = nodeConnectorRef(connector);
163 target.setNodeConnector(nodeConnectorRef);
165 return target.build();
168 private static Action action(
169 org.opendaylight.controller.sal.action.Action sourceAction) {
171 ActionBuilder targetActionBuilder = new ActionBuilder();
172 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.Action targetAction = null;
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();
189 OutputActionBuilder outputActionBuilder = new OutputActionBuilder();
191 .setOutputNodeConnector(nodeConnectorToUri(nodeConnector));
192 targetAction = outputActionBuilder.build();
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();
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();
209 setDlDstActionBuilder.setAddress(new MacAddress(new String(setDlDst
211 targetAction = setDlDstActionBuilder.build();
212 } else if (sourceAction instanceof SetDlSrc) {
213 SetDlSrc setDlSrc = (SetDlSrc) sourceAction;
214 SetDlSrcActionBuilder setDlSrcActionBuilder = new SetDlSrcActionBuilder();
216 setDlSrcActionBuilder.setAddress(new MacAddress(new String(setDlSrc
218 targetAction = setDlSrcActionBuilder.build();
219 } else if (sourceAction instanceof SetDlType) {
220 SetDlType setDlType = (SetDlType) sourceAction;
221 SetDlTypeActionBuilder setDlTypeActionBuilder = new SetDlTypeActionBuilder();
223 setDlTypeActionBuilder.setDlType(new EtherType(new Long(setDlType
225 targetAction = setDlTypeActionBuilder.build();
226 } else if (sourceAction instanceof SetNextHop) {
227 SetNextHop setNextHop = (SetNextHop) sourceAction;
228 SetNextHopActionBuilder setNextHopActionBuilder = new SetNextHopActionBuilder();
230 InetAddress inetAddress = setNextHop.getAddress();
231 setNextHopActionBuilder.setAddress(addressFromAction(inetAddress));
233 targetAction = setNextHopActionBuilder.build();
234 } else if (sourceAction instanceof SetNwDst) {
235 SetNwDst setNwDst = (SetNwDst) sourceAction;
236 SetNwDstActionBuilder setNwDstActionBuilder = new SetNwDstActionBuilder();
238 InetAddress inetAddress = setNwDst.getAddress();
239 setNwDstActionBuilder.setAddress(addressFromAction(inetAddress));
241 targetAction = setNwDstActionBuilder.build();
242 } else if (sourceAction instanceof SetNwSrc) {
243 SetNwSrc setNwSrc = (SetNwSrc) sourceAction;
244 SetNwSrcActionBuilder setNwSrcActionBuilder = new SetNwSrcActionBuilder();
246 InetAddress inetAddress = setNwSrc.getAddress();
247 setNwSrcActionBuilder.setAddress(addressFromAction(inetAddress));
249 targetAction = setNwSrcActionBuilder.build();
250 } else if (sourceAction instanceof SetNwTos) {
251 SetNwTos setNwTos = (SetNwTos) sourceAction;
252 SetNwTosActionBuilder setNwTosActionBuilder = new SetNwTosActionBuilder();
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();
260 setTpDstActionBuilder.setPort(new PortNumber(setTpDst.getPort()));
262 targetAction = setTpDstActionBuilder.build();
263 } else if (sourceAction instanceof SetTpSrc) {
264 SetTpSrc setTpSrc = (SetTpSrc) sourceAction;
265 SetTpSrcActionBuilder setTpSrcActionBuilder = new SetTpSrcActionBuilder();
267 setTpSrcActionBuilder.setPort(new PortNumber(setTpSrc.getPort()));
269 targetAction = setTpSrcActionBuilder.build();
270 } else if (sourceAction instanceof SetVlanCfi) {
271 SetVlanCfi setVlanCfi = (SetVlanCfi) sourceAction;
272 SetVlanCfiActionBuilder setVlanCfiActionBuilder = new SetVlanCfiActionBuilder();
274 setVlanCfiActionBuilder
275 .setVlanCfi(new VlanCfi(setVlanCfi.getCfi()));
277 targetAction = setVlanCfiActionBuilder.build();
278 } else if (sourceAction instanceof SetVlanId) {
279 SetVlanId setVlanId = (SetVlanId) sourceAction;
280 SetVlanIdActionBuilder setVlanIdActionBuilder = new SetVlanIdActionBuilder();
282 setVlanIdActionBuilder.setVlanId(new VlanId(setVlanId.getVlanId()));
284 targetAction = setVlanIdActionBuilder.build();
285 } else if (sourceAction instanceof SetVlanPcp) {
286 SetVlanPcp setVlanPcp = (SetVlanPcp) sourceAction;
287 SetVlanPcpActionBuilder setVlanPcpActionBuilder = new SetVlanPcpActionBuilder();
289 setVlanPcpActionBuilder.setVlanPcp(new VlanPcp((short) setVlanPcp
292 targetAction = setVlanPcpActionBuilder.build();
293 } else if (sourceAction instanceof SwPath) {
294 targetAction = new SwPathActionBuilder().build();
297 targetActionBuilder.setAction(targetAction);
299 return targetActionBuilder.build();
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();
316 private static List<Uri> nodeConnectorToUri(NodeConnector nodeConnector) {
317 // TODO Define mapping
321 private static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Match match(
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();
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));
332 return targetBuilder.build();
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) {
345 return Layer4MatchAsTcp(sourceMatch);
347 return Layer4MatchAsUdp(sourceMatch);
349 return Layer4MatchAsSctp(sourceMatch);
355 private static Layer4Match Layer4MatchAsSctp(final Match sourceMatch) {
356 SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
358 Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
359 Integer destinationPort = transportPort(sourceMatch,
362 if (sourcePort != null) {
363 sctpMatchBuilder.setSctpSourcePort(new PortNumber(sourcePort));
365 if (destinationPort != null) {
366 sctpMatchBuilder.setSctpDestinationPort(new PortNumber(
370 return sctpMatchBuilder.build();
373 private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
374 UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
376 Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
377 Integer destinationPort = transportPort(sourceMatch,
380 if (sourcePort != null) {
381 udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
384 if (destinationPort != null) {
385 udpMatchBuilder.setUdpDestinationPort(new PortNumber(
389 return udpMatchBuilder.build();
392 private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
393 TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
395 Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
396 Integer destinationPort = transportPort(sourceMatch,
399 if (sourcePort != null) {
400 tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
402 if (destinationPort != null) {
403 tcpMatchBuilder.setTcpDestinationPort(new PortNumber(
407 return tcpMatchBuilder.build();
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
421 private static VlanMatch vlanMatch(final Match sourceMatch) {
422 VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder();
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());
432 MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR);
433 if (vlanPriority != null && vlanPriority.getValue() != null) {
434 vlanMatchBuild.setVlanPcp(new VlanPcp((short) ((byte) vlanPriority
438 return vlanMatchBuild.build();
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
448 targetIpMatchBuild.setIpDscp(dscp);
451 MatchField protocol = sourceMatch.getField(MatchType.NW_PROTO);
452 if (protocol != null && protocol.getValue() != null) {
453 targetIpMatchBuild.setIpProtocol((short) ((byte) protocol
457 return targetIpMatchBuild.build();
461 private static EthernetMatch ethernetMatch(final Match sourceMatch) {
462 final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder();
464 EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder()
465 .setAddress(ethernetSourceAddress(sourceMatch));
466 targetEthMatchBuild.setEthernetSource(ethSourBuild.build());
468 EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder()
469 .setAddress(ethernetDestAddress(sourceMatch));
470 targetEthMatchBuild.setEthernetDestination(ethDestBuild.build());
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()
478 targetEthMatchBuild.setEthernetType(ethType.build());
480 return targetEthMatchBuild.build();
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())));
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());
500 InetAddress inetDestAddress = null;
501 MatchField netDest = sourceMatch.getField(MatchType.NW_DST);
502 if (netSource != null && netSource.getValue() != null) {
503 inetDestAddress = (InetAddress) (netDest.getValue());
506 if ((inetSourceAddress instanceof Inet4Address)
507 && (inetDestAddress instanceof Inet4Address)) {
508 MatchField dataLinkType = sourceMatch.getField(DL_TYPE);
510 if (dataLinkType != null && dataLinkType.getValue() != null) {
511 dLType = (Short) (dataLinkType.getValue());
513 if (dLType != null && dLType.equals(ETHERNET_ARP)) {
514 return setLayer3MatchAsArp(sourceMatch,
515 (Inet4Address) inetSourceAddress,
516 (Inet4Address) inetDestAddress);
518 return setLayer3MatchAsIpv4((Inet4Address) inetSourceAddress,
519 (Inet4Address) inetDestAddress);
521 } else if ((inetSourceAddress instanceof Inet6Address)
522 && (inetDestAddress instanceof Inet6Address)) {
523 return setLayer3MatchAsIpv6((Inet6Address) inetSourceAddress,
524 (Inet6Address) inetDestAddress);
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);
538 String inetDestAddressValue = InetAddresses
539 .toAddrString(inetDestAddress);
540 Ipv4Prefix ipv4DestPrefix = new Ipv4Prefix(inetDestAddressValue);
542 ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
544 arpMatchBuilder.setArpSourceTransportAddress(ipv4SourcePrefix);
545 arpMatchBuilder.setArpTargetTransportAddress(ipv4DestPrefix);
547 ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
548 arpSourceHardwareAddressBuilder
549 .setAddress(ethernetSourceAddress(sourceMatch));
551 .setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder
554 ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
555 arpTargetHardwareAddressBuilder
556 .setAddress(ethernetDestAddress(sourceMatch));
558 .setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder
561 return arpMatchBuilder.build();
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())));
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);
582 Ipv4MatchBuilder layer4MatchBuild = new Ipv4MatchBuilder();
583 layer4MatchBuild.setIpv4Source(new Ipv4Prefix(inetSrcAddressString));
585 .setIpv4Destination(new Ipv4Prefix(inetDstAddressString));
586 return layer4MatchBuild.build();
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();
599 layer6MatchBuild.setIpv6Source(new Ipv6Prefix(inetSrcAddressString));
601 .setIpv6Destination(new Ipv6Prefix(inetDstAddressString));
602 return layer6MatchBuild.build();