Merge "Updated dependency versions of ODL components"
[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                 ExperimenterActionBuilder experimenter = new ExperimenterActionBuilder();
212                 experimenter.setExperimenter(input.readUnsignedInt());
213                 builder.addAugmentation(ExperimenterAction.class, experimenter.build());
214                 break;
215             default: 
216                 break;
217             }
218             listBuilder.setAction(builder.build());
219             actionsList.add(listBuilder.build());
220             length += currentActionLength;
221         } 
222         return actionsList;
223     }
224
225     private static ActionsList createEmptyHeader(Class<? extends org.opendaylight.yang.gen.v1.
226             urn.opendaylight.openflow.common.types.rev130731.Action> action,
227             ByteBuf in, ActionsListBuilder actionsListBuilder) {
228         ActionBuilder actionBuilder = new ActionBuilder();
229         actionBuilder.setType(action);
230         in.skipBytes(PADDING_IN_ACTIONS_HEADER);
231         actionsListBuilder.setAction(actionBuilder.build());
232         return actionsListBuilder.build();
233     }
234     
235     private static ActionsList createCopyTtlInAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
236         return createEmptyHeader(CopyTtlIn.class, in, actionsListBuilder);
237     }
238
239     private static ActionsList createCopyTtlOutAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
240         return createEmptyHeader(CopyTtlOut.class, in, actionsListBuilder);
241     }
242     
243     private static ActionsList createDecMplsTtlOutAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
244         return createEmptyHeader(DecMplsTtl.class, in, actionsListBuilder);
245     }
246     
247     private static ActionsList createPopVlanAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
248         return createEmptyHeader(PopVlan.class, in, actionsListBuilder);
249     }
250     
251     private static ActionsList createDecNwTtlAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
252         return createEmptyHeader(DecNwTtl.class, in, actionsListBuilder);
253     }
254     
255     private static ActionsList createPopPbbAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
256         return createEmptyHeader(PopPbb.class, in, actionsListBuilder);
257     }
258     
259     private static ActionsList createOutputAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
260         ActionBuilder actionBuilder = new ActionBuilder();
261         actionBuilder.setType(Output.class);
262         PortActionBuilder port = new PortActionBuilder();
263         port.setPort(new PortNumber(in.readUnsignedInt()));
264         actionBuilder.addAugmentation(PortAction.class, port.build());
265         MaxLengthActionBuilder maxLen = new MaxLengthActionBuilder();
266         maxLen.setMaxLength(in.readUnsignedShort());
267         actionBuilder.addAugmentation(MaxLengthAction.class, maxLen.build());
268         actionsListBuilder.setAction(actionBuilder.build());
269         in.skipBytes(PADDING_IN_OUTPUT_ACTIONS_HEADER);
270         return actionsListBuilder.build();
271     }
272     
273     private static ActionsList createSetMplsTtlAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
274         ActionBuilder actionBuilder = new ActionBuilder();
275         actionBuilder.setType(SetMplsTtl.class);
276         MplsTtlActionBuilder mplsTtl = new MplsTtlActionBuilder();
277         mplsTtl.setMplsTtl(in.readUnsignedByte());
278         actionBuilder.addAugmentation(MplsTtlAction.class, mplsTtl.build());
279         actionsListBuilder.setAction(actionBuilder.build());
280         in.skipBytes(PADDING_IN_SET_MPLS_TTL_ACTIONS_HEADER);
281         return actionsListBuilder.build();
282     }
283     
284     private static ActionsList createPushAction(Class<? extends org.opendaylight.yang.gen.
285             v1.urn.opendaylight.openflow.common.types.rev130731.Action> action,
286             ByteBuf in, ActionsListBuilder actionsListBuilder) {
287         ActionBuilder actionBuilder = new ActionBuilder();
288         actionBuilder.setType(action);
289         EthertypeActionBuilder etherType = new EthertypeActionBuilder();
290         etherType.setEthertype(new EtherType(in.readUnsignedShort()));
291         actionBuilder.addAugmentation(EthertypeAction.class, etherType.build());
292         actionsListBuilder.setAction(actionBuilder.build());
293         in.skipBytes(PADDING_IN_PUSH_VLAN_ACTIONS_HEADER);
294         return actionsListBuilder.build();
295     }
296     
297     private static ActionsList createPushVlanAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
298         return createPushAction(PushVlan.class, in, actionsListBuilder);
299     }
300     
301     private static ActionsList createPushMplsAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
302         return createPushAction(PushMpls.class, in, actionsListBuilder);
303     }
304     
305     private static ActionsList createPopMplsAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
306         return createPushAction(PopMpls.class, in, actionsListBuilder);
307     }
308     
309     private static ActionsList createPushPbbAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
310         return createPushAction(PushPbb.class, in, actionsListBuilder);
311     }
312     
313     private static ActionsList createSetQueueAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
314         ActionBuilder actionBuilder = new ActionBuilder();
315         actionBuilder.setType(SetQueue.class);
316         QueueIdActionBuilder queueId = new QueueIdActionBuilder();
317         queueId.setQueueId(in.readUnsignedInt());
318         actionBuilder.addAugmentation(QueueIdAction.class, queueId.build());
319         actionsListBuilder.setAction(actionBuilder.build());
320         return actionsListBuilder.build();
321     }
322     
323     private static ActionsList createGroupAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
324         ActionBuilder actionBuilder = new ActionBuilder();
325         actionBuilder.setType(Group.class);
326         GroupIdActionBuilder group = new GroupIdActionBuilder();
327         group.setGroupId(in.readUnsignedInt());
328         actionBuilder.addAugmentation(GroupIdAction.class, group.build());
329         actionsListBuilder.setAction(actionBuilder.build());
330         return actionsListBuilder.build();
331     }
332     
333     private static ActionsList createExperimenterAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
334         ActionBuilder actionBuilder = new ActionBuilder();
335         actionBuilder.setType(Experimenter.class);
336         ExperimenterActionBuilder experimenter = new ExperimenterActionBuilder();
337         experimenter.setExperimenter(in.readUnsignedInt());
338         actionBuilder.addAugmentation(ExperimenterAction.class, experimenter.build());
339         actionsListBuilder.setAction(actionBuilder.build());
340         return actionsListBuilder.build();
341     }
342     
343     private static ActionsList createSetNwTtlAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
344         ActionBuilder actionBuilder = new ActionBuilder();
345         actionBuilder.setType(SetNwTtl.class);
346         NwTtlActionBuilder nwTtl = new NwTtlActionBuilder();
347         nwTtl.setNwTtl(in.readUnsignedByte());
348         actionBuilder.addAugmentation(NwTtlAction.class, nwTtl.build());
349         actionsListBuilder.setAction(actionBuilder.build());
350         in.skipBytes(PADDING_IN_NW_TTL_ACTIONS_HEADER);
351         return actionsListBuilder.build();
352     }
353     
354     private static ActionsList createSetFieldAction(ByteBuf in, ActionsListBuilder actionsListBuilder, int actionLength) {
355         ActionBuilder actionBuilder = new ActionBuilder();
356         actionBuilder.setType(SetField.class);
357         OxmFieldsActionBuilder matchEntries = new OxmFieldsActionBuilder();
358         matchEntries.setMatchEntries(MatchDeserializer.createMatchEntry(in, actionLength  - ACTION_HEADER_LENGTH));
359         actionBuilder.addAugmentation(OxmFieldsAction.class, matchEntries.build());
360         actionsListBuilder.setAction(actionBuilder.build());
361         return actionsListBuilder.build();
362     }
363 }