added arputil module
[vpnservice.git] / arputil / arputil-impl / src / main / java / org / opendaylight / vpnservice / arputil / internal / ArpPacketUtil.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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.vpnservice.arputil.internal;
10
11 import org.opendaylight.controller.liblldp.EtherTypes;
12 import org.opendaylight.controller.liblldp.PacketException;
13 import org.opendaylight.vpnservice.mdsalutil.packet.ARP;
14 import org.opendaylight.vpnservice.mdsalutil.packet.Ethernet;
15
16 public class ArpPacketUtil {
17
18     public static byte[] EthernetDestination_Broadcast = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
19             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
20     public static byte[] MAC_Broadcast = new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
21
22     public static byte[] getPayload(short opCode, byte[] senderMacAddress, byte[] senderIP, byte[] targetMacAddress,
23             byte[] targetIP) throws PacketException {
24         ARP arp = createARPPacket(opCode, senderMacAddress, senderIP, targetMacAddress, targetIP);
25         Ethernet ethernet = createEthernetPacket(senderMacAddress, targetMacAddress, arp);
26         return ethernet.serialize();
27     }
28
29     public static ARP createARPPacket(short opCode, byte[] senderMacAddress, byte[] senderIP, byte[] targetMacAddress,
30             byte[] targetIP) {
31         ARP arp = new ARP();
32         arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
33         arp.setProtocolType(EtherTypes.IPv4.shortValue());
34         arp.setHardwareAddressLength((byte) 6);
35         arp.setProtocolAddressLength((byte) 4);
36         arp.setOpCode(opCode);
37         arp.setSenderHardwareAddress(senderMacAddress);
38         arp.setSenderProtocolAddress(senderIP);
39         arp.setTargetHardwareAddress(targetMacAddress);
40         arp.setTargetProtocolAddress(targetIP);
41         return arp;
42     }
43
44     public static Ethernet createEthernetPacket(byte[] sourceMAC, byte[] targetMAC, ARP arp)
45             throws PacketException {
46         Ethernet ethernet = new Ethernet();
47         ethernet.setSourceMACAddress(sourceMAC);
48         ethernet.setDestinationMACAddress(targetMAC);
49         ethernet.setEtherType(EtherTypes.ARP.shortValue());
50         ethernet.setPayload(arp);
51         return ethernet;
52     }
53 }