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);
215 listBuilder.setAction(builder.build());
216 actionsList.add(listBuilder.build());
217 length += currentActionLength;
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();
232 private static ActionsList createCopyTtlInAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
233 return createEmptyHeader(CopyTtlIn.class, in, actionsListBuilder);
236 private static ActionsList createCopyTtlOutAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
237 return createEmptyHeader(CopyTtlOut.class, in, actionsListBuilder);
240 private static ActionsList createDecMplsTtlOutAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
241 return createEmptyHeader(DecMplsTtl.class, in, actionsListBuilder);
244 private static ActionsList createPopVlanAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
245 return createEmptyHeader(PopVlan.class, in, actionsListBuilder);
248 private static ActionsList createDecNwTtlAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
249 return createEmptyHeader(DecNwTtl.class, in, actionsListBuilder);
252 private static ActionsList createPopPbbAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
253 return createEmptyHeader(PopPbb.class, in, actionsListBuilder);
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();
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();
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();
294 private static ActionsList createPushVlanAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
295 return createPushAction(PushVlan.class, in, actionsListBuilder);
298 private static ActionsList createPushMplsAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
299 return createPushAction(PushMpls.class, in, actionsListBuilder);
302 private static ActionsList createPopMplsAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
303 return createPushAction(PopMpls.class, in, actionsListBuilder);
306 private static ActionsList createPushPbbAction(ByteBuf in, ActionsListBuilder actionsListBuilder) {
307 return createPushAction(PushPbb.class, in, actionsListBuilder);
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();
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();
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();
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();
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();