Removed Equals/HashCodeBuilder for ARP/LLDPTLV
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / packet / ARP.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  */
9
10 package org.opendaylight.controller.sal.packet;
11
12 import java.util.HashMap;
13 import java.util.LinkedHashMap;
14 import java.util.Map;
15
16 import org.apache.commons.lang3.tuple.ImmutablePair;
17 import org.apache.commons.lang3.tuple.Pair;
18
19 /**
20  * Class that represents the ARP packet objects
21  *
22  *
23  */
24
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";
35
36     public static short HW_TYPE_ETHERNET = (short) 0x1;
37     public static short REQUEST = (short) 0x1;
38     public static short REPLY = (short) 0x2;
39
40     private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
41         private static final long serialVersionUID = 1L;
42         {
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));
52
53         }
54     };
55     private Map<String, byte[]> fieldValues;
56
57     /**
58      * Default constructor that creates and sets the HashMap
59      */
60     public ARP() {
61         super();
62         fieldValues = new HashMap<String, byte[]>();
63         hdrFieldCoordMap = fieldCoordinates;
64         hdrFieldsMap = fieldValues;
65     }
66
67     /**
68      * Constructor that sets the access level for the packet and
69      * creates and sets the HashMap
70      */
71     public ARP(boolean writeAccess) {
72         super(writeAccess);
73         fieldValues = new HashMap<String, byte[]>();
74         hdrFieldCoordMap = fieldCoordinates;
75         hdrFieldsMap = fieldValues;
76     }
77
78     /**
79      * Gets the hardware type from the stored ARP header
80      * @return short - the hardwareType
81      */
82     public short getHardwareType() {
83         return (BitBufferHelper.getShort(fieldValues.get(HWTYPE)));
84
85     }
86
87     /**
88      * Gets the protocol type from the stored ARP header
89      * @return short - the protocolType
90      */
91     public short getProtocolType() {
92         return (BitBufferHelper.getShort(fieldValues.get(PTYPE)));
93     }
94
95     /**
96      * Gets the hardware address length from the stored ARP header
97      * @return byte - the protocolAddressLength
98      */
99     public byte getHardwareAddressLength() {
100         return (BitBufferHelper.getByte(fieldValues.get(HWADDRLENGTH)));
101     }
102
103     /**
104      * Get the protocol address length from Protocol header
105      * @return byte - the protocolAddressLength
106      */
107     public byte getProtocolAddressLength() {
108         return (BitBufferHelper.getByte(fieldValues.get(PADDRLENGTH)));
109     }
110
111     /**
112      * Gets the opCode from stored ARP header
113      * @param short - the opCode to set
114      */
115     public short getOpCode() {
116         return (BitBufferHelper.getShort(fieldValues.get(OPCODE)));
117     }
118
119     /**
120      * Gets the sender hardware address from the stored ARP header
121      * @return byte[] - the senderHardwareAddress
122      */
123     public byte[] getSenderHardwareAddress() {
124         return (fieldValues.get(SENDERHWADDR));
125     }
126
127     /**
128      * Gets the IP address from the stored ARP header
129      * @return byte[] - the senderProtocolAddress
130      */
131     public byte[] getSenderProtocolAddress() {
132         return (fieldValues.get(SENDERPADDR));
133     }
134
135     /**
136      * Gets the hardware address from the stored ARP header
137      * @return byte[] - the targetHardwareAddress
138      */
139     public byte[] getTargetHardwareAddress() {
140         return (fieldValues.get(TARGETHWADDR));
141     }
142
143     /**
144      * Sets the hardware Type for the current ARP object instance
145      * @param short - hardwareType the hardwareType to set
146      * @return ARP
147      */
148     public ARP setHardwareType(short hardwareType) {
149         byte[] hwType = BitBufferHelper.toByteArray(hardwareType);
150         fieldValues.put(HWTYPE, hwType);
151         return this;
152     }
153
154     /**
155      * Sets the protocol Type for the current ARP object instance
156      * @param short - the protocolType to set
157      * @return ARP
158      */
159     public ARP setProtocolType(short protocolType) {
160         byte[] protType = BitBufferHelper.toByteArray(protocolType);
161         fieldValues.put(PTYPE, protType);
162         return this;
163     }
164
165     /**
166      * Sets the hardware address length for the current ARP object instance
167      * @param byte - the hardwareAddressLength to set
168      * @return ARP
169      */
170     public ARP setHardwareAddressLength(byte hardwareAddressLength) {
171         byte[] hwAddressLength = BitBufferHelper
172                 .toByteArray(hardwareAddressLength);
173         fieldValues.put(HWADDRLENGTH, hwAddressLength);
174         return this;
175     }
176
177     /**
178      * Sets the Protocol address for the current ARP object instance
179      * @param byte - the protocolAddressLength to set
180      * @return ARP
181      */
182     public ARP setProtocolAddressLength(byte protocolAddressLength) {
183         byte[] protocolAddrLength = BitBufferHelper
184                 .toByteArray(protocolAddressLength);
185         fieldValues.put(PADDRLENGTH, protocolAddrLength);
186         return this;
187     }
188
189     /**
190      * Sets the opCode for the current ARP object instance
191      * @param short - the opCode to set
192      * @return ARP
193      */
194     public ARP setOpCode(short opCode) {
195         byte[] operationCode = BitBufferHelper.toByteArray(opCode);
196         fieldValues.put(OPCODE, operationCode);
197         return this;
198     }
199
200     /**
201      * Sets the sender hardware address for the current ARP object instance
202      * @param byte[] - the senderHardwareAddress to set
203      * @return ARP
204      */
205     public ARP setSenderHardwareAddress(byte[] senderHardwareAddress) {
206         fieldValues.put(SENDERHWADDR, senderHardwareAddress);
207         return this;
208     }
209
210     /**
211      * Sets the target hardware address for the current ARP object instance
212      * @param byte[] - the targetHardwareAddress to set
213      * @return ARP
214      */
215     public ARP setTargetHardwareAddress(byte[] targetHardwareAddress) {
216         fieldValues.put(TARGETHWADDR, targetHardwareAddress);
217         return this;
218     }
219
220     /**
221      * Sets the target protocol address for the current ARP object instance
222      * @param byte[] - the targetProtocolAddress to set
223      * @return ARP
224      */
225     public ARP setTargetProtocolAddress(byte[] targetProtocolAddress) {
226         fieldValues.put(TARGETPADDR, targetProtocolAddress);
227         return this;
228     }
229
230     /**
231      * Sets the sender protocol address for the current ARP object instance
232      * @param byte[] - senderIP
233      * @return ARP
234      */
235     public ARP setSenderProtocolAddress(byte[] senderIP) {
236         fieldValues.put(SENDERPADDR, senderIP);
237         return this;
238     }
239
240     /**
241      * Gets the target protocol address
242      * @return - byte[] targetProtocolAddress
243      */
244     public byte[] getTargetProtocolAddress() {
245         return fieldValues.get(TARGETPADDR);
246     }
247
248     @Override
249     public int hashCode() {
250         final int prime = 31;
251         int result = super.hashCode();
252         result = prime * result
253                 + ((fieldValues == null) ? 0 : fieldValues.hashCode());
254         return result;
255     }
256
257     @Override
258     public boolean equals(Object obj) {
259         if (this == obj)
260             return true;
261         if (!super.equals(obj))
262             return false;
263         if (getClass() != obj.getClass())
264             return false;
265         ARP other = (ARP) obj;
266         if (fieldValues == null) {
267             if (other.fieldValues != null)
268                 return false;
269         } else if (!fieldValues.equals(other.fieldValues))
270             return false;
271         return true;
272     }
273 }