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.Collections;
22 import java.util.List;
24 import org.opendaylight.controller.sal.action.*;
25 import org.opendaylight.controller.sal.core.NodeConnector;
26 import org.opendaylight.controller.sal.flowprogrammer.Flow;
27 import org.opendaylight.controller.sal.match.Match;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.*;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.ActionList;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.VlanCfi;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.*;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.address.Address;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.address.address.Ipv4;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.address.address.Ipv6;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.list.Action;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.MacAddressFilter;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddress;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddress;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetType;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.*;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatch;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatch;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanId;
53 import com.google.common.net.InetAddresses;
55 public class ToSalConversionsUtils {
57 private ToSalConversionsUtils() {
61 public static Flow toFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow source) {
62 final Flow target = new Flow();
64 Integer hardTimeout = source.getHardTimeout();
65 if (hardTimeout != null) {
66 target.setHardTimeout(hardTimeout.shortValue());
69 Integer idleTimeout = source.getIdleTimeout();
70 if (idleTimeout != null) {
71 target.setIdleTimeout(idleTimeout.shortValue());
74 Integer priority = source.getPriority();
75 if (priority != null) {
76 target.setPriority(priority.shortValue());
79 target.setMatch(toMatch(source.getMatch()));
81 List<Action> actions = getAction(source);
82 if (actions != null) {
83 target.setActions(actionFrom(actions));
86 target.setId(source.getCookie().longValue());
90 public static List<Action> getAction(
91 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow source) {
92 if (source.getInstructions() != null) {
93 for (Instruction instruction : source.getInstructions().getInstruction()) {
94 if (instruction.getInstruction() instanceof ActionList) {
95 return (((ActionList) instruction.getInstruction()).getAction());
99 // TODO Auto-generated method stub
100 return Collections.emptyList();
103 public static List<org.opendaylight.controller.sal.action.Action> actionFrom(List<Action> actions) {
104 List<org.opendaylight.controller.sal.action.Action> targetAction = new ArrayList<>();
105 for (Action action : actions) {
106 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.Action sourceAction = action
109 if (sourceAction instanceof ControllerAction) {
110 targetAction.add(new Controller());
111 } else if (sourceAction instanceof OutputAction) {
113 List<Uri> nodeConnectors = ((OutputAction) sourceAction).getOutputNodeConnector();
114 if (nodeConnectors != null) {
115 for (Uri uri : nodeConnectors) {
116 targetAction.add(new Output(fromNodeConnectorRef(uri)));
119 } else if (sourceAction instanceof PopMplsAction) {
120 // TODO: define maping
121 } else if (sourceAction instanceof PushMplsAction) {
122 // TODO: define maping
123 } else if (sourceAction instanceof PushPbbAction) {
124 // TODO: define maping
125 } else if (sourceAction instanceof SetMplsTtlAction) {
126 // TODO: define maping
127 // targetAction = //no action to map
128 } else if (sourceAction instanceof SetNwTtlAction) {
129 // TODO: define maping
130 } else if (sourceAction instanceof SetQueueAction) {
131 // TODO: define maping
132 // targetAction = //no action to map
133 } else if (sourceAction instanceof DropAction) {
134 targetAction.add(new Drop());
135 } else if (sourceAction instanceof FloodAction) {
136 targetAction.add(new Flood());
137 } else if (sourceAction instanceof FloodAllAction) {
138 targetAction.add(new FloodAll());
139 } else if (sourceAction instanceof HwPathAction) {
140 targetAction.add(new HwPath());
141 } else if (sourceAction instanceof LoopbackAction) {
142 targetAction.add(new Loopback());
143 } else if (sourceAction instanceof PopVlanAction) {
144 targetAction.add(new PopVlan());
145 } else if (sourceAction instanceof PushVlanAction) {
146 PushVlanAction pushVlanAction = (PushVlanAction) sourceAction;
147 PushVlan pushVlan = pushVlanFrom(pushVlanAction);
148 if (pushVlan != null) {
149 targetAction.add(pushVlan);
151 } else if (sourceAction instanceof SetDlDstAction) {
152 MacAddress addressL2Dest = ((SetDlDstAction) sourceAction).getAddress();
153 if (addressL2Dest != null) {
154 targetAction.add(new SetDlDst(bytesFrom(addressL2Dest)));
156 } else if (sourceAction instanceof SetDlSrcAction) {
157 MacAddress addressL2Src = ((SetDlSrcAction) sourceAction).getAddress();
158 if (addressL2Src != null) {
159 targetAction.add(new SetDlSrc(bytesFrom(addressL2Src)));
162 } else if (sourceAction instanceof SetDlTypeAction) {
163 EtherType dlType = ((SetDlTypeAction) sourceAction).getDlType();
164 if (dlType != null) {
165 Long dlTypeValue = dlType.getValue();
166 if (dlTypeValue != null) {
167 targetAction.add(new SetDlType(dlTypeValue.intValue()));
170 } else if (sourceAction instanceof SetNextHopAction) {
171 Address addressL3 = ((SetNextHopAction) sourceAction).getAddress();
173 InetAddress inetAddress = inetAddressFrom(addressL3);
174 if (inetAddress != null) {
175 targetAction.add(new SetNextHop(inetAddress));
177 } else if (sourceAction instanceof SetNwDstAction) {
178 Address addressL3 = ((SetNwDstAction) sourceAction).getAddress();
180 InetAddress inetAddress = inetAddressFrom(addressL3);
181 if (inetAddress != null) {
182 targetAction.add(new SetNwDst(inetAddress));
184 } else if (sourceAction instanceof SetNwSrcAction) {
185 Address addressL3 = ((SetNwSrcAction) sourceAction).getAddress();
187 InetAddress inetAddress = inetAddressFrom(addressL3);
188 if (inetAddress != null) {
189 targetAction.add(new SetNwSrc(inetAddress));
191 } else if (sourceAction instanceof SetNwTosAction) {
192 Integer tos = ((SetNwTosAction) sourceAction).getTos();
194 targetAction.add(new SetNwTos(tos));
196 } else if (sourceAction instanceof SetTpDstAction) {
197 PortNumber port = ((SetTpDstAction) sourceAction).getPort();
199 Integer portValue = port.getValue();
200 if (port.getValue() != null) {
201 targetAction.add(new SetTpDst(portValue));
204 } else if (sourceAction instanceof SetTpSrcAction) {
205 PortNumber port = ((SetTpSrcAction) sourceAction).getPort();
207 Integer portValue = port.getValue();
208 if (port.getValue() != null) {
209 targetAction.add(new SetTpSrc(portValue));
212 } else if (sourceAction instanceof SetVlanCfiAction) {
213 VlanCfi vlanCfi = ((SetVlanCfiAction) sourceAction).getVlanCfi();
214 if (vlanCfi != null) {
215 Integer vlanCfiValue = vlanCfi.getValue();
216 if (vlanCfiValue != null) {
217 targetAction.add(new SetVlanCfi(vlanCfiValue));
220 } else if (sourceAction instanceof SetVlanIdAction) {
221 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanID = ((SetVlanIdAction) sourceAction)
223 if (vlanID != null) {
224 Integer vlanIdValue = vlanID.getValue();
225 if (vlanIdValue != null) {
226 targetAction.add(new SetVlanId(vlanIdValue));
229 } else if (sourceAction instanceof SetVlanPcpAction) {
230 VlanPcp vlanPcp = ((SetVlanPcpAction) sourceAction).getVlanPcp();
231 if (vlanPcp != null) {
232 Short vlanPcpValue = vlanPcp.getValue();
233 if (vlanPcpValue != null) {
234 targetAction.add(new SetVlanPcp(vlanPcpValue));
237 } else if (sourceAction instanceof SwPathAction) {
238 targetAction.add(new SwPath());
245 private static InetAddress inetAddressFrom(Address addressL3) {
246 if (addressL3 != null) {
247 if (addressL3 instanceof Ipv4) {
248 Ipv4Prefix addressL3Ipv4 = ((Ipv4) addressL3).getIpv4Address();
249 if (addressL3Ipv4 != null) {
250 return inetAddressFrom(addressL3Ipv4);
252 } else if (addressL3 instanceof Ipv6) {
253 Ipv6Prefix addressL3Ipv6 = ((Ipv6) addressL3).getIpv6Address();
254 if (addressL3Ipv6 != null) {
255 return inetAddressFrom(addressL3Ipv6);
262 private static PushVlan pushVlanFrom(PushVlanAction pushVlanAction) {
268 if (pushVlanAction.getTag() != null) {
269 tag = pushVlanAction.getTag();
270 if (pushVlanAction.getPcp() != null) {
271 pcp = pushVlanAction.getPcp();
272 if (pushVlanAction.getCfi() != null && pushVlanAction.getCfi().getValue() != null) {
273 cfi = pushVlanAction.getCfi().getValue();
274 if (pushVlanAction.getVlanId() != null && pushVlanAction.getVlanId().getValue() != null) {
275 vlanId = pushVlanAction.getVlanId().getValue();
276 return new PushVlan(tag, pcp, cfi, vlanId);
284 private static NodeConnector fromNodeConnectorRef(Uri uri) {
285 // TODO: Define mapping
289 public static Match toMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match source) {
290 Match target = new Match();
291 if (source != null) {
292 fillFrom(target, source.getVlanMatch());
293 fillFrom(target, source.getEthernetMatch());
294 fillFrom(target, source.getLayer3Match());
295 fillFrom(target, source.getLayer4Match());
296 fillFrom(target, source.getIpMatch());
302 private static void fillFrom(Match target, VlanMatch vlanMatch) {
303 if (vlanMatch != null) {
304 VlanId vlanId = vlanMatch.getVlanId();
305 if (vlanId != null) {
306 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanIdInner = vlanId
308 if (vlanIdInner != null) {
309 Integer vlanValue = vlanIdInner.getValue();
310 if (vlanValue != null) {
311 target.setField(DL_VLAN, vlanValue.shortValue());
315 VlanPcp vlanPcp = vlanMatch.getVlanPcp();
316 if (vlanPcp != null) {
317 Short vlanPcpValue = vlanPcp.getValue();
318 if (vlanPcpValue != null) {
319 target.setField(DL_VLAN_PR, vlanPcpValue.byteValue());
325 private static void fillFrom(Match target, IpMatch ipMatch) {
326 if (ipMatch != null) {
327 Short ipProtocol = ipMatch.getIpProtocol();
329 if (ipProtocol != null && target.getField(NW_PROTO) == null) {
330 target.setField(NW_PROTO, ipProtocol.byteValue());
332 Dscp dscp = ipMatch.getIpDscp();
334 Short dscpValue = dscp.getValue();
335 if (dscpValue != null) {
336 target.setField(NW_TOS, dscpValue.byteValue());
342 private static void fillFrom(Match target, Layer4Match layer4Match) {
343 if (layer4Match == null) {
346 if (layer4Match instanceof SctpMatch) {
347 fillTransportLayer(target, (SctpMatch) layer4Match);
348 } else if (layer4Match instanceof TcpMatch) {
349 fillTransportLayer(target, (TcpMatch) layer4Match);
350 } else if (layer4Match instanceof UdpMatch) {
351 fillTransportLayer(target, (UdpMatch) layer4Match);
355 private static void fillTransportLayer(Match target, UdpMatch source) {
356 PortNumber udpSourcePort = source.getUdpSourcePort();
357 if (udpSourcePort != null) {
358 Integer udpSourcePortValue = udpSourcePort.getValue();
359 if (udpSourcePortValue != null) {
360 target.setField(TP_SRC, udpSourcePortValue.shortValue());
364 PortNumber udpDestPort = source.getUdpDestinationPort();
365 if (udpDestPort != null) {
366 Integer udpDestPortValue = udpDestPort.getValue();
367 if (udpDestPortValue != null) {
368 target.setField(TP_DST, udpDestPortValue.shortValue());
372 target.setField(NW_PROTO, UDP);
375 private static void fillTransportLayer(Match target, TcpMatch source) {
376 PortNumber tcpSourcePort = source.getTcpSourcePort();
377 if (tcpSourcePort != null) {
378 Integer tcpSourcePortValue = tcpSourcePort.getValue();
379 if (tcpSourcePortValue != null) {
380 target.setField(TP_SRC, tcpSourcePortValue.shortValue());
384 PortNumber tcpDestPort = source.getTcpDestinationPort();
385 if (tcpDestPort != null) {
386 Integer tcpDestPortValue = tcpDestPort.getValue();
387 if (tcpDestPortValue != null) {
388 target.setField(TP_DST, tcpDestPortValue.shortValue());
392 target.setField(NW_PROTO, TCP);
395 private static void fillTransportLayer(Match target, SctpMatch source) {
396 PortNumber sctpSourcePort = source.getSctpSourcePort();
397 if (sctpSourcePort != null) {
398 Integer sctpSourcePortValue = sctpSourcePort.getValue();
399 if (sctpSourcePortValue != null) {
400 target.setField(TP_SRC, sctpSourcePortValue.shortValue());
403 PortNumber sctpDestPort = source.getSctpDestinationPort();
404 if (sctpDestPort != null) {
405 Integer sctpDestPortValue = sctpDestPort.getValue();
406 if (sctpDestPortValue != null) {
407 target.setField(TP_DST, sctpDestPortValue.shortValue());
411 target.setField(NW_PROTO, SCTP);
415 private static void fillFrom(Match target, Layer3Match source) {
418 if (source instanceof Ipv4Match) {
419 fillFromIpv4(target, (Ipv4Match) source);
420 } else if (source instanceof Ipv6Match) {
421 fillFromIpv6(target, (Ipv6Match) source);
422 } else if (source instanceof ArpMatch) {
423 fillFromArp(target, (ArpMatch) source);
427 private static void fillFromArp(Match target, ArpMatch source) {
428 Ipv4Prefix sourceAddress = source.getArpSourceTransportAddress();
429 if (sourceAddress != null) {
430 target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
432 Ipv4Prefix destAddress = source.getArpTargetTransportAddress();
433 if (destAddress != null) {
434 target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
436 ArpSourceHardwareAddress sourceHwAddress = source.getArpSourceHardwareAddress();
437 if (sourceHwAddress != null) {
438 target.setField(DL_SRC, bytesFrom(sourceHwAddress.getAddress()));
440 ArpTargetHardwareAddress targetHwAddress = source.getArpTargetHardwareAddress();
441 if (targetHwAddress != null) {
442 target.setField(DL_DST, bytesFrom(targetHwAddress.getAddress()));
445 target.setField(DL_TYPE, new Short(ETHERNET_ARP));
449 private static void fillFromIpv6(Match target, Ipv6Match source) {
450 Ipv6Prefix sourceAddress = source.getIpv6Source();
451 if (sourceAddress != null) {
452 target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
454 Ipv6Prefix destAddress = source.getIpv6Destination();
455 if (destAddress != null) {
456 target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
460 private static void fillFromIpv4(Match target, Ipv4Match source) {
461 Ipv4Prefix sourceAddress = source.getIpv4Source();
462 if (sourceAddress != null) {
463 target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
465 Ipv4Prefix destAddress = source.getIpv4Destination();
466 if (destAddress != null) {
467 target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
471 private static InetAddress inetAddressFrom(Ipv4Prefix source) {
472 if (source != null) {
473 String[] parts = source.getValue().split("/");
474 return InetAddresses.forString(parts[0]);
479 private static InetAddress inetAddressFrom(Ipv6Prefix source) {
480 if (source != null) {
481 String[] parts = source.getValue().split("/");
482 return InetAddresses.forString(parts[0]);
487 private static void fillFrom(Match target, EthernetMatch source) {
490 EthernetType ethType = source.getEthernetType();
491 if (ethType != null) {
492 EtherType ethInnerType = ethType.getType();
493 if (ethInnerType != null && target.getField(DL_TYPE) == null) {
494 Long value = ethInnerType.getValue();
495 target.setField(DL_TYPE, value.shortValue());
499 MacAddressFilter ethSource = source.getEthernetSource();
500 if (ethSource != null) {
501 target.setField(DL_SRC, bytesFrom(ethSource.getAddress()));
504 MacAddressFilter ethDest = source.getEthernetDestination();
505 if (ethDest != null) {
506 target.setField(DL_DST, bytesFrom(ethDest.getAddress()));
510 private static byte[] bytesFrom(MacAddress address) {
511 String[] mac = address.getValue().split(":");
512 byte[] macAddress = new byte[6]; // mac.length == 6 bytes
513 for (int i = 0; i < mac.length; i++) {
514 macAddress[i] = Integer.decode("0x" + mac[i]).byteValue();