Add test for generated code checking list of dependencies.
[controller.git] / opendaylight / md-sal / compatibility / sal-compatibility / src / main / java / org / opendaylight / controller / sal / compatibility / MDFlowMapping.xtend
1 package org.opendaylight.controller.sal.compatibility;
2
3 import com.google.common.net.InetAddresses
4 import java.math.BigInteger
5 import java.net.Inet4Address
6 import java.net.Inet6Address
7 import java.util.ArrayList
8 import java.util.List
9 import org.opendaylight.controller.sal.action.Controller
10 import org.opendaylight.controller.sal.action.Drop
11 import org.opendaylight.controller.sal.action.Flood
12 import org.opendaylight.controller.sal.action.FloodAll
13 import org.opendaylight.controller.sal.action.HwPath
14 import org.opendaylight.controller.sal.action.Loopback
15 import org.opendaylight.controller.sal.action.Output
16 import org.opendaylight.controller.sal.action.PopVlan
17 import org.opendaylight.controller.sal.action.PushVlan
18 import org.opendaylight.controller.sal.action.SetDlDst
19 import org.opendaylight.controller.sal.action.SetDlSrc
20 import org.opendaylight.controller.sal.action.SetDlType
21 import org.opendaylight.controller.sal.action.SetNextHop
22 import org.opendaylight.controller.sal.action.SetNwDst
23 import org.opendaylight.controller.sal.action.SetNwSrc
24 import org.opendaylight.controller.sal.action.SetNwTos
25 import org.opendaylight.controller.sal.action.SetTpDst
26 import org.opendaylight.controller.sal.action.SetTpSrc
27 import org.opendaylight.controller.sal.action.SetVlanCfi
28 import org.opendaylight.controller.sal.action.SetVlanId
29 import org.opendaylight.controller.sal.action.SetVlanPcp
30 import org.opendaylight.controller.sal.action.SwPath
31 import org.opendaylight.controller.sal.core.Node
32 import org.opendaylight.controller.sal.core.NodeConnector
33 import org.opendaylight.controller.sal.flowprogrammer.Flow
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAddedBuilder
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInputBuilder
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsInputBuilder
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.VlanCfi
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.Address
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4Builder
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv6Builder
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp
53
54 import static extension org.opendaylight.controller.sal.compatibility.FromSalConversionsUtils.*
55 import static extension org.opendaylight.controller.sal.compatibility.NodeMapping.*
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder
60 import java.util.Collections
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.controller.action._case.ControllerActionBuilder
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropActionBuilder
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.flood.action._case.FloodActionBuilder
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.flood.all.action._case.FloodAllActionBuilder
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.hw.path.action._case.HwPathActionBuilder
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.loopback.action._case.LoopbackActionBuilder
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.pop.vlan.action._case.PopVlanActionBuilder
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.vlan.action._case.PushVlanActionBuilder
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.dl.dst.action._case.SetDlDstActionBuilder
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.dl.src.action._case.SetDlSrcActionBuilder
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.dl.type.action._case.SetDlTypeActionBuilder
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.next.hop.action._case.SetNextHopActionBuilder
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.nw.dst.action._case.SetNwDstActionBuilder
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.nw.src.action._case.SetNwSrcActionBuilder
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.nw.tos.action._case.SetNwTosActionBuilder
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.tp.dst.action._case.SetTpDstActionBuilder
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.tp.src.action._case.SetTpSrcActionBuilder
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.vlan.cfi.action._case.SetVlanCfiActionBuilder
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.vlan.id.action._case.SetVlanIdActionBuilder
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.vlan.pcp.action._case.SetVlanPcpActionBuilder
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.sw.path.action._case.SwPathActionBuilder
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpSrcActionCaseBuilder
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpDstActionCaseBuilder
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwTosActionCaseBuilder
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionCaseBuilder
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwDstActionCaseBuilder
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNextHopActionCaseBuilder
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlTypeActionCaseBuilder
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlDstActionCaseBuilder
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.ControllerActionCaseBuilder
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCaseBuilder
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.FloodActionCaseBuilder
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.FloodAllActionCaseBuilder
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.HwPathActionCaseBuilder
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.LoopbackActionCaseBuilder
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCaseBuilder
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCaseBuilder
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlSrcActionCaseBuilder
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanCfiActionCaseBuilder
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdActionCaseBuilder
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanPcpActionCaseBuilder
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SwPathActionCaseBuilder
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder
109
110 public class MDFlowMapping {
111
112     private new() {
113         throw new UnsupportedOperationException()
114     }
115
116     public static def flowAdded(Flow sourceFlow) {
117         if (sourceFlow == null)
118             throw new IllegalArgumentException();
119         val it = new FlowAddedBuilder();
120
121         hardTimeout = sourceFlow.hardTimeout as int
122         idleTimeout = sourceFlow.idleTimeout as int
123         cookie = BigInteger.valueOf(sourceFlow.id)
124         priority = sourceFlow.priority as int
125
126         val sourceActions = sourceFlow.actions;
127         val targetActions = new ArrayList<Action>();
128         for (sourceAction : sourceActions) {
129             targetActions.add(sourceAction.toAction());
130         }
131         instructions = targetActions.toApplyInstruction();
132         match = sourceFlow.match.toMatch();
133         return it.build();
134
135     }
136     
137     public static def Instructions toApplyInstruction(ArrayList<Action> actions) {
138         val it = new InstructionsBuilder;
139         val applyActions = new InstructionBuilder;
140         applyActions.instruction = new ApplyActionsCaseBuilder().setApplyActions(new ApplyActionsBuilder().setAction(actions).build()).build()
141         instruction = Collections.<Instruction>singletonList(applyActions.build)
142         return it.build;
143     }
144
145     public static def flowStatisticsInput(Node sourceNode, Flow sourceFlow) {
146         val source = flowAdded(sourceFlow);
147         val it = new GetFlowStatisticsInputBuilder(source as org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow);
148         node = sourceNode.toNodeRef();
149         return it.build();
150     }
151
152     public static def removeFlowInput(Node sourceNode, Flow sourceFlow) {
153         val source = flowAdded(sourceFlow);
154         val it = new RemoveFlowInputBuilder(source as org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow);
155         return it.build();
156     }
157
158     public static def addFlowInput(Node sourceNode, Flow sourceFlow) {
159         val source = flowAdded(sourceFlow);
160         val it = new AddFlowInputBuilder(source as org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow);
161         return it.build();
162     }
163
164     public static def updateFlowInput(Node sourceNode, Flow oldFlow, Flow newFlow) {
165         val it = new UpdateFlowInputBuilder();
166         val sourceOld = flowAdded(newFlow);
167
168         val original = new OriginalFlowBuilder(sourceOld as org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow);
169         val sourceNew = flowAdded(newFlow);
170         val updated = new UpdatedFlowBuilder(sourceNew as org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow);
171         originalFlow = original.build()
172         updatedFlow = updated.build();
173         node = sourceNode.toNodeRef()
174         return it.build();
175     }
176
177     public static dispatch def toAction(Controller sourceAction) {
178         val actionBuilder = new ActionBuilder();
179         actionBuilder.action = new ControllerActionCaseBuilder().setControllerAction(new ControllerActionBuilder().build()).build();
180         return actionBuilder.build();
181     }
182
183     public static dispatch def toAction(Drop sourceAction) {
184         val actionBuilder = new ActionBuilder();
185         actionBuilder.action = new DropActionCaseBuilder().setDropAction(new DropActionBuilder().build()).build();
186         return actionBuilder.build();
187     }
188
189     public static dispatch def toAction(Flood sourceAction) {
190         val actionBuilder = new ActionBuilder();
191         actionBuilder.action = new FloodActionCaseBuilder().setFloodAction(new FloodActionBuilder().build).build();
192         return actionBuilder.build();
193     }
194
195     public static dispatch def toAction(FloodAll sourceAction) {
196         val actionBuilder = new ActionBuilder();
197         actionBuilder.action = new FloodAllActionCaseBuilder().setFloodAllAction(new FloodAllActionBuilder().build()).build();
198         return actionBuilder.build();
199     }
200
201     public static dispatch def toAction(HwPath sourceAction) {
202         val actionBuilder = new ActionBuilder();
203         actionBuilder.action = new HwPathActionCaseBuilder().setHwPathAction(new HwPathActionBuilder().build()).build();
204         return actionBuilder.build();
205     }
206
207     public static dispatch def toAction(Loopback sourceAction) {
208         val actionBuilder = new ActionBuilder();
209         actionBuilder.action = new LoopbackActionCaseBuilder().setLoopbackAction(new LoopbackActionBuilder().build()).build();
210         return actionBuilder.build();
211     }
212
213     public static dispatch def toAction(Output sourceAction) {
214         val actionBuilder = new ActionBuilder();
215         val it = new OutputActionBuilder();
216         outputNodeConnector = sourceAction.port.toUri;
217         actionBuilder.action = new OutputActionCaseBuilder().setOutputAction(it.build()).build();
218         return actionBuilder.build();
219
220     }
221
222     public static dispatch def toAction(PopVlan sourceAction) {
223         val actionBuilder = new ActionBuilder();
224         actionBuilder.action = new PopVlanActionCaseBuilder().build();
225         return actionBuilder.build();
226     }
227
228     public static dispatch def toAction(PushVlan sourceAction) {
229         val actionBuilder = new ActionBuilder();
230         val it = new PushVlanActionBuilder();
231         cfi = new VlanCfi(sourceAction.cfi);
232         vlanId = new VlanId(sourceAction.vlanId);
233         pcp = sourceAction.pcp;
234         tag = sourceAction.tag;
235         actionBuilder.action = new PushVlanActionCaseBuilder().setPushVlanAction(it.build()).build();
236         return actionBuilder.build();
237     }
238
239     public static dispatch def toAction(SetDlDst sourceAction) {
240         val actionBuilder = new ActionBuilder();
241         val it = new SetDlDstActionBuilder();
242         address = sourceAction.dlAddress.toMacAddress();
243         actionBuilder.action = new SetDlDstActionCaseBuilder().setSetDlDstAction(it.build()).build;
244         return actionBuilder.build();
245     }
246
247     public static dispatch def toAction(SetDlSrc sourceAction) {
248         val actionBuilder = new ActionBuilder();
249         val it = new SetDlSrcActionBuilder();
250         address = sourceAction.dlAddress.toMacAddress;
251         actionBuilder.action = new SetDlSrcActionCaseBuilder().setSetDlSrcAction(it.build()).build;
252         return actionBuilder.build();
253     }
254
255     public static dispatch def toAction(SetDlType sourceAction) {
256         val actionBuilder = new ActionBuilder();
257         val it = new SetDlTypeActionBuilder();
258         dlType = new EtherType(sourceAction.dlType as long);
259         actionBuilder.action = new SetDlTypeActionCaseBuilder().setSetDlTypeAction(it.build()).build();
260         return actionBuilder.build();
261     }
262
263     public static dispatch def toAction(SetNextHop sourceAction) {
264         val actionBuilder = new ActionBuilder();
265         val it = new SetNextHopActionBuilder();
266         val inetAddress = sourceAction.address;
267         address = inetAddress.toInetAddress;
268         actionBuilder.action = new SetNextHopActionCaseBuilder().setSetNextHopAction(it.build).build();
269         return actionBuilder.build();
270     }
271
272     public static dispatch def toAction(SetNwDst sourceAction) {
273         val actionBuilder = new ActionBuilder();
274         val it = new SetNwDstActionBuilder();
275         val inetAddress = sourceAction.address;
276         address = inetAddress.toInetAddress;
277         actionBuilder.action = new SetNwDstActionCaseBuilder().setSetNwDstAction(it.build()).build();
278         return actionBuilder.build();
279     }
280
281     public static dispatch def toAction(SetNwSrc sourceAction) {
282         val actionBuilder = new ActionBuilder();
283         val it = new SetNwSrcActionBuilder();
284         val inetAddress = sourceAction.address;
285         address = inetAddress.toInetAddress;
286         actionBuilder.action = new SetNwSrcActionCaseBuilder().setSetNwSrcAction(it.build()).build();
287         return actionBuilder.build();
288     }
289
290     public static dispatch def toAction(SetNwTos sourceAction) {
291         val actionBuilder = new ActionBuilder();
292         val it = new SetNwTosActionBuilder();
293         tos = sourceAction.nwTos;
294         actionBuilder.action = new SetNwTosActionCaseBuilder().setSetNwTosAction(it.build).build;
295         return actionBuilder.build();
296     }
297
298     public static dispatch def toAction(SetTpDst sourceAction) {
299         val actionBuilder = new ActionBuilder();
300         val it = new SetTpDstActionBuilder();
301         port = new PortNumber(sourceAction.port);
302         actionBuilder.action = new SetTpDstActionCaseBuilder().setSetTpDstAction(it.build()).build();
303         return actionBuilder.build();
304     }
305
306     public static dispatch def toAction(SetTpSrc sourceAction) {
307         val actionBuilder = new ActionBuilder();
308         val it = new SetTpSrcActionBuilder();
309         port = new PortNumber(sourceAction.port);
310         actionBuilder.action = new SetTpSrcActionCaseBuilder().setSetTpSrcAction(it.build()).build();
311         return actionBuilder.build();
312     }
313
314     public static dispatch def toAction(SetVlanCfi sourceAction) {
315         val actionBuilder = new ActionBuilder();
316         val it = new SetVlanCfiActionBuilder();
317         vlanCfi = new VlanCfi(sourceAction.cfi);
318         actionBuilder.action = new SetVlanCfiActionCaseBuilder().setSetVlanCfiAction(it.build()).build();
319         return actionBuilder.build();
320     }
321
322     public static dispatch def toAction(SetVlanId sourceAction) {
323         val actionBuilder = new ActionBuilder();
324
325         val it = new SetVlanIdActionBuilder();
326         vlanId = new VlanId(sourceAction.vlanId);
327         actionBuilder.action = new SetVlanIdActionCaseBuilder().setSetVlanIdAction(it.build()).build();
328         return actionBuilder.build();
329     }
330
331     public static dispatch def toAction(SetVlanPcp sourceAction) {
332         val actionBuilder = new ActionBuilder();
333         val it = new SetVlanPcpActionBuilder();
334         vlanPcp = new VlanPcp(sourceAction.pcp as short);
335         actionBuilder.action = new SetVlanPcpActionCaseBuilder().setSetVlanPcpAction(it.build).build;
336         return actionBuilder.build();
337     }
338
339     public static dispatch def toAction(SwPath sourceAction) {
340         val actionBuilder = new ActionBuilder();
341         actionBuilder.action = new SwPathActionCaseBuilder().setSwPathAction(new SwPathActionBuilder().build()).build();
342         return actionBuilder.build();
343     }
344
345     public static def dispatch Address toInetAddress(Inet4Address address) {
346         val it = new Ipv4Builder
347         ipv4Address = new Ipv4Prefix(InetAddresses.toAddrString(address))
348         return it.build()
349     }
350
351     public static def dispatch Address toInetAddress(Inet6Address address) {
352         val it = new Ipv6Builder
353         ipv6Address = new Ipv6Prefix(InetAddresses.toAddrString(address))
354         return it.build()
355     }
356
357     public static def Uri toUri(NodeConnector connector) {
358         throw new UnsupportedOperationException("TODO: auto-generated method stub")
359     }
360
361     public static def MacAddress toMacAddress(byte[] bytes) {
362         val sb = new StringBuilder(18);
363         for (byte b : bytes) {
364             if (sb.length() > 0)
365                 sb.append(':');
366             sb.append(String.format("%02x", b));
367         }
368         return new MacAddress(sb.toString());
369     }
370 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.