Fix star import and enable checkstyle rule to prevent it.
[controller.git] / opendaylight / md-sal / sal-compability / src / main / java / org / opendaylight / controller / sal / compability / FromSalConversionsUtils.java
1 package org.opendaylight.controller.sal.compability;
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.math.BigInteger;
8 import java.net.Inet4Address;
9 import java.net.Inet6Address;
10 import java.net.InetAddress;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.List;
14
15 import org.opendaylight.controller.sal.core.NodeConnector;
16
17
18
19 import org.opendaylight.controller.sal.match.Match;
20 import org.opendaylight.controller.sal.match.MatchField;
21 import org.opendaylight.controller.sal.match.MatchType;
22 import org.opendaylight.controller.sal.utils.NetUtils;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
28
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetNodeConnectorStatisticsInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetNodeConnectorStatisticsInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.address.Address;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.address.address.Ipv4Builder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.address.address.Ipv6Builder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddressBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddressBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
60
61 import com.google.common.net.InetAddresses;
62
63
64
65
66
67
68
69
70 import static org.opendaylight.controller.sal.compability.ProtocolConstants.*;
71 import static org.opendaylight.controller.sal.compability.NodeMapping.*;
72
73 public class FromSalConversionsUtils {
74
75     private FromSalConversionsUtils() {
76
77     }
78
79     public static GetNodeConnectorStatisticsInput nodeConnectorStatistics(
80             NodeConnector connector) {
81         GetNodeConnectorStatisticsInputBuilder target = new GetNodeConnectorStatisticsInputBuilder();
82
83         NodeRef nodeRef = toNodeRef(connector.getNode());
84         target.setNode(nodeRef);
85
86         NodeConnectorRef nodeConnectorRef = toNodeConnectorRef(connector);
87         target.setNodeConnector(nodeConnectorRef);
88
89         return target.build();
90     }
91
92     private static Address addressFromAction(InetAddress inetAddress) {
93         String strInetAddresss = InetAddresses.toAddrString(inetAddress);
94         if (inetAddress instanceof Inet4Address) {
95             Ipv4Builder ipv4Builder = new Ipv4Builder();
96             ipv4Builder.setIpv4Address(new Ipv4Prefix(strInetAddresss));
97             return ipv4Builder.build();
98         } else if (inetAddress instanceof Inet6Address) {
99             Ipv6Builder ipv6Builder = new Ipv6Builder();
100             ipv6Builder.setIpv6Address(new Ipv6Prefix(strInetAddresss));
101             return ipv6Builder.build();
102         }
103         return null;
104     }
105
106     public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match toMatch(
107             Match sourceMatch) {
108         if (sourceMatch != null) {
109             MatchBuilder targetBuilder = new MatchBuilder();
110
111             targetBuilder.setEthernetMatch(ethernetMatch(sourceMatch));
112             targetBuilder.setIpMatch(ipMatch(sourceMatch));
113             targetBuilder.setVlanMatch(vlanMatch(sourceMatch));
114             targetBuilder.setLayer3Match(layer3Match(sourceMatch));
115             targetBuilder.setLayer4Match(layer4Match(sourceMatch));
116
117             return targetBuilder.build();
118         }
119         return null;
120
121     }
122
123     private static Layer4Match layer4Match(final Match sourceMatch) {
124         MatchField nwProto = sourceMatch.getField(MatchType.NW_PROTO);
125         Short nwProtocolSource = null;
126         if (nwProto != null && nwProto.getValue() != null) {
127             nwProtocolSource = (short) ((byte) nwProto.getValue());
128             switch (nwProtocolSource) {
129             case TCP:
130                 return Layer4MatchAsTcp(sourceMatch);
131             case UDP:
132                 return Layer4MatchAsUdp(sourceMatch);
133             case SCTP:
134                 return Layer4MatchAsSctp(sourceMatch);
135             }
136         }
137         return null;
138     }
139
140     private static Layer4Match Layer4MatchAsSctp(final Match sourceMatch) {
141         SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
142
143         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
144         Integer destinationPort = transportPort(sourceMatch,
145                 MatchType.TP_DST);
146
147         if (sourcePort != null) {
148             sctpMatchBuilder.setSctpSourcePort(new PortNumber(sourcePort));
149         }
150         if (destinationPort != null) {
151             sctpMatchBuilder.setSctpDestinationPort(new PortNumber(
152                     destinationPort));
153         }
154
155         return sctpMatchBuilder.build();
156     }
157
158     private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
159         UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
160
161         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
162         Integer destinationPort = transportPort(sourceMatch,
163                 MatchType.TP_DST);
164
165         if (sourcePort != null) {
166             udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
167         }
168
169         if (destinationPort != null) {
170             udpMatchBuilder.setUdpDestinationPort(new PortNumber(
171                     destinationPort));
172         }
173
174         return udpMatchBuilder.build();
175     }
176
177     private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
178         TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
179
180         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
181         Integer destinationPort = transportPort(sourceMatch,
182                 MatchType.TP_DST);
183
184         if (sourcePort != null) {
185             tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
186         }
187         if (destinationPort != null) {
188             tcpMatchBuilder.setTcpDestinationPort(new PortNumber(
189                     destinationPort));
190         }
191
192         return tcpMatchBuilder.build();
193     }
194
195     private static Integer transportPort(final Match sourceMatch,
196             final MatchType matchType) {
197         MatchField transportPort = sourceMatch.getField(matchType);
198         if (transportPort != null && transportPort.getValue() != null
199                 && transportPort.getValue().getClass().equals(Short.class)) {
200             return new Integer(NetUtils.getUnsignedShort((short) transportPort
201                     .getValue()));
202         }
203         return null;
204     }
205
206     private static VlanMatch vlanMatch(final Match sourceMatch) {
207         VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder();
208
209         MatchField vlan = sourceMatch.getField(MatchType.DL_VLAN);
210         if (vlan != null && vlan.getValue() != null) {
211             VlanIdBuilder vlanIDBuilder = new VlanIdBuilder();
212             vlanIDBuilder.setVlanId(new VlanId((int) (NetUtils
213                     .getUnsignedShort((short) vlan.getValue()))));
214             vlanMatchBuild.setVlanId(vlanIDBuilder.build());
215         }
216
217         MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR);
218         if (vlanPriority != null && vlanPriority.getValue() != null) {
219             vlanMatchBuild.setVlanPcp(new VlanPcp((short) ((byte) vlanPriority
220                     .getValue())));
221         }
222
223         return vlanMatchBuild.build();
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
242         return targetIpMatchBuild.build();
243
244     }
245
246     private static EthernetMatch ethernetMatch(final Match sourceMatch) {
247         final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder();
248
249         EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder()
250                 .setAddress(ethernetSourceAddress(sourceMatch));
251         targetEthMatchBuild.setEthernetSource(ethSourBuild.build());
252
253         EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder()
254                 .setAddress(ethernetDestAddress(sourceMatch));
255         targetEthMatchBuild.setEthernetDestination(ethDestBuild.build());
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         return targetEthMatchBuild.build();
266     }
267
268     private static MacAddress ethernetSourceAddress(final Match sourceMatch) {
269         final MatchField dataLinkSource = sourceMatch.getField(DL_SRC);
270         if (dataLinkSource != null && dataLinkSource.getValue() != null) {
271             return MDFlowMapping.toMacAddress((byte[])dataLinkSource.getValue());
272         }
273         return null;
274
275     }
276
277     private static Layer3Match layer3Match(final Match sourceMatch) {
278         InetAddress inetSourceAddress = null;
279         MatchField netSource = sourceMatch.getField(MatchType.NW_SRC);
280         if (netSource != null && netSource.getValue() != null) {
281             inetSourceAddress = (InetAddress) (netSource.getValue());
282         }
283
284         InetAddress inetDestAddress = null;
285         MatchField netDest = sourceMatch.getField(MatchType.NW_DST);
286         if (netSource != null && netSource.getValue() != null) {
287             inetDestAddress = (InetAddress) (netDest.getValue());
288         }
289
290         if ((inetSourceAddress instanceof Inet4Address)
291                 && (inetDestAddress instanceof Inet4Address)) {
292             MatchField dataLinkType = sourceMatch.getField(DL_TYPE);
293             Short dLType = null;
294             if (dataLinkType != null && dataLinkType.getValue() != null) {
295                 dLType = (Short) (dataLinkType.getValue());
296             }
297             if (dLType != null && dLType.equals(ETHERNET_ARP)) {
298                 return setLayer3MatchAsArp(sourceMatch,
299                         (Inet4Address) inetSourceAddress,
300                         (Inet4Address) inetDestAddress);
301             } else {
302                 return setLayer3MatchAsIpv4((Inet4Address) inetSourceAddress,
303                         (Inet4Address) inetDestAddress);
304             }
305         } else if ((inetSourceAddress instanceof Inet6Address)
306                 && (inetDestAddress instanceof Inet6Address)) {
307             return setLayer3MatchAsIpv6((Inet6Address) inetSourceAddress,
308                     (Inet6Address) inetDestAddress);
309         }
310
311         return null;
312
313     }
314
315     private static Layer3Match setLayer3MatchAsArp(final Match sourceMatch,
316             final Inet4Address inetSourceAddress,
317             final Inet4Address inetDestAddress) {
318         String inetSourceAddressStr = InetAddresses
319                 .toAddrString(inetSourceAddress);
320         Ipv4Prefix ipv4SourcePrefix = new Ipv4Prefix(inetSourceAddressStr);
321
322         String inetDestAddressValue = InetAddresses
323                 .toAddrString(inetDestAddress);
324         Ipv4Prefix ipv4DestPrefix = new Ipv4Prefix(inetDestAddressValue);
325
326         ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
327
328         arpMatchBuilder.setArpSourceTransportAddress(ipv4SourcePrefix);
329         arpMatchBuilder.setArpTargetTransportAddress(ipv4DestPrefix);
330
331         ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
332         arpSourceHardwareAddressBuilder
333                 .setAddress(ethernetSourceAddress(sourceMatch));
334         arpMatchBuilder
335                 .setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder
336                         .build());
337
338         ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
339         arpTargetHardwareAddressBuilder
340                 .setAddress(ethernetDestAddress(sourceMatch));
341         arpMatchBuilder
342                 .setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder
343                         .build());
344
345         return arpMatchBuilder.build();
346
347     }
348
349     private static MacAddress ethernetDestAddress(final Match sourceMatch) {
350         final MatchField dataLinkDest = sourceMatch.getField(DL_DST);
351         if (dataLinkDest != null && dataLinkDest.getValue() != null) {
352             return MDFlowMapping.toMacAddress((byte[]) dataLinkDest.getValue());
353         }
354         return null;
355     }
356
357     private static Layer3Match setLayer3MatchAsIpv4(
358             final Inet4Address inetSourceAddress,
359             final Inet4Address inetDestAddress) {
360         String inetSrcAddressString = InetAddresses
361                 .toAddrString(inetSourceAddress);
362         String inetDstAddressString = InetAddresses
363                 .toAddrString(inetDestAddress);
364
365         Ipv4MatchBuilder layer4MatchBuild = new Ipv4MatchBuilder();
366         layer4MatchBuild.setIpv4Source(new Ipv4Prefix(inetSrcAddressString));
367         layer4MatchBuild
368                 .setIpv4Destination(new Ipv4Prefix(inetDstAddressString));
369         return layer4MatchBuild.build();
370
371     }
372
373     private static Layer3Match setLayer3MatchAsIpv6(
374             final Inet6Address inetSourceAddress,
375             final Inet6Address inetDestAddress) {
376         String inetSrcAddressString = InetAddresses
377                 .toAddrString(inetSourceAddress);
378         String inetDstAddressString = InetAddresses
379                 .toAddrString(inetDestAddress);
380         Ipv6MatchBuilder layer6MatchBuild = new Ipv6MatchBuilder();
381
382         layer6MatchBuild.setIpv6Source(new Ipv6Prefix(inetSrcAddressString));
383         layer6MatchBuild
384                 .setIpv6Destination(new Ipv6Prefix(inetDstAddressString));
385         return layer6MatchBuild.build();
386     }
387
388 }

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