BUG-944 Fix HostBuilder class conflict for domain/ipv4 address 71/8171/3
authorMaros Marsalek <mmarsale@cisco.com>
Fri, 20 Jun 2014 11:11:33 +0000 (13:11 +0200)
committerMaros Marsalek <mmarsale@cisco.com>
Fri, 20 Jun 2014 12:10:09 +0000 (14:10 +0200)
Ipv4 addresses were matched by ipv4 as well as domain-name pattern in HostBuilder class, which caused a conflict.

Now ipv4 pattern is prefered to domain-name.

Change-Id: Ia449d247c04747d6ca50856f92d3aa77b581e60f
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
model/ietf/ietf-inet-types/pom.xml
model/ietf/ietf-inet-types/src/main/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/inet/types/rev100924/HostBuilder.java
model/ietf/ietf-inet-types/src/test/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/inet/types/rev100924/HostBuilderTest.java [new file with mode: 0644]

index 18ceb60480ef09a01141856c270d430ea2533f82..768948e5c3fb9e77497df0b3b546f4f07ad0f1ee 100644 (file)
     <name>${project.artifactId}</name>
     <description>${project.artifactId}</description>
 
-
+    <dependencies>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
 
     <build>
         <plugins>
index 3a03087e1a037962f70d0d346b603d88dbc51248..ee92e8051efeb5eb453f2bcd004cc9176698e45f 100644 (file)
@@ -22,15 +22,19 @@ public class HostBuilder {
     public static Host getDefaultInstance(String defaultValue) {
 
         List<String> matchers = new ArrayList<>();
-        if (ipv4Pattern.matcher(defaultValue).matches()) {
-            matchers.add(Ipv4Address.class.getSimpleName());
-        }
-        if (ipv6Pattern1.matcher(defaultValue).matches() && ipv6Pattern2.matcher(defaultValue).matches()) {
+        if (ipv6Pattern1.matcher(defaultValue).matches() || ipv6Pattern2.matcher(defaultValue).matches()) {
             matchers.add(Ipv6Address.class.getSimpleName());
         }
-        if (domainPattern.matcher(defaultValue).matches()) {
+
+        // Ipv4 and Domain Name patterns are not exclusive
+        // Address 127.0.0.1 matches both patterns
+        // This way Ipv4 address is preferred to domain name
+        if (ipv4Pattern.matcher(defaultValue).matches()) {
+            matchers.add(Ipv4Address.class.getSimpleName());
+        } else if (domainPattern.matcher(defaultValue).matches()) {
             matchers.add(DomainName.class.getSimpleName());
         }
+
         if (matchers.size() > 1) {
             throw new IllegalArgumentException("Cannot create Host from " + defaultValue + ". Value is ambigious for "
                     + matchers);
@@ -41,7 +45,7 @@ public class HostBuilder {
             IpAddress ipAddress = new IpAddress(ipv4);
             return new Host(ipAddress);
         }
-        if (ipv6Pattern1.matcher(defaultValue).matches() && ipv6Pattern2.matcher(defaultValue).matches()) {
+        if (ipv6Pattern1.matcher(defaultValue).matches() || ipv6Pattern2.matcher(defaultValue).matches()) {
             Ipv6Address ipv6 = new Ipv6Address(defaultValue);
             IpAddress ipAddress = new IpAddress(ipv6);
             return new Host(ipAddress);
diff --git a/model/ietf/ietf-inet-types/src/test/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/inet/types/rev100924/HostBuilderTest.java b/model/ietf/ietf-inet-types/src/test/java/org/opendaylight/yang/gen/v1/urn/ietf/params/xml/ns/yang/ietf/inet/types/rev100924/HostBuilderTest.java
new file mode 100644 (file)
index 0000000..fa316d1
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924;
+
+import junit.framework.Assert;
+import org.junit.Test;
+
+public class HostBuilderTest {
+
+    @Test
+    public void testGetDefaultInstanceIpv4() throws Exception {
+        Host host = HostBuilder.getDefaultInstance("127.0.0.1");
+        Assert.assertEquals(new Host(new IpAddress(new Ipv4Address("127.0.0.1"))), host);
+    }
+
+    @Test
+    public void testGetDefaultInstanceIpv6() throws Exception {
+        testIpv6("2001:db8:8s5a3:0:0:8a2e:370:7334");
+        testIpv6("2001:db8:85a3::8a2e:370:7334");
+    }
+
+    private void testIpv6(String ivp6string) {
+        Host host = HostBuilder.getDefaultInstance(ivp6string);
+        Assert.assertEquals(new Host(new IpAddress(new Ipv6Address(ivp6string))), host);
+    }
+
+    @Test
+    public void testGetDefaultInstanceDomain() throws Exception {
+        Host host = HostBuilder.getDefaultInstance("localhost");
+        Assert.assertEquals(new Host(new DomainName("localhost")), host);
+    }
+}
\ No newline at end of file