71b96ceac5cfe4999ba40413d97be8e75fefce4f
[mdsal.git] / model / ietf / rfc6991-ietf-inet-types / src / main / java / org / opendaylight / yang / gen / v1 / urn / ietf / params / xml / ns / yang / ietf / inet / types / rev130715 / HostBuilder.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
14
15
16 /**
17  *
18  */
19 public class HostBuilder {
20
21     private static final Pattern IPV4_PATTERN = 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}]+)?");
22     private static final Pattern IPV6_PATTERN1 = 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}]+)?");
23     private static final Pattern IPV6_PATTERN2 = Pattern.compile("(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)(%.+)?");
24     private static final Pattern DOMAIN_PATTERN = 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]\\.?)|\\.");
25
26     private HostBuilder() {
27
28     }
29
30     public static Host getDefaultInstance(final String defaultValue) {
31         final Matcher ipv4Matcher = IPV4_PATTERN.matcher(defaultValue);
32         final Matcher ipv6Matcher1 = IPV6_PATTERN1.matcher(defaultValue);
33         final Matcher ipv6Matcher2 = IPV6_PATTERN2.matcher(defaultValue);
34         final Matcher domainMatcher = DOMAIN_PATTERN.matcher(defaultValue);
35
36         List<String> matchers = new ArrayList<>(3);
37         if (ipv6Matcher1.matches() || ipv6Matcher2.matches()) {
38             matchers.add(Ipv6Address.class.getSimpleName());
39         }
40
41         // Ipv4 and Domain Name patterns are not exclusive
42         // Address 127.0.0.1 matches both patterns
43         // This way Ipv4 address is preferred to domain name
44         if (ipv4Matcher.matches()) {
45             matchers.add(Ipv4Address.class.getSimpleName());
46         } else if (domainMatcher.matches()) {
47             matchers.add(DomainName.class.getSimpleName());
48         }
49
50         if (matchers.size() > 1) {
51             throw new IllegalArgumentException("Cannot create Host from " + defaultValue + ". Value is ambigious for "
52                 + matchers);
53         }
54
55         if (ipv4Matcher.matches()) {
56             Ipv4Address ipv4 = new Ipv4Address(defaultValue);
57             IpAddress ipAddress = new IpAddress(ipv4);
58             return new Host(ipAddress);
59         }
60         if (ipv6Matcher1.matches() || ipv6Matcher2.matches()) {
61             Ipv6Address ipv6 = new Ipv6Address(defaultValue);
62             IpAddress ipAddress = new IpAddress(ipv6);
63             return new Host(ipAddress);
64         }
65         if (domainMatcher.matches()) {
66             DomainName domainName = new DomainName(defaultValue);
67             return new Host(domainName);
68         }
69         throw new IllegalArgumentException("Cannot create Host from " + defaultValue);
70     }
71
72 }