Merge "Add pkt handling to DHCPService"
[vpnservice.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / vpnservice / mdsalutil / packet / ARP.java
1 /*
2  * Copyright (c) 2013, 2015 Cisco Systems, Inc. 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.mdsalutil.packet;
10
11 import java.util.HashMap;
12 import java.util.LinkedHashMap;
13 import java.util.Map;
14
15 import org.apache.commons.lang3.tuple.ImmutablePair;
16 import org.apache.commons.lang3.tuple.Pair;
17 import org.opendaylight.controller.liblldp.BitBufferHelper;
18 import org.opendaylight.controller.liblldp.Packet;
19
20 /**
21  * Class that represents the ARP packet objects
22  * taken from opendaylight(helium) adsal bundle
23  *
24  */
25
26 public class ARP extends Packet {
27     private static final String HWTYPE = "HardwareType";
28     private static final String PTYPE = "ProtocolType";
29     private static final String HWADDRLENGTH = "HardwareAddressLength";
30     private static final String PADDRLENGTH = "ProtocolAddressLength";
31     private static final String OPCODE = "OpCode";
32     private static final String SENDERHWADDR = "SenderHardwareAddress";
33     private static final String SENDERPADDR = "SenderProtocolAddress";
34     private static final String TARGETHWADDR = "TargetHardwareAddress";
35     private static final String TARGETPADDR = "TargetProtocolAddress";
36
37     public static short HW_TYPE_ETHERNET = (short) 0x1;
38     public static short REQUEST = (short) 0x1;
39     public static short REPLY = (short) 0x2;
40
41     public static short PROTO_TYPE_IP = 0x800;
42
43     private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
44         private static final long serialVersionUID = 1L;
45         {
46             put(HWTYPE, new ImmutablePair<Integer, Integer>(0, 16));
47             put(PTYPE, new ImmutablePair<Integer, Integer>(16, 16));
48             put(HWADDRLENGTH, new ImmutablePair<Integer, Integer>(32, 8));
49             put(PADDRLENGTH, new ImmutablePair<Integer, Integer>(40, 8));
50             put(OPCODE, new ImmutablePair<Integer, Integer>(48, 16));
51             put(SENDERHWADDR, new ImmutablePair<Integer, Integer>(64, 48));
52             put(SENDERPADDR, new ImmutablePair<Integer, Integer>(112, 32));
53             put(TARGETHWADDR, new ImmutablePair<Integer, Integer>(144, 48));
54             put(TARGETPADDR, new ImmutablePair<Integer, Integer>(192, 32));
55
56         }
57     };
58     private Map<String, byte[]> fieldValues;
59
60     /**
61      * Default constructor that creates and sets the HashMap
62      */
63     public ARP() {
64         super();
65         fieldValues = new HashMap<String, byte[]>();
66         hdrFieldCoordMap = fieldCoordinates;
67         hdrFieldsMap = fieldValues;
68     }
69
70     public ARP(boolean writeAccess) {
71         super(writeAccess);
72         fieldValues = new HashMap<String, byte[]>();
73         hdrFieldCoordMap = fieldCoordinates;
74         hdrFieldsMap = fieldValues;
75     }
76
77     public short getHardwareType() {
78         return (BitBufferHelper.getShort(fieldValues.get(HWTYPE)));
79
80     }
81
82     public short getProtocolType() {
83         return (BitBufferHelper.getShort(fieldValues.get(PTYPE)));
84     }
85
86     public byte getHardwareAddressLength() {
87         return (BitBufferHelper.getByte(fieldValues.get(HWADDRLENGTH)));
88     }
89
90     public byte getProtocolAddressLength() {
91         return (BitBufferHelper.getByte(fieldValues.get(PADDRLENGTH)));
92     }
93
94     public short getOpCode() {
95         return (BitBufferHelper.getShort(fieldValues.get(OPCODE)));
96     }
97
98     public byte[] getSenderHardwareAddress() {
99         return (fieldValues.get(SENDERHWADDR));
100     }
101
102     public byte[] getSenderProtocolAddress() {
103         return (fieldValues.get(SENDERPADDR));
104     }
105
106     public byte[] getTargetHardwareAddress() {
107         return (fieldValues.get(TARGETHWADDR));
108     }
109
110     public ARP setHardwareType(short hardwareType) {
111         byte[] hwType = BitBufferHelper.toByteArray(hardwareType);
112         fieldValues.put(HWTYPE, hwType);
113         return this;
114     }
115
116     public ARP setProtocolType(short protocolType) {
117         byte[] protType = BitBufferHelper.toByteArray(protocolType);
118         fieldValues.put(PTYPE, protType);
119         return this;
120     }
121
122     public ARP setHardwareAddressLength(byte hardwareAddressLength) {
123         byte[] hwAddressLength = BitBufferHelper
124                 .toByteArray(hardwareAddressLength);
125         fieldValues.put(HWADDRLENGTH, hwAddressLength);
126         return this;
127     }
128
129     public ARP setProtocolAddressLength(byte protocolAddressLength) {
130         byte[] protocolAddrLength = BitBufferHelper
131                 .toByteArray(protocolAddressLength);
132         fieldValues.put(PADDRLENGTH, protocolAddrLength);
133         return this;
134     }
135
136     public ARP setOpCode(short opCode) {
137         byte[] operationCode = BitBufferHelper.toByteArray(opCode);
138         fieldValues.put(OPCODE, operationCode);
139         return this;
140     }
141
142     public ARP setSenderHardwareAddress(byte[] senderHardwareAddress) {
143         fieldValues.put(SENDERHWADDR, senderHardwareAddress);
144         return this;
145     }
146
147     public ARP setTargetHardwareAddress(byte[] targetHardwareAddress) {
148         fieldValues.put(TARGETHWADDR, targetHardwareAddress);
149         return this;
150     }
151
152     public ARP setTargetProtocolAddress(byte[] targetProtocolAddress) {
153         fieldValues.put(TARGETPADDR, targetProtocolAddress);
154         return this;
155     }
156
157     public ARP setSenderProtocolAddress(byte[] senderIP) {
158         fieldValues.put(SENDERPADDR, senderIP);
159         return this;
160     }
161
162     public byte[] getTargetProtocolAddress() {
163         return fieldValues.get(TARGETPADDR);
164     }
165
166     @Override
167     public int hashCode() {
168         final int prime = 31;
169         int result = super.hashCode();
170         result = prime * result
171                 + ((fieldValues == null) ? 0 : fieldValues.hashCode());
172         return result;
173     }
174
175     @Override
176     public boolean equals(Object obj) {
177         if (this == obj) {
178             return true;
179         }
180         if (!super.equals(obj)) {
181             return false;
182         }
183         if (getClass() != obj.getClass()) {
184             return false;
185         }
186         ARP other = (ARP) obj;
187         if (fieldValues == null) {
188             if (other.fieldValues != null) {
189                 return false;
190             }
191         } else if (!fieldValues.equals(other.fieldValues)) {
192             return false;
193         }
194         return true;
195     }
196 }