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.core.NodeConnector;
16 import org.opendaylight.controller.sal.match.Match;
17 import org.opendaylight.controller.sal.match.MatchField;
18 import org.opendaylight.controller.sal.match.MatchType;
19 import org.opendaylight.controller.sal.utils.NetUtils;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.Address;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv6Builder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddressBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddressBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
55 import com.google.common.net.InetAddresses;
57 public class FromSalConversionsUtils {
59 private FromSalConversionsUtils() {
63 @SuppressWarnings("unused")
64 private static Address addressFromAction(InetAddress inetAddress) {
65 String strInetAddresss = InetAddresses.toAddrString(inetAddress);
66 if (inetAddress instanceof Inet4Address) {
67 Ipv4Builder ipv4Builder = new Ipv4Builder();
68 ipv4Builder.setIpv4Address(new Ipv4Prefix(strInetAddresss));
69 return ipv4Builder.build();
70 } else if (inetAddress instanceof Inet6Address) {
71 Ipv6Builder ipv6Builder = new Ipv6Builder();
72 ipv6Builder.setIpv6Address(new Ipv6Prefix(strInetAddresss));
73 return ipv6Builder.build();
78 public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match toMatch(
80 if (sourceMatch != null) {
81 MatchBuilder targetBuilder = new MatchBuilder();
83 targetBuilder.setEthernetMatch(ethernetMatch(sourceMatch));
84 targetBuilder.setIpMatch(ipMatch(sourceMatch));
85 targetBuilder.setVlanMatch(vlanMatch(sourceMatch));
86 targetBuilder.setLayer3Match(layer3Match(sourceMatch));
87 targetBuilder.setLayer4Match(layer4Match(sourceMatch));
88 targetBuilder.setInPort(inPortMatch(sourceMatch));
90 return targetBuilder.build();
96 private static NodeConnectorId inPortMatch(Match sourceMatch) {
97 MatchField inPort = sourceMatch.getField(MatchType.IN_PORT);
98 if(inPort != null && inPort.getValue() != null && (inPort.getValue() instanceof NodeConnector)) {
99 return new NodeConnectorId(((NodeConnector) inPort.getValue()).getNodeConnectorIdAsString());
104 private static Layer4Match layer4Match(final Match sourceMatch) {
105 MatchField nwProto = sourceMatch.getField(MatchType.NW_PROTO);
106 Short nwProtocolSource = null;
107 if (nwProto != null && nwProto.getValue() != null) {
108 nwProtocolSource = (short) ((byte) nwProto.getValue());
109 switch (nwProtocolSource) {
111 return Layer4MatchAsTcp(sourceMatch);
113 return Layer4MatchAsUdp(sourceMatch);
115 return Layer4MatchAsSctp(sourceMatch);
121 private static Layer4Match Layer4MatchAsSctp(final Match sourceMatch) {
122 SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
124 Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
125 Integer destinationPort = transportPort(sourceMatch,
128 if (sourcePort != null) {
129 sctpMatchBuilder.setSctpSourcePort(new PortNumber(sourcePort));
131 if (destinationPort != null) {
132 sctpMatchBuilder.setSctpDestinationPort(new PortNumber(
135 if(sourcePort != null || destinationPort != null) {
136 return sctpMatchBuilder.build();
141 private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
142 UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
144 Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
145 Integer destinationPort = transportPort(sourceMatch,
148 if (sourcePort != null) {
149 udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
152 if (destinationPort != null) {
153 udpMatchBuilder.setUdpDestinationPort(new PortNumber(
156 if(sourcePort != null || destinationPort != null) {
157 return udpMatchBuilder.build();
162 private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
163 TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
165 Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
166 Integer destinationPort = transportPort(sourceMatch,
169 if (sourcePort != null) {
170 tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
172 if (destinationPort != null) {
173 tcpMatchBuilder.setTcpDestinationPort(new PortNumber(
176 if(sourcePort != null || destinationPort != null) {
177 return tcpMatchBuilder.build();
182 private static Integer transportPort(final Match sourceMatch,
183 final MatchType matchType) {
184 MatchField transportPort = sourceMatch.getField(matchType);
185 if (transportPort != null && transportPort.getValue() != null
186 && transportPort.getValue().getClass().equals(Short.class)) {
187 return new Integer(NetUtils.getUnsignedShort((short) transportPort
193 private static VlanMatch vlanMatch(final Match sourceMatch) {
194 VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder();
196 MatchField vlan = sourceMatch.getField(MatchType.DL_VLAN);
197 if (vlan != null && vlan.getValue() != null) {
198 VlanIdBuilder vlanIDBuilder = new VlanIdBuilder();
199 vlanIDBuilder.setVlanId(new VlanId((NetUtils
200 .getUnsignedShort((short) vlan.getValue()))));
201 vlanMatchBuild.setVlanId(vlanIDBuilder.build());
204 MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR);
205 if (vlanPriority != null && vlanPriority.getValue() != null) {
206 vlanMatchBuild.setVlanPcp(new VlanPcp((short) ((byte) vlanPriority
209 if((vlan != null && vlan.getValue() != null) || (vlanPriority != null && vlanPriority.getValue() != null)) {
210 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
230 if((networkTos != null && networkTos.getValue() != null) || (protocol != null && protocol.getValue() != null)) {
231 return targetIpMatchBuild.build();
236 private static EthernetMatch ethernetMatch(final Match sourceMatch) {
237 final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder();
238 if(sourceMatch.getField(DL_SRC) != null && sourceMatch.getField(DL_SRC).getValue() != null) {
239 EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder()
240 .setAddress(ethernetSourceAddress(sourceMatch));
241 targetEthMatchBuild.setEthernetSource(ethSourBuild.build());
243 if(sourceMatch.getField(DL_DST) != null && sourceMatch.getField(DL_DST).getValue() != null) {
244 EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder()
245 .setAddress(ethernetDestAddress(sourceMatch));
246 targetEthMatchBuild.setEthernetDestination(ethDestBuild.build());
249 final MatchField dataLinkType = sourceMatch.getField(MatchType.DL_TYPE);
250 if (dataLinkType != null && dataLinkType.getValue() != null) {
251 EtherType etherType = new EtherType(new Long(
252 NetUtils.getUnsignedShort((Short) dataLinkType.getValue())));
253 EthernetTypeBuilder ethType = new EthernetTypeBuilder()
255 targetEthMatchBuild.setEthernetType(ethType.build());
257 if((sourceMatch.getField(DL_SRC) != null && sourceMatch.getField(DL_SRC).getValue() != null) ||
258 (sourceMatch.getField(DL_DST) != null && sourceMatch.getField(DL_DST).getValue() != null)||
259 dataLinkType != null ) {
260 return targetEthMatchBuild.build();
265 private static MacAddress ethernetSourceAddress(final Match sourceMatch) {
266 final MatchField dataLinkSource = sourceMatch.getField(DL_SRC);
267 if (dataLinkSource != null && dataLinkSource.getValue() != null) {
268 return MDFlowMapping.toMacAddress((byte[])dataLinkSource.getValue());
274 private static Layer3Match layer3Match(final Match sourceMatch) {
275 InetAddress inetSourceAddress = null;
276 MatchField netSource = sourceMatch.getField(MatchType.NW_SRC);
277 if (netSource != null && netSource.getValue() != null) {
278 inetSourceAddress = (InetAddress) (netSource.getValue());
281 InetAddress inetDestAddress = null;
282 MatchField netDest = sourceMatch.getField(MatchType.NW_DST);
283 if (netDest != null && netDest.getValue() != null) {
284 inetDestAddress = (InetAddress) (netDest.getValue());
287 if ((inetSourceAddress instanceof Inet4Address)
288 || (inetDestAddress instanceof Inet4Address)) {
289 MatchField dataLinkType = sourceMatch.getField(DL_TYPE);
291 if (dataLinkType != null && dataLinkType.getValue() != null) {
292 dLType = (Short) (dataLinkType.getValue());
294 if (dLType != null && dLType.equals(ETHERNET_ARP)) {
295 return setLayer3MatchAsArp(sourceMatch,
296 (Inet4Address) inetSourceAddress,
297 (Inet4Address) inetDestAddress);
299 return setLayer3MatchAsIpv4((Inet4Address) inetSourceAddress,
300 (Inet4Address) inetDestAddress);
302 } else if ((inetSourceAddress instanceof Inet6Address)
303 || (inetDestAddress instanceof Inet6Address)) {
304 return setLayer3MatchAsIpv6((Inet6Address) inetSourceAddress,
305 (Inet6Address) inetDestAddress);
312 private static Layer3Match setLayer3MatchAsArp(final Match sourceMatch,
313 final Inet4Address inetSourceAddress,
314 final Inet4Address inetDestAddress) {
315 String inetSourceAddressStr = InetAddresses
316 .toAddrString(inetSourceAddress);
317 Ipv4Prefix ipv4SourcePrefix = new Ipv4Prefix(inetSourceAddressStr);
319 String inetDestAddressValue = InetAddresses
320 .toAddrString(inetDestAddress);
321 Ipv4Prefix ipv4DestPrefix = new Ipv4Prefix(inetDestAddressValue);
323 ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
325 arpMatchBuilder.setArpSourceTransportAddress(ipv4SourcePrefix);
326 arpMatchBuilder.setArpTargetTransportAddress(ipv4DestPrefix);
328 ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
329 arpSourceHardwareAddressBuilder
330 .setAddress(ethernetSourceAddress(sourceMatch));
332 .setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder
335 ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
336 arpTargetHardwareAddressBuilder
337 .setAddress(ethernetDestAddress(sourceMatch));
339 .setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder
342 return arpMatchBuilder.build();
346 private static MacAddress ethernetDestAddress(final Match sourceMatch) {
347 final MatchField dataLinkDest = sourceMatch.getField(DL_DST);
348 if (dataLinkDest != null && dataLinkDest.getValue() != null) {
349 return MDFlowMapping.toMacAddress((byte[]) dataLinkDest.getValue());
354 private static Layer3Match setLayer3MatchAsIpv4(
355 final Inet4Address inetSourceAddress,
356 final Inet4Address inetDestAddress) {
357 Ipv4MatchBuilder layer4MatchBuild = new Ipv4MatchBuilder();
358 if(inetSourceAddress != null) {
359 String inetSrcAddressString = InetAddresses
360 .toAddrString(inetSourceAddress);
361 layer4MatchBuild.setIpv4Source(new Ipv4Prefix(inetSrcAddressString));
363 if(inetDestAddress != null) {
364 String inetDstAddressString = InetAddresses
365 .toAddrString(inetDestAddress);
367 .setIpv4Destination(new Ipv4Prefix(inetDstAddressString));
369 return layer4MatchBuild.build();
373 private static Layer3Match setLayer3MatchAsIpv6(
374 final Inet6Address inetSourceAddress,
375 final Inet6Address inetDestAddress) {
376 Ipv6MatchBuilder layer6MatchBuild = new Ipv6MatchBuilder();
377 if(inetSourceAddress != null) {
378 String inetSrcAddressString = InetAddresses
379 .toAddrString(inetSourceAddress);
380 layer6MatchBuild.setIpv6Source(new Ipv6Prefix(inetSrcAddressString));
382 if(inetDestAddress != null) {
383 String inetDstAddressString = InetAddresses
384 .toAddrString(inetDestAddress);
386 .setIpv6Destination(new Ipv6Prefix(inetDstAddressString));
388 return layer6MatchBuild.build();
391 public static boolean flowEquals(Flow statsFlow, Flow storedFlow) {
392 if (statsFlow.getClass() != storedFlow.getClass()) {
395 if (statsFlow.getBufferId()== null) {
396 if (storedFlow.getBufferId() != null) {
399 } else if(!statsFlow.getBufferId().equals(storedFlow.getBufferId())) {
402 if (statsFlow.getContainerName()== null) {
403 if (storedFlow.getContainerName()!= null) {
406 } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
409 if (statsFlow.getCookie()== null) {
410 if (storedFlow.getCookie()!= null) {
413 } else if(!statsFlow.getCookie().equals(storedFlow.getCookie())) {
416 if (statsFlow.getMatch()== null) {
417 if (storedFlow.getMatch() != null) {
420 } else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
423 if (statsFlow.getCookie()== null) {
424 if (storedFlow.getCookie()!= null) {
427 } else if(!statsFlow.getCookie().equals(storedFlow.getCookie())) {
430 if (statsFlow.getHardTimeout() == null) {
431 if (storedFlow.getHardTimeout() != null) {
434 } else if(!statsFlow.getHardTimeout().equals(storedFlow.getHardTimeout() )) {
437 if (statsFlow.getIdleTimeout()== null) {
438 if (storedFlow.getIdleTimeout() != null) {
441 } else if(!statsFlow.getIdleTimeout().equals(storedFlow.getIdleTimeout())) {
444 if (statsFlow.getPriority() == null) {
445 if (storedFlow.getPriority() != null) {
448 } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
451 if (statsFlow.getTableId() == null) {
452 if (storedFlow.getTableId() != null) {
455 } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {