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.match.MatchType.DL_DST;
4 import static org.opendaylight.controller.sal.match.MatchType.DL_SRC;
5 import static org.opendaylight.controller.sal.match.MatchType.DL_TYPE;
6
7 import java.net.Inet4Address;
8 import java.net.Inet6Address;
9 import java.net.InetAddress;
10
11
12 import org.opendaylight.controller.sal.core.NodeConnector;
13
14
15
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.flow.statistics.rev130819.GetNodeConnectorStatisticsInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetNodeConnectorStatisticsInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.Address;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4Builder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv6Builder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddressBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddressBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
56 import com.google.common.net.InetAddresses;
57 import static org.opendaylight.controller.sal.compatibility.NodeMapping.*;
58 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.*;
59
60 public class FromSalConversionsUtils {
61
62     private FromSalConversionsUtils() {
63
64     }
65
66     public static GetNodeConnectorStatisticsInput nodeConnectorStatistics(
67             NodeConnector connector) {
68         GetNodeConnectorStatisticsInputBuilder target = new GetNodeConnectorStatisticsInputBuilder();
69
70         NodeRef nodeRef = toNodeRef(connector.getNode());
71         target.setNode(nodeRef);
72
73         NodeConnectorRef nodeConnectorRef = toNodeConnectorRef(connector);
74         target.setNodeConnector(nodeConnectorRef);
75
76         return target.build();
77     }
78
79     private static Address addressFromAction(InetAddress inetAddress) {
80         String strInetAddresss = InetAddresses.toAddrString(inetAddress);
81         if (inetAddress instanceof Inet4Address) {
82             Ipv4Builder ipv4Builder = new Ipv4Builder();
83             ipv4Builder.setIpv4Address(new Ipv4Prefix(strInetAddresss));
84             return ipv4Builder.build();
85         } else if (inetAddress instanceof Inet6Address) {
86             Ipv6Builder ipv6Builder = new Ipv6Builder();
87             ipv6Builder.setIpv6Address(new Ipv6Prefix(strInetAddresss));
88             return ipv6Builder.build();
89         }
90         return null;
91     }
92
93     public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match toMatch(
94             Match sourceMatch) {
95         if (sourceMatch != null) {
96             MatchBuilder targetBuilder = new MatchBuilder();
97
98             targetBuilder.setEthernetMatch(ethernetMatch(sourceMatch));
99             targetBuilder.setIpMatch(ipMatch(sourceMatch));
100             targetBuilder.setVlanMatch(vlanMatch(sourceMatch));
101             targetBuilder.setLayer3Match(layer3Match(sourceMatch));
102             targetBuilder.setLayer4Match(layer4Match(sourceMatch));
103
104             return targetBuilder.build();
105         }
106         return null;
107
108     }
109
110     private static Layer4Match layer4Match(final Match sourceMatch) {
111         MatchField nwProto = sourceMatch.getField(MatchType.NW_PROTO);
112         Short nwProtocolSource = null;
113         if (nwProto != null && nwProto.getValue() != null) {
114             nwProtocolSource = (short) ((byte) nwProto.getValue());
115             switch (nwProtocolSource) {
116             case TCP:
117                 return Layer4MatchAsTcp(sourceMatch);
118             case UDP:
119                 return Layer4MatchAsUdp(sourceMatch);
120             case CRUDP:
121                 return Layer4MatchAsSctp(sourceMatch);
122             }
123         }
124         return null;
125     }
126
127     private static Layer4Match Layer4MatchAsSctp(final Match sourceMatch) {
128         SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
129
130         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
131         Integer destinationPort = transportPort(sourceMatch,
132                 MatchType.TP_DST);
133
134         if (sourcePort != null) {
135             sctpMatchBuilder.setSctpSourcePort(new PortNumber(sourcePort));
136         }
137         if (destinationPort != null) {
138             sctpMatchBuilder.setSctpDestinationPort(new PortNumber(
139                     destinationPort));
140         }
141
142         return sctpMatchBuilder.build();
143     }
144
145     private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
146         UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
147
148         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
149         Integer destinationPort = transportPort(sourceMatch,
150                 MatchType.TP_DST);
151
152         if (sourcePort != null) {
153             udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
154         }
155
156         if (destinationPort != null) {
157             udpMatchBuilder.setUdpDestinationPort(new PortNumber(
158                     destinationPort));
159         }
160
161         return udpMatchBuilder.build();
162     }
163
164     private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
165         TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
166
167         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
168         Integer destinationPort = transportPort(sourceMatch,
169                 MatchType.TP_DST);
170
171         if (sourcePort != null) {
172             tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
173         }
174         if (destinationPort != null) {
175             tcpMatchBuilder.setTcpDestinationPort(new PortNumber(
176                     destinationPort));
177         }
178
179         return tcpMatchBuilder.build();
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((int) (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
210         return vlanMatchBuild.build();
211     }
212
213     private static IpMatch ipMatch(final Match sourceMatch) {
214         IpMatchBuilder targetIpMatchBuild = new IpMatchBuilder();
215         MatchField networkTos = sourceMatch.getField(MatchType.NW_TOS);
216         if (networkTos != null && networkTos.getValue() != null) {
217             Dscp dscp = new Dscp(
218                     (short) (NetUtils.getUnsignedByte((Byte) networkTos
219                             .getValue())));
220             targetIpMatchBuild.setIpDscp(dscp);
221         }
222
223         MatchField protocol = sourceMatch.getField(MatchType.NW_PROTO);
224         if (protocol != null && protocol.getValue() != null) {
225             targetIpMatchBuild.setIpProtocol((short) ((byte) protocol
226                     .getValue()));
227         }
228
229         return targetIpMatchBuild.build();
230
231     }
232
233     private static EthernetMatch ethernetMatch(final Match sourceMatch) {
234         final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder();
235
236         EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder()
237                 .setAddress(ethernetSourceAddress(sourceMatch));
238         targetEthMatchBuild.setEthernetSource(ethSourBuild.build());
239
240         EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder()
241                 .setAddress(ethernetDestAddress(sourceMatch));
242         targetEthMatchBuild.setEthernetDestination(ethDestBuild.build());
243
244         final MatchField dataLinkType = sourceMatch.getField(MatchType.DL_TYPE);
245         if (dataLinkType != null && dataLinkType.getValue() != null) {
246             EtherType etherType = new EtherType(new Long(
247                     NetUtils.getUnsignedShort((Short) dataLinkType.getValue())));
248             EthernetTypeBuilder ethType = new EthernetTypeBuilder()
249                     .setType(etherType);
250             targetEthMatchBuild.setEthernetType(ethType.build());
251         }
252         return targetEthMatchBuild.build();
253     }
254
255     private static MacAddress ethernetSourceAddress(final Match sourceMatch) {
256         final MatchField dataLinkSource = sourceMatch.getField(DL_SRC);
257         if (dataLinkSource != null && dataLinkSource.getValue() != null) {
258             return MDFlowMapping.toMacAddress((byte[])dataLinkSource.getValue());
259         }
260         return null;
261
262     }
263
264     private static Layer3Match layer3Match(final Match sourceMatch) {
265         InetAddress inetSourceAddress = null;
266         MatchField netSource = sourceMatch.getField(MatchType.NW_SRC);
267         if (netSource != null && netSource.getValue() != null) {
268             inetSourceAddress = (InetAddress) (netSource.getValue());
269         }
270
271         InetAddress inetDestAddress = null;
272         MatchField netDest = sourceMatch.getField(MatchType.NW_DST);
273         if (netSource != null && netSource.getValue() != null) {
274             inetDestAddress = (InetAddress) (netDest.getValue());
275         }
276
277         if ((inetSourceAddress instanceof Inet4Address)
278                 && (inetDestAddress instanceof Inet4Address)) {
279             MatchField dataLinkType = sourceMatch.getField(DL_TYPE);
280             Short dLType = null;
281             if (dataLinkType != null && dataLinkType.getValue() != null) {
282                 dLType = (Short) (dataLinkType.getValue());
283             }
284             if (dLType != null && dLType.equals(ETHERNET_ARP)) {
285                 return setLayer3MatchAsArp(sourceMatch,
286                         (Inet4Address) inetSourceAddress,
287                         (Inet4Address) inetDestAddress);
288             } else {
289                 return setLayer3MatchAsIpv4((Inet4Address) inetSourceAddress,
290                         (Inet4Address) inetDestAddress);
291             }
292         } else if ((inetSourceAddress instanceof Inet6Address)
293                 && (inetDestAddress instanceof Inet6Address)) {
294             return setLayer3MatchAsIpv6((Inet6Address) inetSourceAddress,
295                     (Inet6Address) inetDestAddress);
296         }
297
298         return null;
299
300     }
301
302     private static Layer3Match setLayer3MatchAsArp(final Match sourceMatch,
303             final Inet4Address inetSourceAddress,
304             final Inet4Address inetDestAddress) {
305         String inetSourceAddressStr = InetAddresses
306                 .toAddrString(inetSourceAddress);
307         Ipv4Prefix ipv4SourcePrefix = new Ipv4Prefix(inetSourceAddressStr);
308
309         String inetDestAddressValue = InetAddresses
310                 .toAddrString(inetDestAddress);
311         Ipv4Prefix ipv4DestPrefix = new Ipv4Prefix(inetDestAddressValue);
312
313         ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
314
315         arpMatchBuilder.setArpSourceTransportAddress(ipv4SourcePrefix);
316         arpMatchBuilder.setArpTargetTransportAddress(ipv4DestPrefix);
317
318         ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
319         arpSourceHardwareAddressBuilder
320                 .setAddress(ethernetSourceAddress(sourceMatch));
321         arpMatchBuilder
322                 .setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder
323                         .build());
324
325         ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
326         arpTargetHardwareAddressBuilder
327                 .setAddress(ethernetDestAddress(sourceMatch));
328         arpMatchBuilder
329                 .setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder
330                         .build());
331
332         return arpMatchBuilder.build();
333
334     }
335
336     private static MacAddress ethernetDestAddress(final Match sourceMatch) {
337         final MatchField dataLinkDest = sourceMatch.getField(DL_DST);
338         if (dataLinkDest != null && dataLinkDest.getValue() != null) {
339             return MDFlowMapping.toMacAddress((byte[]) dataLinkDest.getValue());
340         }
341         return null;
342     }
343
344     private static Layer3Match setLayer3MatchAsIpv4(
345             final Inet4Address inetSourceAddress,
346             final Inet4Address inetDestAddress) {
347         String inetSrcAddressString = InetAddresses
348                 .toAddrString(inetSourceAddress);
349         String inetDstAddressString = InetAddresses
350                 .toAddrString(inetDestAddress);
351
352         Ipv4MatchBuilder layer4MatchBuild = new Ipv4MatchBuilder();
353         layer4MatchBuild.setIpv4Source(new Ipv4Prefix(inetSrcAddressString));
354         layer4MatchBuild
355                 .setIpv4Destination(new Ipv4Prefix(inetDstAddressString));
356         return layer4MatchBuild.build();
357
358     }
359
360     private static Layer3Match setLayer3MatchAsIpv6(
361             final Inet6Address inetSourceAddress,
362             final Inet6Address inetDestAddress) {
363         String inetSrcAddressString = InetAddresses
364                 .toAddrString(inetSourceAddress);
365         String inetDstAddressString = InetAddresses
366                 .toAddrString(inetDestAddress);
367         Ipv6MatchBuilder layer6MatchBuild = new Ipv6MatchBuilder();
368
369         layer6MatchBuild.setIpv6Source(new Ipv6Prefix(inetSrcAddressString));
370         layer6MatchBuild
371                 .setIpv6Destination(new Ipv6Prefix(inetDstAddressString));
372         return layer6MatchBuild.build();
373     }
374
375 }

©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.