summaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/pcep-api/src/main/java/org/onosproject/pcep/tools/PcepTools.java
blob: 28eaebdde7a249510b3df2737a21a37dc9b6f6e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.pcep.tools;

import javax.xml.bind.DatatypeConverter;

/**
 * tools fo pcep app.
 */
public abstract class PcepTools {

    private PcepTools() {

    }

    /**
     * Converts decimal byte array to a hex string.
     *
     * @param byteArray byte array
     * @return a hex string
     */
    public static String toHexString(byte[] byteArray) {
        return DatatypeConverter.printHexBinary(byteArray);
    }

    /**
     * Converts a hex string to a decimal byte array.
     *
     * @param hexString a hex string
     * @return byte array
     */
    public static byte[] toByteArray(String hexString) {
        return DatatypeConverter.parseHexBinary(hexString);
    }

    /**
     * Converts a byte array to a decimal string.
     *
     * @param bytes a byte array
     * @return a decimal string
     */
    public static String toDecimalString(byte[] bytes) {
        String str = "";
        for (int i = 0; i < bytes.length; i++) {
            str += String.valueOf(bytes[i]);
        }
        return str;
    }

    /**
     * convert a string to the form of ip address.
     *
     * @param str a string
     * @return a string with ip format
     */
    public static String stringToIp(String str) {
        long ipInt = Long.parseLong(str, 16);
        return longToIp(ipInt);
    }

    /**
     * convert a long to ip format.
     *
     * @param ipLong a decimal number.
     * @return a ip format string
     */
    public static String longToIp(long ipLong) {
        StringBuilder sb = new StringBuilder();
        sb.append((ipLong >> 24) & 0xFF).append(".");
        sb.append((ipLong >> 16) & 0xFF).append(".");
        sb.append((ipLong >> 8) & 0xFF).append(".");
        sb.append(ipLong & 0xFF);
        return sb.toString();
    }

    /**
     * convert a string with ip format to a long.
     *
     * @param strIp a string with ip format
     * @return a long number
     */
    public static long ipToLong(String strIp) {
        long[] ip = new long[4];
        int position1 = strIp.indexOf(".");
        int position2 = strIp.indexOf(".", position1 + 1);
        int position3 = strIp.indexOf(".", position2 + 1);
        ip[0] = Long.parseLong(strIp.substring(0, position1));
        ip[1] = Long.parseLong(strIp.substring(position1 + 1, position2));
        ip[2] = Long.parseLong(strIp.substring(position2 + 1, position3));
        ip[3] = Long.parseLong(strIp.substring(position3 + 1));
        return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
    }

    /**
     * get a integer value from a cut string.
     *
     * @param str a whole string
     * @param base cut the string from this index
     * @param offset the offset when execute the cut
     * @return a integer value
     */
    public static int tranferHexStringToInt(String str, int base, int offset) {
        return Integer.parseInt(str.substring(base, offset), 16);

    }
}