Extensibility support (serialization part)
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / AbstractOxmMatchEntrySerializer.java
diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/match/AbstractOxmMatchEntrySerializer.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/match/AbstractOxmMatchEntrySerializer.java
new file mode 100644 (file)
index 0000000..8b84a88
--- /dev/null
@@ -0,0 +1,73 @@
+/*\r
+ * Copyright (c) 2013 Pantheon Technologies s.r.o. and others.  All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+package org.opendaylight.openflowjava.protocol.impl.serialization.match;\r
+\r
+import io.netty.buffer.ByteBuf;\r
+\r
+import org.opendaylight.openflowjava.protocol.api.extensibility.HeaderSerializer;\r
+import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;\r
+\r
+/**\r
+ * Parent for all match entry serializers\r
+ * @author michal.polkorab\r
+ */\r
+public abstract class AbstractOxmMatchEntrySerializer\r
+    implements OFSerializer<MatchEntries>, HeaderSerializer<MatchEntries>{\r
+\r
+    @Override\r
+    public void serialize(MatchEntries entry, ByteBuf outBuffer) {\r
+        serializeHeader(entry, outBuffer);\r
+    }\r
+\r
+    @Override\r
+    public void serializeHeader(MatchEntries entry, ByteBuf outBuffer) {\r
+        outBuffer.writeShort(getOxmClassCode());\r
+        writeOxmFieldAndLength(outBuffer, getOxmFieldCode(), entry.isHasMask(),\r
+                getValueLength());\r
+    }\r
+\r
+    protected static void writeMask(MatchEntries entry, ByteBuf out, int length) {\r
+        if (entry.isHasMask()) {\r
+            byte[] mask = entry.getAugmentation(MaskMatchEntry.class).getMask();\r
+            if (mask != null && mask.length != length) {\r
+                throw new IllegalArgumentException("incorrect length of mask: "+\r
+                        mask.length + ", expected: " + length);\r
+            }\r
+            out.writeBytes(mask);\r
+        }\r
+    }\r
+\r
+    protected static void writeOxmFieldAndLength(ByteBuf out, int fieldValue, boolean hasMask, int lengthArg) {\r
+        int fieldAndMask = fieldValue << 1;\r
+        int length = lengthArg;\r
+        if (hasMask) {\r
+            fieldAndMask |= 1;\r
+            length *= 2;\r
+        }\r
+        out.writeByte(fieldAndMask);\r
+        out.writeByte(length);\r
+    }\r
+\r
+    /**\r
+     * @return numeric representation of oxm_field\r
+     */\r
+    protected abstract int getOxmFieldCode();\r
+\r
+    /**\r
+     * @return numeric representation of oxm_class\r
+     */\r
+    protected abstract int getOxmClassCode();\r
+\r
+    /**\r
+     * @return match entry value length (without mask length)\r
+     */\r
+    protected abstract int getValueLength();\r
+\r
+}\r