Merge changes from topic 'BUG-3774'
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / main / java / org / opendaylight / openflowjava / nx / codec / match / UdpDstCodec.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
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm0Class;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmClassBase;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
19
20 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntryDeserializerKey;
21 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey;
22 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
23 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
24
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmOfUdpDst;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.of.match.udp.dst.grouping.UdpDstValuesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.UdpDstCaseValue;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.UdpDstCaseValueBuilder;
29
30 /**
31  * @author Aswin Suryanarayanan.
32  */
33
34 public class UdpDstCodec extends AbstractMatchCodec {
35
36     private static final int VALUE_LENGTH = 4;
37     private static final int NXM_FIELD_CODE = 12;
38     public static final MatchEntrySerializerKey<Nxm0Class, NxmOfUdpDst> SERIALIZER_KEY = new MatchEntrySerializerKey<>(
39             EncodeConstants.OF13_VERSION_ID, Nxm0Class.class, NxmOfUdpDst.class);
40     public static final MatchEntryDeserializerKey DESERIALIZER_KEY = new MatchEntryDeserializerKey(
41             EncodeConstants.OF13_VERSION_ID, OxmMatchConstants.NXM_0_CLASS, NXM_FIELD_CODE);
42
43     @Override
44     public void serialize(MatchEntry input, ByteBuf outBuffer) {
45         serializeHeader(input, outBuffer);
46         UdpDstCaseValue udpDstCase = ((UdpDstCaseValue) input.getMatchEntryValue());
47         outBuffer.writeShort(udpDstCase.getUdpDstValues().getPort().getValue());
48         outBuffer.writeShort(udpDstCase.getUdpDstValues().getMask());
49     }
50
51     @Override
52     public MatchEntry deserialize(ByteBuf message) {
53         MatchEntryBuilder matchEntryBuilder = deserializeHeader(message);
54         matchEntryBuilder.setHasMask(true);
55         int portNo = message.readUnsignedShort();
56         int mask = message.readUnsignedShort();
57         message.readBytes(mask);
58         UdpDstCaseValueBuilder caseBuilder = new UdpDstCaseValueBuilder();
59         UdpDstValuesBuilder udpDstValuesBuilder = new UdpDstValuesBuilder();
60         udpDstValuesBuilder.setPort(new PortNumber(portNo));
61         udpDstValuesBuilder.setMask(portNo);
62         caseBuilder.setUdpDstValues(udpDstValuesBuilder.build());
63         matchEntryBuilder.setMatchEntryValue(caseBuilder.build());
64         return matchEntryBuilder.build();
65     }
66
67     @Override
68     public int getNxmFieldCode() {
69         return NXM_FIELD_CODE;
70     }
71
72     @Override
73     public int getOxmClassCode() {
74         return OxmMatchConstants.NXM_0_CLASS;
75     }
76
77     @Override
78     public int getValueLength() {
79         return VALUE_LENGTH;
80     }
81
82     @Override
83     public Class<? extends MatchField> getNxmField() {
84         return NxmOfUdpDst.class;
85     }
86
87     @Override
88     public Class<? extends OxmClassBase> getOxmClass() {
89         return Nxm0Class.class;
90     }
91
92 }