OPNFLWPLUG-898 Improve code quality in liblldp module
[openflowplugin.git] / libraries / liblldp / src / main / java / org / opendaylight / openflowplugin / libraries / liblldp / Packet.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.openflowplugin.libraries.liblldp;
10
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.util.Arrays;
13 import java.util.Map;
14 import java.util.Map.Entry;
15 import org.apache.commons.lang3.tuple.Pair;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Abstract class which represents the generic network packet object It provides
21  * the basic methods which are common for all the packets, like serialize and
22  * deserialize.
23  */
24 public abstract class Packet {
25     protected static final Logger LOG = LoggerFactory.getLogger(Packet.class);
26     // Access level granted to this packet
27     protected boolean writeAccess;
28     // When deserialized from wire, packet could result corrupted
29     protected boolean corrupted;
30     // The packet that encapsulate this packet
31     protected Packet parent;
32     // The packet encapsulated by this packet
33     protected Packet payload;
34     // The unparsed raw payload carried by this packet
35     protected byte[] rawPayload;
36     // Bit coordinates of packet header fields
37     protected Map<String, Pair<Integer, Integer>> hdrFieldCoordMap;
38     // Header fields values: Map<FieldName,Value>
39     protected Map<String, byte[]> hdrFieldsMap;
40     // The class of the encapsulated packet object
41     protected Class<? extends Packet> payloadClass;
42
43     public Packet() {
44         writeAccess = false;
45         corrupted = false;
46     }
47
48     public Packet(final boolean writeAccess) {
49         this.writeAccess = writeAccess;
50         corrupted = false;
51     }
52
53     public Packet getParent() {
54         return parent;
55     }
56
57     public Packet getPayload() {
58         return payload;
59     }
60
61     public void setParent(final Packet parent) {
62         this.parent = parent;
63     }
64
65     public void setPayload(final Packet payload) {
66         this.payload = payload;
67     }
68
69     public void setHeaderField(final String headerField, final byte[] readValue) {
70         hdrFieldsMap.put(headerField, readValue);
71     }
72
73     /**
74      * This method deserializes the data bits obtained from the wire into the
75      * respective header and payload which are of type Packet.
76      *
77      * @param data - data from wire to deserialize
78      * @param bitOffset bit position where packet header starts in data
79      *        array
80      * @param size size of packet in bits
81      * @return Packet
82      * @throws PacketException if deserialization fails
83      */
84     public Packet deserialize(final byte[] data, final int bitOffset, final int size)
85             throws PacketException {
86
87         // Deserialize the header fields one by one
88         int startOffset = 0;
89         int numBits = 0;
90         for (Entry<String, Pair<Integer, Integer>> pairs : hdrFieldCoordMap
91                 .entrySet()) {
92             String hdrField = pairs.getKey();
93             startOffset = bitOffset + this.getfieldOffset(hdrField);
94             numBits = this.getfieldnumBits(hdrField);
95
96             byte[] hdrFieldBytes;
97             try {
98                 hdrFieldBytes = BitBufferHelper.getBits(data, startOffset,
99                         numBits);
100             } catch (final BufferException e) {
101                 throw new PacketException("getBits failed", e);
102             }
103
104             /*
105              * Store the raw read value, checks the payload type and set the
106              * payloadClass accordingly
107              */
108             this.setHeaderField(hdrField, hdrFieldBytes);
109
110             if (LOG.isTraceEnabled()) {
111                 LOG.trace("{}: {}: {} (offset {} bitsize {})", this.getClass().getSimpleName(), hdrField,
112                         HexEncode.bytesToHexString(hdrFieldBytes), startOffset, numBits);
113             }
114         }
115
116         // Deserialize the payload now
117         int payloadStart = startOffset + numBits;
118         int payloadSize = data.length * NetUtils.NUM_BITS_IN_A_BYTE - payloadStart;
119
120         if (payloadClass != null) {
121             try {
122                 payload = payloadClass.newInstance();
123             } catch (InstantiationException | IllegalAccessException e) {
124                 throw new PacketException("Error parsing payload for Ethernet packet", e);
125             }
126             payload.deserialize(data, payloadStart, payloadSize);
127             payload.setParent(this);
128         } else {
129             /*
130              *  The payload class was not set, it means no class for parsing
131              *  this payload is present. Let's store the raw payload if any.
132              */
133             int start = payloadStart / NetUtils.NUM_BITS_IN_A_BYTE;
134             int stop = start + payloadSize / NetUtils.NUM_BITS_IN_A_BYTE;
135             rawPayload = Arrays.copyOfRange(data, start, stop);
136         }
137
138
139         // Take care of computation that can be done only after deserialization
140         postDeserializeCustomOperation(data, payloadStart - getHeaderSize());
141
142         return this;
143     }
144
145     /**
146      * This method serializes the header and payload from the respective
147      * packet class, into a single stream of bytes to be sent on the wire.
148      *
149      * @return The byte array representing the serialized Packet
150      * @throws PacketException if serialization fails
151      */
152     public byte[] serialize() throws PacketException {
153
154         // Acquire or compute the serialized payload
155         byte[] payloadBytes = null;
156         if (payload != null) {
157             payloadBytes = payload.serialize();
158         } else if (rawPayload != null) {
159             payloadBytes = rawPayload;
160         }
161         int payloadSize = payloadBytes == null ? 0 : payloadBytes.length;
162
163         // Allocate the buffer to contain the full (header + payload) packet
164         int headerSize = this.getHeaderSize() / NetUtils.NUM_BITS_IN_A_BYTE;
165         byte[] packetBytes = new byte[headerSize + payloadSize];
166         if (payloadBytes != null) {
167             System.arraycopy(payloadBytes, 0, packetBytes, headerSize, payloadSize);
168         }
169
170         // Serialize this packet header, field by field
171         for (Map.Entry<String, Pair<Integer, Integer>> pairs : hdrFieldCoordMap
172                 .entrySet()) {
173             String field = pairs.getKey();
174             byte[] fieldBytes = hdrFieldsMap.get(field);
175             // Let's skip optional fields when not set
176             if (fieldBytes != null) {
177                 try {
178                     BitBufferHelper.setBytes(packetBytes, fieldBytes,
179                             getfieldOffset(field), getfieldnumBits(field));
180                 } catch (final BufferException e) {
181                     throw new PacketException("setBytes failed", e);
182                 }
183             }
184         }
185
186         // Perform post serialize operations (like checksum computation)
187         postSerializeCustomOperation(packetBytes);
188
189         if (LOG.isTraceEnabled()) {
190             LOG.trace("{}: {}", this.getClass().getSimpleName(),
191                     HexEncode.bytesToHexString(packetBytes));
192         }
193
194         return packetBytes;
195     }
196
197     /**
198      * This method gets called at the end of the serialization process It is
199      * intended for the child packets to insert some custom data into the output
200      * byte stream which cannot be done or cannot be done efficiently during the
201      * normal Packet.serialize() path. An example is the checksum computation
202      * for IPv4
203      *
204      * @param myBytes serialized bytes
205      * @throws PacketException on failure
206      */
207     protected void postSerializeCustomOperation(byte[] myBytes) throws PacketException {
208         // no op
209     }
210
211     /**
212      * This method re-computes the checksum of the bits received on the wire and
213      * validates it with the checksum in the bits received Since the computation
214      * of checksum varies based on the protocol, this method is overridden.
215      * Currently only IPv4 and ICMP do checksum computation and validation. TCP
216      * and UDP need to implement these if required.
217      *
218      * @param data The byte stream representing the Ethernet frame
219      * @param startBitOffset The bit offset from where the byte array corresponding to this Packet starts in the frame
220      * @throws PacketException on failure
221      */
222     protected void postDeserializeCustomOperation(byte[] data, int startBitOffset) throws PacketException {
223         // no op
224     }
225
226     /**
227      * Gets the header length in bits.
228      *
229      * @return int the header length in bits
230      */
231     public int getHeaderSize() {
232         int size = 0;
233         /*
234          * We need to iterate over the fields that were read in the frame
235          * (hdrFieldsMap) not all the possible ones described in
236          * hdrFieldCoordMap. For ex, 802.1Q may or may not be there
237          */
238         for (Map.Entry<String, byte[]> fieldEntry : hdrFieldsMap.entrySet()) {
239             if (fieldEntry.getValue() != null) {
240                 String field = fieldEntry.getKey();
241                 size += getfieldnumBits(field);
242             }
243         }
244         return size;
245     }
246
247     /**
248      * This method fetches the start bit offset for header field specified by
249      * 'fieldname'. The offset is present in the hdrFieldCoordMap of the
250      * respective packet class
251      *
252      * @return Integer - startOffset of the requested field
253      */
254     public int getfieldOffset(final String fieldName) {
255         return hdrFieldCoordMap.get(fieldName).getLeft();
256     }
257
258     /**
259      * This method fetches the number of bits for header field specified by
260      * 'fieldname'. The numBits are present in the hdrFieldCoordMap of the
261      * respective packet class
262      *
263      * @return Integer - number of bits of the requested field
264      */
265     public int getfieldnumBits(final String fieldName) {
266         return hdrFieldCoordMap.get(fieldName).getRight();
267     }
268
269     @Override
270     public String toString() {
271         StringBuilder ret = new StringBuilder();
272         ret.append(this.getClass().getSimpleName());
273         ret.append(": [");
274         for (String field : hdrFieldCoordMap.keySet()) {
275             byte[] value = hdrFieldsMap.get(field);
276             ret.append(field);
277             ret.append(": ");
278             ret.append(HexEncode.bytesToHexString(value));
279             ret.append(", ");
280         }
281         ret.replace(ret.length() - 2, ret.length() - 1, "]");
282         return ret.toString();
283     }
284
285     /**
286      * Returns the raw payload carried by this packet in case payload was not
287      * parsed. Caller can call this function in case the getPaylod() returns null.
288      *
289      * @return The raw payload if not parsable as an array of bytes, null otherwise
290      */
291     @SuppressFBWarnings("EI_EXPOSE_REP")
292     public byte[] getRawPayload() {
293         return rawPayload;
294     }
295
296     /**
297      * Set a raw payload in the packet class.
298      *
299      * @param bytes The raw payload as byte array
300      */
301     public void setRawPayload(final byte[] bytes) {
302         this.rawPayload = Arrays.copyOf(bytes, bytes.length);
303     }
304
305     /**
306      * Return whether the deserialized packet is to be considered corrupted.
307      * This is the case when the checksum computed after reconstructing the
308      * packet received from wire is not equal to the checksum read from the
309      * stream. For the Packet class which do not have a checksum field, this
310      * function will always return false.
311      *
312      *
313      * @return true if the deserialized packet's recomputed checksum is not
314      *         equal to the packet carried checksum
315      */
316     public boolean isCorrupted() {
317         return corrupted;
318     }
319
320     @Override
321     public int hashCode() {
322         final int prime = 31;
323         int result = super.hashCode();
324         result = prime * result
325                 + (this.hdrFieldsMap == null ? 0 : hdrFieldsMap.hashCode());
326         return result;
327     }
328
329     @Override
330     public boolean equals(final Object obj) {
331         if (obj == null) {
332             return false;
333         }
334
335         if (this == obj) {
336             return true;
337         }
338         if (getClass() != obj.getClass()) {
339             return false;
340         }
341         Packet other = (Packet) obj;
342         if (hdrFieldsMap == other.hdrFieldsMap) {
343             return true;
344         }
345         if (hdrFieldsMap == null || other.hdrFieldsMap == null) {
346             return false;
347         }
348         for (Entry<String, byte[]> entry : hdrFieldsMap.entrySet()) {
349             String field = entry.getKey();
350             if (!Arrays.equals(entry.getValue(), other.hdrFieldsMap.get(field))) {
351                 return false;
352             }
353         }
354         return true;
355     }
356 }