Updated Flow Capable Models & AD SAL Compatibility mapping
[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.action.*;
16 import org.opendaylight.controller.sal.core.NodeConnector;
17 import org.opendaylight.controller.sal.core.Node;
18
19
20 import org.opendaylight.controller.sal.flowprogrammer.Flow;
21 import org.opendaylight.controller.sal.match.Match;
22 import org.opendaylight.controller.sal.match.MatchField;
23 import org.opendaylight.controller.sal.match.MatchType;
24 import org.opendaylight.controller.sal.utils.NetUtils;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.*;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAddedBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.*;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.VlanCfi;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.*;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.Address;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.address.Ipv4Builder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.address.Ipv6Builder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Action;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.ActionBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.arp.match.fields.ArpSourceHardwareAddressBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.arp.match.fields.ArpTargetHardwareAddressBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetDestinationBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetSourceBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetTypeBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.*;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.ArpMatchBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv4MatchBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv6MatchBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.SctpMatchBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.TcpMatchBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.UdpMatchBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.vlan.match.fields.VlanIdBuilder;
61
62 import com.google.common.net.InetAddresses;
63
64
65
66
67 import static org.opendaylight.controller.sal.compability.ProtocolConstants.*;
68 import static org.opendaylight.controller.sal.compability.NodeMapping.*;
69
70 public class FromSalConversionsUtils {
71
72     private FromSalConversionsUtils() {
73
74     }
75
76     public static GetNodeConnectorStatisticsInput nodeConnectorStatistics(
77             NodeConnector connector) {
78         GetNodeConnectorStatisticsInputBuilder target = new GetNodeConnectorStatisticsInputBuilder();
79
80         NodeRef nodeRef = toNodeRef(connector.getNode());
81         target.setNode(nodeRef);
82
83         NodeConnectorRef nodeConnectorRef = toNodeConnectorRef(connector);
84         target.setNodeConnector(nodeConnectorRef);
85
86         return target.build();
87     }
88
89     private static Address addressFromAction(InetAddress inetAddress) {
90         String strInetAddresss = InetAddresses.toAddrString(inetAddress);
91         if (inetAddress instanceof Inet4Address) {
92             Ipv4Builder ipv4Builder = new Ipv4Builder();
93             ipv4Builder.setIpv4Address(new Ipv4Prefix(strInetAddresss));
94             return ipv4Builder.build();
95         } else if (inetAddress instanceof Inet6Address) {
96             Ipv6Builder ipv6Builder = new Ipv6Builder();
97             ipv6Builder.setIpv6Address(new Ipv6Prefix(strInetAddresss));
98             return ipv6Builder.build();
99         }
100         return null;
101     }
102
103     public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Match toMatch(
104             Match sourceMatch) {
105         if (sourceMatch != null) {
106             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.MatchBuilder targetBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.MatchBuilder();
107
108             targetBuilder.setEthernetMatch(ethernetMatch(sourceMatch));
109             targetBuilder.setIpMatch(ipMatch(sourceMatch));
110             targetBuilder.setVlanMatch(vlanMatch(sourceMatch));
111             targetBuilder.setLayer3Match(layer3Match(sourceMatch));
112             targetBuilder.setLayer4Match(layer4Match(sourceMatch));
113
114             return targetBuilder.build();
115         }
116         return null;
117
118     }
119
120     private static Layer4Match layer4Match(final Match sourceMatch) {
121         MatchField nwProto = sourceMatch.getField(MatchType.NW_PROTO);
122         Short nwProtocolSource = null;
123         if (nwProto != null && nwProto.getValue() != null) {
124             nwProtocolSource = (short) ((byte) nwProto.getValue());
125             switch (nwProtocolSource) {
126             case TCP:
127                 return Layer4MatchAsTcp(sourceMatch);
128             case UDP:
129                 return Layer4MatchAsUdp(sourceMatch);
130             case SCTP:
131                 return Layer4MatchAsSctp(sourceMatch);
132             }
133         }
134         return null;
135     }
136
137     private static Layer4Match Layer4MatchAsSctp(final Match sourceMatch) {
138         SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
139
140         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
141         Integer destinationPort = transportPort(sourceMatch,
142                 MatchType.TP_DST);
143
144         if (sourcePort != null) {
145             sctpMatchBuilder.setSctpSourcePort(new PortNumber(sourcePort));
146         }
147         if (destinationPort != null) {
148             sctpMatchBuilder.setSctpDestinationPort(new PortNumber(
149                     destinationPort));
150         }
151
152         return sctpMatchBuilder.build();
153     }
154
155     private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
156         UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
157
158         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
159         Integer destinationPort = transportPort(sourceMatch,
160                 MatchType.TP_DST);
161
162         if (sourcePort != null) {
163             udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
164         }
165
166         if (destinationPort != null) {
167             udpMatchBuilder.setUdpDestinationPort(new PortNumber(
168                     destinationPort));
169         }
170
171         return udpMatchBuilder.build();
172     }
173
174     private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
175         TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
176
177         Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
178         Integer destinationPort = transportPort(sourceMatch,
179                 MatchType.TP_DST);
180
181         if (sourcePort != null) {
182             tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
183         }
184         if (destinationPort != null) {
185             tcpMatchBuilder.setTcpDestinationPort(new PortNumber(
186                     destinationPort));
187         }
188
189         return tcpMatchBuilder.build();
190     }
191
192     private static Integer transportPort(final Match sourceMatch,
193             final MatchType matchType) {
194         MatchField transportPort = sourceMatch.getField(matchType);
195         if (transportPort != null && transportPort.getValue() != null
196                 && transportPort.getValue().getClass().equals(Short.class)) {
197             return new Integer(NetUtils.getUnsignedShort((short) transportPort
198                     .getValue()));
199         }
200         return null;
201     }
202
203     private static VlanMatch vlanMatch(final Match sourceMatch) {
204         VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder();
205
206         MatchField vlan = sourceMatch.getField(MatchType.DL_VLAN);
207         if (vlan != null && vlan.getValue() != null) {
208             VlanIdBuilder vlanIDBuilder = new VlanIdBuilder();
209             vlanIDBuilder.setVlanId(new VlanId((int) (NetUtils
210                     .getUnsignedShort((short) vlan.getValue()))));
211             vlanMatchBuild.setVlanId(vlanIDBuilder.build());
212         }
213
214         MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR);
215         if (vlanPriority != null && vlanPriority.getValue() != null) {
216             vlanMatchBuild.setVlanPcp(new VlanPcp((short) ((byte) vlanPriority
217                     .getValue())));
218         }
219
220         return vlanMatchBuild.build();
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
239         return targetIpMatchBuild.build();
240
241     }
242
243     private static EthernetMatch ethernetMatch(final Match sourceMatch) {
244         final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder();
245
246         EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder()
247                 .setAddress(ethernetSourceAddress(sourceMatch));
248         targetEthMatchBuild.setEthernetSource(ethSourBuild.build());
249
250         EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder()
251                 .setAddress(ethernetDestAddress(sourceMatch));
252         targetEthMatchBuild.setEthernetDestination(ethDestBuild.build());
253
254         final MatchField dataLinkType = sourceMatch.getField(MatchType.DL_TYPE);
255         if (dataLinkType != null && dataLinkType.getValue() != null) {
256             EtherType etherType = new EtherType(new Long(
257                     NetUtils.getUnsignedShort((Short) dataLinkType.getValue())));
258             EthernetTypeBuilder ethType = new EthernetTypeBuilder()
259                     .setType(etherType);
260             targetEthMatchBuild.setEthernetType(ethType.build());
261         }
262         return targetEthMatchBuild.build();
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 (netSource != null && netSource.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         String inetSrcAddressString = InetAddresses
358                 .toAddrString(inetSourceAddress);
359         String inetDstAddressString = InetAddresses
360                 .toAddrString(inetDestAddress);
361
362         Ipv4MatchBuilder layer4MatchBuild = new Ipv4MatchBuilder();
363         layer4MatchBuild.setIpv4Source(new Ipv4Prefix(inetSrcAddressString));
364         layer4MatchBuild
365                 .setIpv4Destination(new Ipv4Prefix(inetDstAddressString));
366         return layer4MatchBuild.build();
367
368     }
369
370     private static Layer3Match setLayer3MatchAsIpv6(
371             final Inet6Address inetSourceAddress,
372             final Inet6Address inetDestAddress) {
373         String inetSrcAddressString = InetAddresses
374                 .toAddrString(inetSourceAddress);
375         String inetDstAddressString = InetAddresses
376                 .toAddrString(inetDestAddress);
377         Ipv6MatchBuilder layer6MatchBuild = new Ipv6MatchBuilder();
378
379         layer6MatchBuild.setIpv6Source(new Ipv6Prefix(inetSrcAddressString));
380         layer6MatchBuild
381                 .setIpv6Destination(new Ipv6Prefix(inetDstAddressString));
382         return layer6MatchBuild.build();
383     }
384
385 }