Bug 2756 - Match model update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / AbstractOxmMatchEntrySerializer.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. 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 package org.opendaylight.openflowjava.protocol.impl.serialization.match;
9
10 import io.netty.buffer.ByteBuf;
11
12 import org.opendaylight.openflowjava.protocol.api.extensibility.HeaderSerializer;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
15
16 /**
17  * Parent for all match entry serializers
18  * @author michal.polkorab
19  */
20 public abstract class AbstractOxmMatchEntrySerializer
21     implements OFSerializer<MatchEntry>, HeaderSerializer<MatchEntry>{
22
23     @Override
24     public void serialize(MatchEntry entry, ByteBuf outBuffer) {
25         serializeHeader(entry, outBuffer);
26     }
27
28     @Override
29     public void serializeHeader(MatchEntry entry, ByteBuf outBuffer) {
30         outBuffer.writeShort(getOxmClassCode());
31         writeOxmFieldAndLength(outBuffer, getOxmFieldCode(), entry.isHasMask(),
32                 getValueLength());
33     }
34
35     protected static void writeMask(byte[] mask, ByteBuf out, int length) {
36         if (mask != null && mask.length != length) {
37             throw new IllegalArgumentException("incorrect length of mask: "+
38                     mask.length + ", expected: " + length);
39         }
40         out.writeBytes(mask);
41     }
42
43     protected static void writeOxmFieldAndLength(ByteBuf out, int fieldValue, boolean hasMask, int lengthArg) {
44         int fieldAndMask = fieldValue << 1;
45         int length = lengthArg;
46         if (hasMask) {
47             fieldAndMask |= 1;
48             length *= 2;
49         }
50         out.writeByte(fieldAndMask);
51         out.writeByte(length);
52     }
53
54     /**
55      * @return numeric representation of oxm_field
56      */
57     protected abstract int getOxmFieldCode();
58
59     /**
60      * @return numeric representation of oxm_class
61      */
62     protected abstract int getOxmClassCode();
63
64     /**
65      * @return match entry value length (without mask length)
66      */
67     protected abstract int getValueLength();
68
69 }