2 * Copyright (c) 2013, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.vpnservice.mdsalutil.packet;
11 import java.util.HashMap;
12 import java.util.LinkedHashMap;
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;
21 * Class that represents the ARP packet objects
22 * taken from opendaylight(helium) adsal bundle
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";
37 public static short HW_TYPE_ETHERNET = (short) 0x1;
38 public static short REQUEST = (short) 0x1;
39 public static short REPLY = (short) 0x2;
41 public static short PROTO_TYPE_IP = 0x800;
43 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
44 private static final long serialVersionUID = 1L;
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));
58 private Map<String, byte[]> fieldValues;
61 * Default constructor that creates and sets the HashMap
65 fieldValues = new HashMap<String, byte[]>();
66 hdrFieldCoordMap = fieldCoordinates;
67 hdrFieldsMap = fieldValues;
70 public ARP(boolean writeAccess) {
72 fieldValues = new HashMap<String, byte[]>();
73 hdrFieldCoordMap = fieldCoordinates;
74 hdrFieldsMap = fieldValues;
77 public short getHardwareType() {
78 return (BitBufferHelper.getShort(fieldValues.get(HWTYPE)));
82 public short getProtocolType() {
83 return (BitBufferHelper.getShort(fieldValues.get(PTYPE)));
86 public byte getHardwareAddressLength() {
87 return (BitBufferHelper.getByte(fieldValues.get(HWADDRLENGTH)));
90 public byte getProtocolAddressLength() {
91 return (BitBufferHelper.getByte(fieldValues.get(PADDRLENGTH)));
94 public short getOpCode() {
95 return (BitBufferHelper.getShort(fieldValues.get(OPCODE)));
98 public byte[] getSenderHardwareAddress() {
99 return (fieldValues.get(SENDERHWADDR));
102 public byte[] getSenderProtocolAddress() {
103 return (fieldValues.get(SENDERPADDR));
106 public byte[] getTargetHardwareAddress() {
107 return (fieldValues.get(TARGETHWADDR));
110 public ARP setHardwareType(short hardwareType) {
111 byte[] hwType = BitBufferHelper.toByteArray(hardwareType);
112 fieldValues.put(HWTYPE, hwType);
116 public ARP setProtocolType(short protocolType) {
117 byte[] protType = BitBufferHelper.toByteArray(protocolType);
118 fieldValues.put(PTYPE, protType);
122 public ARP setHardwareAddressLength(byte hardwareAddressLength) {
123 byte[] hwAddressLength = BitBufferHelper
124 .toByteArray(hardwareAddressLength);
125 fieldValues.put(HWADDRLENGTH, hwAddressLength);
129 public ARP setProtocolAddressLength(byte protocolAddressLength) {
130 byte[] protocolAddrLength = BitBufferHelper
131 .toByteArray(protocolAddressLength);
132 fieldValues.put(PADDRLENGTH, protocolAddrLength);
136 public ARP setOpCode(short opCode) {
137 byte[] operationCode = BitBufferHelper.toByteArray(opCode);
138 fieldValues.put(OPCODE, operationCode);
142 public ARP setSenderHardwareAddress(byte[] senderHardwareAddress) {
143 fieldValues.put(SENDERHWADDR, senderHardwareAddress);
147 public ARP setTargetHardwareAddress(byte[] targetHardwareAddress) {
148 fieldValues.put(TARGETHWADDR, targetHardwareAddress);
152 public ARP setTargetProtocolAddress(byte[] targetProtocolAddress) {
153 fieldValues.put(TARGETPADDR, targetProtocolAddress);
157 public ARP setSenderProtocolAddress(byte[] senderIP) {
158 fieldValues.put(SENDERPADDR, senderIP);
162 public byte[] getTargetProtocolAddress() {
163 return fieldValues.get(TARGETPADDR);
167 public int hashCode() {
168 final int prime = 31;
169 int result = super.hashCode();
170 result = prime * result
171 + ((fieldValues == null) ? 0 : fieldValues.hashCode());
176 public boolean equals(Object obj) {
180 if (!super.equals(obj)) {
183 if (getClass() != obj.getClass()) {
186 ARP other = (ARP) obj;
187 if (fieldValues == null) {
188 if (other.fieldValues != null) {
191 } else if (!fieldValues.equals(other.fieldValues)) {