Merge "Refactor nsh fields to new encoding"
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / main / java / org / opendaylight / openflowjava / nx / codec / match / NshNpCodec.java
1 /*
2  * Copyright (c) 2015 Intel, 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
9 package org.opendaylight.openflowjava.nx.codec.match;
10
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.openflowjava.nx.api.NiciraConstants;
13 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntryDeserializerKey;
14 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey;
15 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.ExperimenterClass;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxNshNp;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.nsh.np.grouping.NshNpValues;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.nsh.np.grouping.NshNpValuesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.experimenter.id._case.NxExpMatchEntryValue;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.experimenter.id._case.nx.exp.match.entry.value.NshNpCaseValue;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.experimenter.id._case.nx.exp.match.entry.value.NshNpCaseValueBuilder;
24
25 public class NshNpCodec extends AbstractExperimenterMatchCodec {
26
27     private static final int VALUE_LENGTH = EncodeConstants.SIZE_OF_BYTE_IN_BYTES;
28     private static final int NXM_FIELD_CODE = 3;
29     public static final MatchEntrySerializerKey<ExperimenterClass, NxmNxNshNp> SERIALIZER_KEY =
30             createSerializerKey(
31                     EncodeConstants.OF13_VERSION_ID,
32                     NiciraConstants.NX_NSH_VENDOR_ID,
33                     NxmNxNshNp.class);
34     public static final MatchEntryDeserializerKey DESERIALIZER_KEY =
35             createDeserializerKey(
36                     EncodeConstants.OF13_VERSION_ID,
37                     NiciraConstants.NX_NSH_VENDOR_ID,
38                     NXM_FIELD_CODE);
39
40     @Override
41     protected void serializeValue(NxExpMatchEntryValue value, boolean hasMask, ByteBuf outBuffer) {
42         NshNpCaseValue nshNpCaseValue = (NshNpCaseValue) value;
43         NshNpValues nshNpValues = nshNpCaseValue.getNshNpValues();
44         outBuffer.writeByte(nshNpValues.getValue());
45     }
46
47     @Override
48     protected NxExpMatchEntryValue deserializeValue(ByteBuf message, boolean hasMask) {
49         Short value = message.readUnsignedByte();
50         NshNpValues nshNpValues = new NshNpValuesBuilder().setValue(value).build();
51         return new NshNpCaseValueBuilder().setNshNpValues(nshNpValues).build();
52     }
53
54     @Override
55     public int getNxmFieldCode() {
56         return NXM_FIELD_CODE;
57     }
58
59     @Override
60     protected long getExperimenterId() {
61         return NiciraConstants.NX_NSH_VENDOR_ID;
62     }
63
64     @Override
65     public int getValueLength() {
66         return VALUE_LENGTH;
67     }
68
69     @Override
70     public Class<? extends MatchField> getNxmField() {
71         return NxmNxNshNp.class;
72     }
73 }