702bd31aeb72651edfb1619531f41980c3158d1c
[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.Collections;
22 import java.util.List;
23
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;
52
53 import com.google.common.net.InetAddresses;
54
55 public class ToSalConversionsUtils {
56
57     private ToSalConversionsUtils() {
58
59     }
60
61     public static Flow toFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow source) {
62         final Flow target = new Flow();
63
64         Integer hardTimeout = source.getHardTimeout();
65         if (hardTimeout != null) {
66             target.setHardTimeout(hardTimeout.shortValue());
67         }
68
69         Integer idleTimeout = source.getIdleTimeout();
70         if (idleTimeout != null) {
71             target.setIdleTimeout(idleTimeout.shortValue());
72         }
73
74         Integer priority = source.getPriority();
75         if (priority != null) {
76             target.setPriority(priority.shortValue());
77         }
78
79         target.setMatch(toMatch(source.getMatch()));
80
81         List<Action> actions = getAction(source);
82         if (actions != null) {
83             target.setActions(actionFrom(actions));
84         }
85
86         target.setId(source.getCookie().longValue());
87         return target;
88     }
89
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());
96                 }
97             }
98         }
99         // TODO Auto-generated method stub
100         return Collections.emptyList();
101     }
102
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
107                     .getAction();
108
109             if (sourceAction instanceof ControllerAction) {
110                 targetAction.add(new Controller());
111             } else if (sourceAction instanceof OutputAction) {
112
113                 List<Uri> nodeConnectors = ((OutputAction) sourceAction).getOutputNodeConnector();
114                 if (nodeConnectors != null) {
115                     for (Uri uri : nodeConnectors) {
116                         targetAction.add(new Output(fromNodeConnectorRef(uri)));
117                     }
118                 }
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);
150                 }
151             } else if (sourceAction instanceof SetDlDstAction) {
152                 MacAddress addressL2Dest = ((SetDlDstAction) sourceAction).getAddress();
153                 if (addressL2Dest != null) {
154                     targetAction.add(new SetDlDst(bytesFrom(addressL2Dest)));
155                 }
156             } else if (sourceAction instanceof SetDlSrcAction) {
157                 MacAddress addressL2Src = ((SetDlSrcAction) sourceAction).getAddress();
158                 if (addressL2Src != null) {
159                     targetAction.add(new SetDlSrc(bytesFrom(addressL2Src)));
160
161                 }
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()));
168                     }
169                 }
170             } else if (sourceAction instanceof SetNextHopAction) {
171                 Address addressL3 = ((SetNextHopAction) sourceAction).getAddress();
172
173                 InetAddress inetAddress = inetAddressFrom(addressL3);
174                 if (inetAddress != null) {
175                     targetAction.add(new SetNextHop(inetAddress));
176                 }
177             } else if (sourceAction instanceof SetNwDstAction) {
178                 Address addressL3 = ((SetNwDstAction) sourceAction).getAddress();
179
180                 InetAddress inetAddress = inetAddressFrom(addressL3);
181                 if (inetAddress != null) {
182                     targetAction.add(new SetNwDst(inetAddress));
183                 }
184             } else if (sourceAction instanceof SetNwSrcAction) {
185                 Address addressL3 = ((SetNwSrcAction) sourceAction).getAddress();
186
187                 InetAddress inetAddress = inetAddressFrom(addressL3);
188                 if (inetAddress != null) {
189                     targetAction.add(new SetNwSrc(inetAddress));
190                 }
191             } else if (sourceAction instanceof SetNwTosAction) {
192                 Integer tos = ((SetNwTosAction) sourceAction).getTos();
193                 if (tos != null) {
194                     targetAction.add(new SetNwTos(tos));
195                 }
196             } else if (sourceAction instanceof SetTpDstAction) {
197                 PortNumber port = ((SetTpDstAction) sourceAction).getPort();
198                 if (port != null) {
199                     Integer portValue = port.getValue();
200                     if (port.getValue() != null) {
201                         targetAction.add(new SetTpDst(portValue));
202                     }
203                 }
204             } else if (sourceAction instanceof SetTpSrcAction) {
205                 PortNumber port = ((SetTpSrcAction) sourceAction).getPort();
206                 if (port != null) {
207                     Integer portValue = port.getValue();
208                     if (port.getValue() != null) {
209                         targetAction.add(new SetTpSrc(portValue));
210                     }
211                 }
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));
218                     }
219                 }
220             } else if (sourceAction instanceof SetVlanIdAction) {
221                 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanID = ((SetVlanIdAction) sourceAction)
222                         .getVlanId();
223                 if (vlanID != null) {
224                     Integer vlanIdValue = vlanID.getValue();
225                     if (vlanIdValue != null) {
226                         targetAction.add(new SetVlanId(vlanIdValue));
227                     }
228                 }
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));
235                     }
236                 }
237             } else if (sourceAction instanceof SwPathAction) {
238                 targetAction.add(new SwPath());
239             }
240         }
241
242         return targetAction;
243     }
244
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);
251                 }
252             } else if (addressL3 instanceof Ipv6) {
253                 Ipv6Prefix addressL3Ipv6 = ((Ipv6) addressL3).getIpv6Address();
254                 if (addressL3Ipv6 != null) {
255                     return inetAddressFrom(addressL3Ipv6);
256                 }
257             }
258         }
259         return null;
260     }
261
262     private static PushVlan pushVlanFrom(PushVlanAction pushVlanAction) {
263         final int tag;
264         final int pcp;
265         final int cfi;
266         final int vlanId;
267
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);
277                     }
278                 }
279             }
280         }
281         return null;
282     }
283
284     private static NodeConnector fromNodeConnectorRef(Uri uri) {
285         // TODO: Define mapping
286         return null;
287     }
288
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());
297         }
298
299         return target;
300     }
301
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
307                         .getVlanId();
308                 if (vlanIdInner != null) {
309                     Integer vlanValue = vlanIdInner.getValue();
310                     if (vlanValue != null) {
311                         target.setField(DL_VLAN, vlanValue.shortValue());
312                     }
313                 }
314             }
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());
320                 }
321             }
322         }
323     }
324
325     private static void fillFrom(Match target, IpMatch ipMatch) {
326         if (ipMatch != null) {
327             Short ipProtocol = ipMatch.getIpProtocol();
328
329             if (ipProtocol != null && target.getField(NW_PROTO) == null) {
330                 target.setField(NW_PROTO, ipProtocol.byteValue());
331             }
332             Dscp dscp = ipMatch.getIpDscp();
333             if (dscp != null) {
334                 Short dscpValue = dscp.getValue();
335                 if (dscpValue != null) {
336                     target.setField(NW_TOS, dscpValue.byteValue());
337                 }
338             }
339         }
340     }
341
342     private static void fillFrom(Match target, Layer4Match layer4Match) {
343         if (layer4Match == null) {
344             return;
345         }
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);
352         }
353     }
354
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());
361             }
362         }
363
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());
369             }
370         }
371
372         target.setField(NW_PROTO, UDP);
373     }
374
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());
381             }
382         }
383
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());
389             }
390         }
391
392         target.setField(NW_PROTO, TCP);
393     }
394
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());
401             }
402         }
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());
408             }
409         }
410
411         target.setField(NW_PROTO, SCTP);
412
413     }
414
415     private static void fillFrom(Match target, Layer3Match source) {
416         if (source == null)
417             return;
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);
424         }
425     }
426
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);
431         }
432         Ipv4Prefix destAddress = source.getArpTargetTransportAddress();
433         if (destAddress != null) {
434             target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
435         }
436         ArpSourceHardwareAddress sourceHwAddress = source.getArpSourceHardwareAddress();
437         if (sourceHwAddress != null) {
438             target.setField(DL_SRC, bytesFrom(sourceHwAddress.getAddress()));
439         }
440         ArpTargetHardwareAddress targetHwAddress = source.getArpTargetHardwareAddress();
441         if (targetHwAddress != null) {
442             target.setField(DL_DST, bytesFrom(targetHwAddress.getAddress()));
443         }
444
445         target.setField(DL_TYPE, new Short(ETHERNET_ARP));
446
447     }
448
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);
453         }
454         Ipv6Prefix destAddress = source.getIpv6Destination();
455         if (destAddress != null) {
456             target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
457         }
458     }
459
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);
464         }
465         Ipv4Prefix destAddress = source.getIpv4Destination();
466         if (destAddress != null) {
467             target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
468         }
469     }
470
471     private static InetAddress inetAddressFrom(Ipv4Prefix source) {
472         if (source != null) {
473             String[] parts = source.getValue().split("/");
474             return InetAddresses.forString(parts[0]);
475         }
476         return null;
477     }
478
479     private static InetAddress inetAddressFrom(Ipv6Prefix source) {
480         if (source != null) {
481             String[] parts = source.getValue().split("/");
482             return InetAddresses.forString(parts[0]);
483         }
484         return null;
485     }
486
487     private static void fillFrom(Match target, EthernetMatch source) {
488         if (source == null)
489             return;
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());
496             }
497         }
498
499         MacAddressFilter ethSource = source.getEthernetSource();
500         if (ethSource != null) {
501             target.setField(DL_SRC, bytesFrom(ethSource.getAddress()));
502         }
503
504         MacAddressFilter ethDest = source.getEthernetDestination();
505         if (ethDest != null) {
506             target.setField(DL_DST, bytesFrom(ethDest.getAddress()));
507         }
508     }
509
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();
515         }
516         return macAddress;
517     }
518 }