b2f3f449e19745b0f5e79c1212aed9ab5ee29f2f
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / main / java / org / opendaylight / openflowjava / nx / codec / match / TunIdCodec.java
1 package org.opendaylight.openflowjava.nx.codec.match;
2
3 import io.netty.buffer.ByteBuf;
4 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntryDeserializerKey;
5 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey;
6 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
7 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
8 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
9 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm1Class;
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmClassBase;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.TunnelId;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.TunnelIdCase;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.TunnelIdCaseBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.tunnel.id._case.TunnelIdBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxTunId;
19 import java.math.BigInteger;
20
21 public class TunIdCodec extends AbstractMatchCodec {
22
23     private static final int VALUE_LENGTH = 8;
24     private static final int NXM_FIELD_CODE = 16;
25     public static final MatchEntrySerializerKey<Nxm1Class, NxmNxTunId> SERIALIZER_KEY = new MatchEntrySerializerKey<>(
26             EncodeConstants.OF13_VERSION_ID, Nxm1Class.class, NxmNxTunId.class);
27     public static final MatchEntryDeserializerKey DESERIALIZER_KEY = new MatchEntryDeserializerKey(
28             EncodeConstants.OF13_VERSION_ID, OxmMatchConstants.NXM_1_CLASS, NXM_FIELD_CODE);
29
30     @Override
31     public void serialize(MatchEntry input, ByteBuf outBuffer) {
32         serializeHeader(input, outBuffer);
33         TunnelIdCase value = ((TunnelIdCase) input.getMatchEntryValue());
34         outBuffer.writeBytes(value.getTunnelId().getTunnelId());
35         byte[] mask = value.getTunnelId().getTunnelId();
36         if (null != mask) {
37             outBuffer.writeBytes(mask);
38         }
39     }
40
41     @Override
42     public MatchEntry deserialize(ByteBuf message) {
43         MatchEntryBuilder matchEntriesBuilder = deserializeHeader(message);
44         TunnelIdCaseBuilder tunnelIdCaseBuilder = new TunnelIdCaseBuilder();
45         TunnelIdBuilder tunnelIdBuilder = new TunnelIdBuilder();
46         tunnelIdBuilder.setTunnelId(BigInteger.valueOf(message.readLong()).toByteArray());
47         tunnelIdCaseBuilder.setTunnelId(tunnelIdBuilder.build());
48         matchEntriesBuilder.setOxmClass(OpenflowBasicClass.class);
49         matchEntriesBuilder.setOxmMatchField(TunnelId.class);
50         matchEntriesBuilder.setMatchEntryValue(tunnelIdCaseBuilder.build());
51         return matchEntriesBuilder.build();
52     }
53
54     @Override
55     public int getNxmFieldCode() {
56         return NXM_FIELD_CODE;
57     }
58
59     @Override
60     public int getOxmClassCode() {
61         return OxmMatchConstants.NXM_1_CLASS;
62     }
63
64     @Override
65     public int getValueLength() {
66         return VALUE_LENGTH;
67     }
68
69     @Override
70     public Class<? extends MatchField> getNxmField() {
71         return NxmNxTunId.class;
72     }
73
74     @Override
75     public Class<? extends OxmClassBase> getOxmClass() {
76         return Nxm1Class.class;
77     }
78
79 }