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.flowprogrammer.Flow;
18 import org.opendaylight.controller.sal.match.Match;
19 import org.opendaylight.controller.sal.match.MatchField;
20 import org.opendaylight.controller.sal.match.MatchType;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.*;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAddedBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.VlanCfi;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.*;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.Address;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.address.Ipv4Builder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.address.Ipv6Builder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Action;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.ActionBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.arp.match.fields.ArpSourceHardwareAddressBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.arp.match.fields.ArpTargetHardwareAddressBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetDestinationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetSourceBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetTypeBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.*;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.ArpMatchBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv4MatchBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv6MatchBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.SctpMatchBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.TcpMatchBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.UdpMatchBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.vlan.match.fields.VlanIdBuilder;
49 public class FromSalConversionsUtils {
51 // source: http://en.wikipedia.org/wiki/Ethertype
52 private static final Short ETHERNET_ARP = new Short((short) 0x0806);
54 // source: http://en.wikipedia.org/wiki/List_of_IP_protocol_numbers
55 private static final short TCP = (short) 0x06;
56 private static final short UDP = (short) 0x11;
57 private static final short SCTP = (short) 0x84;
59 private FromSalConversionsUtils() {
63 public static FlowAdded flowFrom(Flow sourceFlow) {
64 if (sourceFlow != null) {
65 final FlowAddedBuilder targetFlow = new FlowAddedBuilder();
67 targetFlow.setHardTimeout(new Integer(sourceFlow.getHardTimeout()));
68 targetFlow.setIdleTimeout(new Integer(sourceFlow.getIdleTimeout()));
69 targetFlow.setPriority(new Integer(sourceFlow.getPriority()));
70 targetFlow.setCookie(new BigInteger(String.valueOf(sourceFlow.getId())));
72 List<org.opendaylight.controller.sal.action.Action> sourceActions = sourceFlow.getActions();
73 List<Action> targetActions = new ArrayList<>();
74 for (org.opendaylight.controller.sal.action.Action sourceAction : sourceActions) {
75 targetActions.add(actionFrom(sourceAction));
77 targetFlow.setAction(targetActions);
79 targetFlow.setMatch(matchFrom(sourceFlow.getMatch()));
81 return targetFlow.build();
86 private static Action actionFrom(org.opendaylight.controller.sal.action.Action sourceAction) {
88 ActionBuilder targetActionBuilder = new ActionBuilder();
89 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.Action targetAction = null;
91 if (sourceAction instanceof Controller) {
92 targetAction = new ControllerActionBuilder().build();
93 } else if (sourceAction instanceof Drop) {
94 targetAction = new DropActionBuilder().build();
95 } else if (sourceAction instanceof Flood) {
96 targetAction = new FloodActionBuilder().build();
97 } else if (sourceAction instanceof FloodAll) {
98 targetAction = new FloodAllActionBuilder().build();
99 } else if (sourceAction instanceof HwPath) {
100 targetAction = new HwPathActionBuilder().build();
101 } else if (sourceAction instanceof Loopback) {
102 targetAction = new LoopbackActionBuilder().build();
103 } else if (sourceAction instanceof Output) {
104 NodeConnector nodeConnector = ((Output) sourceAction).getPort();
106 OutputActionBuilder outputActionBuilder = new OutputActionBuilder();
107 outputActionBuilder.setOutputNodeConnector(nodeConnectorToUri(nodeConnector));
108 targetAction = outputActionBuilder.build();
110 } else if (sourceAction instanceof PopVlan) {
111 targetAction = new PopVlanActionBuilder().build();
112 } else if (sourceAction instanceof PushVlan) {
113 PushVlan pushVlan = (PushVlan) sourceAction;
114 PushVlanActionBuilder pushVlanActionBuilder = new PushVlanActionBuilder();
116 pushVlanActionBuilder.setCfi(new VlanCfi(pushVlan.getCfi()));
117 pushVlanActionBuilder.setVlanId(new VlanId(pushVlan.getVlanId()));
118 pushVlanActionBuilder.setPcp(pushVlan.getPcp());
119 pushVlanActionBuilder.setTag(pushVlan.getTag());
120 targetAction = pushVlanActionBuilder.build();
121 } else if (sourceAction instanceof SetDlDst) {
122 SetDlDst setDlDst = (SetDlDst) sourceAction;
123 SetDlDstActionBuilder setDlDstActionBuilder = new SetDlDstActionBuilder();
125 setDlDstActionBuilder.setAddress(new MacAddress(Arrays.toString(setDlDst.getDlAddress())));
126 targetAction = setDlDstActionBuilder.build();
127 } else if (sourceAction instanceof SetDlSrc) {
128 SetDlSrc setDlSrc = (SetDlSrc) sourceAction;
129 SetDlSrcActionBuilder setDlSrcActionBuilder = new SetDlSrcActionBuilder();
131 setDlSrcActionBuilder.setAddress(new MacAddress(Arrays.toString(setDlSrc.getDlAddress())));
132 targetAction = setDlSrcActionBuilder.build();
133 } else if (sourceAction instanceof SetDlType) {
134 SetDlType setDlType = (SetDlType) sourceAction;
135 SetDlTypeActionBuilder setDlTypeActionBuilder = new SetDlTypeActionBuilder();
137 setDlTypeActionBuilder.setDlType(new EtherType(new Long(setDlType.getDlType())));
138 targetAction = setDlTypeActionBuilder.build();
139 } else if (sourceAction instanceof SetNextHop) {
140 SetNextHop setNextHop = (SetNextHop) sourceAction;
141 SetNextHopActionBuilder setNextHopActionBuilder = new SetNextHopActionBuilder();
143 InetAddress inetAddress = setNextHop.getAddress();
144 setNextHopActionBuilder.setAddress(addressFromAction(inetAddress));
146 targetAction = setNextHopActionBuilder.build();
147 } else if (sourceAction instanceof SetNwDst) {
148 SetNwDst setNwDst = (SetNwDst) sourceAction;
149 SetNwDstActionBuilder setNwDstActionBuilder = new SetNwDstActionBuilder();
151 InetAddress inetAddress = setNwDst.getAddress();
152 setNwDstActionBuilder.setAddress(addressFromAction(inetAddress));
154 targetAction = setNwDstActionBuilder.build();
155 } else if (sourceAction instanceof SetNwSrc) {
156 SetNwSrc setNwSrc = (SetNwSrc) sourceAction;
157 SetNwSrcActionBuilder setNwSrcActionBuilder = new SetNwSrcActionBuilder();
159 InetAddress inetAddress = setNwSrc.getAddress();
160 setNwSrcActionBuilder.setAddress(addressFromAction(inetAddress));
162 targetAction = setNwSrcActionBuilder.build();
163 } else if (sourceAction instanceof SetNwTos) {
164 SetNwTos setNwTos = (SetNwTos) sourceAction;
165 SetNwTosActionBuilder setNwTosActionBuilder = new SetNwTosActionBuilder();
167 setNwTosActionBuilder.setTos(setNwTos.getNwTos());
168 targetAction = setNwTosActionBuilder.build();
169 } else if (sourceAction instanceof SetTpDst) {
170 SetTpDst setTpDst = (SetTpDst) sourceAction;
171 SetTpDstActionBuilder setTpDstActionBuilder = new SetTpDstActionBuilder();
173 setTpDstActionBuilder.setPort(new PortNumber(setTpDst.getPort()));
175 targetAction = setTpDstActionBuilder.build();
176 } else if (sourceAction instanceof SetTpSrc) {
177 SetTpSrc setTpSrc = (SetTpSrc) sourceAction;
178 SetTpSrcActionBuilder setTpSrcActionBuilder = new SetTpSrcActionBuilder();
180 setTpSrcActionBuilder.setPort(new PortNumber(setTpSrc.getPort()));
182 targetAction = setTpSrcActionBuilder.build();
183 } else if (sourceAction instanceof SetVlanCfi) {
184 SetVlanCfi setVlanCfi = (SetVlanCfi) sourceAction;
185 SetVlanCfiActionBuilder setVlanCfiActionBuilder = new SetVlanCfiActionBuilder();
187 setVlanCfiActionBuilder.setVlanCfi(new VlanCfi(setVlanCfi.getCfi()));
189 targetAction = setVlanCfiActionBuilder.build();
190 } else if (sourceAction instanceof SetVlanId) {
191 SetVlanId setVlanId = (SetVlanId) sourceAction;
192 SetVlanIdActionBuilder setVlanIdActionBuilder = new SetVlanIdActionBuilder();
194 setVlanIdActionBuilder.setVlanId(new VlanId(setVlanId.getVlanId()));
196 targetAction = setVlanIdActionBuilder.build();
197 } else if (sourceAction instanceof SetVlanPcp) {
198 SetVlanPcp setVlanPcp = (SetVlanPcp) sourceAction;
199 SetVlanPcpActionBuilder setVlanPcpActionBuilder = new SetVlanPcpActionBuilder();
201 setVlanPcpActionBuilder.setVlanPcp(new VlanPcp((short) setVlanPcp.getPcp()));
203 targetAction = setVlanPcpActionBuilder.build();
204 } else if (sourceAction instanceof SwPath) {
205 targetAction = new SwPathActionBuilder().build();
208 targetActionBuilder.setAction(targetAction);
210 return targetActionBuilder.build();
213 private static Address addressFromAction(InetAddress inetAddress) {
214 byte[] byteInetAddresss = inetAddress.getAddress();
215 if (inetAddress instanceof Inet4Address) {
216 Ipv4Builder ipv4Builder = new Ipv4Builder();
217 ipv4Builder.setIpv4Address(new Ipv4Prefix(Arrays.toString(byteInetAddresss)));
218 return ipv4Builder.build();
219 } else if (inetAddress instanceof Inet6Address) {
220 Ipv6Builder ipv6Builder = new Ipv6Builder();
221 ipv6Builder.setIpv6Address(new Ipv6Prefix(Arrays.toString(byteInetAddresss)));
222 return ipv6Builder.build();
227 private static List<Uri> nodeConnectorToUri(NodeConnector nodeConnector) {
228 // TODO Define mapping
232 private static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Match matchFrom(
234 if (sourceMatch != null) {
235 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();
237 targetBuilder.setEthernetMatch(ethernetMatchFrom(sourceMatch));
238 targetBuilder.setIpMatch(ipMatchFrom(sourceMatch));
239 targetBuilder.setVlanMatch(vlanMatchFrom(sourceMatch));
240 targetBuilder.setLayer3Match(layer3Match(sourceMatch));
241 targetBuilder.setLayer4Match(layer4Match(sourceMatch));
243 return targetBuilder.build();
249 private static Layer4Match layer4Match(final Match sourceMatch) {
250 MatchField nwProto = sourceMatch.getField(MatchType.NW_PROTO);
251 Short nwProtocolSource = null;
252 if (nwProto != null && nwProto.getValue() != null) {
253 nwProtocolSource = (Short) (nwProto.getValue());
256 switch (nwProtocolSource) {
258 return Layer4MatchAsTcp(sourceMatch);
260 return Layer4MatchAsUdp(sourceMatch);
262 return Layer4MatchAsSctp(sourceMatch);
267 private static Layer4Match Layer4MatchAsSctp(final Match sourceMatch) {
268 SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
270 Integer sourcePort = transportPortFrom(sourceMatch, MatchType.TP_SRC);
271 Integer destinationPort = transportPortFrom(sourceMatch, MatchType.TP_DST);
273 if (sourcePort != null) {
274 sctpMatchBuilder.setSctpSourcePort(new PortNumber(sourcePort));
276 if (destinationPort != null) {
277 sctpMatchBuilder.setSctpDestinationPort(new PortNumber(destinationPort));
280 return sctpMatchBuilder.build();
283 private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
284 UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
286 Integer sourcePort = transportPortFrom(sourceMatch, MatchType.TP_SRC);
287 Integer destinationPort = transportPortFrom(sourceMatch, MatchType.TP_DST);
289 if (sourcePort != null) {
290 udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
293 if (destinationPort != null) {
294 udpMatchBuilder.setUdpDestinationPort(new PortNumber(destinationPort));
297 return udpMatchBuilder.build();
300 private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
301 TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
303 Integer sourcePort = transportPortFrom(sourceMatch, MatchType.TP_SRC);
304 Integer destinationPort = transportPortFrom(sourceMatch, MatchType.TP_DST);
306 if (sourcePort != null) {
307 tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
309 if (destinationPort != null) {
310 tcpMatchBuilder.setTcpDestinationPort(new PortNumber(destinationPort));
313 return tcpMatchBuilder.build();
316 private static Integer transportPortFrom(final Match sourceMatch, final MatchType matchType) {
317 MatchField transportPort = sourceMatch.getField(matchType);
318 if (transportPort != null && transportPort.getValue() != null) {
319 return (Integer) (transportPort.getValue());
324 private static VlanMatch vlanMatchFrom(final Match sourceMatch) {
325 VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder();
327 MatchField vlan = sourceMatch.getField(MatchType.DL_VLAN);
328 if (vlan != null && vlan.getValue() != null) {
329 VlanIdBuilder vlanIDBuilder = new VlanIdBuilder();
330 vlanIDBuilder.setVlanId(new VlanId((Integer) (vlan.getValue())));
331 vlanMatchBuild.setVlanId(vlanIDBuilder.build());
334 MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR);
335 if (vlanPriority != null && vlanPriority.getValue() != null) {
336 vlanMatchBuild.setVlanPcp(new VlanPcp((Short) (vlanPriority.getValue())));
339 return vlanMatchBuild.build();
342 private static IpMatch ipMatchFrom(final Match sourceMatch) {
343 IpMatchBuilder targetIpMatchBuild = new IpMatchBuilder();
344 MatchField networkTos = sourceMatch.getField(MatchType.NW_TOS);
345 if (networkTos != null && networkTos.getValue() != null) {
346 Dscp dscp = new Dscp((Short) (networkTos.getValue()));
347 targetIpMatchBuild.setIpDscp(dscp);
350 MatchField protocol = sourceMatch.getField(MatchType.NW_PROTO);
351 if (protocol != null && protocol.getValue() != null) {
352 targetIpMatchBuild.setIpProtocol((Short) (protocol.getValue()));
355 return targetIpMatchBuild.build();
359 private static EthernetMatch ethernetMatchFrom(final Match sourceMatch) {
360 final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder();
362 EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder()
363 .setAddress(ethernetSourceAddressFrom(sourceMatch));
364 targetEthMatchBuild.setEthernetSource(ethSourBuild.build());
366 EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder()
367 .setAddress(ethernetDestAddressFrom(sourceMatch));
368 targetEthMatchBuild.setEthernetDestination(ethDestBuild.build());
370 final MatchField dataLinkType = sourceMatch.getField(MatchType.DL_TYPE);
371 if (dataLinkType != null && dataLinkType.getValue() != null) {
372 EtherType etherType = new EtherType((Long) (dataLinkType.getValue()));
373 EthernetTypeBuilder ethType = new EthernetTypeBuilder().setType(etherType);
374 targetEthMatchBuild.setEthernetType(ethType.build());
376 return targetEthMatchBuild.build();
379 private static MacAddress ethernetSourceAddressFrom(final Match sourceMatch) {
380 final MatchField dataLinkSource = sourceMatch.getField(DL_SRC);
381 if (dataLinkSource != null && dataLinkSource.getValue() != null) {
382 return new MacAddress(new MacAddress((String) (dataLinkSource.getValue())));
388 private static Layer3Match layer3Match(final Match sourceMatch) {
389 InetAddress inetSourceAddress = null;
390 MatchField netSource = sourceMatch.getField(MatchType.NW_SRC);
391 if (netSource != null && netSource.getValue() != null) {
392 inetSourceAddress = (InetAddress) (netSource.getValue());
395 InetAddress inetDestAddress = null;
396 MatchField netDest = sourceMatch.getField(MatchType.NW_DST);
397 if (netSource != null && netSource.getValue() != null) {
398 inetDestAddress = (InetAddress) (netDest.getValue());
401 if ((inetSourceAddress instanceof Inet4Address) && (inetDestAddress instanceof Inet4Address)) {
402 MatchField dataLinkType = sourceMatch.getField(DL_TYPE);
404 if (dataLinkType != null && dataLinkType.getValue() != null) {
405 dLType = (Short) (dataLinkType.getValue());
407 if (dLType.equals(ETHERNET_ARP)) {
408 return setLayer3MatchAsArp(sourceMatch, (Inet4Address) inetSourceAddress,
409 (Inet4Address) inetDestAddress);
411 return setLayer3MatchAsIpv4((Inet4Address) inetSourceAddress, (Inet4Address) inetDestAddress);
413 } else if ((inetSourceAddress instanceof Inet6Address) && (inetDestAddress instanceof Inet6Address)) {
414 return setLayer3MatchAsIpv6((Inet6Address) inetSourceAddress, (Inet6Address) inetDestAddress);
421 private static Layer3Match setLayer3MatchAsArp(final Match sourceMatch, final Inet4Address inetSourceAddress,
422 final Inet4Address inetDestAddress) {
423 byte[] inetSourceAddressValue = inetSourceAddress.getAddress();
424 Ipv4Prefix ipv4SourcePrefix = new Ipv4Prefix(Arrays.toString(inetSourceAddressValue));
426 byte[] inetDestAddressValue = inetDestAddress.getAddress();
427 Ipv4Prefix ipv4DestPrefix = new Ipv4Prefix(Arrays.toString(inetDestAddressValue));
429 ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
431 arpMatchBuilder.setArpSourceTransportAddress(ipv4SourcePrefix);
432 arpMatchBuilder.setArpSourceTransportAddress(ipv4DestPrefix);
434 ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
435 arpSourceHardwareAddressBuilder.setAddress(ethernetSourceAddressFrom(sourceMatch));
436 arpMatchBuilder.setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder.build());
438 ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
439 arpTargetHardwareAddressBuilder.setAddress(ethernetDestAddressFrom(sourceMatch));
440 arpMatchBuilder.setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder.build());
442 return arpMatchBuilder.build();
446 private static MacAddress ethernetDestAddressFrom(final Match sourceMatch) {
447 final MatchField dataLinkDest = sourceMatch.getField(DL_DST);
448 if (dataLinkDest != null && dataLinkDest.getValue() != null) {
449 return new MacAddress((String) (dataLinkDest.getValue()));
454 private static Layer3Match setLayer3MatchAsIpv4(final Inet4Address inetSourceAddress,
455 final Inet4Address inetDestAddress) {
456 byte[] inetAddressValue = inetSourceAddress.getAddress();
458 Ipv4MatchBuilder layer4MatchBuild = new Ipv4MatchBuilder();
459 layer4MatchBuild.setIpv4Source(new Ipv4Prefix(Arrays.toString(inetAddressValue)));
460 return layer4MatchBuild.build();
464 private static Layer3Match setLayer3MatchAsIpv6(final Inet6Address inetSourceAddress,
465 final Inet6Address inetDestAddress) {
466 byte[] inetAddressValue = inetSourceAddress.getAddress();
467 Ipv6MatchBuilder layer6MatchBuild = new Ipv6MatchBuilder();
469 layer6MatchBuild.setIpv6Source(new Ipv6Prefix(Arrays.toString(inetAddressValue)));
470 return layer6MatchBuild.build();