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