6503a3031e96461e89240d637d6f90e7c768a5d0
[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.*;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
25
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.*;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.address.Address;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.address.address.Ipv4Builder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.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.*;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
49
50 import com.google.common.net.InetAddresses;
51
52
53
54
55
56
57
58
59 import static org.opendaylight.controller.sal.compability.ProtocolConstants.*;
60 import static org.opendaylight.controller.sal.compability.NodeMapping.*;
61
62 public class FromSalConversionsUtils {
63
64     private FromSalConversionsUtils() {
65
66     }
67
68     public static GetNodeConnectorStatisticsInput nodeConnectorStatistics(
69             NodeConnector connector) {
70         GetNodeConnectorStatisticsInputBuilder target = new GetNodeConnectorStatisticsInputBuilder();
71
72         NodeRef nodeRef = toNodeRef(connector.getNode());
73         target.setNode(nodeRef);
74
75         NodeConnectorRef nodeConnectorRef = toNodeConnectorRef(connector);
76         target.setNodeConnector(nodeConnectorRef);
77
78         return target.build();
79     }
80
81     private static Address addressFromAction(InetAddress inetAddress) {
82         String strInetAddresss = InetAddresses.toAddrString(inetAddress);
83         if (inetAddress instanceof Inet4Address) {
84             Ipv4Builder ipv4Builder = new Ipv4Builder();
85             ipv4Builder.setIpv4Address(new Ipv4Prefix(strInetAddresss));
86             return ipv4Builder.build();
87         } else if (inetAddress instanceof Inet6Address) {
88             Ipv6Builder ipv6Builder = new Ipv6Builder();
89             ipv6Builder.setIpv6Address(new Ipv6Prefix(strInetAddresss));
90             return ipv6Builder.build();
91         }
92         return null;
93     }
94
95     public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match toMatch(
96             Match sourceMatch) {
97         if (sourceMatch != null) {
98             MatchBuilder targetBuilder = new MatchBuilder();
99
100             targetBuilder.setEthernetMatch(ethernetMatch(sourceMatch));
101             targetBuilder.setIpMatch(ipMatch(sourceMatch));
102             targetBuilder.setVlanMatch(vlanMatch(sourceMatch));
103             targetBuilder.setLayer3Match(layer3Match(sourceMatch));
104             targetBuilder.setLayer4Match(layer4Match(sourceMatch));
105
106             return targetBuilder.build();
107         }
108         return null;
109
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 SCTP:
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
144         return sctpMatchBuilder.build();
145     }
146
147     private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
148         UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
149
150         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
151         Integer destinationPort = transportPort(sourceMatch,
152                 MatchType.TP_DST);
153
154         if (sourcePort != null) {
155             udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
156         }
157
158         if (destinationPort != null) {
159             udpMatchBuilder.setUdpDestinationPort(new PortNumber(
160                     destinationPort));
161         }
162
163         return udpMatchBuilder.build();
164     }
165
166     private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
167         TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
168
169         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
170         Integer destinationPort = transportPort(sourceMatch,
171                 MatchType.TP_DST);
172
173         if (sourcePort != null) {
174             tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
175         }
176         if (destinationPort != null) {
177             tcpMatchBuilder.setTcpDestinationPort(new PortNumber(
178                     destinationPort));
179         }
180
181         return tcpMatchBuilder.build();
182     }
183
184     private static Integer transportPort(final Match sourceMatch,
185             final MatchType matchType) {
186         MatchField transportPort = sourceMatch.getField(matchType);
187         if (transportPort != null && transportPort.getValue() != null
188                 && transportPort.getValue().getClass().equals(Short.class)) {
189             return new Integer(NetUtils.getUnsignedShort((short) transportPort
190                     .getValue()));
191         }
192         return null;
193     }
194
195     private static VlanMatch vlanMatch(final Match sourceMatch) {
196         VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder();
197
198         MatchField vlan = sourceMatch.getField(MatchType.DL_VLAN);
199         if (vlan != null && vlan.getValue() != null) {
200             VlanIdBuilder vlanIDBuilder = new VlanIdBuilder();
201             vlanIDBuilder.setVlanId(new VlanId((int) (NetUtils
202                     .getUnsignedShort((short) vlan.getValue()))));
203             vlanMatchBuild.setVlanId(vlanIDBuilder.build());
204         }
205
206         MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR);
207         if (vlanPriority != null && vlanPriority.getValue() != null) {
208             vlanMatchBuild.setVlanPcp(new VlanPcp((short) ((byte) vlanPriority
209                     .getValue())));
210         }
211
212         return vlanMatchBuild.build();
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
231         return targetIpMatchBuild.build();
232
233     }
234
235     private static EthernetMatch ethernetMatch(final Match sourceMatch) {
236         final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder();
237
238         EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder()
239                 .setAddress(ethernetSourceAddress(sourceMatch));
240         targetEthMatchBuild.setEthernetSource(ethSourBuild.build());
241
242         EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder()
243                 .setAddress(ethernetDestAddress(sourceMatch));
244         targetEthMatchBuild.setEthernetDestination(ethDestBuild.build());
245
246         final MatchField dataLinkType = sourceMatch.getField(MatchType.DL_TYPE);
247         if (dataLinkType != null && dataLinkType.getValue() != null) {
248             EtherType etherType = new EtherType(new Long(
249                     NetUtils.getUnsignedShort((Short) dataLinkType.getValue())));
250             EthernetTypeBuilder ethType = new EthernetTypeBuilder()
251                     .setType(etherType);
252             targetEthMatchBuild.setEthernetType(ethType.build());
253         }
254         return targetEthMatchBuild.build();
255     }
256
257     private static MacAddress ethernetSourceAddress(final Match sourceMatch) {
258         final MatchField dataLinkSource = sourceMatch.getField(DL_SRC);
259         if (dataLinkSource != null && dataLinkSource.getValue() != null) {
260             return MDFlowMapping.toMacAddress((byte[])dataLinkSource.getValue());
261         }
262         return null;
263
264     }
265
266     private static Layer3Match layer3Match(final Match sourceMatch) {
267         InetAddress inetSourceAddress = null;
268         MatchField netSource = sourceMatch.getField(MatchType.NW_SRC);
269         if (netSource != null && netSource.getValue() != null) {
270             inetSourceAddress = (InetAddress) (netSource.getValue());
271         }
272
273         InetAddress inetDestAddress = null;
274         MatchField netDest = sourceMatch.getField(MatchType.NW_DST);
275         if (netSource != null && netSource.getValue() != null) {
276             inetDestAddress = (InetAddress) (netDest.getValue());
277         }
278
279         if ((inetSourceAddress instanceof Inet4Address)
280                 && (inetDestAddress instanceof Inet4Address)) {
281             MatchField dataLinkType = sourceMatch.getField(DL_TYPE);
282             Short dLType = null;
283             if (dataLinkType != null && dataLinkType.getValue() != null) {
284                 dLType = (Short) (dataLinkType.getValue());
285             }
286             if (dLType != null && dLType.equals(ETHERNET_ARP)) {
287                 return setLayer3MatchAsArp(sourceMatch,
288                         (Inet4Address) inetSourceAddress,
289                         (Inet4Address) inetDestAddress);
290             } else {
291                 return setLayer3MatchAsIpv4((Inet4Address) inetSourceAddress,
292                         (Inet4Address) inetDestAddress);
293             }
294         } else if ((inetSourceAddress instanceof Inet6Address)
295                 && (inetDestAddress instanceof Inet6Address)) {
296             return setLayer3MatchAsIpv6((Inet6Address) inetSourceAddress,
297                     (Inet6Address) inetDestAddress);
298         }
299
300         return null;
301
302     }
303
304     private static Layer3Match setLayer3MatchAsArp(final Match sourceMatch,
305             final Inet4Address inetSourceAddress,
306             final Inet4Address inetDestAddress) {
307         String inetSourceAddressStr = InetAddresses
308                 .toAddrString(inetSourceAddress);
309         Ipv4Prefix ipv4SourcePrefix = new Ipv4Prefix(inetSourceAddressStr);
310
311         String inetDestAddressValue = InetAddresses
312                 .toAddrString(inetDestAddress);
313         Ipv4Prefix ipv4DestPrefix = new Ipv4Prefix(inetDestAddressValue);
314
315         ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
316
317         arpMatchBuilder.setArpSourceTransportAddress(ipv4SourcePrefix);
318         arpMatchBuilder.setArpTargetTransportAddress(ipv4DestPrefix);
319
320         ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
321         arpSourceHardwareAddressBuilder
322                 .setAddress(ethernetSourceAddress(sourceMatch));
323         arpMatchBuilder
324                 .setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder
325                         .build());
326
327         ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
328         arpTargetHardwareAddressBuilder
329                 .setAddress(ethernetDestAddress(sourceMatch));
330         arpMatchBuilder
331                 .setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder
332                         .build());
333
334         return arpMatchBuilder.build();
335
336     }
337
338     private static MacAddress ethernetDestAddress(final Match sourceMatch) {
339         final MatchField dataLinkDest = sourceMatch.getField(DL_DST);
340         if (dataLinkDest != null && dataLinkDest.getValue() != null) {
341             return MDFlowMapping.toMacAddress((byte[]) dataLinkDest.getValue());
342         }
343         return null;
344     }
345
346     private static Layer3Match setLayer3MatchAsIpv4(
347             final Inet4Address inetSourceAddress,
348             final Inet4Address inetDestAddress) {
349         String inetSrcAddressString = InetAddresses
350                 .toAddrString(inetSourceAddress);
351         String inetDstAddressString = InetAddresses
352                 .toAddrString(inetDestAddress);
353
354         Ipv4MatchBuilder layer4MatchBuild = new Ipv4MatchBuilder();
355         layer4MatchBuild.setIpv4Source(new Ipv4Prefix(inetSrcAddressString));
356         layer4MatchBuild
357                 .setIpv4Destination(new Ipv4Prefix(inetDstAddressString));
358         return layer4MatchBuild.build();
359
360     }
361
362     private static Layer3Match setLayer3MatchAsIpv6(
363             final Inet6Address inetSourceAddress,
364             final Inet6Address inetDestAddress) {
365         String inetSrcAddressString = InetAddresses
366                 .toAddrString(inetSourceAddress);
367         String inetDstAddressString = InetAddresses
368                 .toAddrString(inetDestAddress);
369         Ipv6MatchBuilder layer6MatchBuild = new Ipv6MatchBuilder();
370
371         layer6MatchBuild.setIpv6Source(new Ipv6Prefix(inetSrcAddressString));
372         layer6MatchBuild
373                 .setIpv6Destination(new Ipv6Prefix(inetDstAddressString));
374         return layer6MatchBuild.build();
375     }
376
377 }

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