From 608ba65d9884ee3f8574e722a85901371108a4c1 Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Fri, 20 Jun 2014 13:11:33 +0200 Subject: [PATCH] BUG-944 Fix HostBuilder class conflict for domain/ipv4 address 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 --- model/ietf/ietf-inet-types/pom.xml | 8 +++- .../inet/types/rev100924/HostBuilder.java | 16 +++++--- .../inet/types/rev100924/HostBuilderTest.java | 38 +++++++++++++++++++ 3 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 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 diff --git a/model/ietf/ietf-inet-types/pom.xml b/model/ietf/ietf-inet-types/pom.xml index 18ceb60480..768948e5c3 100644 --- a/model/ietf/ietf-inet-types/pom.xml +++ b/model/ietf/ietf-inet-types/pom.xml @@ -21,7 +21,13 @@ ${project.artifactId} ${project.artifactId} - + + + junit + junit + test + + diff --git a/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 b/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 index 3a03087e1a..ee92e8051e 100644 --- a/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 +++ b/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 @@ -22,15 +22,19 @@ public class HostBuilder { public static Host getDefaultInstance(String defaultValue) { List 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 index 0000000000..fa316d174a --- /dev/null +++ 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 @@ -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 -- 2.36.6