Updated Flow Capable Models & AD SAL Compatibility mapping
[controller.git] / opendaylight / md-sal / sal-compability / src / main / java / org / opendaylight / controller / sal / compability / ToSalConversionsUtils.java
1 package org.opendaylight.controller.sal.compability;
2
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;
18
19 import java.net.InetAddress;
20 import java.util.ArrayList;
21 import java.util.List;
22
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;
50
51 import com.google.common.net.InetAddresses;
52
53 public class ToSalConversionsUtils {
54
55     private ToSalConversionsUtils() {
56
57     }
58
59     public static Flow toFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.Flow source) {
60         final Flow target = new Flow();
61
62         Integer hardTimeout = source.getHardTimeout();
63         if (hardTimeout != null) {
64             target.setHardTimeout(hardTimeout.shortValue());
65         }
66
67         Integer idleTimeout = source.getIdleTimeout();
68         if (idleTimeout != null) {
69             target.setIdleTimeout(idleTimeout.shortValue());
70         }
71
72         Integer priority = source.getPriority();
73         if (priority != null) {
74             target.setPriority(priority.shortValue());
75         }
76
77         target.setMatch(toMatch(source.getMatch()));
78
79         List<Action> actions = source.getAction();
80         if (actions != null) {
81             target.setActions(actionFrom(actions));
82         }
83
84         target.setId(source.getCookie().longValue());
85         return target;
86     }
87
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
92                     .getAction();
93
94             if (sourceAction instanceof ControllerAction) {
95                 targetAction.add(new Controller());
96             } else if (sourceAction instanceof OutputAction) {
97
98                 List<Uri> nodeConnectors = ((OutputAction) sourceAction).getOutputNodeConnector();
99                 if (nodeConnectors != null) {
100                     for (Uri uri : nodeConnectors) {
101                         targetAction.add(new Output(fromNodeConnectorRef(uri)));
102                     }
103                 }
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);
135                 }
136             } else if (sourceAction instanceof SetDlDstAction) {
137                 MacAddress addressL2Dest = ((SetDlDstAction) sourceAction).getAddress();
138                 if (addressL2Dest != null) {
139                         targetAction.add(new SetDlDst(bytesFrom(addressL2Dest)));
140                 }
141             } else if (sourceAction instanceof SetDlSrcAction) {
142                 MacAddress addressL2Src = ((SetDlSrcAction) sourceAction).getAddress();
143                 if (addressL2Src != null) {
144                         targetAction.add(new SetDlSrc(bytesFrom(addressL2Src)));
145                     
146                 }
147             } else if (sourceAction instanceof SetDlTypeAction) {
148                 EtherType dlType = ((SetDlTypeAction) sourceAction).getDlType();
149                 if (dlType != null) {
150                     Long dlTypeValue = dlType.getValue();
151                     if (dlTypeValue != null) {
152                         targetAction.add(new SetDlType(dlTypeValue.intValue()));
153                     }
154                 }
155             } else if (sourceAction instanceof SetNextHopAction) {
156                 Address addressL3 = ((SetNextHopAction) sourceAction).getAddress();
157
158                 InetAddress inetAddress = inetAddressFrom(addressL3);
159                 if (inetAddress != null) {
160                     targetAction.add(new SetNextHop(inetAddress));
161                 }
162             } else if (sourceAction instanceof SetNwDstAction) {
163                 Address addressL3 = ((SetNwDstAction) sourceAction).getAddress();
164
165                 InetAddress inetAddress = inetAddressFrom(addressL3);
166                 if (inetAddress != null) {
167                     targetAction.add(new SetNwDst(inetAddress));
168                 }
169             } else if (sourceAction instanceof SetNwSrcAction) {
170                 Address addressL3 = ((SetNwSrcAction) sourceAction).getAddress();
171
172                 InetAddress inetAddress = inetAddressFrom(addressL3);
173                 if (inetAddress != null) {
174                     targetAction.add(new SetNwSrc(inetAddress));
175                 }
176             } else if (sourceAction instanceof SetNwTosAction) {
177                 Integer tos = ((SetNwTosAction) sourceAction).getTos();
178                 if (tos != null) {
179                     targetAction.add(new SetNwTos(tos));
180                 }
181             } else if (sourceAction instanceof SetTpDstAction) {
182                 PortNumber port = ((SetTpDstAction) sourceAction).getPort();
183                 if (port != null) {
184                     Integer portValue = port.getValue();
185                     if (port.getValue() != null) {
186                         targetAction.add(new SetTpDst(portValue));
187                     }
188                 }
189             } else if (sourceAction instanceof SetTpSrcAction) {
190                 PortNumber port = ((SetTpSrcAction) sourceAction).getPort();
191                 if (port != null) {
192                     Integer portValue = port.getValue();
193                     if (port.getValue() != null) {
194                         targetAction.add(new SetTpSrc(portValue));
195                     }
196                 }
197             } else if (sourceAction instanceof SetVlanCfiAction) {
198                 VlanCfi vlanCfi = ((SetVlanCfiAction) sourceAction).getVlanCfi();
199                 if (vlanCfi != null) {
200                     Integer vlanCfiValue = vlanCfi.getValue();
201                     if (vlanCfiValue != null) {
202                         targetAction.add(new SetVlanCfi(vlanCfiValue));
203                     }
204                 }
205             } else if (sourceAction instanceof SetVlanIdAction) {
206                 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanID = ((SetVlanIdAction) sourceAction)
207                         .getVlanId();
208                 if (vlanID != null) {
209                     Integer vlanIdValue = vlanID.getValue();
210                     if (vlanIdValue != null) {
211                         targetAction.add(new SetVlanId(vlanIdValue));
212                     }
213                 }
214             } else if (sourceAction instanceof SetVlanPcpAction) {
215                 VlanPcp vlanPcp = ((SetVlanPcpAction) sourceAction).getVlanPcp();
216                 if (vlanPcp != null) {
217                     Short vlanPcpValue = vlanPcp.getValue();
218                     if (vlanPcpValue != null) {
219                         targetAction.add(new SetVlanPcp(vlanPcpValue));
220                     }
221                 }
222             } else if (sourceAction instanceof SwPathAction) {
223                 targetAction.add(new SwPath());
224             }
225         }
226
227         return targetAction;
228     }
229
230     private static InetAddress inetAddressFrom(Address addressL3) {
231         if (addressL3 != null) {
232             if (addressL3 instanceof Ipv4) {
233                 Ipv4Prefix addressL3Ipv4 = ((Ipv4) addressL3).getIpv4Address();
234                 if (addressL3Ipv4 != null) {
235                     return inetAddressFrom(addressL3Ipv4);
236                 }
237             } else if (addressL3 instanceof Ipv6) {
238                 Ipv6Prefix addressL3Ipv6 = ((Ipv6) addressL3).getIpv6Address();
239                 if (addressL3Ipv6 != null) {
240                     return inetAddressFrom(addressL3Ipv6);
241                 }
242             }
243         }
244         return null;
245     }
246
247     private static PushVlan pushVlanFrom(PushVlanAction pushVlanAction) {
248         final int tag;
249         final int pcp;
250         final int cfi;
251         final int vlanId;
252
253         if (pushVlanAction.getTag() != null) {
254             tag = pushVlanAction.getTag();
255             if (pushVlanAction.getPcp() != null) {
256                 pcp = pushVlanAction.getPcp();
257                 if (pushVlanAction.getCfi() != null && pushVlanAction.getCfi().getValue() != null) {
258                     cfi = pushVlanAction.getCfi().getValue();
259                     if (pushVlanAction.getVlanId() != null && pushVlanAction.getVlanId().getValue() != null) {
260                         vlanId = pushVlanAction.getVlanId().getValue();
261                         return new PushVlan(tag, pcp, cfi, vlanId);
262                     }
263                 }
264             }
265         }
266         return null;
267     }
268
269     private static NodeConnector fromNodeConnectorRef(Uri uri) {
270         // TODO: Define mapping
271         return null;
272     }
273
274     public static Match toMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Match source) {
275         Match target = new Match();
276         if (source != null) {
277             fillFrom(target, source.getVlanMatch());
278             fillFrom(target, source.getEthernetMatch());
279             fillFrom(target, source.getLayer3Match());
280             fillFrom(target, source.getLayer4Match());
281             fillFrom(target, source.getIpMatch());
282         }
283
284         return target;
285     }
286
287     private static void fillFrom(Match target, VlanMatch vlanMatch) {
288         if (vlanMatch != null) {
289             VlanId vlanId = vlanMatch.getVlanId();
290             if (vlanId != null) {
291                 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanIdInner = vlanId
292                         .getVlanId();
293                 if (vlanIdInner != null) {
294                     Integer vlanValue = vlanIdInner.getValue();
295                     if (vlanValue != null) {
296                         target.setField(DL_VLAN, vlanValue.shortValue());
297                     }
298                 }
299             }
300             VlanPcp vlanPcp = vlanMatch.getVlanPcp();
301             if (vlanPcp != null) {
302                 Short vlanPcpValue = vlanPcp.getValue();
303                 if (vlanPcpValue != null) {
304                     target.setField(DL_VLAN_PR, vlanPcpValue.byteValue());
305                 }
306             }
307         }
308     }
309
310     private static void fillFrom(Match target, IpMatch ipMatch) {
311         if (ipMatch != null) {
312             Short ipProtocol = ipMatch.getIpProtocol();
313
314             if (ipProtocol != null && target.getField(NW_PROTO) == null) {
315                 target.setField(NW_PROTO, ipProtocol.byteValue());
316             }
317             Dscp dscp = ipMatch.getIpDscp();
318             if (dscp != null) {
319                 Short dscpValue = dscp.getValue();
320                 if (dscpValue != null) {
321                     target.setField(NW_TOS, dscpValue.byteValue());
322                 }
323             }
324         }
325     }
326
327     private static void fillFrom(Match target, Layer4Match layer4Match) {
328         if (layer4Match == null) {
329             return;
330         }
331         if (layer4Match instanceof SctpMatch) {
332             fillTransportLayer(target, (SctpMatch) layer4Match);
333         } else if (layer4Match instanceof TcpMatch) {
334             fillTransportLayer(target, (TcpMatch) layer4Match);
335         } else if (layer4Match instanceof UdpMatch) {
336             fillTransportLayer(target, (UdpMatch) layer4Match);
337         }
338     }
339
340     private static void fillTransportLayer(Match target, UdpMatch source) {
341         PortNumber udpSourcePort = source.getUdpSourcePort();
342         if (udpSourcePort != null) {
343             Integer udpSourcePortValue = udpSourcePort.getValue();
344             if (udpSourcePortValue != null) {
345                 target.setField(TP_SRC, udpSourcePortValue.shortValue());
346             }
347         }
348
349         PortNumber udpDestPort = source.getUdpDestinationPort();
350         if (udpDestPort != null) {
351             Integer udpDestPortValue = udpDestPort.getValue();
352             if (udpDestPortValue != null) {
353                 target.setField(TP_DST, udpDestPortValue.shortValue());
354             }
355         }
356
357         target.setField(NW_PROTO, UDP);
358     }
359
360     private static void fillTransportLayer(Match target, TcpMatch source) {
361         PortNumber tcpSourcePort = source.getTcpSourcePort();
362         if (tcpSourcePort != null) {
363             Integer tcpSourcePortValue = tcpSourcePort.getValue();
364             if (tcpSourcePortValue != null) {
365                 target.setField(TP_SRC, tcpSourcePortValue.shortValue());
366             }
367         }
368
369         PortNumber tcpDestPort = source.getTcpDestinationPort();
370         if (tcpDestPort != null) {
371             Integer tcpDestPortValue = tcpDestPort.getValue();
372             if (tcpDestPortValue != null) {
373                 target.setField(TP_DST, tcpDestPortValue.shortValue());
374             }
375         }
376
377         target.setField(NW_PROTO, TCP);
378     }
379
380     private static void fillTransportLayer(Match target, SctpMatch source) {
381         PortNumber sctpSourcePort = source.getSctpSourcePort();
382         if (sctpSourcePort != null) {
383             Integer sctpSourcePortValue = sctpSourcePort.getValue();
384             if (sctpSourcePortValue != null) {
385                 target.setField(TP_SRC, sctpSourcePortValue.shortValue());
386             }
387         }
388         PortNumber sctpDestPort = source.getSctpDestinationPort();
389         if (sctpDestPort != null) {
390             Integer sctpDestPortValue = sctpDestPort.getValue();
391             if (sctpDestPortValue != null) {
392                 target.setField(TP_DST, sctpDestPortValue.shortValue());
393             }
394         }
395
396         target.setField(NW_PROTO, SCTP);
397
398     }
399
400     private static void fillFrom(Match target, Layer3Match source) {
401         if (source == null)
402             return;
403         if (source instanceof Ipv4Match) {
404             fillFromIpv4(target, (Ipv4Match) source);
405         } else if (source instanceof Ipv6Match) {
406             fillFromIpv6(target, (Ipv6Match) source);
407         } else if (source instanceof ArpMatch) {
408             fillFromArp(target, (ArpMatch) source);
409         }
410     }
411
412     private static void fillFromArp(Match target, ArpMatch source) {
413         Ipv4Prefix sourceAddress = source.getArpSourceTransportAddress();
414         if (sourceAddress != null) {
415             target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
416         }
417         Ipv4Prefix destAddress = source.getArpTargetTransportAddress();
418         if (destAddress != null) {
419             target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
420         }
421         ArpSourceHardwareAddress sourceHwAddress = source.getArpSourceHardwareAddress();
422         if (sourceHwAddress != null) {
423             target.setField(DL_SRC, bytesFrom(sourceHwAddress.getAddress()));
424         }
425         ArpTargetHardwareAddress targetHwAddress = source.getArpTargetHardwareAddress();
426         if (targetHwAddress != null) {
427             target.setField(DL_DST, bytesFrom(targetHwAddress.getAddress()));
428         }
429
430         target.setField(DL_TYPE, new Short(ETHERNET_ARP));
431
432     }
433
434     private static void fillFromIpv6(Match target, Ipv6Match source) {
435         Ipv6Prefix sourceAddress = source.getIpv6Source();
436         if (sourceAddress != null) {
437             target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
438         }
439         Ipv6Prefix destAddress = source.getIpv6Destination();
440         if (destAddress != null) {
441             target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
442         }
443     }
444
445     private static void fillFromIpv4(Match target, Ipv4Match source) {
446         Ipv4Prefix sourceAddress = source.getIpv4Source();
447         if (sourceAddress != null) {
448             target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
449         }
450         Ipv4Prefix destAddress = source.getIpv4Destination();
451         if (destAddress != null) {
452             target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
453         }
454     }
455
456     private static InetAddress inetAddressFrom(Ipv4Prefix source) {
457         if (source != null) {
458             String[] parts = source.getValue().split("/");
459             return InetAddresses.forString(parts[0]);
460         }
461         return null;
462     }
463
464     private static InetAddress inetAddressFrom(Ipv6Prefix source) {
465         if (source != null) {
466             String[] parts = source.getValue().split("/");
467             return InetAddresses.forString(parts[0]);
468         }
469         return null;
470     }
471
472     private static void fillFrom(Match target, EthernetMatch source) {
473         if (source == null)
474             return;
475         EthernetType ethType = source.getEthernetType();
476         if (ethType != null) {
477             EtherType ethInnerType = ethType.getType();
478             if (ethInnerType != null && target.getField(DL_TYPE) == null) {
479                 Long value = ethInnerType.getValue();
480                 target.setField(DL_TYPE, value.shortValue());
481             }
482         }
483
484         MacAddressFilter ethSource = source.getEthernetSource();
485         if (ethSource != null) {
486             target.setField(DL_SRC, bytesFrom(ethSource.getAddress()));
487         }
488
489         MacAddressFilter ethDest = source.getEthernetDestination();
490         if (ethDest != null) {
491             target.setField(DL_DST, bytesFrom(ethDest.getAddress()));
492         }
493     }
494
495     private static byte[] bytesFrom(MacAddress address) {
496         String[] mac = address.getValue().split(":");
497         byte[] macAddress = new byte[6];        // mac.length == 6 bytes
498         for(int i = 0; i < mac.length; i++) {
499             macAddress[i] = Integer.decode("0x" + mac[i]).byteValue();
500         }
501         return macAddress;
502     }
503 }