BUG-2771: String.split() replaced by Guava's splitter in nicira-extensions 42/39742/1
authorShuva Kar <shuva.jyoti.kar@ericsson.com>
Thu, 2 Jun 2016 04:28:37 +0000 (09:58 +0530)
committerShuva Kar <shuva.jyoti.kar@ericsson.com>
Thu, 2 Jun 2016 04:28:37 +0000 (09:58 +0530)
Added tests, reorganised packages

Change-Id: I4942c5a60ef46f3aa07ec51f9760f253d88f1629
Signed-off-by: Shuva Kar <shuva.jyoti.kar@ericsson.com>
extension/openflowplugin-extension-nicira/src/main/java/org/opendaylight/openflowplugin/extension/vendor/nicira/convertor/IpConverter.java
extension/openflowplugin-extension-nicira/src/test/java/org/opendaylight/openflowplugin/extension/vendor/nicira/convertor/IpConverterTest.java [new file with mode: 0644]
extension/openflowplugin-extension-nicira/src/test/java/org/opendaylight/openflowplugin/extension/vendor/nicira/convertor/MatchUtilTest.java [moved from extension/openflowplugin-extension-nicira/src/test/java/org/opendaylight/openflowplugin/extension/vendor/nicira/convertor/match/MatchUtilTest.java with 98% similarity]

index 2054f24bc7089cf16732e1f128d81e68d60b7e43..de26eb09c47e087ca557690dd620daa42b522922 100644 (file)
@@ -7,8 +7,13 @@
  */
 package org.opendaylight.openflowplugin.extension.vendor.nicira.convertor;
 
+import com.google.common.base.Splitter;
+import com.google.common.collect.Lists;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
 
+import java.util.List;
+import java.util.ListIterator;
+
 /**
  * @author msunal
  *
@@ -16,11 +21,19 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.
 public final class IpConverter {
 
     public static long Ipv4AddressToLong(Ipv4Address ipv4Address) {
-        String ipAddress = ipv4Address.getValue();
-        long result = 0;
-        String[] atoms = ipAddress.split("\\.");
-        for (int i = 3; i >= 0; i--) {
-            result |= (Long.parseLong(atoms[3 - i]) << (i * 8));
+        long result = 0 ;
+        Iterable<String> splitted = Splitter.on('.')
+                .trimResults()
+                .omitEmptyStrings()
+                .split(ipv4Address.getValue());
+
+        List<String> splittedAddress = Lists.newArrayList(splitted.iterator());
+        int maxIndex = splittedAddress.size() - 1;
+        ListIterator<String> listIter = splittedAddress.listIterator();
+        while(listIter.hasNext()) {
+            String current = listIter.next();
+            int i = splittedAddress.indexOf(current);
+            result |= (Long.parseLong(current) << ((maxIndex-i) * 8));
         }
         return result & 0xFFFFFFFF;
     }
diff --git a/extension/openflowplugin-extension-nicira/src/test/java/org/opendaylight/openflowplugin/extension/vendor/nicira/convertor/IpConverterTest.java b/extension/openflowplugin-extension-nicira/src/test/java/org/opendaylight/openflowplugin/extension/vendor/nicira/convertor/IpConverterTest.java
new file mode 100644 (file)
index 0000000..f8e27ff
--- /dev/null
@@ -0,0 +1,24 @@
+package org.opendaylight.openflowplugin.extension.vendor.nicira.convertor;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+
+public class IpConverterTest {
+
+    @Test
+    public void testIpv4AddressToLong() {
+        Ipv4Address ipAddress = new Ipv4Address("192.168.1.2");
+        long address = 0xc0a80102L;
+        assertEquals(address, IpConverter.Ipv4AddressToLong(ipAddress));
+    }
+
+    @Test
+    public void testIpv4AddressToLong2() {
+        Ipv4Address ipAddress = new Ipv4Address("10.168.1.2");
+        long address = 0x0aa80102L;
+        assertEquals(address, IpConverter.Ipv4AddressToLong(ipAddress));
+    }
+
+}