Merge "Added validation for protocol field while adding flow"
[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.HashSet;
17 import java.util.List;
18 import java.util.Set;
19
20 import org.opendaylight.controller.sal.action.Controller;
21 import org.opendaylight.controller.sal.action.Output;
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.Dscp;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeFlow;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.ControllerAction;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.OutputAction;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.PopMplsAction;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.PushMplsAction;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.PushPbbAction;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.PushVlanAction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.SetMplsTtlAction;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.SetNwTtlAction;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.SetQueueAction;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Action;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.MacAddressFilter;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.ethernet.match.fields.EthernetType;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.EthernetMatch;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.IpMatch;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.Layer3Match;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.Layer4Match;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.VlanMatch;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.ArpMatch;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv4Match;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv6Match;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.SctpMatch;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.TcpMatch;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.UdpMatch;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.vlan.match.fields.VlanId;
58
59 import com.google.common.net.InetAddresses;
60
61 public class ToSalConversionsUtils {
62
63     private ToSalConversionsUtils() {
64
65     }
66
67     public static Flow flowFrom(NodeFlow source) {
68         final Flow target = new Flow();
69
70         Integer hardTimeout = source.getHardTimeout();
71         if (hardTimeout != null) {
72             target.setHardTimeout(hardTimeout.shortValue());
73         }
74
75         Integer idleTimeout = source.getIdleTimeout();
76         if (idleTimeout != null) {
77             target.setIdleTimeout(idleTimeout.shortValue());
78         }
79
80         Integer priority = source.getPriority();
81         if (priority != null) {
82             target.setPriority(priority.shortValue());
83         }
84
85         target.setMatch(matchFrom(source.getMatch()));
86
87         List<Action> actions = source.getAction();
88         if (actions != null) {
89             for (Action sourceAction : actions) {
90                 Set<org.opendaylight.controller.sal.action.Action> targetActions = actionFrom(sourceAction);
91                 for (org.opendaylight.controller.sal.action.Action targetAction : targetActions) {
92                     target.addAction(targetAction);
93                 }
94             }
95         }
96
97         target.setId(source.getCookie().longValue());
98         return target;
99     }
100
101     public static Set<org.opendaylight.controller.sal.action.Action> actionFrom(Action source) {
102         org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.Action sourceAction = source
103                 .getAction();
104         Set<org.opendaylight.controller.sal.action.Action> targetAction = new HashSet<>();
105         if (sourceAction instanceof ControllerAction) {
106             targetAction.add(new Controller());
107         } else if (sourceAction instanceof OutputAction) {
108
109             List<Uri> nodeConnectors = ((OutputAction) sourceAction).getOutputNodeConnector();
110             for (Uri uri : nodeConnectors) {
111                 targetAction.add(new Output(fromNodeConnectorRef(uri)));
112             }
113         } else if (sourceAction instanceof PopMplsAction) {
114             // TODO: define maping
115         } else if (sourceAction instanceof PushMplsAction) {
116             // TODO: define maping
117         } else if (sourceAction instanceof PushPbbAction) {
118             // TODO: define maping
119         } else if (sourceAction instanceof PushVlanAction) {
120             // TODO: define maping
121             // PushVlanAction vlanAction = (PushVlanAction) sourceAction;
122             // targetAction.add(new PushVlan(vlanAction., pcp, cfi, vlanId);
123         } else if (sourceAction instanceof SetMplsTtlAction) {
124             // TODO: define maping
125             // targetAction = //no action to map
126         } else if (sourceAction instanceof SetNwTtlAction) {
127             // TODO: define maping
128         } else if (sourceAction instanceof SetQueueAction) {
129             // TODO: define maping
130             // targetAction = //no action to map
131         }
132
133         return targetAction;
134     }
135
136     private static NodeConnector fromNodeConnectorRef(Uri uri) {
137         // TODO: Define mapping
138         return null;
139     }
140
141     public static Match matchFrom(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Match source) {
142         Match target = new Match();
143         if (source != null) {
144             fillFrom(target, source.getVlanMatch());
145             fillFrom(target, source.getEthernetMatch());
146             fillFrom(target, source.getLayer3Match());
147             fillFrom(target, source.getLayer4Match());
148             fillFrom(target, source.getIpMatch());
149         }
150
151         return target;
152     }
153
154     private static void fillFrom(Match target, VlanMatch vlanMatch) {
155         if (vlanMatch != null) {
156             VlanId vlanId = vlanMatch.getVlanId();
157             if (vlanId != null) {
158                 org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanIdInner = vlanId
159                         .getVlanId();
160                 if (vlanIdInner != null) {
161                     Integer vlanValue = vlanIdInner.getValue();
162                     if (vlanValue != null) {
163                         target.setField(DL_VLAN, vlanValue.shortValue());
164                     }
165                 }
166             }
167             VlanPcp vlanPcp = vlanMatch.getVlanPcp();
168             if (vlanPcp != null) {
169                 Short vlanPcpValue = vlanPcp.getValue();
170                 if (vlanPcpValue != null) {
171                     target.setField(DL_VLAN_PR, vlanPcpValue.byteValue());
172                 }
173             }
174         }
175     }
176
177     private static void fillFrom(Match target, IpMatch ipMatch) {
178         if (ipMatch != null) {
179             Short ipProtocol = ipMatch.getIpProtocol();
180             if (ipProtocol != null) {
181                 target.setField(NW_PROTO, ipProtocol.byteValue());
182             }
183             Dscp dscp = ipMatch.getIpDscp();
184             if (dscp != null) {
185                 Short dscpValue = dscp.getValue();
186                 if (dscpValue != null) {
187                     target.setField(NW_TOS, dscpValue.byteValue());
188                 }
189             }
190         }
191     }
192
193     private static void fillFrom(Match target, Layer4Match layer4Match) {
194         if (layer4Match == null) {
195             return;
196         }
197         if (layer4Match instanceof SctpMatch) {
198             fillTransportLayer(target, (SctpMatch) layer4Match);
199         } else if (layer4Match instanceof TcpMatch) {
200             fillTransportLayer(target, (TcpMatch) layer4Match);
201         } else if (layer4Match instanceof UdpMatch) {
202             fillTransportLayer(target, (UdpMatch) layer4Match);
203         }
204     }
205
206     private static void fillTransportLayer(Match target, UdpMatch source) {
207         PortNumber udpSourcePort = source.getUdpSourcePort();
208         if (udpSourcePort != null) {
209             Integer udpSourcePortValue = udpSourcePort.getValue();
210             if (udpSourcePortValue != null) {
211                 target.setField(TP_SRC, udpSourcePortValue.shortValue());
212             }
213         }
214
215         PortNumber udpDestPort = source.getUdpDestinationPort();
216         if (udpDestPort != null) {
217             Integer udpDestPortValue = udpDestPort.getValue();
218             if (udpDestPortValue != null) {
219                 target.setField(TP_DST, udpDestPortValue.shortValue());
220             }
221         }
222     }
223
224     private static void fillTransportLayer(Match target, TcpMatch source) {
225         PortNumber tcpSourcePort = source.getTcpSourcePort();
226         if (tcpSourcePort != null) {
227             Integer tcpSourcePortValue = tcpSourcePort.getValue();
228             if (tcpSourcePortValue != null) {
229                 target.setField(TP_SRC, tcpSourcePortValue.shortValue());
230             }
231         }
232
233         PortNumber tcpDestPort = source.getTcpDestinationPort();
234         if (tcpDestPort != null) {
235             Integer tcpDestPortValue = tcpDestPort.getValue();
236             if (tcpDestPortValue != null) {
237                 target.setField(TP_DST, tcpDestPortValue.shortValue());
238             }
239         }
240     }
241
242     private static void fillTransportLayer(Match target, SctpMatch source) {
243         PortNumber sctpSourcePort = source.getSctpSourcePort();
244         if (sctpSourcePort != null) {
245             Integer sctpSourcePortValue = sctpSourcePort.getValue();
246             if (sctpSourcePortValue != null) {
247                 target.setField(TP_SRC, sctpSourcePortValue.shortValue());
248             }
249         }
250         PortNumber sctpDestPort = source.getSctpDestinationPort();
251         if (sctpDestPort != null) {
252             Integer sctpDestPortValue = sctpDestPort.getValue();
253             if (sctpDestPortValue != null) {
254                 target.setField(TP_DST, sctpDestPortValue.shortValue());
255             }
256         }
257     }
258
259     private static void fillFrom(Match target, Layer3Match source) {
260         if (source == null)
261             return;
262         if (source instanceof Ipv4Match) {
263             fillFromIpv4(target, (Ipv4Match) source);
264         } else if (source instanceof Ipv6Match) {
265             fillFromIpv6(target, (Ipv6Match) source);
266         } else if (source instanceof ArpMatch) {
267             fillFromArp(target, (ArpMatch) source);
268         }
269     }
270
271     private static void fillFromArp(Match target, ArpMatch source) {
272         Ipv4Prefix sourceAddress = source.getArpSourceTransportAddress();
273         if (sourceAddress != null) {
274             target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
275         }
276         Ipv4Prefix destAddress = source.getArpSourceTransportAddress();
277         if (destAddress != null) {
278             target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
279         }
280     }
281
282     private static void fillFromIpv6(Match target, Ipv6Match source) {
283         Ipv6Prefix sourceAddress = source.getIpv6Source();
284         if (sourceAddress != null) {
285             target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
286         }
287         Ipv6Prefix destAddress = source.getIpv6Source();
288         if (destAddress != null) {
289             target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
290         }
291     }
292
293     private static void fillFromIpv4(Match target, Ipv4Match source) {
294         Ipv4Prefix sourceAddress = source.getIpv4Source();
295         if (sourceAddress != null) {
296             target.setField(NW_SRC, (InetAddress) inetAddressFrom(sourceAddress), null);
297         }
298         Ipv4Prefix destAddress = source.getIpv4Source();
299         if (destAddress != null) {
300             target.setField(NW_DST, (InetAddress) inetAddressFrom(destAddress), null);
301         }
302     }
303
304     private static InetAddress inetAddressFrom(Ipv4Prefix source) {
305         if (source != null) {
306             String[] parts = source.getValue().split("/");
307             return InetAddresses.forString(parts[0]);
308         }
309         return null;
310     }
311
312     private static InetAddress inetAddressFrom(Ipv6Prefix source) {
313         if (source != null) {
314             String[] parts = source.getValue().split("/");
315             return InetAddresses.forString(parts[0]);
316         }
317         return null;
318     }
319
320     private static void fillFrom(Match target, EthernetMatch source) {
321         if (source == null)
322             return;
323         EthernetType ethType = source.getEthernetType();
324         if (ethType != null) {
325             EtherType ethInnerType = ethType.getType();
326             if (ethInnerType != null) {
327                 Long value = ethInnerType.getValue();
328                 target.setField(DL_TYPE, value.shortValue());
329             }
330         }
331
332         MacAddressFilter ethSource = source.getEthernetSource();
333         if (ethSource != null) {
334             target.setField(DL_SRC, bytesFrom(ethSource.getAddress()));
335         }
336
337         MacAddressFilter ethDest = source.getEthernetDestination();
338         if (ethDest != null) {
339             target.setField(DL_DST, bytesFrom(ethDest.getAddress()));
340         }
341     }
342
343     private static byte[] bytesFrom(MacAddress address) {
344         if (address != null) {
345             return address.getValue().getBytes();
346         }
347         return null;
348     }
349 }