ofp_packet_queue structure - implemented missing fields
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / ActionsDeserializer.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.opendaylight.openflowjava.protocol.impl.util;\r
3 \r
4 import io.netty.buffer.ByteBuf;\r
5 \r
6 import java.util.ArrayList;\r
7 import java.util.List;\r
8 \r
9 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthertypeAction;\r
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthertypeActionBuilder;\r
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterAction;\r
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterActionBuilder;\r
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.GroupIdAction;\r
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.GroupIdActionBuilder;\r
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthActionBuilder;\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MplsTtlAction;\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MplsTtlActionBuilder;\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NwTtlAction;\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NwTtlActionBuilder;\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OxmFieldsAction;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.OxmFieldsActionBuilder;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortActionBuilder;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.QueueIdAction;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.QueueIdActionBuilder;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.CopyTtlIn;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.CopyTtlOut;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.DecMplsTtl;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.DecNwTtl;\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Experimenter;\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Group;\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output;\r
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopMpls;\r
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopPbb;\r
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopVlan;\r
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushMpls;\r
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushPbb;\r
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushVlan;\r
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetField;\r
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetMplsTtl;\r
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetNwTtl;\r
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetQueue;\r
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsList;\r
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsListBuilder;\r
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.actions.list.ActionBuilder;\r
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;\r
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;\r
49 \r
50 /**\r
51  * Class for easy deserialization of actions\r
52  * \r
53  * @author timotej.kubas\r
54  * @author michal.polkorab\r
55  */\r
56 public abstract class ActionsDeserializer {\r
57     final static byte PAD_ACTION_LENGTH = 2;\r
58     private static ActionBuilder actionBuilder = new ActionBuilder();\r
59     private static ActionsListBuilder actionsListBuilder = new ActionsListBuilder();\r
60     private static List<ActionsList> actionsList = new ArrayList<>();\r
61     \r
62     /**\r
63      * @param input input ByteBuf\r
64      * @param bucketsLength length of buckets\r
65      * @return ActionsList\r
66      */\r
67     public static List<ActionsList> createActionsList(ByteBuf input, int bucketsLength) {\r
68         final byte BUCKET_HEADER_LENGTH = 16;\r
69         int bucketsCurrentLength = BUCKET_HEADER_LENGTH;\r
70         int actionsLength = 0;\r
71             \r
72             while (bucketsCurrentLength < bucketsLength) {\r
73                 switch(input.readUnsignedShort()) {\r
74                 case 0: actionsLength = input.readUnsignedShort(); //outputActionLength\r
75                         actionsList.add(ActionsDeserializer.createOutputAction(input));\r
76                         break;\r
77                 case 11: \r
78                          actionsLength = input.readUnsignedShort();//empty header length\r
79                          actionsList.add(ActionsDeserializer.createCopyTtlOutAction(input));\r
80                          break;\r
81                          \r
82                 case 12: \r
83                          actionsLength = input.readUnsignedShort();//empty header length\r
84                          actionsList.add(ActionsDeserializer.createCopyTtlInAction(input));\r
85                          break;\r
86                 \r
87                 case 15: \r
88                          actionsLength = input.readUnsignedShort();//empty header length\r
89                          actionsList.add(ActionsDeserializer.createSetMplsTtlAction(input));\r
90                          break;\r
91                          \r
92                 case 16:                              \r
93                          actionsLength = input.readUnsignedShort();//empty header length\r
94                          actionsList.add(ActionsDeserializer.createDecMplsTtlOutAction(input));\r
95                          break;\r
96                          \r
97                 case 17: \r
98                          actionsLength = input.readUnsignedShort();\r
99                          actionsList.add(ActionsDeserializer.createPushVlanAction(input));\r
100                          break;\r
101                          \r
102                 case 18:                              \r
103                          actionsLength = input.readUnsignedShort();//empty header length\r
104                          actionsList.add(ActionsDeserializer.createPopVlanAction(input));\r
105                          break;\r
106                          \r
107                 case 19: \r
108                          actionsLength = input.readUnsignedShort();//8\r
109                          actionsList.add(ActionsDeserializer.createPushMplsAction(input));\r
110                          break;\r
111                          \r
112                 case 20: \r
113                          actionsLength = input.readUnsignedShort();//8\r
114                          actionsList.add(ActionsDeserializer.createPopMplsAction(input));\r
115                          break;\r
116                          \r
117                 case 21: \r
118                          actionsLength = input.readUnsignedShort();\r
119                          actionsList.add(ActionsDeserializer.createSetQueueAction(input));\r
120                          break;\r
121                          \r
122                 case 22: \r
123                         actionsLength = input.readUnsignedShort();//8\r
124                         actionsList.add(ActionsDeserializer.createGroupAction(input));\r
125                         break;\r
126                         \r
127                 case 23: \r
128                          actionsLength = input.readUnsignedShort();//8\r
129                          actionsList.add(ActionsDeserializer.createSetNwTtlAction(input));\r
130                          break;\r
131                         \r
132                 case 24:                              \r
133                         actionsLength = input.readUnsignedShort();//empty header length\r
134                         actionsList.add(ActionsDeserializer.createDecNwTtlAction(input));\r
135                         break;\r
136                         \r
137                 case 25:\r
138                         actionsLength = input.readUnsignedShort();//8\r
139                         actionsList.add(ActionsDeserializer.createSetFieldAction(input, actionsLength));\r
140                         break; \r
141                 case 26: \r
142                          actionsLength = input.readUnsignedShort();//8\r
143                          actionsList.add(ActionsDeserializer.createPushPbbAction(input));\r
144                          break;\r
145                          \r
146                 case 27:                              \r
147                         actionsLength = input.readUnsignedShort();//empty header length\r
148                         actionsList.add(ActionsDeserializer.createPopPbbAction(input));\r
149                         break;\r
150                         \r
151                 case 0xFFFF: \r
152                         actionsLength = input.readUnsignedShort();\r
153                         actionsList.add(ActionsDeserializer.createExperimenterAction(input));\r
154                         break;\r
155                 default: \r
156                          break;\r
157                 }\r
158                 bucketsCurrentLength += actionsLength;\r
159             } \r
160         return actionsList;\r
161     }\r
162     \r
163     \r
164     /**\r
165      * @param action input action that contains empty header\r
166      * @param in input ByteBuf \r
167      * @return Action\r
168      */\r
169     private static ActionsList createEmptyHeader(Class<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Action> action, ByteBuf in) {\r
170         final byte PADDING_IN_ACTIONS_HEADER = 4;\r
171         \r
172         actionBuilder.setType(action);\r
173         in.skipBytes(PADDING_IN_ACTIONS_HEADER);\r
174         actionsListBuilder.setAction(actionBuilder.build());\r
175         return actionsListBuilder.build();\r
176     }\r
177     \r
178     /**\r
179      * @param in input ByteBuf\r
180      * @return Action\r
181      */\r
182     public static ActionsList createCopyTtlInAction(ByteBuf in) {\r
183         return createEmptyHeader(CopyTtlIn.class, in);\r
184     }\r
185     \r
186     /**\r
187      * @param in input ByteBuf\r
188      * @return Action\r
189      */\r
190     public static ActionsList createCopyTtlOutAction(ByteBuf in) {\r
191         return createEmptyHeader(CopyTtlOut.class, in);\r
192     }\r
193     \r
194     /**\r
195      * @param in input ByteBuf\r
196      * @return Action\r
197      */\r
198     public static ActionsList createDecMplsTtlOutAction(ByteBuf in) {\r
199         return createEmptyHeader(DecMplsTtl.class, in);\r
200     }\r
201     \r
202     /**\r
203      * @param in input ByteBuf\r
204      * @return Action\r
205      */\r
206     public static ActionsList createPopVlanAction(ByteBuf in) {\r
207         return createEmptyHeader(PopVlan.class, in);\r
208     }\r
209     \r
210     /**\r
211      * @param in input ByteBuf\r
212      * @return Action\r
213      */\r
214     public static ActionsList createDecNwTtlAction(ByteBuf in) {\r
215         return createEmptyHeader(DecNwTtl.class, in);\r
216     }\r
217     \r
218     /**\r
219      * @param in input ByteBuf\r
220      * @return Action\r
221      */\r
222     public static ActionsList createPopPbbAction(ByteBuf in) {\r
223         return createEmptyHeader(PopPbb.class, in);\r
224     }\r
225     \r
226     /**\r
227      * @param in input ByteBuf\r
228      * @return ActionList\r
229      */\r
230     public static ActionsList createOutputAction(ByteBuf in) {\r
231         final byte PADDING_IN_OUTPUT_ACTIONS_HEADER = 6;\r
232         \r
233         actionBuilder.setType(Output.class);\r
234         PortActionBuilder port = new PortActionBuilder();\r
235         port.setPort(new PortNumber(in.readUnsignedInt()));\r
236         actionBuilder.addAugmentation(PortAction.class, port.build());\r
237         MaxLengthActionBuilder maxLen = new MaxLengthActionBuilder();\r
238         maxLen.setMaxLength(in.readUnsignedShort());\r
239         actionBuilder.addAugmentation(MaxLengthAction.class, maxLen.build());\r
240         actionsListBuilder.setAction(actionBuilder.build());\r
241         in.skipBytes(PADDING_IN_OUTPUT_ACTIONS_HEADER);\r
242         \r
243         return actionsListBuilder.build();\r
244     }\r
245     \r
246     /**\r
247      * @param in input ByteBuf\r
248      * @return ActionList\r
249      */\r
250     public static ActionsList createSetMplsTtlAction(ByteBuf in) {\r
251         final byte PADDING_IN_SET_MPLS_TTL_ACTIONS_HEADER = 3;\r
252         \r
253         actionBuilder.setType(SetMplsTtl.class);\r
254         MplsTtlActionBuilder mplsTtl = new MplsTtlActionBuilder();\r
255         mplsTtl.setMplsTtl(in.readUnsignedByte());\r
256         actionBuilder.addAugmentation(MplsTtlAction.class, mplsTtl.build());\r
257         actionsListBuilder.setAction(actionBuilder.build());\r
258         in.skipBytes(PADDING_IN_SET_MPLS_TTL_ACTIONS_HEADER);\r
259         \r
260         return actionsListBuilder.build();\r
261     }\r
262     \r
263     /**\r
264      * @param action input action that contains push\r
265      * @param in input ByteBuf\r
266      * @return ActionList\r
267      */\r
268     private static ActionsList createPushAction(Class<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Action> action, ByteBuf in) {\r
269         final byte PADDING_IN_PUSH_VLAN_ACTIONS_HEADER = 2;\r
270         \r
271         actionBuilder.setType(action);\r
272         EthertypeActionBuilder etherType = new EthertypeActionBuilder();\r
273         etherType.setEthertype(new EtherType(in.readUnsignedShort()));\r
274         actionBuilder.addAugmentation(EthertypeAction.class, etherType.build());\r
275         actionsListBuilder.setAction(actionBuilder.build());\r
276         in.skipBytes(PADDING_IN_PUSH_VLAN_ACTIONS_HEADER);\r
277         \r
278         return actionsListBuilder.build();\r
279     }\r
280     \r
281     /**\r
282      * @param in input ByteBuf\r
283      * @return Action\r
284      */\r
285     public static ActionsList createPushVlanAction(ByteBuf in) {\r
286         return createPushAction(PushVlan.class, in);\r
287     }\r
288     \r
289     /**\r
290      * @param in input ByteBuf\r
291      * @return Action\r
292      */\r
293     public static ActionsList createPushMplsAction(ByteBuf in) {\r
294         return createPushAction(PushMpls.class, in);\r
295     }\r
296     \r
297     /**\r
298      * @param in input ByteBuf\r
299      * @return Action\r
300      */\r
301     public static ActionsList createPopMplsAction(ByteBuf in) {\r
302         return createPushAction(PopMpls.class, in);\r
303     }\r
304     \r
305     /**\r
306      * @param in input ByteBuf\r
307      * @return Action\r
308      */\r
309     public static ActionsList createPushPbbAction(ByteBuf in) {\r
310         return createPushAction(PushPbb.class, in);\r
311     }\r
312     \r
313     /**\r
314      * @param in input ByteBuf\r
315      * @return ActionList\r
316      */\r
317     public static ActionsList createSetQueueAction(ByteBuf in) {\r
318         actionBuilder.setType(SetQueue.class);\r
319         QueueIdActionBuilder queueId = new QueueIdActionBuilder();\r
320         queueId.setQueueId(in.readUnsignedInt());\r
321         actionBuilder.addAugmentation(QueueIdAction.class, queueId.build());\r
322         actionsListBuilder.setAction(actionBuilder.build());\r
323         \r
324         return actionsListBuilder.build();\r
325     }\r
326     \r
327     /**\r
328      * @param in input ByteBuf\r
329      * @return ActionList\r
330      */\r
331     public static ActionsList createGroupAction(ByteBuf in) {\r
332         actionBuilder.setType(Group.class);\r
333         GroupIdActionBuilder group = new GroupIdActionBuilder();\r
334         group.setGroupId(in.readUnsignedInt());\r
335         actionBuilder.addAugmentation(GroupIdAction.class, group.build());\r
336         actionsListBuilder.setAction(actionBuilder.build());\r
337         \r
338         return actionsListBuilder.build();\r
339     }\r
340     \r
341     /**\r
342      * @param in input ByteBuf\r
343      * @return ActionList\r
344      */\r
345     public static ActionsList createExperimenterAction(ByteBuf in) {\r
346         actionBuilder.setType(Experimenter.class);\r
347         ExperimenterActionBuilder experimenter = new ExperimenterActionBuilder();\r
348         experimenter.setExperimenter(in.readUnsignedInt());\r
349         actionBuilder.addAugmentation(ExperimenterAction.class, experimenter.build());\r
350         actionsListBuilder.setAction(actionBuilder.build());\r
351         \r
352         return actionsListBuilder.build();\r
353     }\r
354     \r
355     /**\r
356      * @param in input ByteBuf\r
357      * @return ActionList\r
358      */\r
359     public static ActionsList createSetNwTtlAction(ByteBuf in) {\r
360         final byte PADDING_IN_NW_TTL_ACTIONS_HEADER = 3;\r
361         \r
362         actionBuilder.setType(SetNwTtl.class);\r
363         NwTtlActionBuilder nwTtl = new NwTtlActionBuilder();\r
364         nwTtl.setNwTtl(in.readUnsignedByte());\r
365         actionBuilder.addAugmentation(NwTtlAction.class, nwTtl.build());\r
366         actionsListBuilder.setAction(actionBuilder.build());\r
367         in.skipBytes(PADDING_IN_NW_TTL_ACTIONS_HEADER);\r
368         \r
369         return actionsListBuilder.build();\r
370     }\r
371     \r
372     /**\r
373      * @param in input ByteBuf\r
374      * @param actionLength length of action\r
375      * @return ActionList\r
376      */\r
377     public static ActionsList createSetFieldAction(ByteBuf in, int actionLength) {\r
378         actionBuilder.setType(SetField.class);\r
379         OxmFieldsActionBuilder matchEntries = new OxmFieldsActionBuilder();\r
380         matchEntries.setMatchEntries(MatchDeserializer.createMatchEntries(in, actionLength - 4));\r
381         actionBuilder.addAugmentation(OxmFieldsAction.class, matchEntries.build());\r
382         actionsListBuilder.setAction(actionBuilder.build());\r
383         \r
384         return actionsListBuilder.build();\r
385     }\r
386 }\r