f15409f3ee57ab1c31e1260375ee275573cb1e57
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / instruction / AbstractInstructionDeserializer.java
1 /*\r
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 \r
9 package org.opendaylight.openflowjava.protocol.impl.deserialization.instruction;\r
10 \r
11 import io.netty.buffer.ByteBuf;\r
12 \r
13 import org.opendaylight.openflowjava.protocol.api.extensibility.HeaderDeserializer;\r
14 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;\r
15 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions;\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions;\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable;\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter;\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions;\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.InstructionBuilder;\r
24 \r
25 /**\r
26  * @author michal.polkorab\r
27  *\r
28  */\r
29 public abstract class AbstractInstructionDeserializer implements OFDeserializer<Instruction>,\r
30         HeaderDeserializer<Instruction> {\r
31 \r
32     @Override\r
33     public Instruction deserializeHeader(ByteBuf rawMessage) {\r
34         InstructionBuilder builder = processHeader(rawMessage);\r
35         rawMessage.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);\r
36         return builder.build();\r
37     }\r
38 \r
39     protected InstructionBuilder processHeader(ByteBuf input) {\r
40         InstructionBuilder builder = new InstructionBuilder();\r
41         int type = input.readUnsignedShort();\r
42         switch (type) {\r
43         case 1:\r
44             builder.setType(GotoTable.class);\r
45             break;\r
46         case 2:\r
47             builder.setType(WriteMetadata.class);\r
48             break;\r
49         case 3:\r
50             builder.setType(WriteActions.class);\r
51             break;\r
52         case 4:\r
53             builder.setType(ApplyActions.class);\r
54             break;\r
55         case 5:\r
56             builder.setType(ClearActions.class);\r
57             break;\r
58         case 6:\r
59             builder.setType(Meter.class);\r
60             break;\r
61         default:\r
62             throw new IllegalStateException("Unknown instruction type received, type: " + type);\r
63         }\r
64         return builder;\r
65     }\r
66 }\r