50b843eb90f30aa3831c8b950506d7b032c8e3b3
[mdsal.git] / model / ietf / ietf-type-util / src / main / java / org / opendaylight / mdsal / model / ietf / util / Ipv4Utils.java
1 /*
2  * Copyright (c) 2016 Pantheon Technologies s.r.o. 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.mdsal.model.ietf.util;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.annotations.Beta;
13 import org.eclipse.jdt.annotation.NonNull;
14
15 /**
16  * IPv4 address parsing for ietf-inet-types ipv4-address. This is an internal implementation class, not meant to be
17  * exposed in any shape or form to the outside world, as the code relies on the fact that the strings presented to it
18  * have been previously validated to conform to the regular expressions defined in the YANG model.
19  */
20 @Beta
21 public final class Ipv4Utils {
22     static final int INET4_LENGTH = 4;
23
24     private Ipv4Utils() {
25         // Hidden on purpose
26     }
27
28     static void fillIpv4Bytes(final byte @NonNull[] bytes, final int byteStart, final String str, final int strStart,
29             final int strLimit) {
30         int out = byteStart;
31         int val = 0;
32         for (int i = strStart; i < strLimit; ++i) {
33             final char c = str.charAt(i);
34             if (c == '.') {
35                 bytes[out++] = (byte) val;
36                 val = 0;
37             } else {
38                 val = 10 * val + c - '0';
39             }
40         }
41
42         bytes[out] = (byte) val;
43     }
44
45     public static int addressBits(final String str, final int limit) {
46         int prev = 0;
47         int current = 0;
48         for (int i = 0; i < limit; ++i) {
49             final char c = str.charAt(i);
50             if (c == '.') {
51                 prev = prev << 8 | current;
52                 current = 0;
53             } else {
54                 current = 10 * current + c - '0';
55             }
56         }
57         return prev << 8 | current;
58     }
59
60     public static byte @NonNull[] addressBytes(final String str, final int limit) {
61         final byte[] bytes = new byte[4];
62         Ipv4Utils.fillIpv4Bytes(bytes, 0, str, 0, limit);
63         return bytes;
64     }
65
66     public static String addressString(final int bits) {
67         return (bits >>> 24) + "." + (bits >>> 16 & 0xFF) + "." + (bits >>> 8 & 0xFF) + "." + (bits & 0xFF);
68     }
69
70     public static String addressString(final byte @NonNull[] bytes) {
71         final StringBuilder sb = new StringBuilder(15);
72         appendIpv4String(sb, bytes);
73         return sb.toString();
74     }
75
76     static void appendIpv4String(final StringBuilder sb, final byte @NonNull[] bytes) {
77         checkArgument(bytes.length == INET4_LENGTH, "IPv4 address length is 4 bytes");
78
79         sb.append(Byte.toUnsignedInt(bytes[0]));
80         for (int i = 1; i < INET4_LENGTH; ++i) {
81             sb.append('.').append(Byte.toUnsignedInt(bytes[i]));
82         }
83     }
84 }