2 * Copyright (c) 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.services.arp;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
14 import javax.annotation.Nullable;
16 import org.opendaylight.controller.liblldp.EtherTypes;
17 import org.opendaylight.controller.liblldp.Ethernet;
18 import org.opendaylight.controller.liblldp.HexEncode;
19 import org.opendaylight.controller.liblldp.Packet;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
23 import com.google.common.net.InetAddresses;
25 public class ArpUtils {
28 throw new UnsupportedOperationException("Cannot create an instance.");
32 * Returns Ethernet and ARP in readable string format
34 public static String getArpFrameToStringFormat(Ethernet eth) {
35 String ethernetString = "Ethernet [getEtherType()="
36 + EtherTypes.loadFromString(String.valueOf(eth.getEtherType())) + ", getSourceMACAddress()="
37 + HexEncode.bytesToHexStringFormat(eth.getSourceMACAddress()) + ", getDestinationMACAddress()="
38 + HexEncode.bytesToHexStringFormat(eth.getDestinationMACAddress()) + "]\n";
39 Packet potentialArp = eth.getPayload();
40 String arpString = null;
41 if (potentialArp instanceof Arp) {
42 Arp arp = (Arp) potentialArp;
43 arpString = ArpUtils.getArpToStringFormat(arp);
45 arpString = "ARP was not found in Ethernet frame.";
47 return ethernetString.concat(arpString);
51 * Returns ARP in readable string format
53 public static String getArpToStringFormat(Arp arp) {
55 return "Arp [getHardwareType()=" + arp.getHardwareType() + ", getProtocolType()=" + arp.getProtocolType()
56 + ", getHardwareLength()=" + arp.getHardwareLength() + ", getProtocolLength()="
57 + arp.getProtocolLength() + ", getOperation()=" + ArpOperation.loadFromInt(arp.getOperation())
58 + ", getSenderHardwareAddress()="
59 + HexEncode.bytesToHexStringFormat(arp.getSenderHardwareAddress())
60 + ", getSenderProtocolAddress()="
61 + InetAddress.getByAddress(arp.getSenderProtocolAddress()).getHostAddress()
62 + ", getTargetHardwareAddress()="
63 + HexEncode.bytesToHexStringFormat(arp.getTargetHardwareAddress())
64 + ", getTargetProtocolAddress()="
65 + InetAddress.getByAddress(arp.getTargetProtocolAddress()).getHostAddress() + "]\n";
66 } catch (UnknownHostException e1) {
67 return "Error during parsing Arp " + arp;
71 public static byte[] macToBytes(MacAddress mac) {
72 return HexEncode.bytesFromHexString(mac.getValue());
75 public static @Nullable MacAddress bytesToMac(byte[] macBytes) {
76 String mac = HexEncode.bytesToHexStringFormat(macBytes);
77 if (!"null".equals(mac)) {
78 return new MacAddress(mac);
83 public static byte[] ipToBytes(Ipv4Address ip) {
84 return InetAddresses.forString(ip.getValue()).getAddress();
87 public static @Nullable Ipv4Address bytesToIp(byte[] ipv4AsBytes) {
89 return new Ipv4Address(InetAddress.getByAddress(ipv4AsBytes).getHostAddress());
90 } catch (UnknownHostException e) {