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