2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.util;
11 import io.netty.buffer.ByteBuf;
13 import java.util.ArrayList;
14 import java.util.List;
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;
58 * Deserializes ofp_actions (OpenFlow v1.3)
59 * @author timotej.kubas
60 * @author michal.polkorab
62 public abstract class ActionsDeserializer {
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;
72 * Creates list of actions (OpenFlow v1.3)
73 * @param input input ByteBuf
74 * @param actionsLength length of buckets
77 public static List<ActionsList> createActionsList(ByteBuf input, int actionsLength) {
78 ActionsListBuilder actionsListBuilder = new ActionsListBuilder();
79 List<ActionsList> actionsList = new ArrayList<>();
81 while (length < actionsLength) {
82 int type = input.readUnsignedShort();
83 int currentActionLength = input.readUnsignedShort();
86 actionsList.add(createOutputAction(input, actionsListBuilder));
89 actionsList.add(createCopyTtlOutAction(input, actionsListBuilder));
92 actionsList.add(createCopyTtlInAction(input, actionsListBuilder));
95 actionsList.add(createSetMplsTtlAction(input, actionsListBuilder));
98 actionsList.add(createDecMplsTtlOutAction(input, actionsListBuilder));
101 actionsList.add(createPushVlanAction(input, actionsListBuilder));
104 actionsList.add(createPopVlanAction(input, actionsListBuilder));
107 actionsList.add(createPushMplsAction(input, actionsListBuilder));
110 actionsList.add(createPopMplsAction(input, actionsListBuilder));
113 actionsList.add(createSetQueueAction(input, actionsListBuilder));
116 actionsList.add(createGroupAction(input, actionsListBuilder));
119 actionsList.add(createSetNwTtlAction(input, actionsListBuilder));
122 actionsList.add(createDecNwTtlAction(input, actionsListBuilder));
125 actionsList.add(createSetFieldAction(input, actionsListBuilder, currentActionLength));
128 actionsList.add(createPushPbbAction(input, actionsListBuilder));
131 actionsList.add(createPopPbbAction(input, actionsListBuilder));
134 actionsList.add(createExperimenterAction(input, actionsListBuilder));
139 length += currentActionLength;
145 * Creates action ids - actions without values (OpenFlow v1.3)
146 * @param input input ByteBuf
147 * @param actionsLength length of actions
148 * @return ActionsList
150 public static List<ActionsList> createActionIds(ByteBuf input, int actionsLength) {
151 List<ActionsList> actionsList = new ArrayList<>();
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();
162 builder.setType(Output.class);
165 builder.setType(CopyTtlOut.class);
168 builder.setType(CopyTtlIn.class);
171 builder.setType(SetMplsTtl.class);
174 builder.setType(DecMplsTtl.class);
177 builder.setType(PushVlan.class);
180 builder.setType(PopVlan.class);
183 builder.setType(PushMpls.class);
186 builder.setType(PopMpls.class);
189 builder.setType(SetQueue.class);
192 builder.setType(Group.class);
195 builder.setType(SetNwTtl.class);
198 builder.setType(DecNwTtl.class);
201 builder.setType(SetField.class);
204 builder.setType(PushPbb.class);
207 builder.setType(PopPbb.class);
210 builder.setType(Experimenter.class);
211 ExperimenterActionBuilder experimenter = new ExperimenterActionBuilder();
212 experimenter.setExperimenter(input.readUnsignedInt());
213 builder.addAugmentation(ExperimenterAction.class, experimenter.build());
218 listBuilder.setAction(builder.build());
219 actionsList.add(listBuilder.build());
220 length += currentActionLength;
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();
235 private static ActionsList createCopyTtlInAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
236 return createEmptyHeader(CopyTtlIn.class, in, actionsListBuilder);
239 private static ActionsList createCopyTtlOutAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
240 return createEmptyHeader(CopyTtlOut.class, in, actionsListBuilder);
243 private static ActionsList createDecMplsTtlOutAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
244 return createEmptyHeader(DecMplsTtl.class, in, actionsListBuilder);
247 private static ActionsList createPopVlanAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
248 return createEmptyHeader(PopVlan.class, in, actionsListBuilder);
251 private static ActionsList createDecNwTtlAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
252 return createEmptyHeader(DecNwTtl.class, in, actionsListBuilder);
255 private static ActionsList createPopPbbAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
256 return createEmptyHeader(PopPbb.class, in, actionsListBuilder);
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();
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();
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();
297 private static ActionsList createPushVlanAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
298 return createPushAction(PushVlan.class, in, actionsListBuilder);
301 private static ActionsList createPushMplsAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
302 return createPushAction(PushMpls.class, in, actionsListBuilder);
305 private static ActionsList createPopMplsAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
306 return createPushAction(PopMpls.class, in, actionsListBuilder);
309 private static ActionsList createPushPbbAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
310 return createPushAction(PushPbb.class, in, actionsListBuilder);
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();
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();
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();
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();
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();