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