Bug 2866 - Fixed ipv6 address parsing
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / AbstractOxmIpv6AddressSerializer.java
index 9fa730bdc95670f889d77f4f3181c4c0e38c2bec..b84d81cfd8ee28c4377db3d906016ee9b3322a6f 100644 (file)
@@ -9,14 +9,9 @@ package org.opendaylight.openflowjava.protocol.impl.serialization.match;
 
 import io.netty.buffer.ByteBuf;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
 import org.opendaylight.openflowjava.util.ByteBufUtils;
 
-import com.google.common.collect.Lists;
+import com.google.common.net.InetAddresses;
 
 /**
  * Parent for Ipv6 address based match entry serializers
@@ -25,53 +20,12 @@ import com.google.common.collect.Lists;
 public abstract class AbstractOxmIpv6AddressSerializer extends AbstractOxmMatchEntrySerializer {
 
     protected void writeIpv6Address(String textAddress, final ByteBuf outBuffer) {
-        List<String> address;
-        if (textAddress.equals("::")) {
-            String[] tmp = new String[EncodeConstants.GROUPS_IN_IPV6_ADDRESS];
-            Arrays.fill(tmp, "0");
-            address = Arrays.asList(tmp);
+        if (InetAddresses.isInetAddress(textAddress)) {
+            byte[] binaryAddress = InetAddresses.forString(textAddress).getAddress();
+            outBuffer.writeBytes(binaryAddress);
         } else {
-            address = parseIpv6Address(Lists.newArrayList(ByteBufUtils.COLON_SPLITTER.split(textAddress)));
-        }
-        for (String group : address) {
-            outBuffer.writeShort(Integer.parseInt(group, 16));
+            throw new IllegalArgumentException("Invalid ipv6 address received: " + textAddress);
         }
     }
 
-    private static List<String> parseIpv6Address(final List<String> addressGroups) {
-        int countEmpty = 0;
-        for (String group : addressGroups) {
-            if (group.equals("")) {
-                countEmpty++;
-            }
-        }
-        List<String> ready = new ArrayList<>(EncodeConstants.GROUPS_IN_IPV6_ADDRESS);
-        switch (countEmpty) {
-        case 0:
-            ready = addressGroups;
-            break;
-        case 1:
-            int zerosToBePushed = EncodeConstants.GROUPS_IN_IPV6_ADDRESS - addressGroups.size() + 1;
-            for (String group : addressGroups) {
-                if (group.equals("")) {
-                    for (int j = 0; j < zerosToBePushed; j++) {
-                        ready.add("0");
-                    }
-                } else {
-                    ready.add(group);
-                }
-            }
-            break;
-        case 2:
-            String[] tmp = new String[EncodeConstants.GROUPS_IN_IPV6_ADDRESS];
-            Arrays.fill(tmp, "0");
-            tmp[EncodeConstants.GROUPS_IN_IPV6_ADDRESS - 1] =
-                    addressGroups.get(addressGroups.size() - 1);
-            ready = Arrays.asList(tmp);
-            break;
-        default:
-            throw new IllegalStateException("Incorrect ipv6 address");
-        }
-        return ready;
-    }
 }