1 package org.opendaylight.controller.sal.compatibility;
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.net.Inet4Address;
8 import java.net.Inet6Address;
9 import java.net.InetAddress;
12 import org.opendaylight.controller.sal.compatibility.MDFlowMapping;
13 import org.opendaylight.controller.sal.core.NodeConnector;
17 import org.opendaylight.controller.sal.match.Match;
18 import org.opendaylight.controller.sal.match.MatchField;
19 import org.opendaylight.controller.sal.match.MatchType;
20 import org.opendaylight.controller.sal.utils.NetUtils;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
25 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.GetNodeConnectorStatisticsInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetNodeConnectorStatisticsInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.Address;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4Builder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv6Builder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddressBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddressBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
57 import com.google.common.net.InetAddresses;
58 import static org.opendaylight.controller.sal.compatibility.NodeMapping.*;
59 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.*;
61 public class FromSalConversionsUtils {
63 private FromSalConversionsUtils() {
67 public static GetNodeConnectorStatisticsInput nodeConnectorStatistics(
68 NodeConnector connector) {
69 GetNodeConnectorStatisticsInputBuilder target = new GetNodeConnectorStatisticsInputBuilder();
71 NodeRef nodeRef = toNodeRef(connector.getNode());
72 target.setNode(nodeRef);
74 NodeConnectorRef nodeConnectorRef = toNodeConnectorRef(connector);
75 target.setNodeConnector(nodeConnectorRef);
77 return target.build();
80 private static Address addressFromAction(InetAddress inetAddress) {
81 String strInetAddresss = InetAddresses.toAddrString(inetAddress);
82 if (inetAddress instanceof Inet4Address) {
83 Ipv4Builder ipv4Builder = new Ipv4Builder();
84 ipv4Builder.setIpv4Address(new Ipv4Prefix(strInetAddresss));
85 return ipv4Builder.build();
86 } else if (inetAddress instanceof Inet6Address) {
87 Ipv6Builder ipv6Builder = new Ipv6Builder();
88 ipv6Builder.setIpv6Address(new Ipv6Prefix(strInetAddresss));
89 return ipv6Builder.build();
94 public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match toMatch(
96 if (sourceMatch != null) {
97 MatchBuilder targetBuilder = new MatchBuilder();
99 targetBuilder.setEthernetMatch(ethernetMatch(sourceMatch));
100 targetBuilder.setIpMatch(ipMatch(sourceMatch));
101 targetBuilder.setVlanMatch(vlanMatch(sourceMatch));
102 targetBuilder.setLayer3Match(layer3Match(sourceMatch));
103 targetBuilder.setLayer4Match(layer4Match(sourceMatch));
105 return targetBuilder.build();
111 private static Layer4Match layer4Match(final Match sourceMatch) {
112 MatchField nwProto = sourceMatch.getField(MatchType.NW_PROTO);
113 Short nwProtocolSource = null;
114 if (nwProto != null && nwProto.getValue() != null) {
115 nwProtocolSource = (short) ((byte) nwProto.getValue());
116 switch (nwProtocolSource) {
118 return Layer4MatchAsTcp(sourceMatch);
120 return Layer4MatchAsUdp(sourceMatch);
122 return Layer4MatchAsSctp(sourceMatch);
128 private static Layer4Match Layer4MatchAsSctp(final Match sourceMatch) {
129 SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
131 Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
132 Integer destinationPort = transportPort(sourceMatch,
135 if (sourcePort != null) {
136 sctpMatchBuilder.setSctpSourcePort(new PortNumber(sourcePort));
138 if (destinationPort != null) {
139 sctpMatchBuilder.setSctpDestinationPort(new PortNumber(
143 return sctpMatchBuilder.build();
146 private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
147 UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
149 Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
150 Integer destinationPort = transportPort(sourceMatch,
153 if (sourcePort != null) {
154 udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
157 if (destinationPort != null) {
158 udpMatchBuilder.setUdpDestinationPort(new PortNumber(
162 return udpMatchBuilder.build();
165 private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
166 TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
168 Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
169 Integer destinationPort = transportPort(sourceMatch,
172 if (sourcePort != null) {
173 tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
175 if (destinationPort != null) {
176 tcpMatchBuilder.setTcpDestinationPort(new PortNumber(
180 return tcpMatchBuilder.build();
183 private static Integer transportPort(final Match sourceMatch,
184 final MatchType matchType) {
185 MatchField transportPort = sourceMatch.getField(matchType);
186 if (transportPort != null && transportPort.getValue() != null
187 && transportPort.getValue().getClass().equals(Short.class)) {
188 return new Integer(NetUtils.getUnsignedShort((short) transportPort
194 private static VlanMatch vlanMatch(final Match sourceMatch) {
195 VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder();
197 MatchField vlan = sourceMatch.getField(MatchType.DL_VLAN);
198 if (vlan != null && vlan.getValue() != null) {
199 VlanIdBuilder vlanIDBuilder = new VlanIdBuilder();
200 vlanIDBuilder.setVlanId(new VlanId((int) (NetUtils
201 .getUnsignedShort((short) vlan.getValue()))));
202 vlanMatchBuild.setVlanId(vlanIDBuilder.build());
205 MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR);
206 if (vlanPriority != null && vlanPriority.getValue() != null) {
207 vlanMatchBuild.setVlanPcp(new VlanPcp((short) ((byte) vlanPriority
211 return vlanMatchBuild.build();
214 private static IpMatch ipMatch(final Match sourceMatch) {
215 IpMatchBuilder targetIpMatchBuild = new IpMatchBuilder();
216 MatchField networkTos = sourceMatch.getField(MatchType.NW_TOS);
217 if (networkTos != null && networkTos.getValue() != null) {
218 Dscp dscp = new Dscp(
219 (short) (NetUtils.getUnsignedByte((Byte) networkTos
221 targetIpMatchBuild.setIpDscp(dscp);
224 MatchField protocol = sourceMatch.getField(MatchType.NW_PROTO);
225 if (protocol != null && protocol.getValue() != null) {
226 targetIpMatchBuild.setIpProtocol((short) ((byte) protocol
230 return targetIpMatchBuild.build();
234 private static EthernetMatch ethernetMatch(final Match sourceMatch) {
235 final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder();
237 EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder()
238 .setAddress(ethernetSourceAddress(sourceMatch));
239 targetEthMatchBuild.setEthernetSource(ethSourBuild.build());
241 EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder()
242 .setAddress(ethernetDestAddress(sourceMatch));
243 targetEthMatchBuild.setEthernetDestination(ethDestBuild.build());
245 final MatchField dataLinkType = sourceMatch.getField(MatchType.DL_TYPE);
246 if (dataLinkType != null && dataLinkType.getValue() != null) {
247 EtherType etherType = new EtherType(new Long(
248 NetUtils.getUnsignedShort((Short) dataLinkType.getValue())));
249 EthernetTypeBuilder ethType = new EthernetTypeBuilder()
251 targetEthMatchBuild.setEthernetType(ethType.build());
253 return targetEthMatchBuild.build();
256 private static MacAddress ethernetSourceAddress(final Match sourceMatch) {
257 final MatchField dataLinkSource = sourceMatch.getField(DL_SRC);
258 if (dataLinkSource != null && dataLinkSource.getValue() != null) {
259 return MDFlowMapping.toMacAddress((byte[])dataLinkSource.getValue());
265 private static Layer3Match layer3Match(final Match sourceMatch) {
266 InetAddress inetSourceAddress = null;
267 MatchField netSource = sourceMatch.getField(MatchType.NW_SRC);
268 if (netSource != null && netSource.getValue() != null) {
269 inetSourceAddress = (InetAddress) (netSource.getValue());
272 InetAddress inetDestAddress = null;
273 MatchField netDest = sourceMatch.getField(MatchType.NW_DST);
274 if (netSource != null && netSource.getValue() != null) {
275 inetDestAddress = (InetAddress) (netDest.getValue());
278 if ((inetSourceAddress instanceof Inet4Address)
279 && (inetDestAddress instanceof Inet4Address)) {
280 MatchField dataLinkType = sourceMatch.getField(DL_TYPE);
282 if (dataLinkType != null && dataLinkType.getValue() != null) {
283 dLType = (Short) (dataLinkType.getValue());
285 if (dLType != null && dLType.equals(ETHERNET_ARP)) {
286 return setLayer3MatchAsArp(sourceMatch,
287 (Inet4Address) inetSourceAddress,
288 (Inet4Address) inetDestAddress);
290 return setLayer3MatchAsIpv4((Inet4Address) inetSourceAddress,
291 (Inet4Address) inetDestAddress);
293 } else if ((inetSourceAddress instanceof Inet6Address)
294 && (inetDestAddress instanceof Inet6Address)) {
295 return setLayer3MatchAsIpv6((Inet6Address) inetSourceAddress,
296 (Inet6Address) inetDestAddress);
303 private static Layer3Match setLayer3MatchAsArp(final Match sourceMatch,
304 final Inet4Address inetSourceAddress,
305 final Inet4Address inetDestAddress) {
306 String inetSourceAddressStr = InetAddresses
307 .toAddrString(inetSourceAddress);
308 Ipv4Prefix ipv4SourcePrefix = new Ipv4Prefix(inetSourceAddressStr);
310 String inetDestAddressValue = InetAddresses
311 .toAddrString(inetDestAddress);
312 Ipv4Prefix ipv4DestPrefix = new Ipv4Prefix(inetDestAddressValue);
314 ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
316 arpMatchBuilder.setArpSourceTransportAddress(ipv4SourcePrefix);
317 arpMatchBuilder.setArpTargetTransportAddress(ipv4DestPrefix);
319 ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
320 arpSourceHardwareAddressBuilder
321 .setAddress(ethernetSourceAddress(sourceMatch));
323 .setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder
326 ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
327 arpTargetHardwareAddressBuilder
328 .setAddress(ethernetDestAddress(sourceMatch));
330 .setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder
333 return arpMatchBuilder.build();
337 private static MacAddress ethernetDestAddress(final Match sourceMatch) {
338 final MatchField dataLinkDest = sourceMatch.getField(DL_DST);
339 if (dataLinkDest != null && dataLinkDest.getValue() != null) {
340 return MDFlowMapping.toMacAddress((byte[]) dataLinkDest.getValue());
345 private static Layer3Match setLayer3MatchAsIpv4(
346 final Inet4Address inetSourceAddress,
347 final Inet4Address inetDestAddress) {
348 String inetSrcAddressString = InetAddresses
349 .toAddrString(inetSourceAddress);
350 String inetDstAddressString = InetAddresses
351 .toAddrString(inetDestAddress);
353 Ipv4MatchBuilder layer4MatchBuild = new Ipv4MatchBuilder();
354 layer4MatchBuild.setIpv4Source(new Ipv4Prefix(inetSrcAddressString));
356 .setIpv4Destination(new Ipv4Prefix(inetDstAddressString));
357 return layer4MatchBuild.build();
361 private static Layer3Match setLayer3MatchAsIpv6(
362 final Inet6Address inetSourceAddress,
363 final Inet6Address inetDestAddress) {
364 String inetSrcAddressString = InetAddresses
365 .toAddrString(inetSourceAddress);
366 String inetDstAddressString = InetAddresses
367 .toAddrString(inetDestAddress);
368 Ipv6MatchBuilder layer6MatchBuild = new Ipv6MatchBuilder();
370 layer6MatchBuild.setIpv6Source(new Ipv6Prefix(inetSrcAddressString));
372 .setIpv6Destination(new Ipv6Prefix(inetDstAddressString));
373 return layer6MatchBuild.build();