1 package org.opendaylight.controller.sal.compability;
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;
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;
15 import org.opendaylight.controller.sal.action.*;
16 import org.opendaylight.controller.sal.core.NodeConnector;
17 import org.opendaylight.controller.sal.core.Node;
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;
62 import com.google.common.net.InetAddresses;
67 import static org.opendaylight.controller.sal.compability.ProtocolConstants.*;
68 import static org.opendaylight.controller.sal.compability.NodeMapping.*;
70 public class FromSalConversionsUtils {
72 private FromSalConversionsUtils() {
76 public static GetNodeConnectorStatisticsInput nodeConnectorStatistics(
77 NodeConnector connector) {
78 GetNodeConnectorStatisticsInputBuilder target = new GetNodeConnectorStatisticsInputBuilder();
80 NodeRef nodeRef = toNodeRef(connector.getNode());
81 target.setNode(nodeRef);
83 NodeConnectorRef nodeConnectorRef = toNodeConnectorRef(connector);
84 target.setNodeConnector(nodeConnectorRef);
86 return target.build();
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();
103 public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Match toMatch(
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();
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));
114 return targetBuilder.build();
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) {
127 return Layer4MatchAsTcp(sourceMatch);
129 return Layer4MatchAsUdp(sourceMatch);
131 return Layer4MatchAsSctp(sourceMatch);
137 private static Layer4Match Layer4MatchAsSctp(final Match sourceMatch) {
138 SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
140 Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
141 Integer destinationPort = transportPort(sourceMatch,
144 if (sourcePort != null) {
145 sctpMatchBuilder.setSctpSourcePort(new PortNumber(sourcePort));
147 if (destinationPort != null) {
148 sctpMatchBuilder.setSctpDestinationPort(new PortNumber(
152 return sctpMatchBuilder.build();
155 private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
156 UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
158 Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
159 Integer destinationPort = transportPort(sourceMatch,
162 if (sourcePort != null) {
163 udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
166 if (destinationPort != null) {
167 udpMatchBuilder.setUdpDestinationPort(new PortNumber(
171 return udpMatchBuilder.build();
174 private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
175 TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
177 Integer sourcePort = transportPort(sourceMatch, MatchType.TP_SRC);
178 Integer destinationPort = transportPort(sourceMatch,
181 if (sourcePort != null) {
182 tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
184 if (destinationPort != null) {
185 tcpMatchBuilder.setTcpDestinationPort(new PortNumber(
189 return tcpMatchBuilder.build();
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
203 private static VlanMatch vlanMatch(final Match sourceMatch) {
204 VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder();
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());
214 MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR);
215 if (vlanPriority != null && vlanPriority.getValue() != null) {
216 vlanMatchBuild.setVlanPcp(new VlanPcp((short) ((byte) vlanPriority
220 return vlanMatchBuild.build();
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
230 targetIpMatchBuild.setIpDscp(dscp);
233 MatchField protocol = sourceMatch.getField(MatchType.NW_PROTO);
234 if (protocol != null && protocol.getValue() != null) {
235 targetIpMatchBuild.setIpProtocol((short) ((byte) protocol
239 return targetIpMatchBuild.build();
243 private static EthernetMatch ethernetMatch(final Match sourceMatch) {
244 final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder();
246 EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder()
247 .setAddress(ethernetSourceAddress(sourceMatch));
248 targetEthMatchBuild.setEthernetSource(ethSourBuild.build());
250 EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder()
251 .setAddress(ethernetDestAddress(sourceMatch));
252 targetEthMatchBuild.setEthernetDestination(ethDestBuild.build());
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()
260 targetEthMatchBuild.setEthernetType(ethType.build());
262 return targetEthMatchBuild.build();
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());
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());
281 InetAddress inetDestAddress = null;
282 MatchField netDest = sourceMatch.getField(MatchType.NW_DST);
283 if (netSource != null && netSource.getValue() != null) {
284 inetDestAddress = (InetAddress) (netDest.getValue());
287 if ((inetSourceAddress instanceof Inet4Address)
288 && (inetDestAddress instanceof Inet4Address)) {
289 MatchField dataLinkType = sourceMatch.getField(DL_TYPE);
291 if (dataLinkType != null && dataLinkType.getValue() != null) {
292 dLType = (Short) (dataLinkType.getValue());
294 if (dLType != null && dLType.equals(ETHERNET_ARP)) {
295 return setLayer3MatchAsArp(sourceMatch,
296 (Inet4Address) inetSourceAddress,
297 (Inet4Address) inetDestAddress);
299 return setLayer3MatchAsIpv4((Inet4Address) inetSourceAddress,
300 (Inet4Address) inetDestAddress);
302 } else if ((inetSourceAddress instanceof Inet6Address)
303 && (inetDestAddress instanceof Inet6Address)) {
304 return setLayer3MatchAsIpv6((Inet6Address) inetSourceAddress,
305 (Inet6Address) inetDestAddress);
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);
319 String inetDestAddressValue = InetAddresses
320 .toAddrString(inetDestAddress);
321 Ipv4Prefix ipv4DestPrefix = new Ipv4Prefix(inetDestAddressValue);
323 ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
325 arpMatchBuilder.setArpSourceTransportAddress(ipv4SourcePrefix);
326 arpMatchBuilder.setArpTargetTransportAddress(ipv4DestPrefix);
328 ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
329 arpSourceHardwareAddressBuilder
330 .setAddress(ethernetSourceAddress(sourceMatch));
332 .setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder
335 ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
336 arpTargetHardwareAddressBuilder
337 .setAddress(ethernetDestAddress(sourceMatch));
339 .setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder
342 return arpMatchBuilder.build();
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());
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);
362 Ipv4MatchBuilder layer4MatchBuild = new Ipv4MatchBuilder();
363 layer4MatchBuild.setIpv4Source(new Ipv4Prefix(inetSrcAddressString));
365 .setIpv4Destination(new Ipv4Prefix(inetDstAddressString));
366 return layer4MatchBuild.build();
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();
379 layer6MatchBuild.setIpv6Source(new Ipv6Prefix(inetSrcAddressString));
381 .setIpv6Destination(new Ipv6Prefix(inetDstAddressString));
382 return layer6MatchBuild.build();