ae24d8ba938b7468763cf363590b9661e54532fd
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / util / ActionUtil.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.util;
10
11 import java.util.Objects;
12
13 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
15 import org.opendaylight.openflowjava.protocol.api.keys.ActionDeserializerKey;
16 import org.opendaylight.openflowjava.protocol.api.keys.ExperimenterActionDeserializerKey;
17 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
20 import org.opendaylight.openflowplugin.api.openflow.protocol.deserialization.MessageCodeExperimenterKey;
21 import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
22 import org.opendaylight.openflowplugin.openflow.md.core.extension.ActionExtensionHelper;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
24
25 import io.netty.buffer.ByteBuf;
26
27 /**
28  * Utility class for action deserialization
29  */
30 public class ActionUtil {
31
32     /**
33      * Deserialize OpenFlow action, using extension converter if available
34      * TODO: Remove also extension converters
35      *
36      * @param version OpenFlow version
37      * @param message OpenFlow buffered message
38      * @param registry deserializer registry
39      * @param path Action path
40      */
41     public static Action readAction(short version, ByteBuf message, DeserializerRegistry registry,
42             ActionPath path) {
43         int type = message.getUnsignedShort(message.readerIndex());
44         Long expId = null;
45
46         if (type == EncodeConstants.EXPERIMENTER_VALUE) {
47             expId = message.getUnsignedInt(message.readerIndex()
48                     + 2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
49         }
50
51         try {
52             final MessageCodeExperimenterKey key = new MessageCodeExperimenterKey(
53                 version, type, Action.class, expId);
54
55             final OFDeserializer<Action> deserializer = registry.getDeserializer(key);
56
57             return deserializer.deserialize(message);
58         } catch (IllegalStateException e) {
59             final MessageCodeKey key = Objects.nonNull(expId)
60                 ? new ExperimenterActionDeserializerKey(version, expId)
61                 : new ActionDeserializerKey(version, type, expId);
62
63             final OFDeserializer<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203
64                 .actions.grouping.Action> deserializer = registry.getDeserializer(key);
65
66             return ActionExtensionHelper.processAlienAction(deserializer.deserialize(message),
67                     OpenflowVersion.get(version), path);
68         }
69     }
70
71 }