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.List;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterInstruction;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsList;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Experimenter;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.Instructions;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Instruction;
32 * Serializes ofp_instruction (OpenFlow v 1.3) structure
33 * @author michal.polkorab
34 * @author timotej.kubas
36 public abstract class InstructionsSerializer {
38 private static final byte GOTO_TABLE_TYPE = 1;
39 private static final byte WRITE_METADATA_TYPE = 2;
40 private static final byte WRITE_ACTIONS_TYPE = 3;
41 private static final byte APPLY_ACTIONS_TYPE = 4;
42 private static final byte CLEAR_ACTIONS_TYPE = 5;
43 private static final byte METER_TYPE = 6;
44 private static final byte EXPERIMENTER_TYPE = 7;
45 private static final byte GOTO_TABLE_LENGTH = 8;
46 private static final byte WRITE_METADATA_LENGTH = 24;
47 private static final byte WRITE_ACTIONS_LENGTH = 8;
48 private static final byte APPLY_ACTIONS_LENGTH = 8;
49 private static final byte CLEAR_ACTIONS_LENGTH = 8;
50 private static final byte METER_LENGTH = 8;
51 private static final byte EXPERIMENTER_LENGTH = 8;
52 private static final byte PADDING_IN_GOTO_TABLE = 3;
53 private static final byte PADDING_IN_WRITE_METADATA = 4;
54 private static final byte PADDING_IN_CLEAR_ACTIONS = 4;
55 private static final byte INSTRUCTION_IDS_LENGTH = 4;
58 * Encodes instructions
59 * @param instructions List of instructions
60 * @param out output buffer
62 public static void encodeInstructions(List<Instructions> instructions, ByteBuf out) {
63 if (instructions != null) {
64 for (Instructions instruction : instructions) {
65 Class<? extends Instruction> type = instruction.getType();
66 if (type.isAssignableFrom(GotoTable.class)) {
67 writeTypeAndLength(out, GOTO_TABLE_TYPE, GOTO_TABLE_LENGTH);
68 out.writeByte(instruction.getAugmentation(TableIdInstruction.class).getTableId());
69 ByteBufUtils.padBuffer(PADDING_IN_GOTO_TABLE, out);
70 } else if (type.isAssignableFrom(WriteMetadata.class)) {
71 writeTypeAndLength(out, WRITE_METADATA_TYPE, WRITE_METADATA_LENGTH);
72 ByteBufUtils.padBuffer(PADDING_IN_WRITE_METADATA, out);
73 MetadataInstruction metadata = instruction.getAugmentation(MetadataInstruction.class);
74 out.writeBytes(metadata.getMetadata());
75 out.writeBytes(metadata.getMetadataMask());
76 } else if (type.isAssignableFrom(WriteActions.class)) {
77 writeActionsInstruction(out, instruction, WRITE_ACTIONS_TYPE);
78 } else if (type.isAssignableFrom(ApplyActions.class)) {
79 writeActionsInstruction(out, instruction, APPLY_ACTIONS_TYPE);
80 } else if (type.isAssignableFrom(ClearActions.class)) {
81 writeTypeAndLength(out, CLEAR_ACTIONS_TYPE, CLEAR_ACTIONS_LENGTH);
82 ByteBufUtils.padBuffer(PADDING_IN_CLEAR_ACTIONS, out);
83 } else if (type.isAssignableFrom(Meter.class)) {
84 writeTypeAndLength(out, METER_TYPE, METER_LENGTH);
85 out.writeInt(instruction.getAugmentation(MeterIdInstruction.class).getMeterId().intValue());
86 } else if (type.isAssignableFrom(Experimenter.class)) {
87 ExperimenterInstruction experimenter = instruction.getAugmentation(ExperimenterInstruction.class);
88 byte[] data = experimenter.getData();
89 writeTypeAndLength(out, EXPERIMENTER_TYPE, EXPERIMENTER_LENGTH + data.length);
90 out.writeInt(experimenter.getExperimenter().intValue());
99 * Encodes instruction ids (for Multipart - TableFeatures messages)
100 * @param instructions List of instruction identifiers (without values)
101 * @param out output buffer
103 public static void encodeInstructionIds(List<Instructions> instructions, ByteBuf out) {
104 if (instructions != null) {
105 for (Instructions instruction : instructions) {
106 Class<? extends Instruction> type = instruction.getType();
107 if (type.isAssignableFrom(GotoTable.class)) {
108 writeTypeAndLength(out, GOTO_TABLE_TYPE, INSTRUCTION_IDS_LENGTH);
109 } else if (type.isAssignableFrom(WriteMetadata.class)) {
110 writeTypeAndLength(out, WRITE_METADATA_TYPE, INSTRUCTION_IDS_LENGTH);
111 } else if (type.isAssignableFrom(WriteActions.class)) {
112 writeTypeAndLength(out, WRITE_ACTIONS_TYPE, INSTRUCTION_IDS_LENGTH);
113 } else if (type.isAssignableFrom(ApplyActions.class)) {
114 writeTypeAndLength(out, APPLY_ACTIONS_TYPE, INSTRUCTION_IDS_LENGTH);
115 } else if (type.isAssignableFrom(ClearActions.class)) {
116 writeTypeAndLength(out, CLEAR_ACTIONS_TYPE, INSTRUCTION_IDS_LENGTH);
117 } else if (type.isAssignableFrom(Meter.class)) {
118 writeTypeAndLength(out, METER_TYPE, INSTRUCTION_IDS_LENGTH);
119 } else if (type.isAssignableFrom(Experimenter.class)) {
120 ExperimenterInstruction experimenter = instruction.getAugmentation(ExperimenterInstruction.class);
121 writeTypeAndLength(out, EXPERIMENTER_TYPE, EncodeConstants.EXPERIMENTER_IDS_LENGTH);
122 out.writeInt(experimenter.getExperimenter().intValue());
128 private static void writeTypeAndLength(ByteBuf out, int type, int length) {
129 out.writeShort(type);
130 out.writeShort(length);
133 private static void writeActionsInstruction(ByteBuf out,
134 Instructions instruction, int type) {
135 final byte ACTIONS_INSTRUCTION_LENGTH = 8;
136 final byte PADDING_IN_ACTIONS_INSTRUCTION = 4;
137 out.writeShort(type);
138 List<ActionsList> actions = instruction.getAugmentation(ActionsInstruction.class).getActionsList();
139 out.writeShort(ACTIONS_INSTRUCTION_LENGTH + ActionsSerializer.computeLengthOfActions(actions));
140 ByteBufUtils.padBuffer(PADDING_IN_ACTIONS_INSTRUCTION, out);
141 ActionsSerializer.encodeActions(actions, out);
145 * Computes length of instructions
146 * @param instructions List of instructions
147 * @return length of instructions (in bytes)
149 public static int computeInstructionsLength(List<Instructions> instructions) {
151 if (instructions != null) {
152 for (Instructions instruction : instructions) {
153 Class<? extends Instruction> type = instruction.getType();
154 if (type.isAssignableFrom(GotoTable.class)) {
155 length += GOTO_TABLE_LENGTH;
156 } else if (type.isAssignableFrom(WriteMetadata.class)) {
157 length += WRITE_METADATA_LENGTH;
158 } else if (type.isAssignableFrom(WriteActions.class)) {
159 length += WRITE_ACTIONS_LENGTH + ActionsSerializer.computeLengthOfActions(
160 instruction.getAugmentation(ActionsInstruction.class).getActionsList());
161 } else if (type.isAssignableFrom(ApplyActions.class)) {
162 length += APPLY_ACTIONS_LENGTH + ActionsSerializer.computeLengthOfActions(
163 instruction.getAugmentation(ActionsInstruction.class).getActionsList());
164 } else if (type.isAssignableFrom(ClearActions.class)) {
165 length += CLEAR_ACTIONS_LENGTH;
166 } else if (type.isAssignableFrom(Meter.class)) {
167 length += METER_LENGTH;
168 } else if (type.isAssignableFrom(Experimenter.class)) {
169 ExperimenterInstruction experimenter = instruction.getAugmentation(ExperimenterInstruction.class);
170 byte[] data = experimenter.getData();
171 length += EXPERIMENTER_LENGTH + data.length;