chengfan | 8ea6a56 | 2016-11-26 16:37:02 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.teyang.utils.tunnel; |
| 18 | |
| 19 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev20130715.ietfinettypes.IpAddress; |
| 20 | |
| 21 | /** |
Henry Yu | ad44114 | 2016-12-01 17:00:37 -0500 | [diff] [blame^] | 22 | * Basic converter tools for ietf NBI & SBI. |
chengfan | 8ea6a56 | 2016-11-26 16:37:02 +0800 | [diff] [blame] | 23 | */ |
| 24 | public abstract class BasicConverter { |
| 25 | |
| 26 | //no instantiation |
| 27 | private BasicConverter() { |
| 28 | |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Converts a long value to IpAddress. |
| 33 | * |
| 34 | * @param value long value |
| 35 | * @return ip address |
| 36 | */ |
| 37 | static IpAddress longToIp(long value) { |
| 38 | StringBuilder sb = new StringBuilder(); |
| 39 | sb.append((value >> 24) & 0xFF).append("."); |
| 40 | sb.append((value >> 16) & 0xFF).append("."); |
| 41 | sb.append((value >> 8) & 0xFF).append("."); |
| 42 | sb.append(value & 0xFF); |
| 43 | return IpAddress.fromString(sb.toString()); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Converts a long value to byte array. |
| 48 | * |
| 49 | * @param value long value |
| 50 | * @return byte array |
| 51 | */ |
| 52 | static byte[] longToByte(long value) { |
| 53 | long temp = value; |
| 54 | byte[] b = new byte[8]; |
| 55 | for (int i = 0; i < b.length; i++) { |
| 56 | b[i] = new Long(temp & 0xff).byteValue(); |
| 57 | temp = temp >> 8; |
| 58 | } |
| 59 | return b; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Converts a IP address to long value. |
| 64 | * |
| 65 | * @param ipAddress IP address |
| 66 | * @return long value |
| 67 | */ |
| 68 | static long ipToLong(IpAddress ipAddress) { |
| 69 | long[] ip = new long[4]; |
| 70 | String strIp = ipAddress.toString(); |
| 71 | int position1 = strIp.indexOf("."); |
| 72 | int position2 = strIp.indexOf(".", position1 + 1); |
| 73 | int position3 = strIp.indexOf(".", position2 + 1); |
| 74 | ip[0] = Long.parseLong(strIp.substring(0, position1)); |
| 75 | ip[1] = Long.parseLong(strIp.substring(position1 + 1, position2)); |
| 76 | ip[2] = Long.parseLong(strIp.substring(position2 + 1, position3)); |
| 77 | ip[3] = Long.parseLong(strIp.substring(position3 + 1)); |
| 78 | return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3]; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Converts byte array to long value. |
| 83 | * |
| 84 | * @param bytes byte array |
| 85 | * @return long value |
| 86 | */ |
| 87 | static long bytesToLong(byte[] bytes) { |
| 88 | return ((long) bytes[7] & 255L) << 56 | |
| 89 | ((long) bytes[6] & 255L) << 48 | |
| 90 | ((long) bytes[5] & 255L) << 40 | |
| 91 | ((long) bytes[4] & 255L) << 32 | |
| 92 | ((long) bytes[3] & 255L) << 24 | |
| 93 | ((long) bytes[2] & 255L) << 16 | |
| 94 | ((long) bytes[1] & 255L) << 8 | |
| 95 | (long) bytes[0] & 255L; |
| 96 | } |
| 97 | } |