Merge "Add test for generated code checking list of dependencies."
[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.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;
54
55 import com.google.common.net.InetAddresses;
56
57 public class FromSalConversionsUtils {
58
59     private FromSalConversionsUtils() {
60
61     }
62
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();
74         }
75         return null;
76     }
77
78     public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match toMatch(
79             Match sourceMatch) {
80         if (sourceMatch != null) {
81             MatchBuilder targetBuilder = new MatchBuilder();
82
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));
89
90             return targetBuilder.build();
91         }
92         return null;
93
94     }
95
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());
100         }
101         return null;
102     }
103
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) {
110             case TCP:
111                 return Layer4MatchAsTcp(sourceMatch);
112             case UDP:
113                 return Layer4MatchAsUdp(sourceMatch);
114             case CRUDP:
115                 return Layer4MatchAsSctp(sourceMatch);
116             }
117         }
118         return null;
119     }
120
121     private static Layer4Match Layer4MatchAsSctp(final Match sourceMatch) {
122         SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
123
124         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
125         Integer destinationPort = transportPort(sourceMatch,
126                 MatchType.TP_DST);
127
128         if (sourcePort != null) {
129             sctpMatchBuilder.setSctpSourcePort(new PortNumber(sourcePort));
130         }
131         if (destinationPort != null) {
132             sctpMatchBuilder.setSctpDestinationPort(new PortNumber(
133                     destinationPort));
134         }
135         if(sourcePort != null || destinationPort != null) {
136             return sctpMatchBuilder.build();
137         }
138         return null;
139     }
140
141     private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
142         UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
143
144         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
145         Integer destinationPort = transportPort(sourceMatch,
146                 MatchType.TP_DST);
147
148         if (sourcePort != null) {
149             udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
150         }
151
152         if (destinationPort != null) {
153             udpMatchBuilder.setUdpDestinationPort(new PortNumber(
154                     destinationPort));
155         }
156         if(sourcePort != null || destinationPort != null) {
157             return udpMatchBuilder.build();
158         }
159         return null;
160     }
161
162     private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
163         TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
164
165         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
166         Integer destinationPort = transportPort(sourceMatch,
167                 MatchType.TP_DST);
168
169         if (sourcePort != null) {
170             tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
171         }
172         if (destinationPort != null) {
173             tcpMatchBuilder.setTcpDestinationPort(new PortNumber(
174                     destinationPort));
175         }
176         if(sourcePort != null || destinationPort != null) {
177             return tcpMatchBuilder.build();
178         }
179         return null;
180     }
181
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
188                     .getValue()));
189         }
190         return null;
191     }
192
193     private static VlanMatch vlanMatch(final Match sourceMatch) {
194         VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder();
195
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());
202         }
203
204         MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR);
205         if (vlanPriority != null && vlanPriority.getValue() != null) {
206             vlanMatchBuild.setVlanPcp(new VlanPcp((short) ((byte) vlanPriority
207                     .getValue())));
208         }
209         if((vlan != null && vlan.getValue() != null) || (vlanPriority != null && vlanPriority.getValue() != null)) {
210             return vlanMatchBuild.build();
211         }
212         return null;
213     }
214
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
221                             .getValue())));
222             targetIpMatchBuild.setIpDscp(dscp);
223         }
224
225         MatchField protocol = sourceMatch.getField(MatchType.NW_PROTO);
226         if (protocol != null && protocol.getValue() != null) {
227             targetIpMatchBuild.setIpProtocol((short) ((byte) protocol
228                     .getValue()));
229         }
230         if((networkTos != null && networkTos.getValue() != null) || (protocol != null && protocol.getValue() != null)) {
231             return targetIpMatchBuild.build();
232         }
233         return null;
234     }
235
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());
242         }
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());
247         }
248
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()
254                     .setType(etherType);
255             targetEthMatchBuild.setEthernetType(ethType.build());
256         }
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();            
261         } 
262         return null;
263     }
264
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());
269         }
270         return null;
271
272     }
273
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());
279         }
280
281         InetAddress inetDestAddress = null;
282         MatchField netDest = sourceMatch.getField(MatchType.NW_DST);
283         if (netDest != null && netDest.getValue() != null) {
284             inetDestAddress = (InetAddress) (netDest.getValue());
285         }
286
287         if ((inetSourceAddress instanceof Inet4Address)
288                 || (inetDestAddress instanceof Inet4Address)) {
289             MatchField dataLinkType = sourceMatch.getField(DL_TYPE);
290             Short dLType = null;
291             if (dataLinkType != null && dataLinkType.getValue() != null) {
292                 dLType = (Short) (dataLinkType.getValue());
293             }
294             if (dLType != null && dLType.equals(ETHERNET_ARP)) {
295                 return setLayer3MatchAsArp(sourceMatch,
296                         (Inet4Address) inetSourceAddress,
297                         (Inet4Address) inetDestAddress);
298             } else {
299                 return setLayer3MatchAsIpv4((Inet4Address) inetSourceAddress,
300                         (Inet4Address) inetDestAddress);
301             }
302         } else if ((inetSourceAddress instanceof Inet6Address)
303                 || (inetDestAddress instanceof Inet6Address)) {
304             return setLayer3MatchAsIpv6((Inet6Address) inetSourceAddress,
305                     (Inet6Address) inetDestAddress);
306         }
307
308         return null;
309
310     }
311
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);
318
319         String inetDestAddressValue = InetAddresses
320                 .toAddrString(inetDestAddress);
321         Ipv4Prefix ipv4DestPrefix = new Ipv4Prefix(inetDestAddressValue);
322
323         ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
324
325         arpMatchBuilder.setArpSourceTransportAddress(ipv4SourcePrefix);
326         arpMatchBuilder.setArpTargetTransportAddress(ipv4DestPrefix);
327
328         ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
329         arpSourceHardwareAddressBuilder
330                 .setAddress(ethernetSourceAddress(sourceMatch));
331         arpMatchBuilder
332                 .setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder
333                         .build());
334
335         ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
336         arpTargetHardwareAddressBuilder
337                 .setAddress(ethernetDestAddress(sourceMatch));
338         arpMatchBuilder
339                 .setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder
340                         .build());
341
342         return arpMatchBuilder.build();
343
344     }
345
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());
350         }
351         return null;
352     }
353
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));
362         }
363         if(inetDestAddress != null) {
364             String inetDstAddressString = InetAddresses
365                     .toAddrString(inetDestAddress);
366             layer4MatchBuild
367             .setIpv4Destination(new Ipv4Prefix(inetDstAddressString));
368         }       
369         return layer4MatchBuild.build();
370
371     }
372
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));
381         }
382         if(inetDestAddress != null) {
383             String inetDstAddressString = InetAddresses
384                     .toAddrString(inetDestAddress);    
385             layer6MatchBuild
386                     .setIpv6Destination(new Ipv6Prefix(inetDstAddressString));
387         }
388         return layer6MatchBuild.build();
389     }
390     
391     public static boolean flowEquals(Flow statsFlow, Flow storedFlow) {
392         if (statsFlow.getClass() != storedFlow.getClass()) {
393             return false;
394         }
395         if (statsFlow.getBufferId()== null) {
396             if (storedFlow.getBufferId() != null) {
397                 return false;
398             }
399         } else if(!statsFlow.getBufferId().equals(storedFlow.getBufferId())) {
400             return false;
401         }
402         if (statsFlow.getContainerName()== null) {
403             if (storedFlow.getContainerName()!= null) {
404                 return false;
405             }
406         } else if(!statsFlow.getContainerName().equals(storedFlow.getContainerName())) {
407             return false;
408         }
409         if (statsFlow.getCookie()== null) {
410             if (storedFlow.getCookie()!= null) {
411                 return false;
412             }
413         } else if(!statsFlow.getCookie().equals(storedFlow.getCookie())) {
414             return false;
415         }
416         if (statsFlow.getMatch()== null) {
417             if (storedFlow.getMatch() != null) {
418                 return false;
419             }
420         } else if(!statsFlow.getMatch().equals(storedFlow.getMatch())) {
421             return false;
422         }
423         if (statsFlow.getCookie()== null) {
424             if (storedFlow.getCookie()!= null) {
425                 return false;
426             }
427         } else if(!statsFlow.getCookie().equals(storedFlow.getCookie())) {
428             return false;
429         }
430         if (statsFlow.getHardTimeout() == null) {
431             if (storedFlow.getHardTimeout() != null) {
432                 return false;
433             }
434         } else if(!statsFlow.getHardTimeout().equals(storedFlow.getHardTimeout() )) {
435             return false;
436         }
437         if (statsFlow.getIdleTimeout()== null) {
438             if (storedFlow.getIdleTimeout() != null) {
439                 return false;
440             }
441         } else if(!statsFlow.getIdleTimeout().equals(storedFlow.getIdleTimeout())) {
442             return false;
443         }
444         if (statsFlow.getPriority() == null) {
445             if (storedFlow.getPriority() != null) {
446                 return false;
447             }
448         } else if(!statsFlow.getPriority().equals(storedFlow.getPriority())) {
449             return false;
450         }
451         if (statsFlow.getTableId() == null) {
452             if (storedFlow.getTableId() != null) {
453                 return false;
454             }
455         } else if(!statsFlow.getTableId().equals(storedFlow.getTableId())) {
456             return false;
457         }
458         return true;
459     }
460
461
462 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.