196a7246314fc17e43bde66737e2f884851f3d59
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / InstructionsSerializer.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.List;
14
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;
30
31 /**
32  * Serializes ofp_instruction (OpenFlow v 1.3) structure
33  * @author michal.polkorab
34  * @author timotej.kubas
35  */
36 public abstract class InstructionsSerializer {
37
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;
56     
57     /**
58      * Encodes instructions
59      * @param instructions List of instructions
60      * @param out output buffer
61      */
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());
91                     out.writeBytes(data);
92                 }
93             }
94         }
95         
96     }
97     
98     /**
99      * Encodes instruction ids (for Multipart - TableFeatures messages)
100      * @param instructions List of instruction identifiers (without values)
101      * @param out output buffer
102      */
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                     writeTypeAndLength(out, EXPERIMENTER_TYPE, EXPERIMENTER_LENGTH);
121                 }
122             }
123         }
124     }
125
126     private static void writeTypeAndLength(ByteBuf out, int type, int length) {
127         out.writeShort(type);
128         out.writeShort(length);
129     }
130
131     private static void writeActionsInstruction(ByteBuf out,
132             Instructions instruction, int type) {
133         final byte ACTIONS_INSTRUCTION_LENGTH = 8;
134         final byte PADDING_IN_ACTIONS_INSTRUCTION = 4;
135         out.writeShort(type);
136         List<ActionsList> actions = instruction.getAugmentation(ActionsInstruction.class).getActionsList();
137         out.writeShort(ACTIONS_INSTRUCTION_LENGTH + ActionsSerializer.computeLengthOfActions(actions));
138         ByteBufUtils.padBuffer(PADDING_IN_ACTIONS_INSTRUCTION, out);
139         ActionsSerializer.encodeActions(actions, out);
140     }
141     
142     /**
143      * Computes length of instructions
144      * @param instructions List of instructions
145      * @return length of instructions (in bytes)
146      */
147     public static int computeInstructionsLength(List<Instructions> instructions) {
148         int length = 0;
149         if (instructions != null) {
150             for (Instructions instruction : instructions) {
151                 Class<? extends Instruction> type = instruction.getType();
152                 if (type.isAssignableFrom(GotoTable.class)) {
153                     length += GOTO_TABLE_LENGTH;
154                 } else if (type.isAssignableFrom(WriteMetadata.class)) {
155                     length += WRITE_METADATA_LENGTH;
156                 } else if (type.isAssignableFrom(WriteActions.class)) {
157                     length += WRITE_ACTIONS_LENGTH + ActionsSerializer.computeLengthOfActions(
158                             instruction.getAugmentation(ActionsInstruction.class).getActionsList());
159                 } else if (type.isAssignableFrom(ApplyActions.class)) {
160                     length += APPLY_ACTIONS_LENGTH + ActionsSerializer.computeLengthOfActions(
161                             instruction.getAugmentation(ActionsInstruction.class).getActionsList());
162                 } else if (type.isAssignableFrom(ClearActions.class)) {
163                     length += CLEAR_ACTIONS_LENGTH;
164                 } else if (type.isAssignableFrom(Meter.class)) {
165                     length += METER_LENGTH;
166                 } else if (type.isAssignableFrom(Experimenter.class)) {
167                     ExperimenterInstruction experimenter = instruction.getAugmentation(ExperimenterInstruction.class);
168                     byte[] data = experimenter.getData();
169                     length += EXPERIMENTER_LENGTH + data.length;
170                 }
171             }
172         }
173         return length;
174     }
175 }