Add missing copyright headers
[mdsal.git] / model / ietf / ietf-inet-types / src / main / java / org / opendaylight / yang / gen / v1 / urn / ietf / params / xml / ns / yang / ietf / inet / types / rev100924 / IpAddressBuilder.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
13 /**
14 **/
15 public class IpAddressBuilder {
16
17     public static IpAddress 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])(%[\\p{N}\\p{L}]+)?";
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])))(%[\\p{N}\\p{L}]+)?";
20         String ipv6Pattern2 = "(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)(%.+)?";
21
22         List<String> matchers = new ArrayList<>();
23         if (defaultValue.matches(ipv4Pattern)) {
24             matchers.add(Ipv4Address.class.getSimpleName());
25         }
26         if (defaultValue.matches(ipv6Pattern1) && defaultValue.matches(ipv6Pattern2)) {
27             matchers.add(Ipv6Address.class.getSimpleName());
28         }
29         if (matchers.size() > 1) {
30             throw new IllegalArgumentException("Cannot create IpAddress from " + defaultValue
31                     + ". Value is ambigious for " + matchers);
32         }
33
34         if (defaultValue.matches(ipv4Pattern)) {
35             Ipv4Address ipv4 = new Ipv4Address(defaultValue);
36             return new IpAddress(ipv4);
37         }
38         if (defaultValue.matches(ipv6Pattern1) && defaultValue.matches(ipv6Pattern2)) {
39             Ipv6Address ipv6 = new Ipv6Address(defaultValue);
40             return new IpAddress(ipv6);
41         }
42         throw new IllegalArgumentException("Cannot create IpAddress from " + defaultValue);
43     }
44
45 }