3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.sal.packet;
12 import java.util.HashMap;
13 import java.util.LinkedHashMap;
16 import org.apache.commons.lang3.tuple.ImmutablePair;
17 import org.apache.commons.lang3.tuple.Pair;
20 * Class that represents the ARP packet objects
25 public class ARP extends Packet {
26 private static final String HWTYPE = "HardwareType";
27 private static final String PTYPE = "ProtocolType";
28 private static final String HWADDRLENGTH = "HardwareAddressLength";
29 private static final String PADDRLENGTH = "ProtocolAddressLength";
30 private static final String OPCODE = "OpCode";
31 private static final String SENDERHWADDR = "SenderHardwareAddress";
32 private static final String SENDERPADDR = "SenderProtocolAddress";
33 private static final String TARGETHWADDR = "TargetHardwareAddress";
34 private static final String TARGETPADDR = "TargetProtocolAddress";
36 public static short HW_TYPE_ETHERNET = (short) 0x1;
37 public static short REQUEST = (short) 0x1;
38 public static short REPLY = (short) 0x2;
40 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
41 private static final long serialVersionUID = 1L;
43 put(HWTYPE, new ImmutablePair<Integer, Integer>(0, 16));
44 put(PTYPE, new ImmutablePair<Integer, Integer>(16, 16));
45 put(HWADDRLENGTH, new ImmutablePair<Integer, Integer>(32, 8));
46 put(PADDRLENGTH, new ImmutablePair<Integer, Integer>(40, 8));
47 put(OPCODE, new ImmutablePair<Integer, Integer>(48, 16));
48 put(SENDERHWADDR, new ImmutablePair<Integer, Integer>(64, 48));
49 put(SENDERPADDR, new ImmutablePair<Integer, Integer>(112, 32));
50 put(TARGETHWADDR, new ImmutablePair<Integer, Integer>(144, 48));
51 put(TARGETPADDR, new ImmutablePair<Integer, Integer>(192, 32));
55 private Map<String, byte[]> fieldValues;
58 * Default constructor that creates and sets the HashMap
62 fieldValues = new HashMap<String, byte[]>();
63 hdrFieldCoordMap = fieldCoordinates;
64 hdrFieldsMap = fieldValues;
68 * Constructor that sets the access level for the packet and
69 * creates and sets the HashMap
71 public ARP(boolean writeAccess) {
73 fieldValues = new HashMap<String, byte[]>();
74 hdrFieldCoordMap = fieldCoordinates;
75 hdrFieldsMap = fieldValues;
79 * Gets the hardware type from the stored ARP header
80 * @return short - the hardwareType
82 public short getHardwareType() {
83 return (BitBufferHelper.getShort(fieldValues.get(HWTYPE)));
88 * Gets the protocol type from the stored ARP header
89 * @return short - the protocolType
91 public short getProtocolType() {
92 return (BitBufferHelper.getShort(fieldValues.get(PTYPE)));
96 * Gets the hardware address length from the stored ARP header
97 * @return byte - the protocolAddressLength
99 public byte getHardwareAddressLength() {
100 return (BitBufferHelper.getByte(fieldValues.get(HWADDRLENGTH)));
104 * Get the protocol address length from Protocol header
105 * @return byte - the protocolAddressLength
107 public byte getProtocolAddressLength() {
108 return (BitBufferHelper.getByte(fieldValues.get(PADDRLENGTH)));
112 * Gets the opCode from stored ARP header
113 * @param short - the opCode to set
115 public short getOpCode() {
116 return (BitBufferHelper.getShort(fieldValues.get(OPCODE)));
120 * Gets the sender hardware address from the stored ARP header
121 * @return byte[] - the senderHardwareAddress
123 public byte[] getSenderHardwareAddress() {
124 return (fieldValues.get(SENDERHWADDR));
128 * Gets the IP address from the stored ARP header
129 * @return byte[] - the senderProtocolAddress
131 public byte[] getSenderProtocolAddress() {
132 return (fieldValues.get(SENDERPADDR));
136 * Gets the hardware address from the stored ARP header
137 * @return byte[] - the targetHardwareAddress
139 public byte[] getTargetHardwareAddress() {
140 return (fieldValues.get(TARGETHWADDR));
144 * Sets the hardware Type for the current ARP object instance
145 * @param short - hardwareType the hardwareType to set
148 public ARP setHardwareType(short hardwareType) {
149 byte[] hwType = BitBufferHelper.toByteArray(hardwareType);
150 fieldValues.put(HWTYPE, hwType);
155 * Sets the protocol Type for the current ARP object instance
156 * @param short - the protocolType to set
159 public ARP setProtocolType(short protocolType) {
160 byte[] protType = BitBufferHelper.toByteArray(protocolType);
161 fieldValues.put(PTYPE, protType);
166 * Sets the hardware address length for the current ARP object instance
167 * @param byte - the hardwareAddressLength to set
170 public ARP setHardwareAddressLength(byte hardwareAddressLength) {
171 byte[] hwAddressLength = BitBufferHelper
172 .toByteArray(hardwareAddressLength);
173 fieldValues.put(HWADDRLENGTH, hwAddressLength);
178 * Sets the Protocol address for the current ARP object instance
179 * @param byte - the protocolAddressLength to set
182 public ARP setProtocolAddressLength(byte protocolAddressLength) {
183 byte[] protocolAddrLength = BitBufferHelper
184 .toByteArray(protocolAddressLength);
185 fieldValues.put(PADDRLENGTH, protocolAddrLength);
190 * Sets the opCode for the current ARP object instance
191 * @param short - the opCode to set
194 public ARP setOpCode(short opCode) {
195 byte[] operationCode = BitBufferHelper.toByteArray(opCode);
196 fieldValues.put(OPCODE, operationCode);
201 * Sets the sender hardware address for the current ARP object instance
202 * @param byte[] - the senderHardwareAddress to set
205 public ARP setSenderHardwareAddress(byte[] senderHardwareAddress) {
206 fieldValues.put(SENDERHWADDR, senderHardwareAddress);
211 * Sets the target hardware address for the current ARP object instance
212 * @param byte[] - the targetHardwareAddress to set
215 public ARP setTargetHardwareAddress(byte[] targetHardwareAddress) {
216 fieldValues.put(TARGETHWADDR, targetHardwareAddress);
221 * Sets the target protocol address for the current ARP object instance
222 * @param byte[] - the targetProtocolAddress to set
225 public ARP setTargetProtocolAddress(byte[] targetProtocolAddress) {
226 fieldValues.put(TARGETPADDR, targetProtocolAddress);
231 * Sets the sender protocol address for the current ARP object instance
232 * @param byte[] - senderIP
235 public ARP setSenderProtocolAddress(byte[] senderIP) {
236 fieldValues.put(SENDERPADDR, senderIP);
241 * Gets the target protocol address
242 * @return - byte[] targetProtocolAddress
244 public byte[] getTargetProtocolAddress() {
245 return fieldValues.get(TARGETPADDR);
249 public int hashCode() {
250 final int prime = 31;
251 int result = super.hashCode();
252 result = prime * result
253 + ((fieldValues == null) ? 0 : fieldValues.hashCode());
258 public boolean equals(Object obj) {
261 if (!super.equals(obj))
263 if (getClass() != obj.getClass())
265 ARP other = (ARP) obj;
266 if (fieldValues == null) {
267 if (other.fieldValues != null)
269 } else if (!fieldValues.equals(other.fieldValues))