To SAL conversion test.
[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.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;
14
15 import java.net.InetAddress;
16 import java.util.ArrayList;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Set;
20
21 import org.opendaylight.controller.sal.action.*;
22 import org.opendaylight.controller.sal.core.NodeConnector;
23 import org.opendaylight.controller.sal.flowprogrammer.Flow;
24 import org.opendaylight.controller.sal.match.Match;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.*;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeFlow;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.VlanCfi;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.*;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.Address;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.address.Ipv4;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.address.Ipv6;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Action;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.MacAddressFilter;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.*;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.ArpMatch;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv4Match;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv6Match;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.SctpMatch;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.TcpMatch;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.UdpMatch;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.vlan.match.fields.VlanId;
46
47 import com.google.common.net.InetAddresses;
48
49 public class ToSalConversionsUtils {
50
51     private ToSalConversionsUtils() {
52
53     }
54
55     public static Flow flowFrom(NodeFlow source) {
56         final Flow target = new Flow();
57
58         Integer hardTimeout = source.getHardTimeout();
59         if (hardTimeout != null) {
60             target.setHardTimeout(hardTimeout.shortValue());
61         }
62
63         Integer idleTimeout = source.getIdleTimeout();
64         if (idleTimeout != null) {
65             target.setIdleTimeout(idleTimeout.shortValue());
66         }
67
68         Integer priority = source.getPriority();
69         if (priority != null) {
70             target.setPriority(priority.shortValue());
71         }
72
73         target.setMatch(matchFrom(source.getMatch()));
74
75         List<Action> actions = source.getAction();
76         if (actions != null) {
77             target.setActions(actionFrom(actions));
78         }
79
80         target.setId(source.getCookie().longValue());
81         return target;
82     }
83
84     public static List<org.opendaylight.controller.sal.action.Action> actionFrom(List<Action> actions) {
85         List<org.opendaylight.controller.sal.action.Action> targetAction = new ArrayList<>();
86         for (Action action : actions) {
87             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.Action sourceAction = action
88                     .getAction();
89
90             if (sourceAction instanceof ControllerAction) {
91                 targetAction.add(new Controller());
92             } else if (sourceAction instanceof OutputAction) {
93
94                 List<Uri> nodeConnectors = ((OutputAction) sourceAction).getOutputNodeConnector();
95                 if (nodeConnectors != null) {
96                     for (Uri uri : nodeConnectors) {
97                         targetAction.add(new Output(fromNodeConnectorRef(uri)));
98                     }
99                 }
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 SetMplsTtlAction) {
107                 // TODO: define maping
108                 // targetAction = //no action to map
109             } else if (sourceAction instanceof SetNwTtlAction) {
110                 // TODO: define maping
111             } else if (sourceAction instanceof SetQueueAction) {
112                 // TODO: define maping
113                 // targetAction = //no action to map
114             } else if (sourceAction instanceof DropAction) {
115                 targetAction.add(new Drop());
116             } else if (sourceAction instanceof FloodAction) {
117                 targetAction.add(new Flood());
118             } else if (sourceAction instanceof FloodAllAction) {
119                 targetAction.add(new FloodAll());
120             } else if (sourceAction instanceof HwPathAction) {
121                 targetAction.add(new HwPath());
122             } else if (sourceAction instanceof LoopbackAction) {
123                 targetAction.add(new Loopback());
124             } else if (sourceAction instanceof PopVlanAction) {
125                 targetAction.add(new PopVlan());
126             } else if (sourceAction instanceof PushVlanAction) {
127                 PushVlanAction pushVlanAction = (PushVlanAction) sourceAction;
128                 PushVlan pushVlan = pushVlanFrom(pushVlanAction);
129                 if (pushVlan != null) {
130                     targetAction.add(pushVlan);
131                 }
132             } else if (sourceAction instanceof SetDlDstAction) {
133                 MacAddress addressL2Dest = ((SetDlDstAction) sourceAction).getAddress();
134                 if (addressL2Dest != null) {
135                     String addressValue = addressL2Dest.getValue();
136                     if (addressValue != null) {
137                         targetAction.add(new SetDlDst(addressValue.getBytes()));
138                     }
139                 }
140             } else if (sourceAction instanceof SetDlSrcAction) {
141                 MacAddress addressL2Src = ((SetDlSrcAction) sourceAction).getAddress();
142                 if (addressL2Src != null) {
143                     String addressValue = addressL2Src.getValue();
144                     if (addressValue != null) {
145                         targetAction.add(new SetDlSrc(addressValue.getBytes()));
146                     }
147                 }
148             } else if (sourceAction instanceof SetDlTypeAction) {
149                 EtherType dlType = ((SetDlTypeAction) sourceAction).getDlType();
150                 if (dlType != null) {
151                     Long dlTypeValue = dlType.getValue();
152                     if (dlTypeValue != null) {
153                         targetAction.add(new SetDlType(dlTypeValue.intValue()));
154                     }
155                 }
156             } else if (sourceAction instanceof SetNextHopAction) {
157                 Address addressL3 = ((SetNextHopAction) sourceAction).getAddress();
158
159                 InetAddress inetAddress = inetAddressFrom(addressL3);
160                 if (inetAddress != null) {
161                     targetAction.add(new SetNextHop(inetAddress));
162                 }
163             } else if (sourceAction instanceof SetNwDstAction) {
164                 Address addressL3 = ((SetNwDstAction) sourceAction).getAddress();
165
166                 InetAddress inetAddress = inetAddressFrom(addressL3);
167                 if (inetAddress != null) {
168                     targetAction.add(new SetNwDst(inetAddress));
169                 }
170             } else if (sourceAction instanceof SetNwSrcAction) {
171                 Address addressL3 = ((SetNwSrcAction) sourceAction).getAddress();
172
173                 InetAddress inetAddress = inetAddressFrom(addressL3);
174                 if (inetAddress != null) {
175                     targetAction.add(new SetNwSrc(inetAddress));
176                 }
177             } else if (sourceAction instanceof SetNwTosAction) {
178                 Integer tos = ((SetNwTosAction) sourceAction).getTos();
179                 if (tos != null) {
180                     targetAction.add(new SetNwTos(tos));
181                 }
182             } else if (sourceAction instanceof SetTpDstAction) {
183                 PortNumber port = ((SetTpDstAction) sourceAction).getPort();
184                 if (port != null) {
185                     Integer portValue = port.getValue();
186                     if (port.getValue() != null) {
187                         targetAction.add(new SetTpDst(portValue));
188                     }
189                 }
190             } else if (sourceAction instanceof SetTpSrcAction) {
191                 PortNumber port = ((SetTpSrcAction) sourceAction).getPort();
192                 if (port != null) {
193                     Integer portValue = port.getValue();
194                     if (port.getValue() != null) {
195                         targetAction.add(new SetTpSrc(portValue));
196                     }
197                 }
198             } else if (sourceAction instanceof SetVlanCfiAction) {
199                 VlanCfi vlanCfi = ((SetVlanCfiAction) sourceAction).getVlanCfi();
200                 if (vlanCfi != null) {
201                     Integer vlanCfiValue = vlanCfi.getValue();
202                     if (vlanCfiValue != null) {
203                         targetAction.add(new SetVlanCfi(vlanCfiValue));
204                     }
205                 }
206             } else if (sourceAction instanceof SetVlanIdAction) {
207                 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanID = ((SetVlanIdAction) sourceAction)
208                         .getVlanId();
209                 if (vlanID != null) {
210                     Integer vlanIdValue = vlanID.getValue();
211                     if (vlanIdValue != null) {
212                         targetAction.add(new SetVlanId(vlanIdValue));
213                     }
214                 }
215             } else if (sourceAction instanceof SetVlanPcpAction) {
216                 VlanPcp vlanPcp = ((SetVlanPcpAction) sourceAction).getVlanPcp();
217                 if (vlanPcp != null) {
218                     Short vlanPcpValue = vlanPcp.getValue();
219                     if (vlanPcpValue != null) {
220                         targetAction.add(new SetVlanPcp(vlanPcpValue));
221                     }
222                 }
223             } else if (sourceAction instanceof SwPathAction) {
224                 targetAction.add(new SwPath());
225             }
226         }
227
228         return targetAction;
229     }
230
231     private static InetAddress inetAddressFrom(Address addressL3) {
232         if (addressL3 != null) {
233             if (addressL3 instanceof Ipv4) {
234                 Ipv4Prefix addressL3Ipv4 = ((Ipv4) addressL3).getIpv4Address();
235                 if (addressL3Ipv4 != null) {
236                     return inetAddressFrom(addressL3Ipv4);
237                 }
238             } else if (addressL3 instanceof Ipv6) {
239                 Ipv6Prefix addressL3Ipv6 = ((Ipv6) addressL3).getIpv6Address();
240                 if (addressL3Ipv6 != null) {
241                     return inetAddressFrom(addressL3Ipv6);
242                 }
243             }
244         }
245         return null;
246     }
247
248     private static PushVlan pushVlanFrom(PushVlanAction pushVlanAction) {
249         final int tag;
250         final int pcp;
251         final int cfi;
252         final int vlanId;
253
254         if (pushVlanAction.getTag() != null) {
255             tag = pushVlanAction.getTag();
256             if (pushVlanAction.getPcp() != null) {
257                 pcp = pushVlanAction.getPcp();
258                 if (pushVlanAction.getCfi() != null && pushVlanAction.getCfi().getValue() != null) {
259                     cfi = pushVlanAction.getCfi().getValue();
260                     if (pushVlanAction.getVlanId() != null && pushVlanAction.getVlanId().getValue() != null) {
261                         vlanId = pushVlanAction.getVlanId().getValue();
262                         return new PushVlan(tag, pcp, cfi, vlanId);
263                     }
264                 }
265             }
266         }
267         return null;
268     }
269
270     private static NodeConnector fromNodeConnectorRef(Uri uri) {
271         // TODO: Define mapping
272         return null;
273     }
274
275     public static Match matchFrom(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Match source) {
276         Match target = new Match();
277         if (source != null) {
278             fillFrom(target, source.getVlanMatch());
279             fillFrom(target, source.getEthernetMatch());
280             fillFrom(target, source.getLayer3Match());
281             fillFrom(target, source.getLayer4Match());
282             fillFrom(target, source.getIpMatch());
283         }
284
285         return target;
286     }
287
288     private static void fillFrom(Match target, VlanMatch vlanMatch) {
289         if (vlanMatch != null) {
290             VlanId vlanId = vlanMatch.getVlanId();
291             if (vlanId != null) {
292                 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanIdInner = vlanId
293                         .getVlanId();
294                 if (vlanIdInner != null) {
295                     Integer vlanValue = vlanIdInner.getValue();
296                     if (vlanValue != null) {
297                         target.setField(DL_VLAN, vlanValue.shortValue());
298                     }
299                 }
300             }
301             VlanPcp vlanPcp = vlanMatch.getVlanPcp();
302             if (vlanPcp != null) {
303                 Short vlanPcpValue = vlanPcp.getValue();
304                 if (vlanPcpValue != null) {
305                     target.setField(DL_VLAN_PR, vlanPcpValue.byteValue());
306                 }
307             }
308         }
309     }
310
311     private static void fillFrom(Match target, IpMatch ipMatch) {
312         if (ipMatch != null) {
313             Short ipProtocol = ipMatch.getIpProtocol();
314             if (ipProtocol != 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
358     private static void fillTransportLayer(Match target, TcpMatch source) {
359         PortNumber tcpSourcePort = source.getTcpSourcePort();
360         if (tcpSourcePort != null) {
361             Integer tcpSourcePortValue = tcpSourcePort.getValue();
362             if (tcpSourcePortValue != null) {
363                 target.setField(TP_SRC, tcpSourcePortValue.shortValue());
364             }
365         }
366
367         PortNumber tcpDestPort = source.getTcpDestinationPort();
368         if (tcpDestPort != null) {
369             Integer tcpDestPortValue = tcpDestPort.getValue();
370             if (tcpDestPortValue != null) {
371                 target.setField(TP_DST, tcpDestPortValue.shortValue());
372             }
373         }
374     }
375
376     private static void fillTransportLayer(Match target, SctpMatch source) {
377         PortNumber sctpSourcePort = source.getSctpSourcePort();
378         if (sctpSourcePort != null) {
379             Integer sctpSourcePortValue = sctpSourcePort.getValue();
380             if (sctpSourcePortValue != null) {
381                 target.setField(TP_SRC, sctpSourcePortValue.shortValue());
382             }
383         }
384         PortNumber sctpDestPort = source.getSctpDestinationPort();
385         if (sctpDestPort != null) {
386             Integer sctpDestPortValue = sctpDestPort.getValue();
387             if (sctpDestPortValue != null) {
388                 target.setField(TP_DST, sctpDestPortValue.shortValue());
389             }
390         }
391     }
392
393     private static void fillFrom(Match target, Layer3Match source) {
394         if (source == null)
395             return;
396         if (source instanceof Ipv4Match) {
397             fillFromIpv4(target, (Ipv4Match) source);
398         } else if (source instanceof Ipv6Match) {
399             fillFromIpv6(target, (Ipv6Match) source);
400         } else if (source instanceof ArpMatch) {
401             fillFromArp(target, (ArpMatch) source);
402         }
403     }
404
405     private static void fillFromArp(Match target, ArpMatch source) {
406         Ipv4Prefix sourceAddress = source.getArpSourceTransportAddress();
407         if (sourceAddress != null) {
408             target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
409         }
410         Ipv4Prefix destAddress = source.getArpSourceTransportAddress();
411         if (destAddress != null) {
412             target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
413         }
414     }
415
416     private static void fillFromIpv6(Match target, Ipv6Match source) {
417         Ipv6Prefix sourceAddress = source.getIpv6Source();
418         if (sourceAddress != null) {
419             target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
420         }
421         Ipv6Prefix destAddress = source.getIpv6Source();
422         if (destAddress != null) {
423             target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
424         }
425     }
426
427     private static void fillFromIpv4(Match target, Ipv4Match source) {
428         Ipv4Prefix sourceAddress = source.getIpv4Source();
429         if (sourceAddress != null) {
430             target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
431         }
432         Ipv4Prefix destAddress = source.getIpv4Source();
433         if (destAddress != null) {
434             target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
435         }
436     }
437
438     private static InetAddress inetAddressFrom(Ipv4Prefix source) {
439         if (source != null) {
440             String[] parts = source.getValue().split("/");
441             return InetAddresses.forString(parts[0]);
442         }
443         return null;
444     }
445
446     private static InetAddress inetAddressFrom(Ipv6Prefix source) {
447         if (source != null) {
448             String[] parts = source.getValue().split("/");
449             return InetAddresses.forString(parts[0]);
450         }
451         return null;
452     }
453
454     private static void fillFrom(Match target, EthernetMatch source) {
455         if (source == null)
456             return;
457         EthernetType ethType = source.getEthernetType();
458         if (ethType != null) {
459             EtherType ethInnerType = ethType.getType();
460             if (ethInnerType != null) {
461                 Long value = ethInnerType.getValue();
462                 target.setField(DL_TYPE, value.shortValue());
463             }
464         }
465
466         MacAddressFilter ethSource = source.getEthernetSource();
467         if (ethSource != null) {
468             target.setField(DL_SRC, bytesFrom(ethSource.getAddress()));
469         }
470
471         MacAddressFilter ethDest = source.getEthernetDestination();
472         if (ethDest != null) {
473             target.setField(DL_DST, bytesFrom(ethDest.getAddress()));
474         }
475     }
476
477     private static byte[] bytesFrom(MacAddress address) {
478         if (address != null) {
479             return address.getValue().getBytes();
480         }
481         return null;
482     }
483 }