Fix checkstyle violations in openflowplugin extensions
[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
22 /**
23  * Utility class for action deserialization.
24  */
25 public final class InstructionUtil {
26
27     private InstructionUtil() {
28     }
29
30     /**
31      * Deserialize OpenFlow instruction.
32      *
33      * @param version  OpenFlow version
34      * @param message  OpenFlow buffered message
35      * @param registry deserializer registry
36      */
37     @SuppressWarnings("checkstyle:LineLength")
38     public static Instruction readInstruction(final short 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 * EncodeConstants.SIZE_OF_SHORT_IN_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     @SuppressWarnings("checkstyle:LineLength")
79     public static Instruction readInstructionHeader(final short version,
80                                                     final ByteBuf message,
81                                                     final DeserializerRegistry registry) {
82         final int type = message.getUnsignedShort(message.readerIndex());
83         final HeaderDeserializer<Instruction> deserializer;
84
85         if (InstructionConstants.APPLY_ACTIONS_TYPE == type) {
86             deserializer = registry.getDeserializer(
87                     new MessageCodeActionExperimenterKey(
88                             version, type, Instruction.class,
89                             ActionPath.INVENTORY_FLOWNODE_TABLE_APPLY_ACTIONS,
90                             null));
91         } else if (InstructionConstants.WRITE_ACTIONS_TYPE == type) {
92             deserializer = registry.getDeserializer(
93                     new MessageCodeActionExperimenterKey(
94                             version, type, Instruction.class,
95                             ActionPath.INVENTORY_FLOWNODE_TABLE_WRITE_ACTIONS,
96                             null));
97         } else {
98             Long expId = null;
99
100             if (EncodeConstants.EXPERIMENTER_VALUE == type) {
101                 expId = message.getUnsignedInt(message.readerIndex() + 2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
102             }
103
104             deserializer = registry.getDeserializer(
105                     new MessageCodeExperimenterKey(
106                             version, type, Instruction.class, expId));
107         }
108
109         return deserializer.deserializeHeader(message);
110     }
111
112 }