1 package org.opendaylight.controller.sal.compability;
3 import static org.opendaylight.controller.sal.compability.ProtocolConstants.ETHERNET_ARP;
4 import static org.opendaylight.controller.sal.compability.ProtocolConstants.SCTP;
5 import static org.opendaylight.controller.sal.compability.ProtocolConstants.TCP;
6 import static org.opendaylight.controller.sal.compability.ProtocolConstants.UDP;
7 import static org.opendaylight.controller.sal.match.MatchType.DL_DST;
8 import static org.opendaylight.controller.sal.match.MatchType.DL_SRC;
9 import static org.opendaylight.controller.sal.match.MatchType.DL_TYPE;
10 import static org.opendaylight.controller.sal.match.MatchType.DL_VLAN;
11 import static org.opendaylight.controller.sal.match.MatchType.DL_VLAN_PR;
12 import static org.opendaylight.controller.sal.match.MatchType.NW_DST;
13 import static org.opendaylight.controller.sal.match.MatchType.NW_PROTO;
14 import static org.opendaylight.controller.sal.match.MatchType.NW_SRC;
15 import static org.opendaylight.controller.sal.match.MatchType.NW_TOS;
16 import static org.opendaylight.controller.sal.match.MatchType.TP_DST;
17 import static org.opendaylight.controller.sal.match.MatchType.TP_SRC;
19 import java.net.InetAddress;
20 import java.util.ArrayList;
21 import java.util.List;
23 import org.opendaylight.controller.sal.action.*;
24 import org.opendaylight.controller.sal.core.NodeConnector;
25 import org.opendaylight.controller.sal.flowprogrammer.Flow;
26 import org.opendaylight.controller.sal.match.Match;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.*;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeFlow;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.VlanCfi;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.*;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.Address;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.address.Ipv4;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.address.Ipv6;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Action;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.MacAddressFilter;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.arp.match.fields.ArpSourceHardwareAddress;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.arp.match.fields.ArpTargetHardwareAddress;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetType;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.*;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.ArpMatch;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv4Match;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv6Match;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.SctpMatch;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.TcpMatch;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.UdpMatch;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.vlan.match.fields.VlanId;
51 import com.google.common.net.InetAddresses;
53 public class ToSalConversionsUtils {
55 private ToSalConversionsUtils() {
59 public static Flow flowFrom(NodeFlow source) {
60 final Flow target = new Flow();
62 Integer hardTimeout = source.getHardTimeout();
63 if (hardTimeout != null) {
64 target.setHardTimeout(hardTimeout.shortValue());
67 Integer idleTimeout = source.getIdleTimeout();
68 if (idleTimeout != null) {
69 target.setIdleTimeout(idleTimeout.shortValue());
72 Integer priority = source.getPriority();
73 if (priority != null) {
74 target.setPriority(priority.shortValue());
77 target.setMatch(matchFrom(source.getMatch()));
79 List<Action> actions = source.getAction();
80 if (actions != null) {
81 target.setActions(actionFrom(actions));
84 target.setId(source.getCookie().longValue());
88 public static List<org.opendaylight.controller.sal.action.Action> actionFrom(List<Action> actions) {
89 List<org.opendaylight.controller.sal.action.Action> targetAction = new ArrayList<>();
90 for (Action action : actions) {
91 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.Action sourceAction = action
94 if (sourceAction instanceof ControllerAction) {
95 targetAction.add(new Controller());
96 } else if (sourceAction instanceof OutputAction) {
98 List<Uri> nodeConnectors = ((OutputAction) sourceAction).getOutputNodeConnector();
99 if (nodeConnectors != null) {
100 for (Uri uri : nodeConnectors) {
101 targetAction.add(new Output(fromNodeConnectorRef(uri)));
104 } else if (sourceAction instanceof PopMplsAction) {
105 // TODO: define maping
106 } else if (sourceAction instanceof PushMplsAction) {
107 // TODO: define maping
108 } else if (sourceAction instanceof PushPbbAction) {
109 // TODO: define maping
110 } else if (sourceAction instanceof SetMplsTtlAction) {
111 // TODO: define maping
112 // targetAction = //no action to map
113 } else if (sourceAction instanceof SetNwTtlAction) {
114 // TODO: define maping
115 } else if (sourceAction instanceof SetQueueAction) {
116 // TODO: define maping
117 // targetAction = //no action to map
118 } else if (sourceAction instanceof DropAction) {
119 targetAction.add(new Drop());
120 } else if (sourceAction instanceof FloodAction) {
121 targetAction.add(new Flood());
122 } else if (sourceAction instanceof FloodAllAction) {
123 targetAction.add(new FloodAll());
124 } else if (sourceAction instanceof HwPathAction) {
125 targetAction.add(new HwPath());
126 } else if (sourceAction instanceof LoopbackAction) {
127 targetAction.add(new Loopback());
128 } else if (sourceAction instanceof PopVlanAction) {
129 targetAction.add(new PopVlan());
130 } else if (sourceAction instanceof PushVlanAction) {
131 PushVlanAction pushVlanAction = (PushVlanAction) sourceAction;
132 PushVlan pushVlan = pushVlanFrom(pushVlanAction);
133 if (pushVlan != null) {
134 targetAction.add(pushVlan);
136 } else if (sourceAction instanceof SetDlDstAction) {
137 MacAddress addressL2Dest = ((SetDlDstAction) sourceAction).getAddress();
138 if (addressL2Dest != null) {
139 String addressValue = addressL2Dest.getValue();
140 if (addressValue != null) {
141 targetAction.add(new SetDlDst(addressValue.getBytes()));
144 } else if (sourceAction instanceof SetDlSrcAction) {
145 MacAddress addressL2Src = ((SetDlSrcAction) sourceAction).getAddress();
146 if (addressL2Src != null) {
147 String addressValue = addressL2Src.getValue();
148 if (addressValue != null) {
149 targetAction.add(new SetDlSrc(addressValue.getBytes()));
152 } else if (sourceAction instanceof SetDlTypeAction) {
153 EtherType dlType = ((SetDlTypeAction) sourceAction).getDlType();
154 if (dlType != null) {
155 Long dlTypeValue = dlType.getValue();
156 if (dlTypeValue != null) {
157 targetAction.add(new SetDlType(dlTypeValue.intValue()));
160 } else if (sourceAction instanceof SetNextHopAction) {
161 Address addressL3 = ((SetNextHopAction) sourceAction).getAddress();
163 InetAddress inetAddress = inetAddressFrom(addressL3);
164 if (inetAddress != null) {
165 targetAction.add(new SetNextHop(inetAddress));
167 } else if (sourceAction instanceof SetNwDstAction) {
168 Address addressL3 = ((SetNwDstAction) sourceAction).getAddress();
170 InetAddress inetAddress = inetAddressFrom(addressL3);
171 if (inetAddress != null) {
172 targetAction.add(new SetNwDst(inetAddress));
174 } else if (sourceAction instanceof SetNwSrcAction) {
175 Address addressL3 = ((SetNwSrcAction) sourceAction).getAddress();
177 InetAddress inetAddress = inetAddressFrom(addressL3);
178 if (inetAddress != null) {
179 targetAction.add(new SetNwSrc(inetAddress));
181 } else if (sourceAction instanceof SetNwTosAction) {
182 Integer tos = ((SetNwTosAction) sourceAction).getTos();
184 targetAction.add(new SetNwTos(tos));
186 } else if (sourceAction instanceof SetTpDstAction) {
187 PortNumber port = ((SetTpDstAction) sourceAction).getPort();
189 Integer portValue = port.getValue();
190 if (port.getValue() != null) {
191 targetAction.add(new SetTpDst(portValue));
194 } else if (sourceAction instanceof SetTpSrcAction) {
195 PortNumber port = ((SetTpSrcAction) sourceAction).getPort();
197 Integer portValue = port.getValue();
198 if (port.getValue() != null) {
199 targetAction.add(new SetTpSrc(portValue));
202 } else if (sourceAction instanceof SetVlanCfiAction) {
203 VlanCfi vlanCfi = ((SetVlanCfiAction) sourceAction).getVlanCfi();
204 if (vlanCfi != null) {
205 Integer vlanCfiValue = vlanCfi.getValue();
206 if (vlanCfiValue != null) {
207 targetAction.add(new SetVlanCfi(vlanCfiValue));
210 } else if (sourceAction instanceof SetVlanIdAction) {
211 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanID = ((SetVlanIdAction) sourceAction)
213 if (vlanID != null) {
214 Integer vlanIdValue = vlanID.getValue();
215 if (vlanIdValue != null) {
216 targetAction.add(new SetVlanId(vlanIdValue));
219 } else if (sourceAction instanceof SetVlanPcpAction) {
220 VlanPcp vlanPcp = ((SetVlanPcpAction) sourceAction).getVlanPcp();
221 if (vlanPcp != null) {
222 Short vlanPcpValue = vlanPcp.getValue();
223 if (vlanPcpValue != null) {
224 targetAction.add(new SetVlanPcp(vlanPcpValue));
227 } else if (sourceAction instanceof SwPathAction) {
228 targetAction.add(new SwPath());
235 private static InetAddress inetAddressFrom(Address addressL3) {
236 if (addressL3 != null) {
237 if (addressL3 instanceof Ipv4) {
238 Ipv4Prefix addressL3Ipv4 = ((Ipv4) addressL3).getIpv4Address();
239 if (addressL3Ipv4 != null) {
240 return inetAddressFrom(addressL3Ipv4);
242 } else if (addressL3 instanceof Ipv6) {
243 Ipv6Prefix addressL3Ipv6 = ((Ipv6) addressL3).getIpv6Address();
244 if (addressL3Ipv6 != null) {
245 return inetAddressFrom(addressL3Ipv6);
252 private static PushVlan pushVlanFrom(PushVlanAction pushVlanAction) {
258 if (pushVlanAction.getTag() != null) {
259 tag = pushVlanAction.getTag();
260 if (pushVlanAction.getPcp() != null) {
261 pcp = pushVlanAction.getPcp();
262 if (pushVlanAction.getCfi() != null && pushVlanAction.getCfi().getValue() != null) {
263 cfi = pushVlanAction.getCfi().getValue();
264 if (pushVlanAction.getVlanId() != null && pushVlanAction.getVlanId().getValue() != null) {
265 vlanId = pushVlanAction.getVlanId().getValue();
266 return new PushVlan(tag, pcp, cfi, vlanId);
274 private static NodeConnector fromNodeConnectorRef(Uri uri) {
275 // TODO: Define mapping
279 public static Match matchFrom(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Match source) {
280 Match target = new Match();
281 if (source != null) {
282 fillFrom(target, source.getVlanMatch());
283 fillFrom(target, source.getEthernetMatch());
284 fillFrom(target, source.getLayer3Match());
285 fillFrom(target, source.getLayer4Match());
286 fillFrom(target, source.getIpMatch());
292 private static void fillFrom(Match target, VlanMatch vlanMatch) {
293 if (vlanMatch != null) {
294 VlanId vlanId = vlanMatch.getVlanId();
295 if (vlanId != null) {
296 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanIdInner = vlanId
298 if (vlanIdInner != null) {
299 Integer vlanValue = vlanIdInner.getValue();
300 if (vlanValue != null) {
301 target.setField(DL_VLAN, vlanValue.shortValue());
305 VlanPcp vlanPcp = vlanMatch.getVlanPcp();
306 if (vlanPcp != null) {
307 Short vlanPcpValue = vlanPcp.getValue();
308 if (vlanPcpValue != null) {
309 target.setField(DL_VLAN_PR, vlanPcpValue.byteValue());
315 private static void fillFrom(Match target, IpMatch ipMatch) {
316 if (ipMatch != null) {
317 Short ipProtocol = ipMatch.getIpProtocol();
319 if (ipProtocol != null && target.getField(NW_PROTO) == null) {
320 target.setField(NW_PROTO, ipProtocol.byteValue());
322 Dscp dscp = ipMatch.getIpDscp();
324 Short dscpValue = dscp.getValue();
325 if (dscpValue != null) {
326 target.setField(NW_TOS, dscpValue.byteValue());
332 private static void fillFrom(Match target, Layer4Match layer4Match) {
333 if (layer4Match == null) {
336 if (layer4Match instanceof SctpMatch) {
337 fillTransportLayer(target, (SctpMatch) layer4Match);
338 } else if (layer4Match instanceof TcpMatch) {
339 fillTransportLayer(target, (TcpMatch) layer4Match);
340 } else if (layer4Match instanceof UdpMatch) {
341 fillTransportLayer(target, (UdpMatch) layer4Match);
345 private static void fillTransportLayer(Match target, UdpMatch source) {
346 PortNumber udpSourcePort = source.getUdpSourcePort();
347 if (udpSourcePort != null) {
348 Integer udpSourcePortValue = udpSourcePort.getValue();
349 if (udpSourcePortValue != null) {
350 target.setField(TP_SRC, udpSourcePortValue.shortValue());
354 PortNumber udpDestPort = source.getUdpDestinationPort();
355 if (udpDestPort != null) {
356 Integer udpDestPortValue = udpDestPort.getValue();
357 if (udpDestPortValue != null) {
358 target.setField(TP_DST, udpDestPortValue.shortValue());
362 target.setField(NW_PROTO, UDP);
365 private static void fillTransportLayer(Match target, TcpMatch source) {
366 PortNumber tcpSourcePort = source.getTcpSourcePort();
367 if (tcpSourcePort != null) {
368 Integer tcpSourcePortValue = tcpSourcePort.getValue();
369 if (tcpSourcePortValue != null) {
370 target.setField(TP_SRC, tcpSourcePortValue.shortValue());
374 PortNumber tcpDestPort = source.getTcpDestinationPort();
375 if (tcpDestPort != null) {
376 Integer tcpDestPortValue = tcpDestPort.getValue();
377 if (tcpDestPortValue != null) {
378 target.setField(TP_DST, tcpDestPortValue.shortValue());
382 target.setField(NW_PROTO, TCP);
385 private static void fillTransportLayer(Match target, SctpMatch source) {
386 PortNumber sctpSourcePort = source.getSctpSourcePort();
387 if (sctpSourcePort != null) {
388 Integer sctpSourcePortValue = sctpSourcePort.getValue();
389 if (sctpSourcePortValue != null) {
390 target.setField(TP_SRC, sctpSourcePortValue.shortValue());
393 PortNumber sctpDestPort = source.getSctpDestinationPort();
394 if (sctpDestPort != null) {
395 Integer sctpDestPortValue = sctpDestPort.getValue();
396 if (sctpDestPortValue != null) {
397 target.setField(TP_DST, sctpDestPortValue.shortValue());
401 target.setField(NW_PROTO, SCTP);
405 private static void fillFrom(Match target, Layer3Match source) {
408 if (source instanceof Ipv4Match) {
409 fillFromIpv4(target, (Ipv4Match) source);
410 } else if (source instanceof Ipv6Match) {
411 fillFromIpv6(target, (Ipv6Match) source);
412 } else if (source instanceof ArpMatch) {
413 fillFromArp(target, (ArpMatch) source);
417 private static void fillFromArp(Match target, ArpMatch source) {
418 Ipv4Prefix sourceAddress = source.getArpSourceTransportAddress();
419 if (sourceAddress != null) {
420 target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
422 Ipv4Prefix destAddress = source.getArpTargetTransportAddress();
423 if (destAddress != null) {
424 target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
426 ArpSourceHardwareAddress sourceHwAddress = source.getArpSourceHardwareAddress();
427 if (sourceHwAddress != null) {
428 target.setField(DL_SRC, sourceHwAddress.getAddress().getValue().getBytes());
430 ArpTargetHardwareAddress targetHwAddress = source.getArpTargetHardwareAddress();
431 if (targetHwAddress != null) {
432 target.setField(DL_DST, targetHwAddress.getAddress().getValue().getBytes());
435 target.setField(DL_TYPE, new Short(ETHERNET_ARP));
439 private static void fillFromIpv6(Match target, Ipv6Match source) {
440 Ipv6Prefix sourceAddress = source.getIpv6Source();
441 if (sourceAddress != null) {
442 target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
444 Ipv6Prefix destAddress = source.getIpv6Destination();
445 if (destAddress != null) {
446 target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
450 private static void fillFromIpv4(Match target, Ipv4Match source) {
451 Ipv4Prefix sourceAddress = source.getIpv4Source();
452 if (sourceAddress != null) {
453 target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
455 Ipv4Prefix destAddress = source.getIpv4Destination();
456 if (destAddress != null) {
457 target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
461 private static InetAddress inetAddressFrom(Ipv4Prefix source) {
462 if (source != null) {
463 String[] parts = source.getValue().split("/");
464 return InetAddresses.forString(parts[0]);
469 private static InetAddress inetAddressFrom(Ipv6Prefix source) {
470 if (source != null) {
471 String[] parts = source.getValue().split("/");
472 return InetAddresses.forString(parts[0]);
477 private static void fillFrom(Match target, EthernetMatch source) {
480 EthernetType ethType = source.getEthernetType();
481 if (ethType != null) {
482 EtherType ethInnerType = ethType.getType();
483 if (ethInnerType != null && target.getField(DL_TYPE) == null) {
484 Long value = ethInnerType.getValue();
485 target.setField(DL_TYPE, value.shortValue());
489 MacAddressFilter ethSource = source.getEthernetSource();
490 if (ethSource != null) {
491 target.setField(DL_SRC, bytesFrom(ethSource.getAddress()));
494 MacAddressFilter ethDest = source.getEthernetDestination();
495 if (ethDest != null) {
496 target.setField(DL_DST, bytesFrom(ethDest.getAddress()));
500 private static byte[] bytesFrom(MacAddress address) {
501 if (address != null) {
502 return address.getValue().getBytes();