3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
10 package org.opendaylight.controller.sal.packet;
12 import java.util.HashMap;
13 import java.util.LinkedHashMap;
16 import org.apache.commons.lang3.builder.EqualsBuilder;
17 import org.apache.commons.lang3.builder.HashCodeBuilder;
18 import org.apache.commons.lang3.tuple.ImmutablePair;
19 import org.apache.commons.lang3.tuple.Pair;
22 * Class that represents the ICMP packet objects
27 public class ICMP extends Packet {
28 private static final String TYPECODE = "TypeCode";
29 private static final String CODE = "Code";
30 private static final String HEADERCHECKSUM = "HeaderChecksum";
31 private static final String IDENTIFIER = "Identifier";
32 private static final String SEQNUMBER = "SequenceNumber";
34 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
35 private static final long serialVersionUID = 1L;
38 put(TYPECODE, new ImmutablePair<Integer, Integer>(0, 8));
39 put(CODE, new ImmutablePair<Integer, Integer>(8, 8));
40 put(HEADERCHECKSUM, new ImmutablePair<Integer, Integer>(16, 16));
41 put(IDENTIFIER, new ImmutablePair<Integer, Integer>(32, 16));
42 put(SEQNUMBER, new ImmutablePair<Integer, Integer>(48, 16));
48 * Default constructor that creates and sets the hash map values
52 fieldValues = new HashMap<String, byte[]>();
53 hdrFieldCoordMap = fieldCoordinates;
54 hdrFieldsMap = fieldValues;
58 * Constructor that sets the access level for the packet
60 public ICMP(boolean writeAccess) {
62 fieldValues = new HashMap<String, byte[]>();
63 hdrFieldCoordMap = fieldCoordinates;
64 hdrFieldsMap = fieldValues;
67 private Map<String, byte[]> fieldValues;
70 public void setHeaderField(String headerField, byte[] readValue) {
71 hdrFieldsMap.put(headerField, readValue);
75 * Sets the TypeCode of ICMP for the current ICMP object instance
76 * @param short - typeCode
79 public ICMP setTypeCode(short typeCode) {
80 byte[] icmpTypeCode = BitBufferHelper.toByteArray(typeCode);
81 fieldValues.put(TYPECODE, icmpTypeCode);
86 * Sets the ICMP checksum for the current ICMP object instance
87 * @param short - checksum
90 public ICMP setChecksum(short checksum) {
91 byte[] icmpChecksum = BitBufferHelper.toByteArray(checksum);
92 fieldValues.put(HEADERCHECKSUM, icmpChecksum);
97 * Sets the ICMP identifier for the current ICMP object instance
98 * @param short - identifier
101 public ICMP setIdentifier(short identifier) {
102 byte[] icmpIdentifier = BitBufferHelper.toByteArray(identifier);
103 fieldValues.put(IDENTIFIER, icmpIdentifier);
108 * Sets the ICMP sequence number for the current ICMP object instance
109 * @param short - seqNumber
112 public ICMP setSequenceNumber(short seqNumber) {
113 byte[] icmpSeqNumber = BitBufferHelper.toByteArray(seqNumber);
114 fieldValues.put(SEQNUMBER, icmpSeqNumber);
119 public int hashCode() {
120 return HashCodeBuilder.reflectionHashCode(this);
124 public boolean equals(Object obj) {
125 return EqualsBuilder.reflectionEquals(this, obj);