Initial opendaylight infrastructure commit!!
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / packet / ICMP.java
1
2 /*
3  * Copyright (c) 2013 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.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;
20
21 /**
22  * Class that represents the ICMP packet objects
23  *
24  *
25  */
26
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";
33
34     private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
35         private static final long serialVersionUID = 1L;
36
37         {
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));
43
44         }
45     };
46
47     /**
48      * Default constructor that creates and sets the hash map values
49      */
50     public ICMP() {
51         super();
52         fieldValues = new HashMap<String, byte[]>();
53         hdrFieldCoordMap = fieldCoordinates;
54         hdrFieldsMap = fieldValues;
55     }
56
57     /**
58      * Constructor that sets the access level for the packet
59      */
60     public ICMP(boolean writeAccess) {
61         super(writeAccess);
62         fieldValues = new HashMap<String, byte[]>();
63         hdrFieldCoordMap = fieldCoordinates;
64         hdrFieldsMap = fieldValues;
65     }
66
67     private Map<String, byte[]> fieldValues;
68
69     @Override
70     public void setHeaderField(String headerField, byte[] readValue) {
71         hdrFieldsMap.put(headerField, readValue);
72     }
73
74     /**
75      * Sets the TypeCode of ICMP  for the current ICMP object instance
76      * @param short - typeCode
77      * @return ICMP
78      */
79     public ICMP setTypeCode(short typeCode) {
80         byte[] icmpTypeCode = BitBufferHelper.toByteArray(typeCode);
81         fieldValues.put(TYPECODE, icmpTypeCode);
82         return this;
83     }
84
85     /**
86      * Sets the ICMP checksum  for the current ICMP object instance
87      * @param short - checksum
88      * @return ICMP
89      */
90     public ICMP setChecksum(short checksum) {
91         byte[] icmpChecksum = BitBufferHelper.toByteArray(checksum);
92         fieldValues.put(HEADERCHECKSUM, icmpChecksum);
93         return this;
94     }
95
96     /**
97      * Sets the ICMP identifier  for the current ICMP object instance
98      * @param short - identifier
99      * @return ICMP
100      */
101     public ICMP setIdentifier(short identifier) {
102         byte[] icmpIdentifier = BitBufferHelper.toByteArray(identifier);
103         fieldValues.put(IDENTIFIER, icmpIdentifier);
104         return this;
105     }
106
107     /**
108      * Sets the ICMP sequence number for the current ICMP object instance
109      * @param short - seqNumber
110      * @return ICMP
111      */
112     public ICMP setSequenceNumber(short seqNumber) {
113         byte[] icmpSeqNumber = BitBufferHelper.toByteArray(seqNumber);
114         fieldValues.put(SEQNUMBER, icmpSeqNumber);
115         return this;
116     }
117
118     @Override
119     public int hashCode() {
120         return HashCodeBuilder.reflectionHashCode(this);
121     }
122
123     @Override
124     public boolean equals(Object obj) {
125         return EqualsBuilder.reflectionEquals(this, obj);
126     }
127 }