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.core.NodeConnector;
19 import org.opendaylight.controller.sal.match.Match;
20 import org.opendaylight.controller.sal.match.MatchField;
21 import org.opendaylight.controller.sal.match.MatchType;
22 import org.opendaylight.controller.sal.utils.NetUtils;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.*;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.*;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.address.Address;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.address.address.Ipv4Builder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.address.address.Ipv6Builder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
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.rev131026.arp.match.fields.ArpSourceHardwareAddressBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddressBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.*;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
50 import com.google.common.net.InetAddresses;
59 import static org.opendaylight.controller.sal.compability.ProtocolConstants.*;
60 import static org.opendaylight.controller.sal.compability.NodeMapping.*;
62 public class FromSalConversionsUtils {
64 private FromSalConversionsUtils() {
68 public static GetNodeConnectorStatisticsInput nodeConnectorStatistics(
69 NodeConnector connector) {
70 GetNodeConnectorStatisticsInputBuilder target = new GetNodeConnectorStatisticsInputBuilder();
72 NodeRef nodeRef = toNodeRef(connector.getNode());
73 target.setNode(nodeRef);
75 NodeConnectorRef nodeConnectorRef = toNodeConnectorRef(connector);
76 target.setNodeConnector(nodeConnectorRef);
78 return target.build();
81 private static Address addressFromAction(InetAddress inetAddress) {
82 String strInetAddresss = InetAddresses.toAddrString(inetAddress);
83 if (inetAddress instanceof Inet4Address) {
84 Ipv4Builder ipv4Builder = new Ipv4Builder();
85 ipv4Builder.setIpv4Address(new Ipv4Prefix(strInetAddresss));
86 return ipv4Builder.build();
87 } else if (inetAddress instanceof Inet6Address) {
88 Ipv6Builder ipv6Builder = new Ipv6Builder();
89 ipv6Builder.setIpv6Address(new Ipv6Prefix(strInetAddresss));
90 return ipv6Builder.build();
95 public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match toMatch(
97 if (sourceMatch != null) {
98 MatchBuilder targetBuilder = new MatchBuilder();
100 targetBuilder.setEthernetMatch(ethernetMatch(sourceMatch));
101 targetBuilder.setIpMatch(ipMatch(sourceMatch));
102 targetBuilder.setVlanMatch(vlanMatch(sourceMatch));
103 targetBuilder.setLayer3Match(layer3Match(sourceMatch));
104 targetBuilder.setLayer4Match(layer4Match(sourceMatch));
106 return targetBuilder.build();
112 private static Layer4Match layer4Match(final Match sourceMatch) {
113 MatchField nwProto = sourceMatch.getField(MatchType.NW_PROTO);
114 Short nwProtocolSource = null;
115 if (nwProto != null && nwProto.getValue() != null) {
116 nwProtocolSource = (short) ((byte) nwProto.getValue());
117 switch (nwProtocolSource) {
119 return Layer4MatchAsTcp(sourceMatch);
121 return Layer4MatchAsUdp(sourceMatch);
123 return Layer4MatchAsSctp(sourceMatch);
129 private static Layer4Match Layer4MatchAsSctp(final Match sourceMatch) {
130 SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
132 Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
133 Integer destinationPort = transportPort(sourceMatch,
136 if (sourcePort != null) {
137 sctpMatchBuilder.setSctpSourcePort(new PortNumber(sourcePort));
139 if (destinationPort != null) {
140 sctpMatchBuilder.setSctpDestinationPort(new PortNumber(
144 return sctpMatchBuilder.build();
147 private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
148 UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
150 Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
151 Integer destinationPort = transportPort(sourceMatch,
154 if (sourcePort != null) {
155 udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
158 if (destinationPort != null) {
159 udpMatchBuilder.setUdpDestinationPort(new PortNumber(
163 return udpMatchBuilder.build();
166 private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
167 TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
169 Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
170 Integer destinationPort = transportPort(sourceMatch,
173 if (sourcePort != null) {
174 tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
176 if (destinationPort != null) {
177 tcpMatchBuilder.setTcpDestinationPort(new PortNumber(
181 return tcpMatchBuilder.build();
184 private static Integer transportPort(final Match sourceMatch,
185 final MatchType matchType) {
186 MatchField transportPort = sourceMatch.getField(matchType);
187 if (transportPort != null && transportPort.getValue() != null
188 && transportPort.getValue().getClass().equals(Short.class)) {
189 return new Integer(NetUtils.getUnsignedShort((short) transportPort
195 private static VlanMatch vlanMatch(final Match sourceMatch) {
196 VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder();
198 MatchField vlan = sourceMatch.getField(MatchType.DL_VLAN);
199 if (vlan != null && vlan.getValue() != null) {
200 VlanIdBuilder vlanIDBuilder = new VlanIdBuilder();
201 vlanIDBuilder.setVlanId(new VlanId((int) (NetUtils
202 .getUnsignedShort((short) vlan.getValue()))));
203 vlanMatchBuild.setVlanId(vlanIDBuilder.build());
206 MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR);
207 if (vlanPriority != null && vlanPriority.getValue() != null) {
208 vlanMatchBuild.setVlanPcp(new VlanPcp((short) ((byte) vlanPriority
212 return vlanMatchBuild.build();
215 private static IpMatch ipMatch(final Match sourceMatch) {
216 IpMatchBuilder targetIpMatchBuild = new IpMatchBuilder();
217 MatchField networkTos = sourceMatch.getField(MatchType.NW_TOS);
218 if (networkTos != null && networkTos.getValue() != null) {
219 Dscp dscp = new Dscp(
220 (short) (NetUtils.getUnsignedByte((Byte) networkTos
222 targetIpMatchBuild.setIpDscp(dscp);
225 MatchField protocol = sourceMatch.getField(MatchType.NW_PROTO);
226 if (protocol != null && protocol.getValue() != null) {
227 targetIpMatchBuild.setIpProtocol((short) ((byte) protocol
231 return targetIpMatchBuild.build();
235 private static EthernetMatch ethernetMatch(final Match sourceMatch) {
236 final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder();
238 EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder()
239 .setAddress(ethernetSourceAddress(sourceMatch));
240 targetEthMatchBuild.setEthernetSource(ethSourBuild.build());
242 EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder()
243 .setAddress(ethernetDestAddress(sourceMatch));
244 targetEthMatchBuild.setEthernetDestination(ethDestBuild.build());
246 final MatchField dataLinkType = sourceMatch.getField(MatchType.DL_TYPE);
247 if (dataLinkType != null && dataLinkType.getValue() != null) {
248 EtherType etherType = new EtherType(new Long(
249 NetUtils.getUnsignedShort((Short) dataLinkType.getValue())));
250 EthernetTypeBuilder ethType = new EthernetTypeBuilder()
252 targetEthMatchBuild.setEthernetType(ethType.build());
254 return targetEthMatchBuild.build();
257 private static MacAddress ethernetSourceAddress(final Match sourceMatch) {
258 final MatchField dataLinkSource = sourceMatch.getField(DL_SRC);
259 if (dataLinkSource != null && dataLinkSource.getValue() != null) {
260 return MDFlowMapping.toMacAddress((byte[])dataLinkSource.getValue());
266 private static Layer3Match layer3Match(final Match sourceMatch) {
267 InetAddress inetSourceAddress = null;
268 MatchField netSource = sourceMatch.getField(MatchType.NW_SRC);
269 if (netSource != null && netSource.getValue() != null) {
270 inetSourceAddress = (InetAddress) (netSource.getValue());
273 InetAddress inetDestAddress = null;
274 MatchField netDest = sourceMatch.getField(MatchType.NW_DST);
275 if (netSource != null && netSource.getValue() != null) {
276 inetDestAddress = (InetAddress) (netDest.getValue());
279 if ((inetSourceAddress instanceof Inet4Address)
280 && (inetDestAddress instanceof Inet4Address)) {
281 MatchField dataLinkType = sourceMatch.getField(DL_TYPE);
283 if (dataLinkType != null && dataLinkType.getValue() != null) {
284 dLType = (Short) (dataLinkType.getValue());
286 if (dLType != null && dLType.equals(ETHERNET_ARP)) {
287 return setLayer3MatchAsArp(sourceMatch,
288 (Inet4Address) inetSourceAddress,
289 (Inet4Address) inetDestAddress);
291 return setLayer3MatchAsIpv4((Inet4Address) inetSourceAddress,
292 (Inet4Address) inetDestAddress);
294 } else if ((inetSourceAddress instanceof Inet6Address)
295 && (inetDestAddress instanceof Inet6Address)) {
296 return setLayer3MatchAsIpv6((Inet6Address) inetSourceAddress,
297 (Inet6Address) inetDestAddress);
304 private static Layer3Match setLayer3MatchAsArp(final Match sourceMatch,
305 final Inet4Address inetSourceAddress,
306 final Inet4Address inetDestAddress) {
307 String inetSourceAddressStr = InetAddresses
308 .toAddrString(inetSourceAddress);
309 Ipv4Prefix ipv4SourcePrefix = new Ipv4Prefix(inetSourceAddressStr);
311 String inetDestAddressValue = InetAddresses
312 .toAddrString(inetDestAddress);
313 Ipv4Prefix ipv4DestPrefix = new Ipv4Prefix(inetDestAddressValue);
315 ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
317 arpMatchBuilder.setArpSourceTransportAddress(ipv4SourcePrefix);
318 arpMatchBuilder.setArpTargetTransportAddress(ipv4DestPrefix);
320 ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
321 arpSourceHardwareAddressBuilder
322 .setAddress(ethernetSourceAddress(sourceMatch));
324 .setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder
327 ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
328 arpTargetHardwareAddressBuilder
329 .setAddress(ethernetDestAddress(sourceMatch));
331 .setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder
334 return arpMatchBuilder.build();
338 private static MacAddress ethernetDestAddress(final Match sourceMatch) {
339 final MatchField dataLinkDest = sourceMatch.getField(DL_DST);
340 if (dataLinkDest != null && dataLinkDest.getValue() != null) {
341 return MDFlowMapping.toMacAddress((byte[]) dataLinkDest.getValue());
346 private static Layer3Match setLayer3MatchAsIpv4(
347 final Inet4Address inetSourceAddress,
348 final Inet4Address inetDestAddress) {
349 String inetSrcAddressString = InetAddresses
350 .toAddrString(inetSourceAddress);
351 String inetDstAddressString = InetAddresses
352 .toAddrString(inetDestAddress);
354 Ipv4MatchBuilder layer4MatchBuild = new Ipv4MatchBuilder();
355 layer4MatchBuild.setIpv4Source(new Ipv4Prefix(inetSrcAddressString));
357 .setIpv4Destination(new Ipv4Prefix(inetDstAddressString));
358 return layer4MatchBuild.build();
362 private static Layer3Match setLayer3MatchAsIpv6(
363 final Inet6Address inetSourceAddress,
364 final Inet6Address inetDestAddress) {
365 String inetSrcAddressString = InetAddresses
366 .toAddrString(inetSourceAddress);
367 String inetDstAddressString = InetAddresses
368 .toAddrString(inetDestAddress);
369 Ipv6MatchBuilder layer6MatchBuild = new Ipv6MatchBuilder();
371 layer6MatchBuild.setIpv6Source(new Ipv6Prefix(inetSrcAddressString));
373 .setIpv6Destination(new Ipv6Prefix(inetDstAddressString));
374 return layer6MatchBuild.build();