8b84a88c26ef513e7b2bd6bda856404a14a2da1f
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / AbstractOxmMatchEntrySerializer.java
1 /*\r
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.openflowjava.protocol.impl.serialization.match;\r
9 \r
10 import io.netty.buffer.ByteBuf;\r
11 \r
12 import org.opendaylight.openflowjava.protocol.api.extensibility.HeaderSerializer;\r
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;\r
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;\r
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;\r
16 \r
17 /**\r
18  * Parent for all match entry serializers\r
19  * @author michal.polkorab\r
20  */\r
21 public abstract class AbstractOxmMatchEntrySerializer\r
22     implements OFSerializer<MatchEntries>, HeaderSerializer<MatchEntries>{\r
23 \r
24     @Override\r
25     public void serialize(MatchEntries entry, ByteBuf outBuffer) {\r
26         serializeHeader(entry, outBuffer);\r
27     }\r
28 \r
29     @Override\r
30     public void serializeHeader(MatchEntries entry, ByteBuf outBuffer) {\r
31         outBuffer.writeShort(getOxmClassCode());\r
32         writeOxmFieldAndLength(outBuffer, getOxmFieldCode(), entry.isHasMask(),\r
33                 getValueLength());\r
34     }\r
35 \r
36     protected static void writeMask(MatchEntries entry, ByteBuf out, int length) {\r
37         if (entry.isHasMask()) {\r
38             byte[] mask = entry.getAugmentation(MaskMatchEntry.class).getMask();\r
39             if (mask != null && mask.length != length) {\r
40                 throw new IllegalArgumentException("incorrect length of mask: "+\r
41                         mask.length + ", expected: " + length);\r
42             }\r
43             out.writeBytes(mask);\r
44         }\r
45     }\r
46 \r
47     protected static void writeOxmFieldAndLength(ByteBuf out, int fieldValue, boolean hasMask, int lengthArg) {\r
48         int fieldAndMask = fieldValue << 1;\r
49         int length = lengthArg;\r
50         if (hasMask) {\r
51             fieldAndMask |= 1;\r
52             length *= 2;\r
53         }\r
54         out.writeByte(fieldAndMask);\r
55         out.writeByte(length);\r
56     }\r
57 \r
58     /**\r
59      * @return numeric representation of oxm_field\r
60      */\r
61     protected abstract int getOxmFieldCode();\r
62 \r
63     /**\r
64      * @return numeric representation of oxm_class\r
65      */\r
66     protected abstract int getOxmClassCode();\r
67 \r
68     /**\r
69      * @return match entry value length (without mask length)\r
70      */\r
71     protected abstract int getValueLength();\r
72 \r
73 }\r