Copyright update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / InstructionsDeserializer.java
index 078b5d8957e5bc5e7eccc81f62d34da9dfbff481..e77daca17ee53d37547f342804d3d3cadd07e129 100644 (file)
-/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
-package org.opendaylight.openflowjava.protocol.impl.util;\r
-\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstructionBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterInstruction;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterInstructionBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstructionBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstructionBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstructionBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Experimenter;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.Instructions;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.InstructionsBuilder;\r
-\r
-import io.netty.buffer.ByteBuf;\r
-\r
-/**\r
- * Deserializes ofp_instruction (OpenFlow v1.3) structures\r
- * @author michal.polkorab\r
- * @author timotej.kubas\r
- */\r
-public class InstructionsDeserializer {\r
-    \r
-    private static final byte WRITE_APPLY_CLEAR_ACTION_LENGTH = 8;\r
-    private static final byte EXPERIMENTER_HEADER_LENGTH = 8;\r
-    private static final byte GOTO_TABLE_PADDING = 3;\r
-    private static final byte WRITE_METADATA_PADDING = 4;\r
-    private static final byte ACTIONS_RELATED_INSTRUCTION_PADDING = 4;\r
-\r
-    /**\r
-     * Creates list of instructions\r
-     * @param input\r
-     * @param length\r
-     * @return list of ofp_instruction\r
-     */\r
-    public static List<Instructions> createInstructions(ByteBuf input, int length) {\r
-        List<Instructions> instructions = new ArrayList<>();\r
-        if (input.readableBytes() != 0) {\r
-            int lengthOfInstructions = length;\r
-            while (lengthOfInstructions > 0) {\r
-                InstructionsBuilder builder = new InstructionsBuilder();\r
-                int type = input.readUnsignedShort();\r
-                int instructionLength = input.readUnsignedShort();\r
-                lengthOfInstructions -= instructionLength;\r
-                switch (type) {\r
-                case 1:\r
-                    createGotoTableInstruction(builder, input);\r
-                    break;\r
-                case 2:\r
-                    createMetadataInstruction(builder, input);\r
-                    break;\r
-                case 3:\r
-                    builder.setType(WriteActions.class);\r
-                    createActionRelatedInstruction(input, builder, instructionLength - WRITE_APPLY_CLEAR_ACTION_LENGTH);\r
-                    break;\r
-                case 4:\r
-                    builder.setType(ApplyActions.class);\r
-                    createActionRelatedInstruction(input, builder, instructionLength - WRITE_APPLY_CLEAR_ACTION_LENGTH);\r
-                    break;\r
-                case 5:\r
-                    builder.setType(ClearActions.class);\r
-                    createActionRelatedInstruction(input, builder, instructionLength - WRITE_APPLY_CLEAR_ACTION_LENGTH);\r
-                    break;\r
-                case 6:\r
-                    builder.setType(Meter.class);\r
-                    MeterIdInstructionBuilder meterBuilder = new MeterIdInstructionBuilder();\r
-                    meterBuilder.setMeterId(input.readUnsignedInt());\r
-                    builder.addAugmentation(MeterIdInstruction.class, meterBuilder.build());\r
-                    break;\r
-                case 65535:\r
-                    builder.setType(Experimenter.class);\r
-                    ExperimenterInstructionBuilder expBuilder = new ExperimenterInstructionBuilder();\r
-                    expBuilder.setExperimenter(input.readUnsignedInt());\r
-                    int dataLength = instructionLength - EXPERIMENTER_HEADER_LENGTH;\r
-                    if (dataLength > 0) {\r
-                        byte[] data = new byte[dataLength];\r
-                        input.readBytes(data);\r
-                        expBuilder.setData(data);\r
-                    }\r
-                    builder.addAugmentation(ExperimenterInstruction.class, expBuilder.build());\r
-                    break;\r
-                default:\r
-                    break;\r
-                }\r
-                instructions.add(builder.build());\r
-            }\r
-        }\r
-        return instructions;\r
-    }\r
-\r
-    private static void createGotoTableInstruction(InstructionsBuilder builder,\r
-            ByteBuf input) {\r
-        builder.setType(GotoTable.class);\r
-        TableIdInstructionBuilder tableBuilder = new TableIdInstructionBuilder();\r
-        tableBuilder.setTableId(input.readUnsignedByte());\r
-        builder.addAugmentation(TableIdInstruction.class, tableBuilder.build());\r
-        input.skipBytes(GOTO_TABLE_PADDING);\r
-    }\r
-    \r
-    private static void createMetadataInstruction(InstructionsBuilder builder,\r
-            ByteBuf input) {\r
-        input.skipBytes(WRITE_METADATA_PADDING);\r
-        builder.setType(WriteMetadata.class);\r
-        MetadataInstructionBuilder metadataBuilder = new MetadataInstructionBuilder();\r
-        byte[] metadata = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];\r
-        input.readBytes(metadata);\r
-        metadataBuilder.setMetadata(metadata);\r
-        byte[] metadata_mask = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];\r
-        input.readBytes(metadata_mask);\r
-        metadataBuilder.setMetadata(metadata_mask);\r
-        builder.addAugmentation(MetadataInstruction.class, metadataBuilder.build());\r
-    }\r
-    \r
-    private static void createActionRelatedInstruction(ByteBuf input,\r
-            InstructionsBuilder builder, int instructionLength) {\r
-        input.skipBytes(ACTIONS_RELATED_INSTRUCTION_PADDING);\r
-        ActionsInstructionBuilder actionsBuilder = new ActionsInstructionBuilder();\r
-        actionsBuilder.setActionsList(ActionsDeserializer.createActionsList(input, instructionLength));\r
-        builder.addAugmentation(ActionsInstruction.class, actionsBuilder.build());\r
-    }\r
-\r
-}\r
+/*
+ * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowjava.protocol.impl.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstructionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterInstruction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterInstructionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstructionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstructionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstructionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Experimenter;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.Instructions;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.InstructionsBuilder;
+
+import io.netty.buffer.ByteBuf;
+
+/**
+ * Deserializes ofp_instruction (OpenFlow v1.3) structures
+ * @author michal.polkorab
+ * @author timotej.kubas
+ */
+public class InstructionsDeserializer {
+    
+    private static final byte WRITE_APPLY_CLEAR_ACTION_LENGTH = 8;
+    private static final byte EXPERIMENTER_HEADER_LENGTH = 8;
+    private static final byte GOTO_TABLE_PADDING = 3;
+    private static final byte WRITE_METADATA_PADDING = 4;
+    private static final byte ACTIONS_RELATED_INSTRUCTION_PADDING = 4;
+
+    /**
+     * Creates list of instructions
+     * @param input
+     * @param length
+     * @return list of ofp_instruction
+     */
+    public static List<Instructions> createInstructions(ByteBuf input, int length) {
+        List<Instructions> instructions = new ArrayList<>();
+        if (input.readableBytes() != 0) {
+            int lengthOfInstructions = length;
+            while (lengthOfInstructions > 0) {
+                InstructionsBuilder builder = new InstructionsBuilder();
+                int type = input.readUnsignedShort();
+                int instructionLength = input.readUnsignedShort();
+                lengthOfInstructions -= instructionLength;
+                switch (type) {
+                case 1:
+                    createGotoTableInstruction(builder, input);
+                    break;
+                case 2:
+                    createMetadataInstruction(builder, input);
+                    break;
+                case 3:
+                    builder.setType(WriteActions.class);
+                    createActionRelatedInstruction(input, builder, instructionLength - WRITE_APPLY_CLEAR_ACTION_LENGTH);
+                    break;
+                case 4:
+                    builder.setType(ApplyActions.class);
+                    createActionRelatedInstruction(input, builder, instructionLength - WRITE_APPLY_CLEAR_ACTION_LENGTH);
+                    break;
+                case 5:
+                    builder.setType(ClearActions.class);
+                    createActionRelatedInstruction(input, builder, instructionLength - WRITE_APPLY_CLEAR_ACTION_LENGTH);
+                    break;
+                case 6:
+                    builder.setType(Meter.class);
+                    MeterIdInstructionBuilder meterBuilder = new MeterIdInstructionBuilder();
+                    meterBuilder.setMeterId(input.readUnsignedInt());
+                    builder.addAugmentation(MeterIdInstruction.class, meterBuilder.build());
+                    break;
+                case 65535:
+                    builder.setType(Experimenter.class);
+                    ExperimenterInstructionBuilder expBuilder = new ExperimenterInstructionBuilder();
+                    expBuilder.setExperimenter(input.readUnsignedInt());
+                    int dataLength = instructionLength - EXPERIMENTER_HEADER_LENGTH;
+                    if (dataLength > 0) {
+                        byte[] data = new byte[dataLength];
+                        input.readBytes(data);
+                        expBuilder.setData(data);
+                    }
+                    builder.addAugmentation(ExperimenterInstruction.class, expBuilder.build());
+                    break;
+                default:
+                    break;
+                }
+                instructions.add(builder.build());
+            }
+        }
+        return instructions;
+    }
+
+    private static void createGotoTableInstruction(InstructionsBuilder builder,
+            ByteBuf input) {
+        builder.setType(GotoTable.class);
+        TableIdInstructionBuilder tableBuilder = new TableIdInstructionBuilder();
+        tableBuilder.setTableId(input.readUnsignedByte());
+        builder.addAugmentation(TableIdInstruction.class, tableBuilder.build());
+        input.skipBytes(GOTO_TABLE_PADDING);
+    }
+    
+    private static void createMetadataInstruction(InstructionsBuilder builder,
+            ByteBuf input) {
+        input.skipBytes(WRITE_METADATA_PADDING);
+        builder.setType(WriteMetadata.class);
+        MetadataInstructionBuilder metadataBuilder = new MetadataInstructionBuilder();
+        byte[] metadata = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
+        input.readBytes(metadata);
+        metadataBuilder.setMetadata(metadata);
+        byte[] metadata_mask = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
+        input.readBytes(metadata_mask);
+        metadataBuilder.setMetadata(metadata_mask);
+        builder.addAugmentation(MetadataInstruction.class, metadataBuilder.build());
+    }
+    
+    private static void createActionRelatedInstruction(ByteBuf input,
+            InstructionsBuilder builder, int instructionLength) {
+        input.skipBytes(ACTIONS_RELATED_INSTRUCTION_PADDING);
+        ActionsInstructionBuilder actionsBuilder = new ActionsInstructionBuilder();
+        actionsBuilder.setActionsList(ActionsDeserializer.createActionsList(input, instructionLength));
+        builder.addAugmentation(ActionsInstruction.class, actionsBuilder.build());
+    }
+
+}