1 package org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924;
3 import java.util.ArrayList;
5 import java.util.regex.Pattern;
9 public class HostBuilder {
10 private static final Pattern ipv4Pattern = Pattern.compile("(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(%[\\p{N}\\p{L}]+)?");
11 private static final Pattern ipv6Pattern1 = Pattern.compile("((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))(%[\\p{N}\\p{L}]+)?");
12 private static final Pattern ipv6Pattern2 = Pattern.compile("(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)(%.+)?");
13 private static final Pattern domainPattern = Pattern.compile("((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)|\\.");
15 public static Host getDefaultInstance(String defaultValue) {
17 List<String> matchers = new ArrayList<>();
18 if (ipv4Pattern.matcher(defaultValue).matches()) {
19 matchers.add(Ipv4Address.class.getSimpleName());
21 if (ipv6Pattern1.matcher(defaultValue).matches() && ipv6Pattern2.matcher(defaultValue).matches()) {
22 matchers.add(Ipv6Address.class.getSimpleName());
24 if (domainPattern.matcher(defaultValue).matches()) {
25 matchers.add(DomainName.class.getSimpleName());
27 if (matchers.size() > 1) {
28 throw new IllegalArgumentException("Cannot create Host from " + defaultValue + ". Value is ambigious for "
32 if (ipv4Pattern.matcher(defaultValue).matches()) {
33 Ipv4Address ipv4 = new Ipv4Address(defaultValue);
34 IpAddress ipAddress = new IpAddress(ipv4);
35 return new Host(ipAddress);
37 if (ipv6Pattern1.matcher(defaultValue).matches() && ipv6Pattern2.matcher(defaultValue).matches()) {
38 Ipv6Address ipv6 = new Ipv6Address(defaultValue);
39 IpAddress ipAddress = new IpAddress(ipv6);
40 return new Host(ipAddress);
42 if (domainPattern.matcher(defaultValue).matches()) {
43 DomainName domainName = new DomainName(defaultValue);
44 return new Host(domainName);
46 throw new IllegalArgumentException("Cannot create Host from " + defaultValue);