Initial pass at changing groupId
[netvirt.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / netvirt / openstack / netvirt / providers / openflow13 / services / arp / ArpUtils.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.services.arp;
10
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13
14 import javax.annotation.Nullable;
15
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;
22
23 import com.google.common.net.InetAddresses;
24
25 public class ArpUtils {
26
27     private ArpUtils() {
28         throw new UnsupportedOperationException("Cannot create an instance.");
29     }
30
31     /**
32      * Returns Ethernet and ARP in readable string format
33      */
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);
44         } else {
45             arpString = "ARP was not found in Ethernet frame.";
46         }
47         return ethernetString.concat(arpString);
48     }
49
50     /**
51      * Returns ARP in readable string format
52      */
53     public static String getArpToStringFormat(Arp arp) {
54         try {
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;
68         }
69     }
70
71     public static byte[] macToBytes(MacAddress mac) {
72         return HexEncode.bytesFromHexString(mac.getValue());
73     }
74
75     public static @Nullable MacAddress bytesToMac(byte[] macBytes) {
76         String mac = HexEncode.bytesToHexStringFormat(macBytes);
77         if (!"null".equals(mac)) {
78             return new MacAddress(mac);
79         }
80         return null;
81     }
82
83     public static byte[] ipToBytes(Ipv4Address ip) {
84         return InetAddresses.forString(ip.getValue()).getAddress();
85     }
86
87     public static @Nullable Ipv4Address bytesToIp(byte[] ipv4AsBytes) {
88         try {
89             return new Ipv4Address(InetAddress.getByAddress(ipv4AsBytes).getHostAddress());
90         } catch (UnknownHostException e) {
91             return null;
92         }
93     }
94
95 }