Fix masked NXM reg length
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / main / java / org / opendaylight / openflowjava / nx / codec / match / AbstractMatchCodec.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, 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
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
15 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmClassBase;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
20
21 public abstract class AbstractMatchCodec implements OFSerializer<MatchEntry>, OFDeserializer<MatchEntry> {
22
23     protected NxmHeader headerWithMask;
24     protected NxmHeader headerWithoutMask;
25
26     protected MatchEntryBuilder deserializeHeader(ByteBuf message) {
27         MatchEntryBuilder builder = new MatchEntryBuilder();
28         builder.setOxmClass(getOxmClass());
29         // skip oxm_class - provided
30         message.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
31         builder.setOxmMatchField(getNxmField());
32         boolean hasMask = (message.readUnsignedByte() & 1) != 0;
33         builder.setHasMask(hasMask);
34         // skip match entry length - not needed
35         message.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
36         return builder;
37     }
38
39     protected void serializeHeader(MatchEntry input, ByteBuf outBuffer) {
40         outBuffer.writeInt(serializeHeaderToLong(input.isHasMask()).intValue());
41     }
42
43     private Long serializeHeaderToLong(boolean hasMask) {
44         if (hasMask) {
45             return getHeaderWithHasMask().toLong();
46         }
47         return getHeaderWithoutHasMask().toLong();
48     }
49
50     public NxmHeader getHeaderWithoutHasMask() {
51         if (headerWithoutMask == null) {
52             headerWithoutMask = new NxmHeader(getOxmClassCode(), getNxmFieldCode(), false, getValueLength());
53         }
54         return headerWithoutMask;
55     }
56
57     public NxmHeader getHeaderWithHasMask() {
58         if (headerWithMask == null) {
59             headerWithMask = new NxmHeader(getOxmClassCode(), getNxmFieldCode(), true, getValueLength());
60         }
61         return headerWithMask;
62     }
63
64     /**
65      * @return numeric representation of nxm_field
66      */
67     public abstract int getNxmFieldCode();
68
69     /**
70      * @return numeric representation of oxm_class
71      */
72     public abstract int getOxmClassCode();
73
74     /**
75      * @return match entry value length
76      */
77     public abstract int getValueLength();
78
79     /**
80      * @return nxm_field class
81      */
82     public abstract Class<? extends MatchField> getNxmField();
83
84     /**
85      * @return oxm_class class
86      */
87     public abstract Class<? extends OxmClassBase> getOxmClass();
88
89 }