5bb9d86f01dc33b97ceb1110a4646a4644263523
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / 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.groupbasedpolicy.renderer.ofoverlay.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      * @param eth {@link Ethernet}
35      * @return returns Ethernet and ARP in readable string format
36      */
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);
47         } else {
48             arpString = "ARP was not found in Ethernet frame.";
49         }
50         return ethernetString.concat(arpString);
51     }
52
53     /**
54      * Returns ARP in readable string format
55      *
56      * @param arp {@link Arp}
57      * @return returns ARP in readable string format
58      */
59     public static String getArpToStringFormat(Arp arp) {
60         try {
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;
74         }
75     }
76
77     public static byte[] macToBytes(MacAddress mac) {
78         return HexEncode.bytesFromHexString(mac.getValue());
79     }
80
81     public static @Nullable MacAddress bytesToMac(byte[] macBytes) {
82         String mac = HexEncode.bytesToHexStringFormat(macBytes);
83         if (!"null".equals(mac)) {
84             return new MacAddress(mac);
85         }
86         return null;
87     }
88
89     public static byte[] ipToBytes(Ipv4Address ip) {
90         return InetAddresses.forString(ip.getValue()).getAddress();
91     }
92
93     public static @Nullable Ipv4Address bytesToIp(byte[] ipv4AsBytes) {
94         try {
95             return new Ipv4Address(InetAddress.getByAddress(ipv4AsBytes).getHostAddress());
96         } catch (UnknownHostException e) {
97             return null;
98         }
99     }
100
101 }