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.Controller;
16 import org.opendaylight.controller.sal.action.Drop;
17 import org.opendaylight.controller.sal.action.Flood;
18 import org.opendaylight.controller.sal.action.FloodAll;
19 import org.opendaylight.controller.sal.action.HwPath;
20 import org.opendaylight.controller.sal.action.Loopback;
21 import org.opendaylight.controller.sal.action.Output;
22 import org.opendaylight.controller.sal.action.PopVlan;
23 import org.opendaylight.controller.sal.action.PushVlan;
24 import org.opendaylight.controller.sal.action.SetDlDst;
25 import org.opendaylight.controller.sal.action.SetDlSrc;
26 import org.opendaylight.controller.sal.action.SetDlType;
27 import org.opendaylight.controller.sal.action.SetNextHop;
28 import org.opendaylight.controller.sal.action.SetNwDst;
29 import org.opendaylight.controller.sal.action.SetNwSrc;
30 import org.opendaylight.controller.sal.action.SetNwTos;
31 import org.opendaylight.controller.sal.action.SetTpDst;
32 import org.opendaylight.controller.sal.action.SetTpSrc;
33 import org.opendaylight.controller.sal.action.SetVlanCfi;
34 import org.opendaylight.controller.sal.action.SetVlanId;
35 import org.opendaylight.controller.sal.action.SetVlanPcp;
36 import org.opendaylight.controller.sal.action.SwPath;
37 import org.opendaylight.controller.sal.core.NodeConnector;
38 import org.opendaylight.controller.sal.flowprogrammer.Flow;
39 import org.opendaylight.controller.sal.match.Match;
40 import org.opendaylight.controller.sal.match.MatchField;
41 import org.opendaylight.controller.sal.match.MatchType;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAddedBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.ControllerActionBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.OutputActionBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Action;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.ActionBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.arp.match.fields.ArpSourceHardwareAddressBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.arp.match.fields.ArpTargetHardwareAddressBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetDestinationBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetSourceBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetTypeBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.EthernetMatch;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.EthernetMatchBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.IpMatch;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.IpMatchBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.Layer3Match;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.Layer4Match;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.VlanMatch;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.VlanMatchBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.ArpMatchBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv4MatchBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv6MatchBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.SctpMatchBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.TcpMatchBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.UdpMatchBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.vlan.match.fields.VlanIdBuilder;
78 public class FromSalConversionsUtils {
80 // source: http://en.wikipedia.org/wiki/Ethertype
81 private static final Short ETHERNET_ARP = new Short((short) 0x0806);
83 // source: http://en.wikipedia.org/wiki/List_of_IP_protocol_numbers
84 private static final short TCP = (short) 0x06;
85 private static final short UDP = (short) 0x11;
86 private static final short SCTP = (short) 0x84;
88 private FromSalConversionsUtils() {
92 public static FlowAdded flowFrom(Flow sourceFlow) {
93 if (sourceFlow != null) {
94 final FlowAddedBuilder targetFlow = new FlowAddedBuilder();
96 targetFlow.setHardTimeout(new Integer(sourceFlow.getHardTimeout()));
97 targetFlow.setIdleTimeout(new Integer(sourceFlow.getIdleTimeout()));
98 targetFlow.setPriority(new Integer(sourceFlow.getPriority()));
99 targetFlow.setCookie(new BigInteger(String.valueOf(sourceFlow.getId())));
101 List<org.opendaylight.controller.sal.action.Action> sourceActions = sourceFlow.getActions();
102 List<Action> targetActions = new ArrayList<>();
103 for (org.opendaylight.controller.sal.action.Action sourceAction : sourceActions) {
104 targetActions.add(actionFrom(sourceAction));
106 targetFlow.setAction(targetActions);
108 targetFlow.setMatch(matchFrom(sourceFlow.getMatch()));
110 return targetFlow.build();
115 private static Action actionFrom(org.opendaylight.controller.sal.action.Action sourceAction) {
117 ActionBuilder targetActionBuilder = new ActionBuilder();
118 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.Action targetAction = null;
120 if (sourceAction instanceof Controller) {
121 targetAction = new ControllerActionBuilder().build();
122 } else if (sourceAction instanceof Drop) {
123 // TODO: define maping
124 } else if (sourceAction instanceof Flood) {
125 // TODO: define maping
126 } else if (sourceAction instanceof FloodAll) {
127 // TODO: define maping
128 } else if (sourceAction instanceof HwPath) {
129 // TODO: define maping
130 } else if (sourceAction instanceof Loopback) {
131 // TODO: define maping
132 } else if (sourceAction instanceof Output) {
133 NodeConnector nodeConnector = ((Output) sourceAction).getPort();
135 OutputActionBuilder outputActionBuilder = new OutputActionBuilder();
136 outputActionBuilder.setOutputNodeConnector(nodeConnectorToUri(nodeConnector));
137 targetAction = outputActionBuilder.build();
139 } else if (sourceAction instanceof PopVlan) {
140 // TODO: define maping
141 } else if (sourceAction instanceof PushVlan) {
142 // TODO: define maping
143 } else if (sourceAction instanceof SetDlDst) {
144 // TODO: define maping
145 } else if (sourceAction instanceof SetDlSrc) {
146 // TODO: define maping
147 } else if (sourceAction instanceof SetDlType) {
148 // TODO: define maping
149 } else if (sourceAction instanceof SetNextHop) {
150 // TODO: define maping
151 } else if (sourceAction instanceof SetNwDst) {
152 // TODO: define maping
153 } else if (sourceAction instanceof SetNwSrc) {
154 // TODO: define maping
155 } else if (sourceAction instanceof SetNwTos) {
156 // TODO: define maping
157 } else if (sourceAction instanceof SetTpDst) {
158 // TODO: define maping
159 } else if (sourceAction instanceof SetTpSrc) {
160 // TODO: define maping
161 } else if (sourceAction instanceof SetVlanCfi) {
162 // TODO: define maping
163 } else if (sourceAction instanceof SetVlanId) {
164 // TODO: define maping
165 } else if (sourceAction instanceof SetVlanPcp) {
166 // TODO: define maping
167 } else if (sourceAction instanceof SwPath) {
168 // TODO: define maping
171 targetActionBuilder.setAction(targetAction);
173 return targetActionBuilder.build();
176 private static List<Uri> nodeConnectorToUri(NodeConnector nodeConnector) {
177 // TODO Define mapping
181 private static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Match matchFrom(
183 if (sourceMatch != null) {
184 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();
186 targetBuilder.setEthernetMatch(ethernetMatchFrom(sourceMatch));
187 targetBuilder.setIpMatch(ipMatchFrom(sourceMatch));
188 targetBuilder.setVlanMatch(vlanMatchFrom(sourceMatch));
189 targetBuilder.setLayer3Match(layer3Match(sourceMatch));
190 targetBuilder.setLayer4Match(layer4Match(sourceMatch));
192 return targetBuilder.build();
198 private static Layer4Match layer4Match(final Match sourceMatch) {
199 MatchField nwProto = sourceMatch.getField(MatchType.NW_PROTO);
200 Short nwProtocolSource = null;
201 if (nwProto != null && nwProto.getValue() != null) {
202 nwProtocolSource = (Short) (nwProto.getValue());
205 switch (nwProtocolSource) {
207 return Layer4MatchAsTcp(sourceMatch);
209 return Layer4MatchAsUdp(sourceMatch);
211 return Layer4MatchAsSctp(sourceMatch);
216 private static Layer4Match Layer4MatchAsSctp(final Match sourceMatch) {
217 SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
219 Integer sourcePort = transportPortFrom(sourceMatch, MatchType.TP_SRC);
220 Integer destinationPort = transportPortFrom(sourceMatch, MatchType.TP_DST);
222 if (sourcePort != null) {
223 sctpMatchBuilder.setSctpSourcePort(new PortNumber(sourcePort));
225 if (destinationPort != null) {
226 sctpMatchBuilder.setSctpDestinationPort(new PortNumber(destinationPort));
229 return sctpMatchBuilder.build();
232 private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
233 UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
235 Integer sourcePort = transportPortFrom(sourceMatch, MatchType.TP_SRC);
236 Integer destinationPort = transportPortFrom(sourceMatch, MatchType.TP_DST);
238 if (sourcePort != null) {
239 udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
242 if (destinationPort != null) {
243 udpMatchBuilder.setUdpDestinationPort(new PortNumber(destinationPort));
246 return udpMatchBuilder.build();
249 private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
250 TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
252 Integer sourcePort = transportPortFrom(sourceMatch, MatchType.TP_SRC);
253 Integer destinationPort = transportPortFrom(sourceMatch, MatchType.TP_DST);
255 if (sourcePort != null) {
256 tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
258 if (destinationPort != null) {
259 tcpMatchBuilder.setTcpDestinationPort(new PortNumber(destinationPort));
262 return tcpMatchBuilder.build();
265 private static Integer transportPortFrom(final Match sourceMatch, final MatchType matchType) {
266 MatchField transportPort = sourceMatch.getField(matchType);
267 if (transportPort != null && transportPort.getValue() != null) {
268 return (Integer) (transportPort.getValue());
273 private static VlanMatch vlanMatchFrom(final Match sourceMatch) {
274 VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder();
276 MatchField vlan = sourceMatch.getField(MatchType.DL_VLAN);
277 if (vlan != null && vlan.getValue() != null) {
278 VlanIdBuilder vlanIDBuilder = new VlanIdBuilder();
279 vlanIDBuilder.setVlanId(new VlanId((Integer) (vlan.getValue())));
280 vlanMatchBuild.setVlanId(vlanIDBuilder.build());
283 MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR);
284 if (vlanPriority != null && vlanPriority.getValue() != null) {
285 vlanMatchBuild.setVlanPcp(new VlanPcp((Short) (vlanPriority.getValue())));
288 return vlanMatchBuild.build();
291 private static IpMatch ipMatchFrom(final Match sourceMatch) {
292 IpMatchBuilder targetIpMatchBuild = new IpMatchBuilder();
293 MatchField networkTos = sourceMatch.getField(MatchType.NW_TOS);
294 if (networkTos != null && networkTos.getValue() != null) {
295 Dscp dscp = new Dscp((Short) (networkTos.getValue()));
296 targetIpMatchBuild.setIpDscp(dscp);
299 MatchField protocol = sourceMatch.getField(MatchType.NW_PROTO);
300 if (protocol != null && protocol.getValue() != null) {
301 targetIpMatchBuild.setIpProtocol((Short) (protocol.getValue()));
304 return targetIpMatchBuild.build();
308 private static EthernetMatch ethernetMatchFrom(final Match sourceMatch) {
309 final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder();
311 EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder()
312 .setAddress(ethernetSourceAddressFrom(sourceMatch));
313 targetEthMatchBuild.setEthernetSource(ethSourBuild.build());
315 final MatchField dataLinkDest = sourceMatch.getField(DL_DST);
316 if (dataLinkDest != null && dataLinkDest.getValue() != null) {
317 final MacAddress macDestAddress;
318 macDestAddress = new MacAddress((String) (dataLinkDest.getValue()));
319 EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder().setAddress(macDestAddress);
320 targetEthMatchBuild.setEthernetDestination(ethDestBuild.build());
323 final MatchField dataLinkType = sourceMatch.getField(MatchType.DL_TYPE);
324 if (dataLinkType != null && dataLinkType.getValue() != null) {
325 EtherType etherType = new EtherType((Long) (dataLinkType.getValue()));
326 EthernetTypeBuilder ethType = new EthernetTypeBuilder().setType(etherType);
327 targetEthMatchBuild.setEthernetType(ethType.build());
329 return targetEthMatchBuild.build();
332 private static MacAddress ethernetSourceAddressFrom(final Match sourceMatch) {
333 final MatchField dataLinkSource = sourceMatch.getField(DL_SRC);
334 if (dataLinkSource != null && dataLinkSource.getValue() != null) {
335 return new MacAddress(new MacAddress((String) (dataLinkSource.getValue())));
341 private static Layer3Match layer3Match(final Match sourceMatch) {
342 InetAddress inetSourceAddress = null;
343 MatchField netSource = sourceMatch.getField(MatchType.NW_SRC);
344 if (netSource != null && netSource.getValue() != null) {
345 inetSourceAddress = (InetAddress) (netSource.getValue());
348 InetAddress inetDestAddress = null;
349 MatchField netDest = sourceMatch.getField(MatchType.NW_DST);
350 if (netSource != null && netSource.getValue() != null) {
351 inetDestAddress = (InetAddress) (netDest.getValue());
354 if ((inetSourceAddress instanceof Inet4Address) && (inetDestAddress instanceof Inet4Address)) {
355 MatchField dataLinkType = sourceMatch.getField(DL_TYPE);
357 if (dataLinkType != null && dataLinkType.getValue() != null) {
358 dLType = (Short) (dataLinkType.getValue());
360 if (dLType.equals(ETHERNET_ARP)) {
361 return setLayer3MatchAsArp(sourceMatch, (Inet4Address) inetSourceAddress,
362 (Inet4Address) inetDestAddress);
364 return setLayer3MatchAsIpv4((Inet4Address) inetSourceAddress, (Inet4Address) inetDestAddress);
366 } else if ((inetSourceAddress instanceof Inet6Address) && (inetDestAddress instanceof Inet6Address)) {
367 return setLayer3MatchAsIpv6((Inet6Address) inetSourceAddress, (Inet6Address) inetDestAddress);
374 private static Layer3Match setLayer3MatchAsArp(final Match sourceMatch, final Inet4Address inetSourceAddress,
375 final Inet4Address inetDestAddress) {
376 byte[] inetSourceAddressValue = inetSourceAddress.getAddress();
377 Ipv4Prefix ipv4SourcePrefix = new Ipv4Prefix(Arrays.toString(inetSourceAddressValue));
379 byte[] inetDestAddressValue = inetDestAddress.getAddress();
380 Ipv4Prefix ipv4DestPrefix = new Ipv4Prefix(Arrays.toString(inetDestAddressValue));
382 ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
384 arpMatchBuilder.setArpSourceTransportAddress(ipv4SourcePrefix);
385 arpMatchBuilder.setArpSourceTransportAddress(ipv4DestPrefix);
387 ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
388 arpSourceHardwareAddressBuilder.setAddress(ethernetSourceAddressFrom(sourceMatch));
389 arpMatchBuilder.setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder.build());
391 ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
392 arpTargetHardwareAddressBuilder.setAddress(ethernetDestAddressFrom(sourceMatch));
393 arpMatchBuilder.setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder.build());
395 return arpMatchBuilder.build();
399 private static MacAddress ethernetDestAddressFrom(final Match sourceMatch) {
400 final MatchField dataLinkDest = sourceMatch.getField(DL_DST);
401 if (dataLinkDest != null && dataLinkDest.getValue() != null) {
402 return new MacAddress((String) (dataLinkDest.getValue()));
407 private static Layer3Match setLayer3MatchAsIpv4(final Inet4Address inetSourceAddress,
408 final Inet4Address inetDestAddress) {
409 byte[] inetAddressValue = inetSourceAddress.getAddress();
411 Ipv4MatchBuilder layer4MatchBuild = new Ipv4MatchBuilder();
412 layer4MatchBuild.setIpv4Source(new Ipv4Prefix(Arrays.toString(inetAddressValue)));
413 return layer4MatchBuild.build();
417 private static Layer3Match setLayer3MatchAsIpv6(final Inet6Address inetSourceAddress,
418 final Inet6Address inetDestAddress) {
419 byte[] inetAddressValue = inetSourceAddress.getAddress();
420 Ipv6MatchBuilder layer6MatchBuild = new Ipv6MatchBuilder();
422 layer6MatchBuild.setIpv6Source(new Ipv6Prefix(Arrays.toString(inetAddressValue)));
423 return layer6MatchBuild.build();