Merge "Introduce HashCodeBuilder"
[yangtools.git] / 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
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.regex.Pattern;
13
14 /**
15 **/
16 public class HostBuilder {
17     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}]+)?");
18     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}]+)?");
19     private static final Pattern ipv6Pattern2 = Pattern.compile("(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)(%.+)?");
20     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]\\.?)|\\.");
21
22     public static Host getDefaultInstance(String defaultValue) {
23
24         List<String> matchers = new ArrayList<>();
25         if (ipv6Pattern1.matcher(defaultValue).matches() || ipv6Pattern2.matcher(defaultValue).matches()) {
26             matchers.add(Ipv6Address.class.getSimpleName());
27         }
28
29         // Ipv4 and Domain Name patterns are not exclusive
30         // Address 127.0.0.1 matches both patterns
31         // This way Ipv4 address is preferred to domain name
32         if (ipv4Pattern.matcher(defaultValue).matches()) {
33             matchers.add(Ipv4Address.class.getSimpleName());
34         } else if (domainPattern.matcher(defaultValue).matches()) {
35             matchers.add(DomainName.class.getSimpleName());
36         }
37
38         if (matchers.size() > 1) {
39             throw new IllegalArgumentException("Cannot create Host from " + defaultValue + ". Value is ambigious for "
40                     + matchers);
41         }
42
43         if (ipv4Pattern.matcher(defaultValue).matches()) {
44             Ipv4Address ipv4 = new Ipv4Address(defaultValue);
45             IpAddress ipAddress = new IpAddress(ipv4);
46             return new Host(ipAddress);
47         }
48         if (ipv6Pattern1.matcher(defaultValue).matches() || ipv6Pattern2.matcher(defaultValue).matches()) {
49             Ipv6Address ipv6 = new Ipv6Address(defaultValue);
50             IpAddress ipAddress = new IpAddress(ipv6);
51             return new Host(ipAddress);
52         }
53         if (domainPattern.matcher(defaultValue).matches()) {
54             DomainName domainName = new DomainName(defaultValue);
55             return new Host(domainName);
56         }
57         throw new IllegalArgumentException("Cannot create Host from " + defaultValue);
58     }
59
60 }