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