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;
6 import static org.opendaylight.controller.sal.match.MatchType.DL_VLAN;
7 import static org.opendaylight.controller.sal.match.MatchType.DL_VLAN_PR;
8 import static org.opendaylight.controller.sal.match.MatchType.NW_DST;
9 import static org.opendaylight.controller.sal.match.MatchType.NW_PROTO;
10 import static org.opendaylight.controller.sal.match.MatchType.NW_SRC;
11 import static org.opendaylight.controller.sal.match.MatchType.NW_TOS;
12 import static org.opendaylight.controller.sal.match.MatchType.TP_DST;
13 import static org.opendaylight.controller.sal.match.MatchType.TP_SRC;
15 import java.net.InetAddress;
16 import java.util.HashSet;
17 import java.util.List;
20 import org.opendaylight.controller.sal.action.*;
21 import org.opendaylight.controller.sal.core.NodeConnector;
22 import org.opendaylight.controller.sal.flowprogrammer.Flow;
23 import org.opendaylight.controller.sal.match.Match;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.*;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeFlow;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.VlanCfi;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.*;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.Address;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.address.Ipv4;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.address.Ipv6;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Action;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
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.MacAddressFilter;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.*;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.ArpMatch;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv4Match;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv6Match;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.SctpMatch;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.TcpMatch;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.UdpMatch;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.vlan.match.fields.VlanId;
46 import com.google.common.net.InetAddresses;
48 public class ToSalConversionsUtils {
50 private ToSalConversionsUtils() {
54 public static Flow flowFrom(NodeFlow source) {
55 final Flow target = new Flow();
57 Integer hardTimeout = source.getHardTimeout();
58 if (hardTimeout != null) {
59 target.setHardTimeout(hardTimeout.shortValue());
62 Integer idleTimeout = source.getIdleTimeout();
63 if (idleTimeout != null) {
64 target.setIdleTimeout(idleTimeout.shortValue());
67 Integer priority = source.getPriority();
68 if (priority != null) {
69 target.setPriority(priority.shortValue());
72 target.setMatch(matchFrom(source.getMatch()));
74 List<Action> actions = source.getAction();
75 if (actions != null) {
76 for (Action sourceAction : actions) {
77 Set<org.opendaylight.controller.sal.action.Action> targetActions = actionFrom(sourceAction);
78 for (org.opendaylight.controller.sal.action.Action targetAction : targetActions) {
79 target.addAction(targetAction);
84 target.setId(source.getCookie().longValue());
88 public static Set<org.opendaylight.controller.sal.action.Action> actionFrom(Action source) {
89 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.Action sourceAction = source
91 Set<org.opendaylight.controller.sal.action.Action> targetAction = new HashSet<>();
92 if (sourceAction instanceof ControllerAction) {
93 targetAction.add(new Controller());
94 } else if (sourceAction instanceof OutputAction) {
96 List<Uri> nodeConnectors = ((OutputAction) sourceAction).getOutputNodeConnector();
97 for (Uri uri : nodeConnectors) {
98 targetAction.add(new Output(fromNodeConnectorRef(uri)));
100 } else if (sourceAction instanceof PopMplsAction) {
101 // TODO: define maping
102 } else if (sourceAction instanceof PushMplsAction) {
103 // TODO: define maping
104 } else if (sourceAction instanceof PushPbbAction) {
105 // TODO: define maping
106 } else if (sourceAction instanceof PushVlanAction) {
107 // TODO: define maping
108 // PushVlanAction vlanAction = (PushVlanAction) sourceAction;
109 // targetAction.add(new PushVlan(vlanAction., pcp, cfi, vlanId);
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 = ((SetNwDstAction) 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());
234 private static InetAddress inetAddressFrom(Address addressL3) {
235 if (addressL3 != null) {
236 if (addressL3 instanceof Ipv4) {
237 Ipv4Prefix addressL3Ipv4 = ((Ipv4) addressL3).getIpv4Address();
238 if (addressL3Ipv4 != null) {
239 return inetAddressFrom(addressL3Ipv4);
241 } else if (addressL3 instanceof Ipv6) {
242 Ipv6Prefix addressL3Ipv6 = ((Ipv6) addressL3).getIpv6Address();
243 if (addressL3Ipv6 != null) {
244 return inetAddressFrom(addressL3Ipv6);
251 private static PushVlan pushVlanFrom(PushVlanAction pushVlanAction) {
257 if (pushVlanAction.getTag() != null) {
258 tag = pushVlanAction.getTag();
259 if (pushVlanAction.getPcp() != null) {
260 pcp = pushVlanAction.getPcp();
261 if (pushVlanAction.getCfi() != null && pushVlanAction.getCfi().getValue() != null) {
262 cfi = pushVlanAction.getCfi().getValue();
263 if (pushVlanAction.getVlanId() != null && pushVlanAction.getVlanId().getValue() != null) {
264 vlanId = pushVlanAction.getVlanId().getValue();
265 return new PushVlan(tag, pcp, cfi, vlanId);
273 private static NodeConnector fromNodeConnectorRef(Uri uri) {
274 // TODO: Define mapping
278 public static Match matchFrom(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Match source) {
279 Match target = new Match();
280 if (source != null) {
281 fillFrom(target, source.getVlanMatch());
282 fillFrom(target, source.getEthernetMatch());
283 fillFrom(target, source.getLayer3Match());
284 fillFrom(target, source.getLayer4Match());
285 fillFrom(target, source.getIpMatch());
291 private static void fillFrom(Match target, VlanMatch vlanMatch) {
292 if (vlanMatch != null) {
293 VlanId vlanId = vlanMatch.getVlanId();
294 if (vlanId != null) {
295 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanIdInner = vlanId
297 if (vlanIdInner != null) {
298 Integer vlanValue = vlanIdInner.getValue();
299 if (vlanValue != null) {
300 target.setField(DL_VLAN, vlanValue.shortValue());
304 VlanPcp vlanPcp = vlanMatch.getVlanPcp();
305 if (vlanPcp != null) {
306 Short vlanPcpValue = vlanPcp.getValue();
307 if (vlanPcpValue != null) {
308 target.setField(DL_VLAN_PR, vlanPcpValue.byteValue());
314 private static void fillFrom(Match target, IpMatch ipMatch) {
315 if (ipMatch != null) {
316 Short ipProtocol = ipMatch.getIpProtocol();
317 if (ipProtocol != null) {
318 target.setField(NW_PROTO, ipProtocol.byteValue());
320 Dscp dscp = ipMatch.getIpDscp();
322 Short dscpValue = dscp.getValue();
323 if (dscpValue != null) {
324 target.setField(NW_TOS, dscpValue.byteValue());
330 private static void fillFrom(Match target, Layer4Match layer4Match) {
331 if (layer4Match == null) {
334 if (layer4Match instanceof SctpMatch) {
335 fillTransportLayer(target, (SctpMatch) layer4Match);
336 } else if (layer4Match instanceof TcpMatch) {
337 fillTransportLayer(target, (TcpMatch) layer4Match);
338 } else if (layer4Match instanceof UdpMatch) {
339 fillTransportLayer(target, (UdpMatch) layer4Match);
343 private static void fillTransportLayer(Match target, UdpMatch source) {
344 PortNumber udpSourcePort = source.getUdpSourcePort();
345 if (udpSourcePort != null) {
346 Integer udpSourcePortValue = udpSourcePort.getValue();
347 if (udpSourcePortValue != null) {
348 target.setField(TP_SRC, udpSourcePortValue.shortValue());
352 PortNumber udpDestPort = source.getUdpDestinationPort();
353 if (udpDestPort != null) {
354 Integer udpDestPortValue = udpDestPort.getValue();
355 if (udpDestPortValue != null) {
356 target.setField(TP_DST, udpDestPortValue.shortValue());
361 private static void fillTransportLayer(Match target, TcpMatch source) {
362 PortNumber tcpSourcePort = source.getTcpSourcePort();
363 if (tcpSourcePort != null) {
364 Integer tcpSourcePortValue = tcpSourcePort.getValue();
365 if (tcpSourcePortValue != null) {
366 target.setField(TP_SRC, tcpSourcePortValue.shortValue());
370 PortNumber tcpDestPort = source.getTcpDestinationPort();
371 if (tcpDestPort != null) {
372 Integer tcpDestPortValue = tcpDestPort.getValue();
373 if (tcpDestPortValue != null) {
374 target.setField(TP_DST, tcpDestPortValue.shortValue());
379 private static void fillTransportLayer(Match target, SctpMatch source) {
380 PortNumber sctpSourcePort = source.getSctpSourcePort();
381 if (sctpSourcePort != null) {
382 Integer sctpSourcePortValue = sctpSourcePort.getValue();
383 if (sctpSourcePortValue != null) {
384 target.setField(TP_SRC, sctpSourcePortValue.shortValue());
387 PortNumber sctpDestPort = source.getSctpDestinationPort();
388 if (sctpDestPort != null) {
389 Integer sctpDestPortValue = sctpDestPort.getValue();
390 if (sctpDestPortValue != null) {
391 target.setField(TP_DST, sctpDestPortValue.shortValue());
396 private static void fillFrom(Match target, Layer3Match source) {
399 if (source instanceof Ipv4Match) {
400 fillFromIpv4(target, (Ipv4Match) source);
401 } else if (source instanceof Ipv6Match) {
402 fillFromIpv6(target, (Ipv6Match) source);
403 } else if (source instanceof ArpMatch) {
404 fillFromArp(target, (ArpMatch) source);
408 private static void fillFromArp(Match target, ArpMatch source) {
409 Ipv4Prefix sourceAddress = source.getArpSourceTransportAddress();
410 if (sourceAddress != null) {
411 target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
413 Ipv4Prefix destAddress = source.getArpSourceTransportAddress();
414 if (destAddress != null) {
415 target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
419 private static void fillFromIpv6(Match target, Ipv6Match source) {
420 Ipv6Prefix sourceAddress = source.getIpv6Source();
421 if (sourceAddress != null) {
422 target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
424 Ipv6Prefix destAddress = source.getIpv6Source();
425 if (destAddress != null) {
426 target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
430 private static void fillFromIpv4(Match target, Ipv4Match source) {
431 Ipv4Prefix sourceAddress = source.getIpv4Source();
432 if (sourceAddress != null) {
433 target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
435 Ipv4Prefix destAddress = source.getIpv4Source();
436 if (destAddress != null) {
437 target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
441 private static InetAddress inetAddressFrom(Ipv4Prefix source) {
442 if (source != null) {
443 String[] parts = source.getValue().split("/");
444 return InetAddresses.forString(parts[0]);
449 private static InetAddress inetAddressFrom(Ipv6Prefix source) {
450 if (source != null) {
451 String[] parts = source.getValue().split("/");
452 return InetAddresses.forString(parts[0]);
457 private static void fillFrom(Match target, EthernetMatch source) {
460 EthernetType ethType = source.getEthernetType();
461 if (ethType != null) {
462 EtherType ethInnerType = ethType.getType();
463 if (ethInnerType != null) {
464 Long value = ethInnerType.getValue();
465 target.setField(DL_TYPE, value.shortValue());
469 MacAddressFilter ethSource = source.getEthernetSource();
470 if (ethSource != null) {
471 target.setField(DL_SRC, bytesFrom(ethSource.getAddress()));
474 MacAddressFilter ethDest = source.getEthernetDestination();
475 if (ethDest != null) {
476 target.setField(DL_DST, bytesFrom(ethDest.getAddress()));
480 private static byte[] bytesFrom(MacAddress address) {
481 if (address != null) {
482 return address.getValue().getBytes();