36045cabf4bfefc672d3edde8001a75deba46385
[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             return new NodeConnectorId(((NodeConnector) inPort.getValue()).getNodeConnectorIdAsString());
107         }
108         return null;
109     }
110
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) {
117             case TCP:
118                 return Layer4MatchAsTcp(sourceMatch);
119             case UDP:
120                 return Layer4MatchAsUdp(sourceMatch);
121             case CRUDP:
122                 return Layer4MatchAsSctp(sourceMatch);
123             }
124         }
125         return null;
126     }
127
128     private static Layer4Match Layer4MatchAsSctp(final Match sourceMatch) {
129         SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
130
131         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
132         Integer destinationPort = transportPort(sourceMatch,
133                 MatchType.TP_DST);
134
135         if (sourcePort != null) {
136             sctpMatchBuilder.setSctpSourcePort(new PortNumber(sourcePort));
137         }
138         if (destinationPort != null) {
139             sctpMatchBuilder.setSctpDestinationPort(new PortNumber(
140                     destinationPort));
141         }
142         if(sourcePort != null || destinationPort != null) {
143             return sctpMatchBuilder.build();
144         }
145         return null;
146     }
147
148     private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
149         UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
150
151         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
152         Integer destinationPort = transportPort(sourceMatch,
153                 MatchType.TP_DST);
154
155         if (sourcePort != null) {
156             udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
157         }
158
159         if (destinationPort != null) {
160             udpMatchBuilder.setUdpDestinationPort(new PortNumber(
161                     destinationPort));
162         }
163         if(sourcePort != null || destinationPort != null) {
164             return udpMatchBuilder.build();
165         }
166         return null;
167     }
168
169     private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
170         TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
171
172         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
173         Integer destinationPort = transportPort(sourceMatch,
174                 MatchType.TP_DST);
175
176         if (sourcePort != null) {
177             tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
178         }
179         if (destinationPort != null) {
180             tcpMatchBuilder.setTcpDestinationPort(new PortNumber(
181                     destinationPort));
182         }
183         if(sourcePort != null || destinationPort != null) {
184             return tcpMatchBuilder.build();
185         }
186         return null;
187     }
188
189     private static Integer transportPort(final Match sourceMatch,
190             final MatchType matchType) {
191         MatchField transportPort = sourceMatch.getField(matchType);
192         if (transportPort != null && transportPort.getValue() != null
193                 && transportPort.getValue().getClass().equals(Short.class)) {
194             return new Integer(NetUtils.getUnsignedShort((short) transportPort
195                     .getValue()));
196         }
197         return null;
198     }
199
200     private static VlanMatch vlanMatch(final Match sourceMatch) {
201         VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder();
202
203         MatchField vlan = sourceMatch.getField(MatchType.DL_VLAN);
204         if (vlan != null && vlan.getValue() != null) {
205             VlanIdBuilder vlanIDBuilder = new VlanIdBuilder();
206             vlanIDBuilder.setVlanId(new VlanId((NetUtils
207                     .getUnsignedShort((short) vlan.getValue()))));
208             vlanIDBuilder.setVlanIdPresent(true);
209             vlanMatchBuild.setVlanId(vlanIDBuilder.build());
210         }
211
212         MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR);
213         if (vlanPriority != null && vlanPriority.getValue() != null) {
214             vlanMatchBuild.setVlanPcp(new VlanPcp((short) ((byte) vlanPriority
215                     .getValue())));
216         }
217         if((vlan != null && vlan.getValue() != null) || (vlanPriority != null && vlanPriority.getValue() != null)) {
218             return vlanMatchBuild.build();
219         }
220         return null;
221     }
222
223     private static IpMatch ipMatch(final Match sourceMatch) {
224         IpMatchBuilder targetIpMatchBuild = new IpMatchBuilder();
225         MatchField networkTos = sourceMatch.getField(MatchType.NW_TOS);
226         if (networkTos != null && networkTos.getValue() != null) {
227             Dscp dscp = new Dscp(
228                     (short) (NetUtils.getUnsignedByte((Byte) networkTos
229                             .getValue())));
230             targetIpMatchBuild.setIpDscp(dscp);
231         }
232
233         MatchField protocol = sourceMatch.getField(MatchType.NW_PROTO);
234         if (protocol != null && protocol.getValue() != null) {
235             targetIpMatchBuild.setIpProtocol((short) ((byte) protocol
236                     .getValue()));
237         }
238         if((networkTos != null && networkTos.getValue() != null) || (protocol != null && protocol.getValue() != null)) {
239             return targetIpMatchBuild.build();
240         }
241         return null;
242     }
243
244     private static EthernetMatch ethernetMatch(final Match sourceMatch) {
245         final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder();
246         if(sourceMatch.getField(DL_SRC) != null && sourceMatch.getField(DL_SRC).getValue() != null) {
247             EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder()
248                     .setAddress(ethernetSourceAddress(sourceMatch));
249             targetEthMatchBuild.setEthernetSource(ethSourBuild.build());
250         }
251         if(sourceMatch.getField(DL_DST) != null && sourceMatch.getField(DL_DST).getValue() != null) {
252             EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder()
253                     .setAddress(ethernetDestAddress(sourceMatch));
254             targetEthMatchBuild.setEthernetDestination(ethDestBuild.build());
255         }
256
257         final MatchField dataLinkType = sourceMatch.getField(MatchType.DL_TYPE);
258         if (dataLinkType != null && dataLinkType.getValue() != null) {
259             EtherType etherType = new EtherType(new Long(
260                     NetUtils.getUnsignedShort((Short) dataLinkType.getValue())));
261             EthernetTypeBuilder ethType = new EthernetTypeBuilder()
262                     .setType(etherType);
263             targetEthMatchBuild.setEthernetType(ethType.build());
264         }
265         if((sourceMatch.getField(DL_SRC) != null && sourceMatch.getField(DL_SRC).getValue() != null) ||
266                 (sourceMatch.getField(DL_DST) != null && sourceMatch.getField(DL_DST).getValue() != null)||
267                 dataLinkType != null ) {
268             return targetEthMatchBuild.build();
269         }
270         return null;
271     }
272
273     private static MacAddress ethernetSourceAddress(final Match sourceMatch) {
274         final MatchField dataLinkSource = sourceMatch.getField(DL_SRC);
275         if (dataLinkSource != null && dataLinkSource.getValue() != null) {
276             return MDFlowMapping.toMacAddress((byte[])dataLinkSource.getValue());
277         }
278         return null;
279
280     }
281
282     private static Layer3Match layer3Match(final Match sourceMatch) {
283         InetAddress inetSourceAddress = null;
284         MatchField netSource = sourceMatch.getField(MatchType.NW_SRC);
285         if (netSource != null && netSource.getValue() != null) {
286             inetSourceAddress = (InetAddress) (netSource.getValue());
287         }
288
289         InetAddress inetDestAddress = null;
290         MatchField netDest = sourceMatch.getField(MatchType.NW_DST);
291         if (netDest != null && netDest.getValue() != null) {
292             inetDestAddress = (InetAddress) (netDest.getValue());
293         }
294
295         if ((inetSourceAddress instanceof Inet4Address)
296                 || (inetDestAddress instanceof Inet4Address)) {
297             MatchField dataLinkType = sourceMatch.getField(DL_TYPE);
298             Short dLType = null;
299             if (dataLinkType != null && dataLinkType.getValue() != null) {
300                 dLType = (Short) (dataLinkType.getValue());
301             }
302             if (dLType != null && dLType.equals(ETHERNET_ARP)) {
303                 return setLayer3MatchAsArp(sourceMatch,
304                         (Inet4Address) inetSourceAddress,
305                         (Inet4Address) inetDestAddress);
306             } else {
307                 return setLayer3MatchAsIpv4((Inet4Address) inetSourceAddress,
308                         (Inet4Address) inetDestAddress);
309             }
310         } else if ((inetSourceAddress instanceof Inet6Address)
311                 || (inetDestAddress instanceof Inet6Address)) {
312             return setLayer3MatchAsIpv6((Inet6Address) inetSourceAddress,
313                     (Inet6Address) inetDestAddress);
314         }
315
316         return null;
317
318     }
319
320     private static Layer3Match setLayer3MatchAsArp(final Match sourceMatch,
321             final Inet4Address inetSourceAddress,
322             final Inet4Address inetDestAddress) {
323         String inetSourceAddressStr = InetAddresses
324                 .toAddrString(inetSourceAddress);
325         Ipv4Prefix ipv4SourcePrefix = new Ipv4Prefix(inetSourceAddressStr);
326
327         String inetDestAddressValue = InetAddresses
328                 .toAddrString(inetDestAddress);
329         Ipv4Prefix ipv4DestPrefix = new Ipv4Prefix(inetDestAddressValue);
330
331         ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
332
333         arpMatchBuilder.setArpSourceTransportAddress(ipv4SourcePrefix);
334         arpMatchBuilder.setArpTargetTransportAddress(ipv4DestPrefix);
335
336         ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
337         arpSourceHardwareAddressBuilder
338                 .setAddress(ethernetSourceAddress(sourceMatch));
339         arpMatchBuilder
340                 .setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder
341                         .build());
342
343         ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
344         arpTargetHardwareAddressBuilder
345                 .setAddress(ethernetDestAddress(sourceMatch));
346         arpMatchBuilder
347                 .setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder
348                         .build());
349
350         return arpMatchBuilder.build();
351
352     }
353
354     private static MacAddress ethernetDestAddress(final Match sourceMatch) {
355         final MatchField dataLinkDest = sourceMatch.getField(DL_DST);
356         if (dataLinkDest != null && dataLinkDest.getValue() != null) {
357             return MDFlowMapping.toMacAddress((byte[]) dataLinkDest.getValue());
358         }
359         return null;
360     }
361
362     private static Layer3Match setLayer3MatchAsIpv4(
363             final Inet4Address inetSourceAddress,
364             final Inet4Address inetDestAddress) {
365         Ipv4MatchBuilder layer4MatchBuild = new Ipv4MatchBuilder();
366         if(inetSourceAddress != null) {
367             String inetSrcAddressString = InetAddresses
368                     .toAddrString(inetSourceAddress);
369             layer4MatchBuild.setIpv4Source(new Ipv4Prefix(inetSrcAddressString));
370         }
371         if(inetDestAddress != null) {
372             String inetDstAddressString = InetAddresses
373                     .toAddrString(inetDestAddress);
374             layer4MatchBuild
375             .setIpv4Destination(new Ipv4Prefix(inetDstAddressString));
376         }
377         return layer4MatchBuild.build();
378
379     }
380
381     private static Layer3Match setLayer3MatchAsIpv6(
382             final Inet6Address inetSourceAddress,
383             final Inet6Address inetDestAddress) {
384         Ipv6MatchBuilder layer6MatchBuild = new Ipv6MatchBuilder();
385         if(inetSourceAddress != null) {
386             String inetSrcAddressString = InetAddresses
387                     .toAddrString(inetSourceAddress);
388             layer6MatchBuild.setIpv6Source(new Ipv6Prefix(inetSrcAddressString));
389         }
390         if(inetDestAddress != null) {
391             String inetDstAddressString = InetAddresses
392                     .toAddrString(inetDestAddress);
393             layer6MatchBuild
394                     .setIpv6Destination(new Ipv6Prefix(inetDstAddressString));
395         }
396         return layer6MatchBuild.build();
397     }
398
399     public static boolean flowEquals(Flow statsFlow, Flow storedFlow) {
400         if (statsFlow.getClass() != storedFlow.getClass()) {
401             return false;
402         }
403         if (statsFlow.getBufferId()== null) {
404             if (storedFlow.getBufferId() != null) {
405                 return false;
406             }
407         } else if(!statsFlow.getBufferId().equals(storedFlow.getBufferId())) {
408             return false;
409         }
410         if (statsFlow.getContainerName()== null) {
411             if (storedFlow.getContainerName()!= null) {
412                 return false;
413             }
414         } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
415             return false;
416         }
417         if (statsFlow.getCookie()== null) {
418             if (storedFlow.getCookie()!= null) {
419                 return false;
420             }
421         } else if(!statsFlow.getCookie().equals(storedFlow.getCookie())) {
422             return false;
423         }
424         if (statsFlow.getMatch()== null) {
425             if (storedFlow.getMatch() != null) {
426                 return false;
427             }
428         } else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
429             return false;
430         }
431         if (statsFlow.getCookie()== null) {
432             if (storedFlow.getCookie()!= null) {
433                 return false;
434             }
435         } else if(!statsFlow.getCookie().equals(storedFlow.getCookie())) {
436             return false;
437         }
438         if (statsFlow.getHardTimeout() == null) {
439             if (storedFlow.getHardTimeout() != null) {
440                 return false;
441             }
442         } else if(!statsFlow.getHardTimeout().equals(storedFlow.getHardTimeout() )) {
443             return false;
444         }
445         if (statsFlow.getIdleTimeout()== null) {
446             if (storedFlow.getIdleTimeout() != null) {
447                 return false;
448             }
449         } else if(!statsFlow.getIdleTimeout().equals(storedFlow.getIdleTimeout())) {
450             return false;
451         }
452         if (statsFlow.getPriority() == null) {
453             if (storedFlow.getPriority() != null) {
454                 return false;
455             }
456         } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
457             return false;
458         }
459         if (statsFlow.getTableId() == null) {
460             if (storedFlow.getTableId() != null) {
461                 return false;
462             }
463         } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
464             return false;
465         }
466         return true;
467     }
468
469
470 }