Extensibility support (serialization part)
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / AbstractOxmIpv6AddressSerializer.java
diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/match/AbstractOxmIpv6AddressSerializer.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/match/AbstractOxmIpv6AddressSerializer.java
new file mode 100644 (file)
index 0000000..d5491c8
--- /dev/null
@@ -0,0 +1,77 @@
+/*\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 java.util.Arrays;\r
+\r
+import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.Ipv6AddressMatchEntry;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;\r
+\r
+/**\r
+ * Parent for Ipv6 address based match entry serializers\r
+ * @author michal.polkorab\r
+ */\r
+public abstract class AbstractOxmIpv6AddressSerializer extends AbstractOxmMatchEntrySerializer {\r
+\r
+    @Override\r
+    public void serialize(MatchEntries entry, ByteBuf outBuffer) {\r
+        super.serialize(entry, outBuffer);\r
+        String textAddress = entry.getAugmentation(Ipv6AddressMatchEntry.class).getIpv6Address().getValue();\r
+        String[] address;\r
+        if (textAddress.equals("::")) {\r
+            address = new String[EncodeConstants.GROUPS_IN_IPV6_ADDRESS];\r
+            Arrays.fill(address, "0");\r
+        } else {\r
+            address = parseIpv6Address(textAddress.split(":"));\r
+        }\r
+        for (int i = 0; i < address.length; i++) {\r
+            outBuffer.writeShort(Integer.parseInt(address[i], 16));\r
+        }\r
+        writeMask(entry, outBuffer, getValueLength());\r
+    }\r
+\r
+    private static String[] parseIpv6Address(String[] addressGroups) {\r
+        int countEmpty = 0;\r
+        for (int i = 0; i < addressGroups.length; i++) {\r
+            if (addressGroups[i].equals("")){\r
+                countEmpty++;\r
+            }\r
+        }\r
+        String[] ready = new String[EncodeConstants.GROUPS_IN_IPV6_ADDRESS];\r
+        switch (countEmpty) {\r
+        case 0:\r
+            ready = addressGroups;\r
+            break;\r
+        case 1:\r
+            int zerosToBePushed = EncodeConstants.GROUPS_IN_IPV6_ADDRESS - addressGroups.length + 1;\r
+            int index = 0;\r
+            for (int i = 0; i < addressGroups.length; i++) {\r
+                if (addressGroups[i].equals("")) {\r
+                    for (int j = 0; j < zerosToBePushed; j++) {\r
+                        ready[index] = "0";\r
+                        index++;\r
+                    }\r
+                } else {\r
+                    ready[index] = addressGroups[i];\r
+                    index++;\r
+                }\r
+            }\r
+            break;\r
+        case 2:\r
+            Arrays.fill(ready, "0");\r
+            ready[ready.length - 1] = addressGroups[addressGroups.length - 1];\r
+            break;\r
+        default:\r
+            throw new IllegalStateException("Incorrect ipv6 address");\r
+        }\r
+        return ready;\r
+    }\r
+}\r