Merge "FRM to remove flows on switchport admin down"
[controller.git] / opendaylight / md-sal / sal-compability / src / main / java / org / opendaylight / controller / sal / compability / FromSalConversionsUtils.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
7 import java.math.BigInteger;
8 import java.net.Inet4Address;
9 import java.net.Inet6Address;
10 import java.net.InetAddress;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.List;
14
15 import org.opendaylight.controller.sal.action.Controller;
16 import org.opendaylight.controller.sal.action.Drop;
17 import org.opendaylight.controller.sal.action.Flood;
18 import org.opendaylight.controller.sal.action.FloodAll;
19 import org.opendaylight.controller.sal.action.HwPath;
20 import org.opendaylight.controller.sal.action.Loopback;
21 import org.opendaylight.controller.sal.action.Output;
22 import org.opendaylight.controller.sal.action.PopVlan;
23 import org.opendaylight.controller.sal.action.PushVlan;
24 import org.opendaylight.controller.sal.action.SetDlDst;
25 import org.opendaylight.controller.sal.action.SetDlSrc;
26 import org.opendaylight.controller.sal.action.SetDlType;
27 import org.opendaylight.controller.sal.action.SetNextHop;
28 import org.opendaylight.controller.sal.action.SetNwDst;
29 import org.opendaylight.controller.sal.action.SetNwSrc;
30 import org.opendaylight.controller.sal.action.SetNwTos;
31 import org.opendaylight.controller.sal.action.SetTpDst;
32 import org.opendaylight.controller.sal.action.SetTpSrc;
33 import org.opendaylight.controller.sal.action.SetVlanCfi;
34 import org.opendaylight.controller.sal.action.SetVlanId;
35 import org.opendaylight.controller.sal.action.SetVlanPcp;
36 import org.opendaylight.controller.sal.action.SwPath;
37 import org.opendaylight.controller.sal.core.NodeConnector;
38 import org.opendaylight.controller.sal.flowprogrammer.Flow;
39 import org.opendaylight.controller.sal.match.Match;
40 import org.opendaylight.controller.sal.match.MatchField;
41 import org.opendaylight.controller.sal.match.MatchType;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Dscp;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAddedBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.ControllerActionBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.OutputActionBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Action;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.ActionBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.arp.match.fields.ArpSourceHardwareAddressBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.arp.match.fields.ArpTargetHardwareAddressBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetDestinationBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetSourceBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetTypeBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.EthernetMatch;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.EthernetMatchBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.IpMatch;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.IpMatchBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.Layer3Match;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.Layer4Match;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.VlanMatch;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.VlanMatchBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.ArpMatchBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv4MatchBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv6MatchBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.SctpMatchBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.TcpMatchBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.UdpMatchBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.vlan.match.fields.VlanIdBuilder;
77
78 public class FromSalConversionsUtils {
79
80     // source: http://en.wikipedia.org/wiki/Ethertype
81     private static final Short ETHERNET_ARP = new Short((short) 0x0806);
82
83     // source: http://en.wikipedia.org/wiki/List_of_IP_protocol_numbers
84     private static final short TCP = (short) 0x06;
85     private static final short UDP = (short) 0x11;
86     private static final short SCTP = (short) 0x84;
87
88     private FromSalConversionsUtils() {
89
90     }
91
92     public static FlowAdded flowFrom(Flow sourceFlow) {
93         if (sourceFlow != null) {
94             final FlowAddedBuilder targetFlow = new FlowAddedBuilder();
95
96             targetFlow.setHardTimeout(new Integer(sourceFlow.getHardTimeout()));
97             targetFlow.setIdleTimeout(new Integer(sourceFlow.getIdleTimeout()));
98             targetFlow.setPriority(new Integer(sourceFlow.getPriority()));
99             targetFlow.setCookie(new BigInteger(String.valueOf(sourceFlow.getId())));
100
101             List<org.opendaylight.controller.sal.action.Action> sourceActions = sourceFlow.getActions();
102             List<Action> targetActions = new ArrayList<>();
103             for (org.opendaylight.controller.sal.action.Action sourceAction : sourceActions) {
104                 targetActions.add(actionFrom(sourceAction));
105             }
106             targetFlow.setAction(targetActions);
107
108             targetFlow.setMatch(matchFrom(sourceFlow.getMatch()));
109
110             return targetFlow.build();
111         }
112         return null;
113     }
114
115     private static Action actionFrom(org.opendaylight.controller.sal.action.Action sourceAction) {
116
117         ActionBuilder targetActionBuilder = new ActionBuilder();
118         org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.Action targetAction = null;
119
120         if (sourceAction instanceof Controller) {
121             targetAction = new ControllerActionBuilder().build();
122         } else if (sourceAction instanceof Drop) {
123             // TODO: define maping
124         } else if (sourceAction instanceof Flood) {
125             // TODO: define maping
126         } else if (sourceAction instanceof FloodAll) {
127             // TODO: define maping
128         } else if (sourceAction instanceof HwPath) {
129             // TODO: define maping
130         } else if (sourceAction instanceof Loopback) {
131             // TODO: define maping
132         } else if (sourceAction instanceof Output) {
133             NodeConnector nodeConnector = ((Output) sourceAction).getPort();
134
135             OutputActionBuilder outputActionBuilder = new OutputActionBuilder();
136             outputActionBuilder.setOutputNodeConnector(nodeConnectorToUri(nodeConnector));
137             targetAction = outputActionBuilder.build();
138
139         } else if (sourceAction instanceof PopVlan) {
140             // TODO: define maping
141         } else if (sourceAction instanceof PushVlan) {
142             // TODO: define maping
143         } else if (sourceAction instanceof SetDlDst) {
144             // TODO: define maping
145         } else if (sourceAction instanceof SetDlSrc) {
146             // TODO: define maping
147         } else if (sourceAction instanceof SetDlType) {
148             // TODO: define maping
149         } else if (sourceAction instanceof SetNextHop) {
150             // TODO: define maping
151         } else if (sourceAction instanceof SetNwDst) {
152             // TODO: define maping
153         } else if (sourceAction instanceof SetNwSrc) {
154             // TODO: define maping
155         } else if (sourceAction instanceof SetNwTos) {
156             // TODO: define maping
157         } else if (sourceAction instanceof SetTpDst) {
158             // TODO: define maping
159         } else if (sourceAction instanceof SetTpSrc) {
160             // TODO: define maping
161         } else if (sourceAction instanceof SetVlanCfi) {
162             // TODO: define maping
163         } else if (sourceAction instanceof SetVlanId) {
164             // TODO: define maping
165         } else if (sourceAction instanceof SetVlanPcp) {
166             // TODO: define maping
167         } else if (sourceAction instanceof SwPath) {
168             // TODO: define maping
169         }
170
171         targetActionBuilder.setAction(targetAction);
172
173         return targetActionBuilder.build();
174     }
175
176     private static List<Uri> nodeConnectorToUri(NodeConnector nodeConnector) {
177         // TODO Define mapping
178         return null;
179     }
180
181     private static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Match matchFrom(
182             Match sourceMatch) {
183         if (sourceMatch != null) {
184             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.MatchBuilder targetBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.MatchBuilder();
185
186             targetBuilder.setEthernetMatch(ethernetMatchFrom(sourceMatch));
187             targetBuilder.setIpMatch(ipMatchFrom(sourceMatch));
188             targetBuilder.setVlanMatch(vlanMatchFrom(sourceMatch));
189             targetBuilder.setLayer3Match(layer3Match(sourceMatch));
190             targetBuilder.setLayer4Match(layer4Match(sourceMatch));
191
192             return targetBuilder.build();
193         }
194         return null;
195
196     }
197
198     private static Layer4Match layer4Match(final Match sourceMatch) {
199         MatchField nwProto = sourceMatch.getField(MatchType.NW_PROTO);
200         Short nwProtocolSource = null;
201         if (nwProto != null && nwProto.getValue() != null) {
202             nwProtocolSource = (Short) (nwProto.getValue());
203         }
204
205         switch (nwProtocolSource) {
206         case TCP:
207             return Layer4MatchAsTcp(sourceMatch);
208         case UDP:
209             return Layer4MatchAsUdp(sourceMatch);
210         case SCTP:
211             return Layer4MatchAsSctp(sourceMatch);
212         }
213         return null;
214     }
215
216     private static Layer4Match Layer4MatchAsSctp(final Match sourceMatch) {
217         SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
218
219         Integer sourcePort = transportPortFrom(sourceMatch, MatchType.TP_SRC);
220         Integer destinationPort = transportPortFrom(sourceMatch, MatchType.TP_DST);
221
222         if (sourcePort != null) {
223             sctpMatchBuilder.setSctpSourcePort(new PortNumber(sourcePort));
224         }
225         if (destinationPort != null) {
226             sctpMatchBuilder.setSctpDestinationPort(new PortNumber(destinationPort));
227         }
228
229         return sctpMatchBuilder.build();
230     }
231
232     private static Layer4Match Layer4MatchAsUdp(final Match sourceMatch) {
233         UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
234
235         Integer sourcePort = transportPortFrom(sourceMatch, MatchType.TP_SRC);
236         Integer destinationPort = transportPortFrom(sourceMatch, MatchType.TP_DST);
237
238         if (sourcePort != null) {
239             udpMatchBuilder.setUdpSourcePort(new PortNumber(sourcePort));
240         }
241
242         if (destinationPort != null) {
243             udpMatchBuilder.setUdpDestinationPort(new PortNumber(destinationPort));
244         }
245
246         return udpMatchBuilder.build();
247     }
248
249     private static Layer4Match Layer4MatchAsTcp(final Match sourceMatch) {
250         TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
251
252         Integer sourcePort = transportPortFrom(sourceMatch, MatchType.TP_SRC);
253         Integer destinationPort = transportPortFrom(sourceMatch, MatchType.TP_DST);
254
255         if (sourcePort != null) {
256             tcpMatchBuilder.setTcpSourcePort(new PortNumber(sourcePort));
257         }
258         if (destinationPort != null) {
259             tcpMatchBuilder.setTcpDestinationPort(new PortNumber(destinationPort));
260         }
261
262         return tcpMatchBuilder.build();
263     }
264
265     private static Integer transportPortFrom(final Match sourceMatch, final MatchType matchType) {
266         MatchField transportPort = sourceMatch.getField(matchType);
267         if (transportPort != null && transportPort.getValue() != null) {
268             return (Integer) (transportPort.getValue());
269         }
270         return null;
271     }
272
273     private static VlanMatch vlanMatchFrom(final Match sourceMatch) {
274         VlanMatchBuilder vlanMatchBuild = new VlanMatchBuilder();
275
276         MatchField vlan = sourceMatch.getField(MatchType.DL_VLAN);
277         if (vlan != null && vlan.getValue() != null) {
278             VlanIdBuilder vlanIDBuilder = new VlanIdBuilder();
279             vlanIDBuilder.setVlanId(new VlanId((Integer) (vlan.getValue())));
280             vlanMatchBuild.setVlanId(vlanIDBuilder.build());
281         }
282
283         MatchField vlanPriority = sourceMatch.getField(MatchType.DL_VLAN_PR);
284         if (vlanPriority != null && vlanPriority.getValue() != null) {
285             vlanMatchBuild.setVlanPcp(new VlanPcp((Short) (vlanPriority.getValue())));
286         }
287
288         return vlanMatchBuild.build();
289     }
290
291     private static IpMatch ipMatchFrom(final Match sourceMatch) {
292         IpMatchBuilder targetIpMatchBuild = new IpMatchBuilder();
293         MatchField networkTos = sourceMatch.getField(MatchType.NW_TOS);
294         if (networkTos != null && networkTos.getValue() != null) {
295             Dscp dscp = new Dscp((Short) (networkTos.getValue()));
296             targetIpMatchBuild.setIpDscp(dscp);
297         }
298
299         MatchField protocol = sourceMatch.getField(MatchType.NW_PROTO);
300         if (protocol != null && protocol.getValue() != null) {
301             targetIpMatchBuild.setIpProtocol((Short) (protocol.getValue()));
302         }
303
304         return targetIpMatchBuild.build();
305
306     }
307
308     private static EthernetMatch ethernetMatchFrom(final Match sourceMatch) {
309         final EthernetMatchBuilder targetEthMatchBuild = new EthernetMatchBuilder();
310
311         EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder()
312                 .setAddress(ethernetSourceAddressFrom(sourceMatch));
313         targetEthMatchBuild.setEthernetSource(ethSourBuild.build());
314
315         final MatchField dataLinkDest = sourceMatch.getField(DL_DST);
316         if (dataLinkDest != null && dataLinkDest.getValue() != null) {
317             final MacAddress macDestAddress;
318             macDestAddress = new MacAddress((String) (dataLinkDest.getValue()));
319             EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder().setAddress(macDestAddress);
320             targetEthMatchBuild.setEthernetDestination(ethDestBuild.build());
321         }
322
323         final MatchField dataLinkType = sourceMatch.getField(MatchType.DL_TYPE);
324         if (dataLinkType != null && dataLinkType.getValue() != null) {
325             EtherType etherType = new EtherType((Long) (dataLinkType.getValue()));
326             EthernetTypeBuilder ethType = new EthernetTypeBuilder().setType(etherType);
327             targetEthMatchBuild.setEthernetType(ethType.build());
328         }
329         return targetEthMatchBuild.build();
330     }
331
332     private static MacAddress ethernetSourceAddressFrom(final Match sourceMatch) {
333         final MatchField dataLinkSource = sourceMatch.getField(DL_SRC);
334         if (dataLinkSource != null && dataLinkSource.getValue() != null) {
335             return new MacAddress(new MacAddress((String) (dataLinkSource.getValue())));
336         }
337         return null;
338
339     }
340
341     private static Layer3Match layer3Match(final Match sourceMatch) {
342         InetAddress inetSourceAddress = null;
343         MatchField netSource = sourceMatch.getField(MatchType.NW_SRC);
344         if (netSource != null && netSource.getValue() != null) {
345             inetSourceAddress = (InetAddress) (netSource.getValue());
346         }
347
348         InetAddress inetDestAddress = null;
349         MatchField netDest = sourceMatch.getField(MatchType.NW_DST);
350         if (netSource != null && netSource.getValue() != null) {
351             inetDestAddress = (InetAddress) (netDest.getValue());
352         }
353
354         if ((inetSourceAddress instanceof Inet4Address) && (inetDestAddress instanceof Inet4Address)) {
355             MatchField dataLinkType = sourceMatch.getField(DL_TYPE);
356             Short dLType = null;
357             if (dataLinkType != null && dataLinkType.getValue() != null) {
358                 dLType = (Short) (dataLinkType.getValue());
359             }
360             if (dLType.equals(ETHERNET_ARP)) {
361                 return setLayer3MatchAsArp(sourceMatch, (Inet4Address) inetSourceAddress,
362                         (Inet4Address) inetDestAddress);
363             } else {
364                 return setLayer3MatchAsIpv4((Inet4Address) inetSourceAddress, (Inet4Address) inetDestAddress);
365             }
366         } else if ((inetSourceAddress instanceof Inet6Address) && (inetDestAddress instanceof Inet6Address)) {
367             return setLayer3MatchAsIpv6((Inet6Address) inetSourceAddress, (Inet6Address) inetDestAddress);
368         }
369
370         return null;
371
372     }
373
374     private static Layer3Match setLayer3MatchAsArp(final Match sourceMatch, final Inet4Address inetSourceAddress,
375             final Inet4Address inetDestAddress) {
376         byte[] inetSourceAddressValue = inetSourceAddress.getAddress();
377         Ipv4Prefix ipv4SourcePrefix = new Ipv4Prefix(Arrays.toString(inetSourceAddressValue));
378
379         byte[] inetDestAddressValue = inetDestAddress.getAddress();
380         Ipv4Prefix ipv4DestPrefix = new Ipv4Prefix(Arrays.toString(inetDestAddressValue));
381
382         ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
383
384         arpMatchBuilder.setArpSourceTransportAddress(ipv4SourcePrefix);
385         arpMatchBuilder.setArpSourceTransportAddress(ipv4DestPrefix);
386
387         ArpSourceHardwareAddressBuilder arpSourceHardwareAddressBuilder = new ArpSourceHardwareAddressBuilder();
388         arpSourceHardwareAddressBuilder.setAddress(ethernetSourceAddressFrom(sourceMatch));
389         arpMatchBuilder.setArpSourceHardwareAddress(arpSourceHardwareAddressBuilder.build());
390
391         ArpTargetHardwareAddressBuilder arpTargetHardwareAddressBuilder = new ArpTargetHardwareAddressBuilder();
392         arpTargetHardwareAddressBuilder.setAddress(ethernetDestAddressFrom(sourceMatch));
393         arpMatchBuilder.setArpTargetHardwareAddress(arpTargetHardwareAddressBuilder.build());
394
395         return arpMatchBuilder.build();
396
397     }
398
399     private static MacAddress ethernetDestAddressFrom(final Match sourceMatch) {
400         final MatchField dataLinkDest = sourceMatch.getField(DL_DST);
401         if (dataLinkDest != null && dataLinkDest.getValue() != null) {
402             return new MacAddress((String) (dataLinkDest.getValue()));
403         }
404         return null;
405     }
406
407     private static Layer3Match setLayer3MatchAsIpv4(final Inet4Address inetSourceAddress,
408             final Inet4Address inetDestAddress) {
409         byte[] inetAddressValue = inetSourceAddress.getAddress();
410
411         Ipv4MatchBuilder layer4MatchBuild = new Ipv4MatchBuilder();
412         layer4MatchBuild.setIpv4Source(new Ipv4Prefix(Arrays.toString(inetAddressValue)));
413         return layer4MatchBuild.build();
414
415     }
416
417     private static Layer3Match setLayer3MatchAsIpv6(final Inet6Address inetSourceAddress,
418             final Inet6Address inetDestAddress) {
419         byte[] inetAddressValue = inetSourceAddress.getAddress();
420         Ipv6MatchBuilder layer6MatchBuild = new Ipv6MatchBuilder();
421
422         layer6MatchBuild.setIpv6Source(new Ipv6Prefix(Arrays.toString(inetAddressValue)));
423         return layer6MatchBuild.build();
424     }
425
426 }