Javadoc update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / InstructionsDeserializer.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.opendaylight.openflowjava.protocol.impl.util;\r
3 \r
4 import java.util.ArrayList;\r
5 import java.util.List;\r
6 \r
7 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;\r
8 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstructionBuilder;\r
9 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterInstruction;\r
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterInstructionBuilder;\r
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;\r
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstructionBuilder;\r
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;\r
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstructionBuilder;\r
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstructionBuilder;\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions;\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions;\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Experimenter;\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable;\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.Instructions;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.InstructionsBuilder;\r
26 \r
27 import io.netty.buffer.ByteBuf;\r
28 \r
29 /**\r
30  * Deserializes ofp_instruction (OpenFlow v1.3) structures\r
31  * @author michal.polkorab\r
32  * @author timotej.kubas\r
33  */\r
34 public class InstructionsDeserializer {\r
35     \r
36     private static final byte WRITE_APPLY_CLEAR_ACTION_LENGTH = 8;\r
37 \r
38     /**\r
39      * Creates list of instructions\r
40      * @param input\r
41      * @param length\r
42      * @return list of ofp_instruction\r
43      */\r
44     public static List<Instructions> createInstructions(ByteBuf input, int length) {\r
45         List<Instructions> instructions = new ArrayList<>();\r
46         if (input.readableBytes() != 0) {\r
47             int lengthOfInstructions = length;\r
48             while (lengthOfInstructions > 0) {\r
49                 InstructionsBuilder builder = new InstructionsBuilder();\r
50                 int type = input.readUnsignedShort();\r
51                 int instructionLength = input.readUnsignedShort();\r
52                 lengthOfInstructions -= instructionLength;\r
53                 switch (type) {\r
54                 case 1:\r
55                     createGotoTableInstruction(builder, input);\r
56                     break;\r
57                 case 2:\r
58                     createMetadataInstruction(builder, input);\r
59                     break;\r
60                 case 3:\r
61                     builder.setType(WriteActions.class);\r
62                     createActionRelatedInstruction(input, builder, instructionLength - WRITE_APPLY_CLEAR_ACTION_LENGTH);\r
63                     break;\r
64                 case 4:\r
65                     builder.setType(ApplyActions.class);\r
66                     createActionRelatedInstruction(input, builder, instructionLength - WRITE_APPLY_CLEAR_ACTION_LENGTH);\r
67                     break;\r
68                 case 5:\r
69                     builder.setType(ClearActions.class);\r
70                     createActionRelatedInstruction(input, builder, instructionLength - WRITE_APPLY_CLEAR_ACTION_LENGTH);\r
71                     break;\r
72                 case 6:\r
73                     builder.setType(Meter.class);\r
74                     MeterIdInstructionBuilder meterBuilder = new MeterIdInstructionBuilder();\r
75                     meterBuilder.setMeterId(input.readUnsignedInt());\r
76                     builder.addAugmentation(MeterIdInstruction.class, meterBuilder.build());\r
77                     break;\r
78                 case 65535:\r
79                     final byte EXPERIMENTER_HEADER_LENGTH = 8;\r
80                     builder.setType(Experimenter.class);\r
81                     ExperimenterInstructionBuilder expBuilder = new ExperimenterInstructionBuilder();\r
82                     expBuilder.setExperimenter(input.readUnsignedInt());\r
83                     int dataLength = instructionLength - EXPERIMENTER_HEADER_LENGTH;\r
84                     if (dataLength > 0) {\r
85                         byte[] data = new byte[dataLength];\r
86                         input.readBytes(data);\r
87                         expBuilder.setData(data);\r
88                     }\r
89                     builder.addAugmentation(ExperimenterInstruction.class, expBuilder.build());\r
90                     break;\r
91                 default:\r
92                     break;\r
93                 }\r
94                 instructions.add(builder.build());\r
95             }\r
96         }\r
97         return instructions;\r
98     }\r
99 \r
100     private static void createGotoTableInstruction(InstructionsBuilder builder,\r
101             ByteBuf input) {\r
102         final byte GOTO_TABLE_PADDING = 3;\r
103         builder.setType(GotoTable.class);\r
104         TableIdInstructionBuilder tableBuilder = new TableIdInstructionBuilder();\r
105         tableBuilder.setTableId(input.readUnsignedByte());\r
106         builder.addAugmentation(TableIdInstruction.class, tableBuilder.build());\r
107         input.skipBytes(GOTO_TABLE_PADDING);\r
108     }\r
109     \r
110     private static void createMetadataInstruction(InstructionsBuilder builder,\r
111             ByteBuf input) {\r
112         final byte WRITE_METADATA_PADDING = 4;\r
113         input.skipBytes(WRITE_METADATA_PADDING);\r
114         builder.setType(WriteMetadata.class);\r
115         MetadataInstructionBuilder metadataBuilder = new MetadataInstructionBuilder();\r
116         byte[] metadata = new byte[Long.SIZE / Byte.SIZE];\r
117         input.readBytes(metadata);\r
118         metadataBuilder.setMetadata(metadata);\r
119         byte[] metadata_mask = new byte[Long.SIZE / Byte.SIZE];\r
120         input.readBytes(metadata_mask);\r
121         metadataBuilder.setMetadata(metadata_mask);\r
122         builder.addAugmentation(MetadataInstruction.class, metadataBuilder.build());\r
123     }\r
124     \r
125     private static void createActionRelatedInstruction(ByteBuf input,\r
126             InstructionsBuilder builder, int instructionLength) {\r
127         final byte ACTIONS_RELATED_INSTRUCTION_PADDING = 4;\r
128         input.skipBytes(ACTIONS_RELATED_INSTRUCTION_PADDING);\r
129         ActionsInstructionBuilder actionsBuilder = new ActionsInstructionBuilder();\r
130         actionsBuilder.setActionsList(ActionsDeserializer.createActionsList(input, instructionLength));\r
131         builder.addAugmentation(ActionsInstruction.class, actionsBuilder.build());\r
132     }\r
133 \r
134 }\r