Merge "Bug 1306: Fixed incorrect AD/MD-SAL flow conversion."
[controller.git] / opendaylight / md-sal / compatibility / sal-compatibility / src / main / java / org / opendaylight / controller / sal / compatibility / FromSalConversionsUtils.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sal.compatibility;
9
10 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.CRUDP;
11 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.ETHERNET_ARP;
12 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.TCP;
13 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.UDP;
14 import static org.opendaylight.controller.sal.match.MatchType.DL_DST;
15 import static org.opendaylight.controller.sal.match.MatchType.DL_SRC;
16 import static org.opendaylight.controller.sal.match.MatchType.DL_TYPE;
17
18 import java.net.Inet4Address;
19 import java.net.Inet6Address;
20 import java.net.InetAddress;
21
22 import org.opendaylight.controller.sal.core.NodeConnector;
23 import org.opendaylight.controller.sal.match.Match;
24 import org.opendaylight.controller.sal.match.MatchField;
25 import org.opendaylight.controller.sal.match.MatchType;
26 import org.opendaylight.controller.sal.utils.NetUtils;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.Address;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4Builder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv6Builder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddressBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddressBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
61
62 import com.google.common.net.InetAddresses;
63
64 public class FromSalConversionsUtils {
65
66     private FromSalConversionsUtils() {
67
68     }
69
70     @SuppressWarnings("unused")
71     private static Address addressFromAction(InetAddress inetAddress) {
72         String strInetAddresss = InetAddresses.toAddrString(inetAddress);
73         if (inetAddress instanceof Inet4Address) {
74             Ipv4Builder ipv4Builder = new Ipv4Builder();
75             ipv4Builder.setIpv4Address(new Ipv4Prefix(strInetAddresss));
76             return ipv4Builder.build();
77         } else if (inetAddress instanceof Inet6Address) {
78             Ipv6Builder ipv6Builder = new Ipv6Builder();
79             ipv6Builder.setIpv6Address(new Ipv6Prefix(strInetAddresss));
80             return ipv6Builder.build();
81         }
82         return null;
83     }
84
85     public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match toMatch(
86             Match sourceMatch) {
87         if (sourceMatch != null) {
88             MatchBuilder targetBuilder = new MatchBuilder();
89
90             targetBuilder.setEthernetMatch(ethernetMatch(sourceMatch));
91             targetBuilder.setIpMatch(ipMatch(sourceMatch));
92             targetBuilder.setVlanMatch(vlanMatch(sourceMatch));
93             targetBuilder.setLayer3Match(layer3Match(sourceMatch));
94             targetBuilder.setLayer4Match(layer4Match(sourceMatch));
95             targetBuilder.setInPort(inPortMatch(sourceMatch));
96
97             return targetBuilder.build();
98         }
99         return null;
100
101     }
102
103     private static NodeConnectorId inPortMatch(Match sourceMatch) {
104         MatchField inPort = sourceMatch.getField(MatchType.IN_PORT);
105         if(inPort != null && inPort.getValue() != null && (inPort.getValue() instanceof NodeConnector)) {
106             NodeConnector port = (NodeConnector)inPort.getValue();
107             return (NodeConnectorId)MDFlowMapping.toUri(port);
108         }
109         return null;
110     }
111
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) {
118             case TCP:
119                 return Layer4MatchAsTcp(sourceMatch);
120             case UDP:
121                 return Layer4MatchAsUdp(sourceMatch);
122             case CRUDP:
123                 return Layer4MatchAsSctp(sourceMatch);
124             }
125         }
126         return null;
127     }
128
129     private static Layer4Match Layer4MatchAsSctp(final Match sourceMatch) {
130         SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
131
132         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
133         Integer destinationPort = transportPort(sourceMatch,
134                 MatchType.TP_DST);
135
136         if (sourcePort != null) {
137             sctpMatchBuilder.setSctpSourcePort(new PortNumber(sourcePort));
138         }
139         if (destinationPort != null) {
140             sctpMatchBuilder.setSctpDestinationPort(new PortNumber(
141                     destinationPort));
142         }
143         if(sourcePort != null || destinationPort != null) {
144             return sctpMatchBuilder.build();
145         }
146         return null;
147     }
148
149     private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
150         UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
151
152         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
153         Integer destinationPort = transportPort(sourceMatch,
154                 MatchType.TP_DST);
155
156         if (sourcePort != null) {
157             udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
158         }
159
160         if (destinationPort != null) {
161             udpMatchBuilder.setUdpDestinationPort(new PortNumber(
162                     destinationPort));
163         }
164         if(sourcePort != null || destinationPort != null) {
165             return udpMatchBuilder.build();
166         }
167         return null;
168     }
169
170     private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
171         TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
172
173         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
174         Integer destinationPort = transportPort(sourceMatch,
175                 MatchType.TP_DST);
176
177         if (sourcePort != null) {
178             tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
179         }
180         if (destinationPort != null) {
181             tcpMatchBuilder.setTcpDestinationPort(new PortNumber(
182                     destinationPort));
183         }
184         if(sourcePort != null || destinationPort != null) {
185             return tcpMatchBuilder.build();
186         }
187         return null;
188     }
189
190     private static Integer transportPort(final Match sourceMatch,
191             final MatchType matchType) {
192         MatchField transportPort = sourceMatch.getField(matchType);
193         if (transportPort != null && transportPort.getValue() != null
194                 && transportPort.getValue().getClass().equals(Short.class)) {
195             return new Integer(NetUtils.getUnsignedShort((short) transportPort
196                     .getValue()));
197         }
198         return null;
199     }
200
201     private static VlanMatch vlanMatch(final Match sourceMatch) {
202         VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder();
203
204         MatchField vlan = sourceMatch.getField(MatchType.DL_VLAN);
205         if (vlan != null && vlan.getValue() != null) {
206             VlanIdBuilder vlanIDBuilder = new VlanIdBuilder();
207             short vid = (short)vlan.getValue();
208             boolean present = (vid != MatchType.DL_VLAN_NONE);
209             vlanIDBuilder.setVlanId(new VlanId((NetUtils
210                     .getUnsignedShort(vid))));
211             vlanIDBuilder.setVlanIdPresent(present);
212             vlanMatchBuild.setVlanId(vlanIDBuilder.build());
213         }
214
215         MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR);
216         if (vlanPriority != null && vlanPriority.getValue() != null) {
217             vlanMatchBuild.setVlanPcp(new VlanPcp((short) ((byte) vlanPriority
218                     .getValue())));
219         }
220         if((vlan != null && vlan.getValue() != null) || (vlanPriority != null && vlanPriority.getValue() != null)) {
221             return vlanMatchBuild.build();
222         }
223         return null;
224     }
225
226     private static IpMatch ipMatch(final Match sourceMatch) {
227         IpMatchBuilder targetIpMatchBuild = new IpMatchBuilder();
228         MatchField networkTos = sourceMatch.getField(MatchType.NW_TOS);
229         if (networkTos != null && networkTos.getValue() != null) {
230             Dscp dscp = new Dscp(
231                     (short) (NetUtils.getUnsignedByte((Byte) networkTos
232                             .getValue())));
233             targetIpMatchBuild.setIpDscp(dscp);
234         }
235
236         MatchField protocol = sourceMatch.getField(MatchType.NW_PROTO);
237         if (protocol != null && protocol.getValue() != null) {
238             targetIpMatchBuild.setIpProtocol((short) ((byte) protocol
239                     .getValue()));
240         }
241         if((networkTos != null && networkTos.getValue() != null) || (protocol != null && protocol.getValue() != null)) {
242             return targetIpMatchBuild.build();
243         }
244         return null;
245     }
246
247     private static EthernetMatch ethernetMatch(final Match sourceMatch) {
248         final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder();
249         if(sourceMatch.getField(DL_SRC) != null && sourceMatch.getField(DL_SRC).getValue() != null) {
250             EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder()
251                     .setAddress(ethernetSourceAddress(sourceMatch));
252             targetEthMatchBuild.setEthernetSource(ethSourBuild.build());
253         }
254         if(sourceMatch.getField(DL_DST) != null && sourceMatch.getField(DL_DST).getValue() != null) {
255             EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder()
256                     .setAddress(ethernetDestAddress(sourceMatch));
257             targetEthMatchBuild.setEthernetDestination(ethDestBuild.build());
258         }
259
260         final MatchField dataLinkType = sourceMatch.getField(MatchType.DL_TYPE);
261         if (dataLinkType != null && dataLinkType.getValue() != null) {
262             EtherType etherType = new EtherType(new Long(
263                     NetUtils.getUnsignedShort((Short) dataLinkType.getValue())));
264             EthernetTypeBuilder ethType = new EthernetTypeBuilder()
265                     .setType(etherType);
266             targetEthMatchBuild.setEthernetType(ethType.build());
267         }
268         if((sourceMatch.getField(DL_SRC) != null && sourceMatch.getField(DL_SRC).getValue() != null) ||
269                 (sourceMatch.getField(DL_DST) != null && sourceMatch.getField(DL_DST).getValue() != null)||
270                 dataLinkType != null ) {
271             return targetEthMatchBuild.build();
272         }
273         return null;
274     }
275
276     private static MacAddress ethernetSourceAddress(final Match sourceMatch) {
277         final MatchField dataLinkSource = sourceMatch.getField(DL_SRC);
278         if (dataLinkSource != null && dataLinkSource.getValue() != null) {
279             return MDFlowMapping.toMacAddress((byte[])dataLinkSource.getValue());
280         }
281         return null;
282
283     }
284
285     private static Layer3Match layer3Match(final Match sourceMatch) {
286         InetAddress inetSourceAddress = null;
287         MatchField netSource = sourceMatch.getField(MatchType.NW_SRC);
288         if (netSource != null && netSource.getValue() != null) {
289             inetSourceAddress = (InetAddress) (netSource.getValue());
290         }
291
292         InetAddress inetDestAddress = null;
293         MatchField netDest = sourceMatch.getField(MatchType.NW_DST);
294         if (netDest != null && netDest.getValue() != null) {
295             inetDestAddress = (InetAddress) (netDest.getValue());
296         }
297
298         if ((inetSourceAddress instanceof Inet4Address)
299                 || (inetDestAddress instanceof Inet4Address)) {
300             MatchField dataLinkType = sourceMatch.getField(DL_TYPE);
301             Short dLType = null;
302             if (dataLinkType != null && dataLinkType.getValue() != null) {
303                 dLType = (Short) (dataLinkType.getValue());
304             }
305             if (dLType != null && dLType.equals(ETHERNET_ARP)) {
306                 return setLayer3MatchAsArp(sourceMatch,
307                         (Inet4Address) inetSourceAddress,
308                         (Inet4Address) inetDestAddress);
309             } else {
310                 return setLayer3MatchAsIpv4((Inet4Address) inetSourceAddress,
311                         (Inet4Address) inetDestAddress);
312             }
313         } else if ((inetSourceAddress instanceof Inet6Address)
314                 || (inetDestAddress instanceof Inet6Address)) {
315             return setLayer3MatchAsIpv6((Inet6Address) inetSourceAddress,
316                     (Inet6Address) inetDestAddress);
317         }
318
319         return null;
320
321     }
322
323     private static Layer3Match setLayer3MatchAsArp(final Match sourceMatch,
324             final Inet4Address inetSourceAddress,
325             final Inet4Address inetDestAddress) {
326         String inetSourceAddressStr = InetAddresses
327                 .toAddrString(inetSourceAddress);
328         Ipv4Prefix ipv4SourcePrefix = new Ipv4Prefix(inetSourceAddressStr);
329
330         String inetDestAddressValue = InetAddresses
331                 .toAddrString(inetDestAddress);
332         Ipv4Prefix ipv4DestPrefix = new Ipv4Prefix(inetDestAddressValue);
333
334         ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
335
336         arpMatchBuilder.setArpSourceTransportAddress(ipv4SourcePrefix);
337         arpMatchBuilder.setArpTargetTransportAddress(ipv4DestPrefix);
338
339         ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
340         arpSourceHardwareAddressBuilder
341                 .setAddress(ethernetSourceAddress(sourceMatch));
342         arpMatchBuilder
343                 .setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder
344                         .build());
345
346         ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
347         arpTargetHardwareAddressBuilder
348                 .setAddress(ethernetDestAddress(sourceMatch));
349         arpMatchBuilder
350                 .setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder
351                         .build());
352
353         return arpMatchBuilder.build();
354
355     }
356
357     private static MacAddress ethernetDestAddress(final Match sourceMatch) {
358         final MatchField dataLinkDest = sourceMatch.getField(DL_DST);
359         if (dataLinkDest != null && dataLinkDest.getValue() != null) {
360             return MDFlowMapping.toMacAddress((byte[]) dataLinkDest.getValue());
361         }
362         return null;
363     }
364
365     private static Layer3Match setLayer3MatchAsIpv4(
366             final Inet4Address inetSourceAddress,
367             final Inet4Address inetDestAddress) {
368         Ipv4MatchBuilder layer4MatchBuild = new Ipv4MatchBuilder();
369         if(inetSourceAddress != null) {
370             String inetSrcAddressString = InetAddresses
371                     .toAddrString(inetSourceAddress);
372             layer4MatchBuild.setIpv4Source(new Ipv4Prefix(inetSrcAddressString));
373         }
374         if(inetDestAddress != null) {
375             String inetDstAddressString = InetAddresses
376                     .toAddrString(inetDestAddress);
377             layer4MatchBuild
378             .setIpv4Destination(new Ipv4Prefix(inetDstAddressString));
379         }
380         return layer4MatchBuild.build();
381
382     }
383
384     private static Layer3Match setLayer3MatchAsIpv6(
385             final Inet6Address inetSourceAddress,
386             final Inet6Address inetDestAddress) {
387         Ipv6MatchBuilder layer6MatchBuild = new Ipv6MatchBuilder();
388         if(inetSourceAddress != null) {
389             String inetSrcAddressString = InetAddresses
390                     .toAddrString(inetSourceAddress);
391             layer6MatchBuild.setIpv6Source(new Ipv6Prefix(inetSrcAddressString));
392         }
393         if(inetDestAddress != null) {
394             String inetDstAddressString = InetAddresses
395                     .toAddrString(inetDestAddress);
396             layer6MatchBuild
397                     .setIpv6Destination(new Ipv6Prefix(inetDstAddressString));
398         }
399         return layer6MatchBuild.build();
400     }
401
402     public static boolean flowEquals(Flow statsFlow, Flow storedFlow) {
403         if (statsFlow.getClass() != storedFlow.getClass()) {
404             return false;
405         }
406         if (statsFlow.getBufferId()== null) {
407             if (storedFlow.getBufferId() != null) {
408                 return false;
409             }
410         } else if(!statsFlow.getBufferId().equals(storedFlow.getBufferId())) {
411             return false;
412         }
413         if (statsFlow.getContainerName()== null) {
414             if (storedFlow.getContainerName()!= null) {
415                 return false;
416             }
417         } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
418             return false;
419         }
420         if (statsFlow.getCookie()== null) {
421             if (storedFlow.getCookie()!= null) {
422                 return false;
423             }
424         } else if(!statsFlow.getCookie().equals(storedFlow.getCookie())) {
425             return false;
426         }
427         if (statsFlow.getMatch()== null) {
428             if (storedFlow.getMatch() != null) {
429                 return false;
430             }
431         } else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
432             return false;
433         }
434         if (statsFlow.getCookie()== null) {
435             if (storedFlow.getCookie()!= null) {
436                 return false;
437             }
438         } else if(!statsFlow.getCookie().equals(storedFlow.getCookie())) {
439             return false;
440         }
441         if (statsFlow.getHardTimeout() == null) {
442             if (storedFlow.getHardTimeout() != null) {
443                 return false;
444             }
445         } else if(!statsFlow.getHardTimeout().equals(storedFlow.getHardTimeout() )) {
446             return false;
447         }
448         if (statsFlow.getIdleTimeout()== null) {
449             if (storedFlow.getIdleTimeout() != null) {
450                 return false;
451             }
452         } else if(!statsFlow.getIdleTimeout().equals(storedFlow.getIdleTimeout())) {
453             return false;
454         }
455         if (statsFlow.getPriority() == null) {
456             if (storedFlow.getPriority() != null) {
457                 return false;
458             }
459         } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
460             return false;
461         }
462         if (statsFlow.getTableId() == null) {
463             if (storedFlow.getTableId() != null) {
464                 return false;
465             }
466         } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
467             return false;
468         }
469         return true;
470     }
471
472
473 }