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>
<name>${project.artifactId}</name>
<description>${project.artifactId}</description>
-
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
<build>
<plugins>
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);
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);
--- /dev/null
+/*
+ * 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