Fixed two small FlowAdapter bugs.
[controller.git] / opendaylight / md-sal / compatibility / sal-compatibility / src / main / java / org / opendaylight / controller / sal / compatibility / FromSalConversionsUtils.java
1 package org.opendaylight.controller.sal.compatibility;
2
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;
10
11 import java.net.Inet4Address;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
14
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;
52
53 import com.google.common.net.InetAddresses;
54
55 public class FromSalConversionsUtils {
56
57     private FromSalConversionsUtils() {
58
59     }
60
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();
72         }
73         return null;
74     }
75
76     public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match toMatch(
77             Match sourceMatch) {
78         if (sourceMatch != null) {
79             MatchBuilder targetBuilder = new MatchBuilder();
80
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));
86
87             return targetBuilder.build();
88         }
89         return null;
90
91     }
92
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) {
99             case TCP:
100                 return Layer4MatchAsTcp(sourceMatch);
101             case UDP:
102                 return Layer4MatchAsUdp(sourceMatch);
103             case CRUDP:
104                 return Layer4MatchAsSctp(sourceMatch);
105             }
106         }
107         return null;
108     }
109
110     private static Layer4Match Layer4MatchAsSctp(final Match sourceMatch) {
111         SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
112
113         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
114         Integer destinationPort = transportPort(sourceMatch,
115                 MatchType.TP_DST);
116
117         if (sourcePort != null) {
118             sctpMatchBuilder.setSctpSourcePort(new PortNumber(sourcePort));
119         }
120         if (destinationPort != null) {
121             sctpMatchBuilder.setSctpDestinationPort(new PortNumber(
122                     destinationPort));
123         }
124
125         return sctpMatchBuilder.build();
126     }
127
128     private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
129         UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
130
131         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
132         Integer destinationPort = transportPort(sourceMatch,
133                 MatchType.TP_DST);
134
135         if (sourcePort != null) {
136             udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
137         }
138
139         if (destinationPort != null) {
140             udpMatchBuilder.setUdpDestinationPort(new PortNumber(
141                     destinationPort));
142         }
143
144         return udpMatchBuilder.build();
145     }
146
147     private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
148         TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
149
150         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
151         Integer destinationPort = transportPort(sourceMatch,
152                 MatchType.TP_DST);
153
154         if (sourcePort != null) {
155             tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
156         }
157         if (destinationPort != null) {
158             tcpMatchBuilder.setTcpDestinationPort(new PortNumber(
159                     destinationPort));
160         }
161
162         return tcpMatchBuilder.build();
163     }
164
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
171                     .getValue()));
172         }
173         return null;
174     }
175
176     private static VlanMatch vlanMatch(final Match sourceMatch) {
177         VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder();
178
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());
185         }
186
187         MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR);
188         if (vlanPriority != null && vlanPriority.getValue() != null) {
189             vlanMatchBuild.setVlanPcp(new VlanPcp((short) ((byte) vlanPriority
190                     .getValue())));
191         }
192
193         return vlanMatchBuild.build();
194     }
195
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
202                             .getValue())));
203             targetIpMatchBuild.setIpDscp(dscp);
204         }
205
206         MatchField protocol = sourceMatch.getField(MatchType.NW_PROTO);
207         if (protocol != null && protocol.getValue() != null) {
208             targetIpMatchBuild.setIpProtocol((short) ((byte) protocol
209                     .getValue()));
210         }
211
212         return targetIpMatchBuild.build();
213
214     }
215
216     private static EthernetMatch ethernetMatch(final Match sourceMatch) {
217         final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder();
218
219         EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder()
220                 .setAddress(ethernetSourceAddress(sourceMatch));
221         targetEthMatchBuild.setEthernetSource(ethSourBuild.build());
222
223         EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder()
224                 .setAddress(ethernetDestAddress(sourceMatch));
225         targetEthMatchBuild.setEthernetDestination(ethDestBuild.build());
226
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()
232                     .setType(etherType);
233             targetEthMatchBuild.setEthernetType(ethType.build());
234         }
235         return targetEthMatchBuild.build();
236     }
237
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());
242         }
243         return null;
244
245     }
246
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());
252         }
253
254         InetAddress inetDestAddress = null;
255         MatchField netDest = sourceMatch.getField(MatchType.NW_DST);
256         if (netDest != null && netDest.getValue() != null) {
257             inetDestAddress = (InetAddress) (netDest.getValue());
258         }
259
260         if ((inetSourceAddress instanceof Inet4Address)
261                 || (inetDestAddress instanceof Inet4Address)) {
262             MatchField dataLinkType = sourceMatch.getField(DL_TYPE);
263             Short dLType = null;
264             if (dataLinkType != null && dataLinkType.getValue() != null) {
265                 dLType = (Short) (dataLinkType.getValue());
266             }
267             if (dLType != null && dLType.equals(ETHERNET_ARP)) {
268                 return setLayer3MatchAsArp(sourceMatch,
269                         (Inet4Address) inetSourceAddress,
270                         (Inet4Address) inetDestAddress);
271             } else {
272                 return setLayer3MatchAsIpv4((Inet4Address) inetSourceAddress,
273                         (Inet4Address) inetDestAddress);
274             }
275         } else if ((inetSourceAddress instanceof Inet6Address)
276                 || (inetDestAddress instanceof Inet6Address)) {
277             return setLayer3MatchAsIpv6((Inet6Address) inetSourceAddress,
278                     (Inet6Address) inetDestAddress);
279         }
280
281         return null;
282
283     }
284
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);
291
292         String inetDestAddressValue = InetAddresses
293                 .toAddrString(inetDestAddress);
294         Ipv4Prefix ipv4DestPrefix = new Ipv4Prefix(inetDestAddressValue);
295
296         ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
297
298         arpMatchBuilder.setArpSourceTransportAddress(ipv4SourcePrefix);
299         arpMatchBuilder.setArpTargetTransportAddress(ipv4DestPrefix);
300
301         ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
302         arpSourceHardwareAddressBuilder
303                 .setAddress(ethernetSourceAddress(sourceMatch));
304         arpMatchBuilder
305                 .setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder
306                         .build());
307
308         ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
309         arpTargetHardwareAddressBuilder
310                 .setAddress(ethernetDestAddress(sourceMatch));
311         arpMatchBuilder
312                 .setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder
313                         .build());
314
315         return arpMatchBuilder.build();
316
317     }
318
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());
323         }
324         return null;
325     }
326
327     private static Layer3Match setLayer3MatchAsIpv4(
328             final Inet4Address inetSourceAddress,
329             final Inet4Address inetDestAddress) {
330         Ipv4MatchBuilder layer4MatchBuild = new Ipv4MatchBuilder();
331         if(inetSourceAddress != null) {
332             String inetSrcAddressString = InetAddresses
333                     .toAddrString(inetSourceAddress);
334             layer4MatchBuild.setIpv4Source(new Ipv4Prefix(inetSrcAddressString));
335         }
336         if(inetDestAddress != null) {
337             String inetDstAddressString = InetAddresses
338                     .toAddrString(inetDestAddress);
339             layer4MatchBuild
340             .setIpv4Destination(new Ipv4Prefix(inetDstAddressString));
341         }       
342         return layer4MatchBuild.build();
343
344     }
345
346     private static Layer3Match setLayer3MatchAsIpv6(
347             final Inet6Address inetSourceAddress,
348             final Inet6Address inetDestAddress) {
349         Ipv6MatchBuilder layer6MatchBuild = new Ipv6MatchBuilder();
350         if(inetSourceAddress != null) {
351             String inetSrcAddressString = InetAddresses
352                     .toAddrString(inetSourceAddress);
353             layer6MatchBuild.setIpv6Source(new Ipv6Prefix(inetSrcAddressString));
354         }
355         if(inetDestAddress != null) {
356             String inetDstAddressString = InetAddresses
357                     .toAddrString(inetDestAddress);    
358             layer6MatchBuild
359                     .setIpv6Destination(new Ipv6Prefix(inetDstAddressString));
360         }
361         return layer6MatchBuild.build();
362     }
363     
364     public static boolean flowEquals(Flow statsFlow, Flow storedFlow) {
365         if (statsFlow.getClass() != storedFlow.getClass()) {
366             return false;
367         }
368         if (statsFlow.getBufferId()== null) {
369             if (storedFlow.getBufferId() != null) {
370                 return false;
371             }
372         } else if(!statsFlow.getBufferId().equals(storedFlow.getBufferId())) {
373             return false;
374         }
375         if (statsFlow.getContainerName()== null) {
376             if (storedFlow.getContainerName()!= null) {
377                 return false;
378             }
379         } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
380             return false;
381         }
382         if (statsFlow.getCookie()== null) {
383             if (storedFlow.getCookie()!= null) {
384                 return false;
385             }
386         } else if(!statsFlow.getCookie().equals(storedFlow.getCookie())) {
387             return false;
388         }
389         if (statsFlow.getMatch()== null) {
390             if (storedFlow.getMatch() != null) {
391                 return false;
392             }
393         } else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
394             return false;
395         }
396         if (statsFlow.getCookie()== null) {
397             if (storedFlow.getCookie()!= null) {
398                 return false;
399             }
400         } else if(!statsFlow.getCookie().equals(storedFlow.getCookie())) {
401             return false;
402         }
403         if (statsFlow.getHardTimeout() == null) {
404             if (storedFlow.getHardTimeout() != null) {
405                 return false;
406             }
407         } else if(!statsFlow.getHardTimeout().equals(storedFlow.getHardTimeout() )) {
408             return false;
409         }
410         if (statsFlow.getIdleTimeout()== null) {
411             if (storedFlow.getIdleTimeout() != null) {
412                 return false;
413             }
414         } else if(!statsFlow.getIdleTimeout().equals(storedFlow.getIdleTimeout())) {
415             return false;
416         }
417         if (statsFlow.getPriority() == null) {
418             if (storedFlow.getPriority() != null) {
419                 return false;
420             }
421         } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
422             return false;
423         }
424         if (statsFlow.getTableId() == null) {
425             if (storedFlow.getTableId() != null) {
426                 return false;
427             }
428         } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
429             return false;
430         }
431         return true;
432     }
433
434
435 }