1 package org.opendaylight.controller.sal.compatibility;
3 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.CRUDP;
4 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.ETHERNET_ARP;
5 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.TCP;
6 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.UDP;
7 import static org.opendaylight.controller.sal.match.MatchType.DL_DST;
8 import static org.opendaylight.controller.sal.match.MatchType.DL_SRC;
9 import static org.opendaylight.controller.sal.match.MatchType.DL_TYPE;
11 import java.net.Inet4Address;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
15 import org.opendaylight.controller.sal.match.Match;
16 import org.opendaylight.controller.sal.match.MatchField;
17 import org.opendaylight.controller.sal.match.MatchType;
18 import org.opendaylight.controller.sal.utils.NetUtils;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
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.action.types.rev131112.address.Address;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4Builder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv6Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddressBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddressBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
53 import com.google.common.net.InetAddresses;
55 public class FromSalConversionsUtils {
57 private FromSalConversionsUtils() {
61 @SuppressWarnings("unused")
62 private static Address addressFromAction(InetAddress inetAddress) {
63 String strInetAddresss = InetAddresses.toAddrString(inetAddress);
64 if (inetAddress instanceof Inet4Address) {
65 Ipv4Builder ipv4Builder = new Ipv4Builder();
66 ipv4Builder.setIpv4Address(new Ipv4Prefix(strInetAddresss));
67 return ipv4Builder.build();
68 } else if (inetAddress instanceof Inet6Address) {
69 Ipv6Builder ipv6Builder = new Ipv6Builder();
70 ipv6Builder.setIpv6Address(new Ipv6Prefix(strInetAddresss));
71 return ipv6Builder.build();
76 public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match toMatch(
78 if (sourceMatch != null) {
79 MatchBuilder targetBuilder = new MatchBuilder();
81 targetBuilder.setEthernetMatch(ethernetMatch(sourceMatch));
82 targetBuilder.setIpMatch(ipMatch(sourceMatch));
83 targetBuilder.setVlanMatch(vlanMatch(sourceMatch));
84 targetBuilder.setLayer3Match(layer3Match(sourceMatch));
85 targetBuilder.setLayer4Match(layer4Match(sourceMatch));
87 return targetBuilder.build();
93 private static Layer4Match layer4Match(final Match sourceMatch) {
94 MatchField nwProto = sourceMatch.getField(MatchType.NW_PROTO);
95 Short nwProtocolSource = null;
96 if (nwProto != null && nwProto.getValue() != null) {
97 nwProtocolSource = (short) ((byte) nwProto.getValue());
98 switch (nwProtocolSource) {
100 return Layer4MatchAsTcp(sourceMatch);
102 return Layer4MatchAsUdp(sourceMatch);
104 return Layer4MatchAsSctp(sourceMatch);
110 private static Layer4Match Layer4MatchAsSctp(final Match sourceMatch) {
111 SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
113 Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
114 Integer destinationPort = transportPort(sourceMatch,
117 if (sourcePort != null) {
118 sctpMatchBuilder.setSctpSourcePort(new PortNumber(sourcePort));
120 if (destinationPort != null) {
121 sctpMatchBuilder.setSctpDestinationPort(new PortNumber(
125 return sctpMatchBuilder.build();
128 private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
129 UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
131 Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
132 Integer destinationPort = transportPort(sourceMatch,
135 if (sourcePort != null) {
136 udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
139 if (destinationPort != null) {
140 udpMatchBuilder.setUdpDestinationPort(new PortNumber(
144 return udpMatchBuilder.build();
147 private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
148 TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
150 Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
151 Integer destinationPort = transportPort(sourceMatch,
154 if (sourcePort != null) {
155 tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
157 if (destinationPort != null) {
158 tcpMatchBuilder.setTcpDestinationPort(new PortNumber(
162 return tcpMatchBuilder.build();
165 private static Integer transportPort(final Match sourceMatch,
166 final MatchType matchType) {
167 MatchField transportPort = sourceMatch.getField(matchType);
168 if (transportPort != null && transportPort.getValue() != null
169 && transportPort.getValue().getClass().equals(Short.class)) {
170 return new Integer(NetUtils.getUnsignedShort((short) transportPort
176 private static VlanMatch vlanMatch(final Match sourceMatch) {
177 VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder();
179 MatchField vlan = sourceMatch.getField(MatchType.DL_VLAN);
180 if (vlan != null && vlan.getValue() != null) {
181 VlanIdBuilder vlanIDBuilder = new VlanIdBuilder();
182 vlanIDBuilder.setVlanId(new VlanId((NetUtils
183 .getUnsignedShort((short) vlan.getValue()))));
184 vlanMatchBuild.setVlanId(vlanIDBuilder.build());
187 MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR);
188 if (vlanPriority != null && vlanPriority.getValue() != null) {
189 vlanMatchBuild.setVlanPcp(new VlanPcp((short) ((byte) vlanPriority
193 return vlanMatchBuild.build();
196 private static IpMatch ipMatch(final Match sourceMatch) {
197 IpMatchBuilder targetIpMatchBuild = new IpMatchBuilder();
198 MatchField networkTos = sourceMatch.getField(MatchType.NW_TOS);
199 if (networkTos != null && networkTos.getValue() != null) {
200 Dscp dscp = new Dscp(
201 (short) (NetUtils.getUnsignedByte((Byte) networkTos
203 targetIpMatchBuild.setIpDscp(dscp);
206 MatchField protocol = sourceMatch.getField(MatchType.NW_PROTO);
207 if (protocol != null && protocol.getValue() != null) {
208 targetIpMatchBuild.setIpProtocol((short) ((byte) protocol
212 return targetIpMatchBuild.build();
216 private static EthernetMatch ethernetMatch(final Match sourceMatch) {
217 final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder();
219 EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder()
220 .setAddress(ethernetSourceAddress(sourceMatch));
221 targetEthMatchBuild.setEthernetSource(ethSourBuild.build());
223 EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder()
224 .setAddress(ethernetDestAddress(sourceMatch));
225 targetEthMatchBuild.setEthernetDestination(ethDestBuild.build());
227 final MatchField dataLinkType = sourceMatch.getField(MatchType.DL_TYPE);
228 if (dataLinkType != null && dataLinkType.getValue() != null) {
229 EtherType etherType = new EtherType(new Long(
230 NetUtils.getUnsignedShort((Short) dataLinkType.getValue())));
231 EthernetTypeBuilder ethType = new EthernetTypeBuilder()
233 targetEthMatchBuild.setEthernetType(ethType.build());
235 return targetEthMatchBuild.build();
238 private static MacAddress ethernetSourceAddress(final Match sourceMatch) {
239 final MatchField dataLinkSource = sourceMatch.getField(DL_SRC);
240 if (dataLinkSource != null && dataLinkSource.getValue() != null) {
241 return MDFlowMapping.toMacAddress((byte[])dataLinkSource.getValue());
247 private static Layer3Match layer3Match(final Match sourceMatch) {
248 InetAddress inetSourceAddress = null;
249 MatchField netSource = sourceMatch.getField(MatchType.NW_SRC);
250 if (netSource != null && netSource.getValue() != null) {
251 inetSourceAddress = (InetAddress) (netSource.getValue());
254 InetAddress inetDestAddress = null;
255 MatchField netDest = sourceMatch.getField(MatchType.NW_DST);
256 if (netDest != null && netDest.getValue() != null) {
257 inetDestAddress = (InetAddress) (netDest.getValue());
260 if ((inetSourceAddress instanceof Inet4Address)
261 && (inetDestAddress instanceof Inet4Address)) {
262 MatchField dataLinkType = sourceMatch.getField(DL_TYPE);
264 if (dataLinkType != null && dataLinkType.getValue() != null) {
265 dLType = (Short) (dataLinkType.getValue());
267 if (dLType != null && dLType.equals(ETHERNET_ARP)) {
268 return setLayer3MatchAsArp(sourceMatch,
269 (Inet4Address) inetSourceAddress,
270 (Inet4Address) inetDestAddress);
272 return setLayer3MatchAsIpv4((Inet4Address) inetSourceAddress,
273 (Inet4Address) inetDestAddress);
275 } else if ((inetSourceAddress instanceof Inet6Address)
276 && (inetDestAddress instanceof Inet6Address)) {
277 return setLayer3MatchAsIpv6((Inet6Address) inetSourceAddress,
278 (Inet6Address) inetDestAddress);
285 private static Layer3Match setLayer3MatchAsArp(final Match sourceMatch,
286 final Inet4Address inetSourceAddress,
287 final Inet4Address inetDestAddress) {
288 String inetSourceAddressStr = InetAddresses
289 .toAddrString(inetSourceAddress);
290 Ipv4Prefix ipv4SourcePrefix = new Ipv4Prefix(inetSourceAddressStr);
292 String inetDestAddressValue = InetAddresses
293 .toAddrString(inetDestAddress);
294 Ipv4Prefix ipv4DestPrefix = new Ipv4Prefix(inetDestAddressValue);
296 ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
298 arpMatchBuilder.setArpSourceTransportAddress(ipv4SourcePrefix);
299 arpMatchBuilder.setArpTargetTransportAddress(ipv4DestPrefix);
301 ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
302 arpSourceHardwareAddressBuilder
303 .setAddress(ethernetSourceAddress(sourceMatch));
305 .setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder
308 ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
309 arpTargetHardwareAddressBuilder
310 .setAddress(ethernetDestAddress(sourceMatch));
312 .setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder
315 return arpMatchBuilder.build();
319 private static MacAddress ethernetDestAddress(final Match sourceMatch) {
320 final MatchField dataLinkDest = sourceMatch.getField(DL_DST);
321 if (dataLinkDest != null && dataLinkDest.getValue() != null) {
322 return MDFlowMapping.toMacAddress((byte[]) dataLinkDest.getValue());
327 private static Layer3Match setLayer3MatchAsIpv4(
328 final Inet4Address inetSourceAddress,
329 final Inet4Address inetDestAddress) {
330 String inetSrcAddressString = InetAddresses
331 .toAddrString(inetSourceAddress);
332 String inetDstAddressString = InetAddresses
333 .toAddrString(inetDestAddress);
335 Ipv4MatchBuilder layer4MatchBuild = new Ipv4MatchBuilder();
336 layer4MatchBuild.setIpv4Source(new Ipv4Prefix(inetSrcAddressString));
338 .setIpv4Destination(new Ipv4Prefix(inetDstAddressString));
339 return layer4MatchBuild.build();
343 private static Layer3Match setLayer3MatchAsIpv6(
344 final Inet6Address inetSourceAddress,
345 final Inet6Address inetDestAddress) {
346 String inetSrcAddressString = InetAddresses
347 .toAddrString(inetSourceAddress);
348 String inetDstAddressString = InetAddresses
349 .toAddrString(inetDestAddress);
350 Ipv6MatchBuilder layer6MatchBuild = new Ipv6MatchBuilder();
352 layer6MatchBuild.setIpv6Source(new Ipv6Prefix(inetSrcAddressString));
354 .setIpv6Destination(new Ipv6Prefix(inetDstAddressString));
355 return layer6MatchBuild.build();
358 public static boolean flowEquals(Flow statsFlow, Flow storedFlow) {
359 if (statsFlow.getClass() != storedFlow.getClass()) {
362 if (statsFlow.getBufferId()== null) {
363 if (storedFlow.getBufferId() != null) {
366 } else if(!statsFlow.getBufferId().equals(storedFlow.getBufferId())) {
369 if (statsFlow.getContainerName()== null) {
370 if (storedFlow.getContainerName()!= null) {
373 } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
376 if (statsFlow.getCookie()== null) {
377 if (storedFlow.getCookie()!= null) {
380 } else if(!statsFlow.getCookie().equals(storedFlow.getCookie())) {
383 if (statsFlow.getMatch()== null) {
384 if (storedFlow.getMatch() != null) {
387 } else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
390 if (statsFlow.getCookie()== null) {
391 if (storedFlow.getCookie()!= null) {
394 } else if(!statsFlow.getCookie().equals(storedFlow.getCookie())) {
397 if (statsFlow.getHardTimeout() == null) {
398 if (storedFlow.getHardTimeout() != null) {
401 } else if(!statsFlow.getHardTimeout().equals(storedFlow.getHardTimeout() )) {
404 if (statsFlow.getIdleTimeout()== null) {
405 if (storedFlow.getIdleTimeout() != null) {
408 } else if(!statsFlow.getIdleTimeout().equals(storedFlow.getIdleTimeout())) {
411 if (statsFlow.getPriority() == null) {
412 if (storedFlow.getPriority() != null) {
415 } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
418 if (statsFlow.getTableId() == null) {
419 if (storedFlow.getTableId() != null) {
422 } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {