459377d69c5f5b64e3e9692c5746152c3f928659
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / main / java / org / opendaylight / openflowjava / nx / codec / action / AbstractActionCodec.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. 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 package org.opendaylight.openflowjava.nx.codec.action;
9
10 import io.netty.buffer.ByteBuf;
11 import org.opendaylight.openflowjava.nx.api.NiciraConstants;
12 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;
18
19 /**
20  * @author msunal
21  */
22 public abstract class AbstractActionCodec implements OFSerializer<Action>, OFDeserializer<Action> {
23
24     protected final static void serializeHeader(final int msgLength, final int subtype, final ByteBuf outBuffer) {
25         outBuffer.writeShort(EncodeConstants.EXPERIMENTER_VALUE);
26         writeMsgLengthVendorIdSubtypeToBuffer(msgLength, subtype, outBuffer);
27     }
28
29     private final static void writeMsgLengthVendorIdSubtypeToBuffer(final int msgLength, final int subtype, final ByteBuf outBuffer) {
30         outBuffer.writeShort(msgLength);
31         outBuffer.writeInt(NiciraConstants.NX_VENDOR_ID.intValue());
32         outBuffer.writeShort(subtype);
33     }
34
35     protected final static ActionBuilder deserializeHeader(final ByteBuf message) {
36         // size of experimenter type
37         message.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
38         // size of length
39         message.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
40         // vendor id
41         message.skipBytes(EncodeConstants.SIZE_OF_INT_IN_BYTES);
42         // subtype
43         message.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
44         ActionBuilder actionBuilder = new ActionBuilder();
45         actionBuilder.setExperimenterId(getExperimenterId());
46         return actionBuilder;
47     }
48
49     protected final static ExperimenterId getExperimenterId(){
50         return new ExperimenterId(NiciraConstants.NX_VENDOR_ID);
51     }
52
53
54 }