2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924;
10 import java.util.ArrayList;
11 import java.util.List;
15 public class IpPrefixBuilder {
17 public static IpPrefix getDefaultInstance(String defaultValue) {
18 String ipv4Pattern = "(([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])/(([0-9])|([1-2][0-9])|(3[0-2]))";
19 String ipv6Pattern1 = "((:|[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])))(/(([0-9])|([0-9]{2})|(1[0-1][0-9])|(12[0-8])))";
20 String ipv6Pattern2 = "(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)(/.+)";
22 List<String> matchers = new ArrayList<>();
23 if (defaultValue.matches(ipv4Pattern)) {
24 matchers.add(Ipv4Address.class.getSimpleName());
26 if (defaultValue.matches(ipv6Pattern1) && defaultValue.matches(ipv6Pattern2)) {
27 matchers.add(Ipv6Address.class.getSimpleName());
29 if (matchers.size() > 1) {
30 throw new IllegalArgumentException("Cannot create IpPrefix from " + defaultValue
31 + ". Value is ambigious for " + matchers);
34 if (defaultValue.matches(ipv4Pattern)) {
35 Ipv4Prefix ipv4 = new Ipv4Prefix(defaultValue);
36 return new IpPrefix(ipv4);
38 if (defaultValue.matches(ipv6Pattern1) && defaultValue.matches(ipv6Pattern2)) {
39 Ipv6Prefix ipv6 = new Ipv6Prefix(defaultValue);
40 return new IpPrefix(ipv6);
42 throw new IllegalArgumentException("Cannot create IpPrefix from " + defaultValue);