4bf20b8d9b747a7fb226582e4680d421f8e6ab34
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / instruction / AbstractActionInstructionDeserializer.java
1 /*
2  * Copyright (c) 2016 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.instruction;
10
11 import io.netty.buffer.ByteBuf;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.openflowjava.protocol.impl.util.InstructionConstants;
18 import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
19 import org.opendaylight.openflowplugin.impl.protocol.deserialization.util.ActionUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
22
23 public abstract class AbstractActionInstructionDeserializer extends AbstractInstructionDeserializer
24         implements DeserializerRegistryInjector {
25
26     private DeserializerRegistry registry;
27     private final ActionPath actionPath;
28
29     /**
30      * Create new instacte of action instruction deserializer.
31      *
32      * @param actionPath action extension path
33      */
34     public AbstractActionInstructionDeserializer(final ActionPath actionPath) {
35         this.actionPath = actionPath;
36     }
37
38     /**
39      * Skip first few bytes of instruction message because they are irrelevant and then return length.
40      *
41      * @param message Openflow buffered message
42      * @return instruction length
43      **/
44     protected static int readHeader(ByteBuf message) {
45         message.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
46         return message.readUnsignedShort();
47     }
48
49     /**
50      * Read list of actions from message.
51      *
52      * @param message Openflow buffered message
53      * @param length  instruction length
54      * @return list of actions
55      **/
56     protected List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list
57             .Action> readActions(ByteBuf message, int length) {
58
59         final int instrLength = length - InstructionConstants.STANDARD_INSTRUCTION_LENGTH;
60
61         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list
62                 .Action> actions = new ArrayList<>();
63
64         if (message.readableBytes() > 0) {
65             final int startIndex = message.readerIndex();
66             int offset = 0;
67
68             while ((message.readerIndex() - startIndex) < instrLength) {
69                 actions.add(new ActionBuilder()
70                         .setKey(new ActionKey(offset))
71                         .setOrder(offset)
72                         .setAction(ActionUtil
73                                 .readAction(EncodeConstants.OF13_VERSION_ID, message, registry, actionPath))
74                         .build());
75
76                 offset++;
77             }
78         }
79
80         return actions;
81     }
82
83     @Override
84     public void injectDeserializerRegistry(DeserializerRegistry deserializerRegistry) {
85         registry = deserializerRegistry;
86     }
87
88 }