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 public static short PROTO_TYPE_IP = 0x800;
42 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
43 private static final long serialVersionUID = 1L;
45 put(HWTYPE, new ImmutablePair<Integer, Integer>(0, 16));
46 put(PTYPE, new ImmutablePair<Integer, Integer>(16, 16));
47 put(HWADDRLENGTH, new ImmutablePair<Integer, Integer>(32, 8));
48 put(PADDRLENGTH, new ImmutablePair<Integer, Integer>(40, 8));
49 put(OPCODE, new ImmutablePair<Integer, Integer>(48, 16));
50 put(SENDERHWADDR, new ImmutablePair<Integer, Integer>(64, 48));
51 put(SENDERPADDR, new ImmutablePair<Integer, Integer>(112, 32));
52 put(TARGETHWADDR, new ImmutablePair<Integer, Integer>(144, 48));
53 put(TARGETPADDR, new ImmutablePair<Integer, Integer>(192, 32));
57 private Map<String, byte[]> fieldValues;
60 * Default constructor that creates and sets the HashMap
64 fieldValues = new HashMap<String, byte[]>();
65 hdrFieldCoordMap = fieldCoordinates;
66 hdrFieldsMap = fieldValues;
70 * Constructor that sets the access level for the packet and
71 * creates and sets the HashMap
73 public ARP(boolean writeAccess) {
75 fieldValues = new HashMap<String, byte[]>();
76 hdrFieldCoordMap = fieldCoordinates;
77 hdrFieldsMap = fieldValues;
81 * Gets the hardware type from the stored ARP header
82 * @return short - the hardwareType
84 public short getHardwareType() {
85 return (BitBufferHelper.getShort(fieldValues.get(HWTYPE)));
90 * Gets the protocol type from the stored ARP header
91 * @return short - the protocolType
93 public short getProtocolType() {
94 return (BitBufferHelper.getShort(fieldValues.get(PTYPE)));
98 * Gets the hardware address length from the stored ARP header
99 * @return byte - the protocolAddressLength
101 public byte getHardwareAddressLength() {
102 return (BitBufferHelper.getByte(fieldValues.get(HWADDRLENGTH)));
106 * Get the protocol address length from Protocol header
107 * @return byte - the protocolAddressLength
109 public byte getProtocolAddressLength() {
110 return (BitBufferHelper.getByte(fieldValues.get(PADDRLENGTH)));
114 * Gets the opCode from stored ARP header
115 * @param short - the opCode to set
117 public short getOpCode() {
118 return (BitBufferHelper.getShort(fieldValues.get(OPCODE)));
122 * Gets the sender hardware address from the stored ARP header
123 * @return byte[] - the senderHardwareAddress
125 public byte[] getSenderHardwareAddress() {
126 return (fieldValues.get(SENDERHWADDR));
130 * Gets the IP address from the stored ARP header
131 * @return byte[] - the senderProtocolAddress
133 public byte[] getSenderProtocolAddress() {
134 return (fieldValues.get(SENDERPADDR));
138 * Gets the hardware address from the stored ARP header
139 * @return byte[] - the targetHardwareAddress
141 public byte[] getTargetHardwareAddress() {
142 return (fieldValues.get(TARGETHWADDR));
146 * Sets the hardware Type for the current ARP object instance
147 * @param short - hardwareType the hardwareType to set
150 public ARP setHardwareType(short hardwareType) {
151 byte[] hwType = BitBufferHelper.toByteArray(hardwareType);
152 fieldValues.put(HWTYPE, hwType);
157 * Sets the protocol Type for the current ARP object instance
158 * @param short - the protocolType to set
161 public ARP setProtocolType(short protocolType) {
162 byte[] protType = BitBufferHelper.toByteArray(protocolType);
163 fieldValues.put(PTYPE, protType);
168 * Sets the hardware address length for the current ARP object instance
169 * @param byte - the hardwareAddressLength to set
172 public ARP setHardwareAddressLength(byte hardwareAddressLength) {
173 byte[] hwAddressLength = BitBufferHelper
174 .toByteArray(hardwareAddressLength);
175 fieldValues.put(HWADDRLENGTH, hwAddressLength);
180 * Sets the Protocol address for the current ARP object instance
181 * @param byte - the protocolAddressLength to set
184 public ARP setProtocolAddressLength(byte protocolAddressLength) {
185 byte[] protocolAddrLength = BitBufferHelper
186 .toByteArray(protocolAddressLength);
187 fieldValues.put(PADDRLENGTH, protocolAddrLength);
192 * Sets the opCode for the current ARP object instance
193 * @param short - the opCode to set
196 public ARP setOpCode(short opCode) {
197 byte[] operationCode = BitBufferHelper.toByteArray(opCode);
198 fieldValues.put(OPCODE, operationCode);
203 * Sets the sender hardware address for the current ARP object instance
204 * @param byte[] - the senderHardwareAddress to set
207 public ARP setSenderHardwareAddress(byte[] senderHardwareAddress) {
208 fieldValues.put(SENDERHWADDR, senderHardwareAddress);
213 * Sets the target hardware address for the current ARP object instance
214 * @param byte[] - the targetHardwareAddress to set
217 public ARP setTargetHardwareAddress(byte[] targetHardwareAddress) {
218 fieldValues.put(TARGETHWADDR, targetHardwareAddress);
223 * Sets the target protocol address for the current ARP object instance
224 * @param byte[] - the targetProtocolAddress to set
227 public ARP setTargetProtocolAddress(byte[] targetProtocolAddress) {
228 fieldValues.put(TARGETPADDR, targetProtocolAddress);
233 * Sets the sender protocol address for the current ARP object instance
234 * @param byte[] - senderIP
237 public ARP setSenderProtocolAddress(byte[] senderIP) {
238 fieldValues.put(SENDERPADDR, senderIP);
243 * Gets the target protocol address
244 * @return - byte[] targetProtocolAddress
246 public byte[] getTargetProtocolAddress() {
247 return fieldValues.get(TARGETPADDR);
251 public int hashCode() {
252 final int prime = 31;
253 int result = super.hashCode();
254 result = prime * result
255 + ((fieldValues == null) ? 0 : fieldValues.hashCode());
260 public boolean equals(Object obj) {
263 if (!super.equals(obj))
265 if (getClass() != obj.getClass())
267 ARP other = (ARP) obj;
268 if (fieldValues == null) {
269 if (other.fieldValues != null)
271 } else if (!fieldValues.equals(other.fieldValues))