Fixed deserialization of IdentityRefs in Restconf URI.
[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.action.action.ControllerActionBuilder
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionBuilder
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.FloodActionBuilder
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.FloodAllActionBuilder
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.HwPathActionBuilder
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.LoopbackActionBuilder
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionBuilder
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionBuilder
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionBuilder
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlDstActionBuilder
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlSrcActionBuilder
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlTypeActionBuilder
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNextHopActionBuilder
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwDstActionBuilder
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionBuilder
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwTosActionBuilder
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpDstActionBuilder
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpSrcActionBuilder
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanCfiActionBuilder
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdActionBuilder
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanPcpActionBuilder
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SwPathActionBuilder
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.Address
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4Builder
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv6Builder
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp
75
76 import static extension org.opendaylight.controller.sal.compatibility.FromSalConversionsUtils.*
77 import static extension org.opendaylight.controller.sal.compatibility.NodeMapping.*
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder
82 import java.util.Collections
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsBuilder
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder
86
87 public class MDFlowMapping {
88
89     private new() {
90         throw new UnsupportedOperationException()
91     }
92
93     public static def flowAdded(Flow sourceFlow) {
94         if (sourceFlow == null)
95             throw new IllegalArgumentException();
96         val it = new FlowAddedBuilder();
97
98         hardTimeout = sourceFlow.hardTimeout as int
99         idleTimeout = sourceFlow.idleTimeout as int
100         cookie = BigInteger.valueOf(sourceFlow.id)
101         priority = sourceFlow.priority as int
102
103         val sourceActions = sourceFlow.actions;
104         val targetActions = new ArrayList<Action>();
105         for (sourceAction : sourceActions) {
106             targetActions.add(sourceAction.toAction());
107         }
108         instructions = targetActions.toApplyInstruction();
109         match = sourceFlow.match.toMatch();
110         return it.build();
111
112     }
113     
114     public static def Instructions toApplyInstruction(ArrayList<Action> actions) {
115         val it = new InstructionsBuilder;
116         val applyActions = new InstructionBuilder;
117         applyActions.instruction = new ApplyActionsBuilder().setAction(actions).build()
118         instruction = Collections.<Instruction>singletonList(applyActions.build)
119         return it.build;
120     }
121
122     public static def flowStatisticsInput(Node sourceNode, Flow sourceFlow) {
123         val source = flowAdded(sourceFlow);
124         val it = new GetFlowStatisticsInputBuilder(source as org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow);
125         node = sourceNode.toNodeRef();
126         return it.build();
127     }
128
129     public static def removeFlowInput(Node sourceNode, Flow sourceFlow) {
130         val source = flowAdded(sourceFlow);
131         val it = new RemoveFlowInputBuilder(source as org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow);
132         return it.build();
133     }
134
135     public static def addFlowInput(Node sourceNode, Flow sourceFlow) {
136         val source = flowAdded(sourceFlow);
137         val it = new AddFlowInputBuilder(source as org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow);
138         return it.build();
139     }
140
141     public static def updateFlowInput(Node sourceNode, Flow oldFlow, Flow newFlow) {
142         val it = new UpdateFlowInputBuilder();
143         val sourceOld = flowAdded(newFlow);
144
145         val original = new OriginalFlowBuilder(sourceOld as org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow);
146         val sourceNew = flowAdded(newFlow);
147         val updated = new UpdatedFlowBuilder(sourceNew as org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow);
148         originalFlow = original.build()
149         updatedFlow = updated.build();
150         node = sourceNode.toNodeRef()
151         return it.build();
152     }
153
154     public static dispatch def toAction(Controller sourceAction) {
155         val actionBuilder = new ActionBuilder();
156         actionBuilder.action = new ControllerActionBuilder().build();
157         return actionBuilder.build();
158     }
159
160     public static dispatch def toAction(Drop sourceAction) {
161         val actionBuilder = new ActionBuilder();
162         actionBuilder.action = new DropActionBuilder().build();
163         return actionBuilder.build();
164     }
165
166     public static dispatch def toAction(Flood sourceAction) {
167         val actionBuilder = new ActionBuilder();
168         actionBuilder.action = new FloodActionBuilder().build();
169         return actionBuilder.build();
170     }
171
172     public static dispatch def toAction(FloodAll sourceAction) {
173         val actionBuilder = new ActionBuilder();
174         actionBuilder.action = new FloodAllActionBuilder().build();
175         return actionBuilder.build();
176     }
177
178     public static dispatch def toAction(HwPath sourceAction) {
179         val actionBuilder = new ActionBuilder();
180         actionBuilder.action = new HwPathActionBuilder().build();
181         return actionBuilder.build();
182     }
183
184     public static dispatch def toAction(Loopback sourceAction) {
185         val actionBuilder = new ActionBuilder();
186         actionBuilder.action = new LoopbackActionBuilder().build();
187         return actionBuilder.build();
188     }
189
190     public static dispatch def toAction(Output sourceAction) {
191         val actionBuilder = new ActionBuilder();
192         val it = new OutputActionBuilder();
193         outputNodeConnector = sourceAction.port.toUri;
194         actionBuilder.action = it.build();
195         return actionBuilder.build();
196
197     }
198
199     public static dispatch def toAction(PopVlan sourceAction) {
200         val actionBuilder = new ActionBuilder();
201         actionBuilder.action = new PopVlanActionBuilder().build();
202         return actionBuilder.build();
203     }
204
205     public static dispatch def toAction(PushVlan sourceAction) {
206         val actionBuilder = new ActionBuilder();
207         val it = new PushVlanActionBuilder();
208         cfi = new VlanCfi(sourceAction.cfi);
209         vlanId = new VlanId(sourceAction.vlanId);
210         pcp = sourceAction.pcp;
211         tag = sourceAction.tag;
212         actionBuilder.action = it.build();
213         return actionBuilder.build();
214     }
215
216     public static dispatch def toAction(SetDlDst sourceAction) {
217         val actionBuilder = new ActionBuilder();
218         val it = new SetDlDstActionBuilder();
219         address = sourceAction.dlAddress.toMacAddress();
220         actionBuilder.action = it.build();
221         return actionBuilder.build();
222     }
223
224     public static dispatch def toAction(SetDlSrc sourceAction) {
225         val actionBuilder = new ActionBuilder();
226         val it = new SetDlSrcActionBuilder();
227         address = sourceAction.dlAddress.toMacAddress;
228         actionBuilder.action = it.build();
229         return actionBuilder.build();
230     }
231
232     public static dispatch def toAction(SetDlType sourceAction) {
233         val actionBuilder = new ActionBuilder();
234         val it = new SetDlTypeActionBuilder();
235         dlType = new EtherType(sourceAction.dlType as long);
236         actionBuilder.action = it.build();
237         return actionBuilder.build();
238     }
239
240     public static dispatch def toAction(SetNextHop sourceAction) {
241         val actionBuilder = new ActionBuilder();
242         val it = new SetNextHopActionBuilder();
243         val inetAddress = sourceAction.address;
244         address = inetAddress.toInetAddress;
245         actionBuilder.action = it.build();
246         return actionBuilder.build();
247     }
248
249     public static dispatch def toAction(SetNwDst sourceAction) {
250         val actionBuilder = new ActionBuilder();
251         val it = new SetNwDstActionBuilder();
252         val inetAddress = sourceAction.address;
253         address = inetAddress.toInetAddress;
254         actionBuilder.action = it.build();
255         return actionBuilder.build();
256     }
257
258     public static dispatch def toAction(SetNwSrc sourceAction) {
259         val actionBuilder = new ActionBuilder();
260         val it = new SetNwSrcActionBuilder();
261         val inetAddress = sourceAction.address;
262         address = inetAddress.toInetAddress;
263         actionBuilder.action = it.build();
264         return actionBuilder.build();
265     }
266
267     public static dispatch def toAction(SetNwTos sourceAction) {
268         val actionBuilder = new ActionBuilder();
269         val it = new SetNwTosActionBuilder();
270         tos = sourceAction.nwTos;
271         actionBuilder.action = it.build();
272         return actionBuilder.build();
273     }
274
275     public static dispatch def toAction(SetTpDst sourceAction) {
276         val actionBuilder = new ActionBuilder();
277         val it = new SetTpDstActionBuilder();
278         port = new PortNumber(sourceAction.port);
279         actionBuilder.action = it.build();
280         return actionBuilder.build();
281     }
282
283     public static dispatch def toAction(SetTpSrc sourceAction) {
284         val actionBuilder = new ActionBuilder();
285         val it = new SetTpSrcActionBuilder();
286         port = new PortNumber(sourceAction.port);
287         actionBuilder.action = it.build();
288         return actionBuilder.build();
289     }
290
291     public static dispatch def toAction(SetVlanCfi sourceAction) {
292         val actionBuilder = new ActionBuilder();
293         val it = new SetVlanCfiActionBuilder();
294         vlanCfi = new VlanCfi(sourceAction.cfi);
295         actionBuilder.action = it.build();
296         return actionBuilder.build();
297     }
298
299     public static dispatch def toAction(SetVlanId sourceAction) {
300         val actionBuilder = new ActionBuilder();
301
302         val it = new SetVlanIdActionBuilder();
303         vlanId = new VlanId(sourceAction.vlanId);
304         actionBuilder.action = it.build();
305         return actionBuilder.build();
306     }
307
308     public static dispatch def toAction(SetVlanPcp sourceAction) {
309         val actionBuilder = new ActionBuilder();
310         val it = new SetVlanPcpActionBuilder();
311         vlanPcp = new VlanPcp(sourceAction.pcp as short);
312         actionBuilder.action = it.build();
313         return actionBuilder.build();
314     }
315
316     public static dispatch def toAction(SwPath sourceAction) {
317         val actionBuilder = new ActionBuilder();
318         actionBuilder.action = new SwPathActionBuilder().build();
319         return actionBuilder.build();
320     }
321
322     public static def dispatch Address toInetAddress(Inet4Address address) {
323         val it = new Ipv4Builder
324         ipv4Address = new Ipv4Prefix(InetAddresses.toAddrString(address))
325         return it.build()
326     }
327
328     public static def dispatch Address toInetAddress(Inet6Address address) {
329         val it = new Ipv6Builder
330         ipv6Address = new Ipv6Prefix(InetAddresses.toAddrString(address))
331         return it.build()
332     }
333
334     public static def Uri toUri(NodeConnector connector) {
335         throw new UnsupportedOperationException("TODO: auto-generated method stub")
336     }
337
338     public static def MacAddress toMacAddress(byte[] bytes) {
339         val sb = new StringBuilder(18);
340         for (byte b : bytes) {
341             if (sb.length() > 0)
342                 sb.append(':');
343             sb.append(String.format("%02x", b));
344         }
345         return new MacAddress(sb.toString());
346     }
347 }