Merge "Bug 6050 - TcpSrcCodec is not maskable"
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / main / java / org / opendaylight / openflowjava / nx / codec / match / TcpSrcCodec.java
1 /*
2  * Copyright (c) 2015 Hewlett-Packard Enterprise 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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm0Class;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmClassBase;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
18
19 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntryDeserializerKey;
20 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey;
21 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
22 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmOfTcpSrc;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.of.match.tcp.src.grouping.TcpSrcValuesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.TcpSrcCaseValue;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.TcpSrcCaseValueBuilder;
27
28 /**
29  * @author Aswin Suryanarayanan.
30  */
31
32 public class TcpSrcCodec extends AbstractMatchCodec {
33
34     private static final int VALUE_LENGTH = 4;
35     private static final int NXM_FIELD_CODE = 9;
36     public static final MatchEntrySerializerKey<Nxm0Class, NxmOfTcpSrc> SERIALIZER_KEY = new MatchEntrySerializerKey<>(
37             EncodeConstants.OF13_VERSION_ID, Nxm0Class.class, NxmOfTcpSrc.class);
38     public static final MatchEntryDeserializerKey DESERIALIZER_KEY = new MatchEntryDeserializerKey(
39             EncodeConstants.OF13_VERSION_ID, OxmMatchConstants.NXM_0_CLASS, NXM_FIELD_CODE);
40
41     @Override
42     public void serialize(MatchEntry input, ByteBuf outBuffer) {
43         serializeHeader(input, outBuffer);
44         TcpSrcCaseValue tcpSrcCase = ((TcpSrcCaseValue) input.getMatchEntryValue());
45         outBuffer.writeShort(tcpSrcCase.getTcpSrcValues().getPort().getValue());
46     }
47
48     @Override
49     public MatchEntry deserialize(ByteBuf message) {
50         MatchEntryBuilder matchEntryBuilder = deserializeHeader(message);
51         matchEntryBuilder.setHasMask(false);
52         int portNo = message.readUnsignedShort();
53         TcpSrcCaseValueBuilder caseBuilder = new TcpSrcCaseValueBuilder();
54         TcpSrcValuesBuilder tcpSrcValuesBuilder = new TcpSrcValuesBuilder();
55         tcpSrcValuesBuilder.setPort(new PortNumber(portNo));
56         caseBuilder.setTcpSrcValues(tcpSrcValuesBuilder.build());
57         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
58         return matchEntryBuilder.build();
59     }
60
61     @Override
62     public int getNxmFieldCode() {
63         return NXM_FIELD_CODE;
64     }
65
66     @Override
67     public int getOxmClassCode() {
68         return OxmMatchConstants.NXM_0_CLASS;
69     }
70
71     @Override
72     public int getValueLength() {
73         return VALUE_LENGTH;
74     }
75
76     @Override
77     public Class<? extends MatchField> getNxmField() {
78         return NxmOfTcpSrc.class;
79     }
80
81     @Override
82     public Class<? extends OxmClassBase> getOxmClass() {
83         return Nxm0Class.class;
84     }
85
86 }