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.groupbasedpolicy.renderer.ofoverlay.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 * @param eth {@link Ethernet}
35 * @return returns Ethernet and ARP in readable string format
37 public static String getArpFrameToStringFormat(Ethernet eth) {
38 String ethernetString = "Ethernet [getEtherType()="
39 + EtherTypes.loadFromString(String.valueOf(eth.getEtherType())) + ", getSourceMACAddress()="
40 + HexEncode.bytesToHexStringFormat(eth.getSourceMACAddress()) + ", getDestinationMACAddress()="
41 + HexEncode.bytesToHexStringFormat(eth.getDestinationMACAddress()) + "]\n";
42 Packet potentialArp = eth.getPayload();
43 String arpString = null;
44 if (potentialArp instanceof Arp) {
45 Arp arp = (Arp) potentialArp;
46 arpString = ArpUtils.getArpToStringFormat(arp);
48 arpString = "ARP was not found in Ethernet frame.";
50 return ethernetString.concat(arpString);
54 * Returns ARP in readable string format
56 * @param arp {@link Arp}
57 * @return returns ARP in readable string format
59 public static String getArpToStringFormat(Arp arp) {
61 return "Arp [getHardwareType()=" + arp.getHardwareType() + ", getProtocolType()=" + arp.getProtocolType()
62 + ", getHardwareLength()=" + arp.getHardwareLength() + ", getProtocolLength()="
63 + arp.getProtocolLength() + ", getOperation()=" + ArpOperation.loadFromInt(arp.getOperation())
64 + ", getSenderHardwareAddress()="
65 + HexEncode.bytesToHexStringFormat(arp.getSenderHardwareAddress())
66 + ", getSenderProtocolAddress()="
67 + InetAddress.getByAddress(arp.getSenderProtocolAddress()).getHostAddress()
68 + ", getTargetHardwareAddress()="
69 + HexEncode.bytesToHexStringFormat(arp.getTargetHardwareAddress())
70 + ", getTargetProtocolAddress()="
71 + InetAddress.getByAddress(arp.getTargetProtocolAddress()).getHostAddress() + "]\n";
72 } catch (UnknownHostException e1) {
73 return "Error during parsing Arp " + arp;
77 public static byte[] macToBytes(MacAddress mac) {
78 return HexEncode.bytesFromHexString(mac.getValue());
81 public static @Nullable MacAddress bytesToMac(byte[] macBytes) {
82 String mac = HexEncode.bytesToHexStringFormat(macBytes);
83 if (!"null".equals(mac)) {
84 return new MacAddress(mac);
89 public static byte[] ipToBytes(Ipv4Address ip) {
90 return InetAddresses.forString(ip.getValue()).getAddress();
93 public static @Nullable Ipv4Address bytesToIp(byte[] ipv4AsBytes) {
95 return new Ipv4Address(InetAddress.getByAddress(ipv4AsBytes).getHostAddress());
96 } catch (UnknownHostException e) {