2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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.vpnservice.mdsalutil;
10 import java.net.InetAddress;
12 import com.google.common.primitives.UnsignedBytes;
16 public static long convertInetAddressToLong(InetAddress address) {
17 byte[] ipAddressRaw = address.getAddress();
18 return (((ipAddressRaw[0] & 0xFF) << (3 * 8))
19 + ((ipAddressRaw[1] & 0xFF) << (2 * 8))
20 + ((ipAddressRaw[2] & 0xFF) << (1 * 8))
21 + (ipAddressRaw[3] & 0xFF))
25 public static byte[] parseIpAddress(String ipAddress) {
28 String[] addressPart = ipAddress.split(".");
29 int size = addressPart.length;
31 byte[] part = new byte[size];
32 for (int i = 0; i < size; i++) {
33 cur = UnsignedBytes.parseUnsignedByte(addressPart[i], 16);
40 public static byte[] parseMacAddress(String macAddress) {
43 String[] addressPart = macAddress.split(":");
44 int size = addressPart.length;
46 byte[] part = new byte[size];
47 for (int i = 0; i < size; i++) {
48 cur = UnsignedBytes.parseUnsignedByte(addressPart[i], 16);
55 public static String toStringIpAddress(byte[] ipAddress)
57 if (ipAddress == null) {
61 StringBuilder sb = new StringBuilder(18);
63 for (int i = 0; i < ipAddress.length; i++) {
64 sb.append(UnsignedBytes.toString(ipAddress[i], 10));
72 public static String toStringMacAddress(byte[] macAddress)
74 if (macAddress == null) {
78 StringBuilder sb = new StringBuilder(18);
80 for (int i = 0; i < macAddress.length; i++) {
81 String tmp = UnsignedBytes.toString(macAddress[i], 16).toUpperCase();
82 if(tmp.length() == 1 || macAddress[i] == (byte)0) {