Use ByteBuf.readRetainedSlice()
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / util / InstructionUtil.java
1 /*
2  * Copyright (c) 2017 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.openflowplugin.impl.protocol.deserialization.util;
10
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.HeaderDeserializer;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
15 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
16 import org.opendaylight.openflowjava.protocol.impl.util.InstructionConstants;
17 import org.opendaylight.openflowplugin.api.openflow.protocol.deserialization.MessageCodeExperimenterKey;
18 import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
19 import org.opendaylight.openflowplugin.impl.protocol.deserialization.key.MessageCodeActionExperimenterKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction;
21 import org.opendaylight.yangtools.yang.common.Uint8;
22
23 /**
24  * Utility class for action deserialization.
25  */
26 public final class InstructionUtil {
27
28     private InstructionUtil() {
29     }
30
31     /**
32      * Deserialize OpenFlow instruction.
33      *
34      * @param version  OpenFlow version
35      * @param message  OpenFlow buffered message
36      * @param registry deserializer registry
37      */
38     public static Instruction readInstruction(final Uint8 version,
39                                               final ByteBuf message,
40                                               final DeserializerRegistry registry) {
41         final int type = message.getUnsignedShort(message.readerIndex());
42         final OFDeserializer<Instruction> deserializer;
43
44         if (InstructionConstants.APPLY_ACTIONS_TYPE == type) {
45             deserializer = registry.getDeserializer(
46                     new MessageCodeActionExperimenterKey(
47                             version, type, Instruction.class,
48                             ActionPath.FLOWS_STATISTICS_UPDATE_APPLY_ACTIONS,
49                             null));
50         } else if (InstructionConstants.WRITE_ACTIONS_TYPE == type) {
51             deserializer = registry.getDeserializer(
52                     new MessageCodeActionExperimenterKey(
53                             version, type, Instruction.class,
54                             ActionPath.FLOWS_STATISTICS_UPDATE_WRITE_ACTIONS,
55                             null));
56         } else {
57             Long expId = null;
58
59             if (EncodeConstants.EXPERIMENTER_VALUE == type) {
60                 expId = message.getUnsignedInt(message.readerIndex() + 2 * Short.BYTES);
61             }
62
63             deserializer = registry.getDeserializer(
64                     new MessageCodeExperimenterKey(
65                             version, type, Instruction.class, expId));
66         }
67
68         return deserializer.deserialize(message);
69     }
70
71     /**
72      * Deserialize OpenFlow instruction header.
73      *
74      * @param version  OpenFlow version
75      * @param message  OpenFlow buffered message
76      * @param registry deserializer registry
77      */
78     public static Instruction readInstructionHeader(final Uint8 version,
79                                                     final ByteBuf message,
80                                                     final DeserializerRegistry registry) {
81         final int type = message.getUnsignedShort(message.readerIndex());
82         final HeaderDeserializer<Instruction> deserializer;
83
84         if (InstructionConstants.APPLY_ACTIONS_TYPE == type) {
85             deserializer = registry.getDeserializer(
86                     new MessageCodeActionExperimenterKey(
87                             version, type, Instruction.class,
88                             ActionPath.INVENTORY_FLOWNODE_TABLE_APPLY_ACTIONS,
89                             null));
90         } else if (InstructionConstants.WRITE_ACTIONS_TYPE == type) {
91             deserializer = registry.getDeserializer(
92                     new MessageCodeActionExperimenterKey(
93                             version, type, Instruction.class,
94                             ActionPath.INVENTORY_FLOWNODE_TABLE_WRITE_ACTIONS,
95                             null));
96         } else {
97             Long expId = null;
98
99             if (EncodeConstants.EXPERIMENTER_VALUE == type) {
100                 expId = message.getUnsignedInt(message.readerIndex() + 2 * Short.BYTES);
101             }
102
103             deserializer = registry.getDeserializer(
104                     new MessageCodeExperimenterKey(
105                             version, type, Instruction.class, expId));
106         }
107
108         return deserializer.deserializeHeader(message);
109     }
110
111 }