added arputil module
[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     /**
71      * Constructor that sets the access level for the packet and
72      * creates and sets the HashMap
73      */
74     public ARP(boolean writeAccess) {
75         super(writeAccess);
76         fieldValues = new HashMap<String, byte[]>();
77         hdrFieldCoordMap = fieldCoordinates;
78         hdrFieldsMap = fieldValues;
79     }
80
81     public short getHardwareType() {
82         return (BitBufferHelper.getShort(fieldValues.get(HWTYPE)));
83
84     }
85
86     public short getProtocolType() {
87         return (BitBufferHelper.getShort(fieldValues.get(PTYPE)));
88     }
89
90     public byte getHardwareAddressLength() {
91         return (BitBufferHelper.getByte(fieldValues.get(HWADDRLENGTH)));
92     }
93
94     public byte getProtocolAddressLength() {
95         return (BitBufferHelper.getByte(fieldValues.get(PADDRLENGTH)));
96     }
97
98     public short getOpCode() {
99         return (BitBufferHelper.getShort(fieldValues.get(OPCODE)));
100     }
101
102     public byte[] getSenderHardwareAddress() {
103         return (fieldValues.get(SENDERHWADDR));
104     }
105
106     public byte[] getSenderProtocolAddress() {
107         return (fieldValues.get(SENDERPADDR));
108     }
109
110     public byte[] getTargetHardwareAddress() {
111         return (fieldValues.get(TARGETHWADDR));
112     }
113
114     public ARP setHardwareType(short hardwareType) {
115         byte[] hwType = BitBufferHelper.toByteArray(hardwareType);
116         fieldValues.put(HWTYPE, hwType);
117         return this;
118     }
119
120     public ARP setProtocolType(short protocolType) {
121         byte[] protType = BitBufferHelper.toByteArray(protocolType);
122         fieldValues.put(PTYPE, protType);
123         return this;
124     }
125
126     public ARP setHardwareAddressLength(byte hardwareAddressLength) {
127         byte[] hwAddressLength = BitBufferHelper
128                 .toByteArray(hardwareAddressLength);
129         fieldValues.put(HWADDRLENGTH, hwAddressLength);
130         return this;
131     }
132
133     public ARP setProtocolAddressLength(byte protocolAddressLength) {
134         byte[] protocolAddrLength = BitBufferHelper
135                 .toByteArray(protocolAddressLength);
136         fieldValues.put(PADDRLENGTH, protocolAddrLength);
137         return this;
138     }
139
140     public ARP setOpCode(short opCode) {
141         byte[] operationCode = BitBufferHelper.toByteArray(opCode);
142         fieldValues.put(OPCODE, operationCode);
143         return this;
144     }
145
146     public ARP setSenderHardwareAddress(byte[] senderHardwareAddress) {
147         fieldValues.put(SENDERHWADDR, senderHardwareAddress);
148         return this;
149     }
150
151     public ARP setTargetHardwareAddress(byte[] targetHardwareAddress) {
152         fieldValues.put(TARGETHWADDR, targetHardwareAddress);
153         return this;
154     }
155
156     public ARP setTargetProtocolAddress(byte[] targetProtocolAddress) {
157         fieldValues.put(TARGETPADDR, targetProtocolAddress);
158         return this;
159     }
160
161     public ARP setSenderProtocolAddress(byte[] senderIP) {
162         fieldValues.put(SENDERPADDR, senderIP);
163         return this;
164     }
165
166     public byte[] getTargetProtocolAddress() {
167         return fieldValues.get(TARGETPADDR);
168     }
169
170     @Override
171     public int hashCode() {
172         final int prime = 31;
173         int result = super.hashCode();
174         result = prime * result
175                 + ((fieldValues == null) ? 0 : fieldValues.hashCode());
176         return result;
177     }
178
179     @Override
180     public boolean equals(Object obj) {
181         if (this == obj) {
182             return true;
183         }
184         if (!super.equals(obj)) {
185             return false;
186         }
187         if (getClass() != obj.getClass()) {
188             return false;
189         }
190         ARP other = (ARP) obj;
191         if (fieldValues == null) {
192             if (other.fieldValues != null) {
193                 return false;
194             }
195         } else if (!fieldValues.equals(other.fieldValues)) {
196             return false;
197         }
198         return true;
199     }
200 }