Migrate uint/ByteBuf interactions
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / main / java / org / opendaylight / openflowjava / nx / codec / match / NsiCodec.java
1 /*
2  * Copyright (c) 2014 Red Hat, 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.match;
9
10 import static org.opendaylight.yangtools.yang.common.netty.ByteBufUtils.readUint8;
11
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.openflowjava.nx.api.NiciraConstants;
14 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntryDeserializerKey;
15 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey;
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.ExperimenterClass;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxNsi;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.nsi.grouping.NsiValues;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.nsi.grouping.NsiValuesBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.experimenter.id._case.NxExpMatchEntryValue;
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.NsiCaseValue;
24 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.NsiCaseValueBuilder;
25 import org.opendaylight.yangtools.yang.common.Uint32;
26 import org.opendaylight.yangtools.yang.common.Uint8;
27
28 public class NsiCodec extends AbstractExperimenterMatchCodec {
29
30     private static final int VALUE_LENGTH = EncodeConstants.SIZE_OF_BYTE_IN_BYTES;
31     private static final int NXM_FIELD_CODE = 5;
32     public static final MatchEntrySerializerKey<ExperimenterClass, NxmNxNsi> SERIALIZER_KEY =
33             createSerializerKey(
34                     EncodeConstants.OF13_VERSION_ID,
35                     NiciraConstants.NX_NSH_VENDOR_ID,
36                     NxmNxNsi.class);
37     public static final MatchEntryDeserializerKey DESERIALIZER_KEY =
38             createDeserializerKey(
39                     EncodeConstants.OF13_VERSION_ID,
40                     NiciraConstants.NX_NSH_VENDOR_ID,
41                     NXM_FIELD_CODE);
42
43     @Override
44     protected void serializeValue(final NxExpMatchEntryValue value, final boolean hasMask, final ByteBuf outBuffer) {
45         NsiCaseValue nsiCaseValue = (NsiCaseValue) value;
46         NsiValues nsiValues = nsiCaseValue.getNsiValues();
47         outBuffer.writeByte(nsiValues.getNsi().toJava());
48         if (hasMask) {
49             outBuffer.writeByte(nsiValues.getMask().toJava());
50         }
51     }
52
53     @Override
54     protected NxExpMatchEntryValue deserializeValue(final ByteBuf message, final boolean hasMask) {
55         Uint8 value = readUint8(message);
56         Uint8 mask = hasMask ? readUint8(message) : null;
57         NsiValues nsiValues = new NsiValuesBuilder().setNsi(value).setMask(mask).build();
58         return new NsiCaseValueBuilder().setNsiValues(nsiValues).build();
59     }
60
61     @Override
62     public int getNxmFieldCode() {
63         return NXM_FIELD_CODE;
64     }
65
66     @Override
67     protected Uint32 getExperimenterId() {
68         return NiciraConstants.NX_NSH_VENDOR_ID;
69     }
70
71     @Override
72     public int getValueLength() {
73         return VALUE_LENGTH;
74     }
75
76     @Override
77     public Class<? extends MatchField> getNxmField() {
78         return NxmNxNsi.class;
79     }
80 }