Merge "new LinkedList() mostly redundant with Arrays.asList()"
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / packet / Packet.java
index 61d6d248c8108c9fd4b263d8cca9207ea1ffd3ef..446ec3e230e8bd11d0b9dc7451f8824b12adfda6 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
  *
@@ -17,16 +16,20 @@ import java.util.Map.Entry;
 import org.apache.commons.lang3.tuple.Pair;
 import org.opendaylight.controller.sal.utils.HexEncode;
 import org.opendaylight.controller.sal.utils.NetUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
- * Abstract class which represents the generic network packet object
- * It provides the basic methods which are common for all the packets,
- * like serialize and deserialize
- *
- *
+ * Abstract class which represents the generic network packet object It provides
+ * the basic methods which are common for all the packets, like serialize and
+ * deserialize
+ * 
+ * 
  */
 
 public abstract class Packet {
+    protected static final Logger logger = LoggerFactory
+            .getLogger(Packet.class);
     // Access level granted to this packet
     protected boolean writeAccess;
     // When deserialized from wire, packet could result corrupted
@@ -73,17 +76,19 @@ public abstract class Packet {
     }
 
     /**
-     * This method deserializes the data bits obtained from the wire
-     * into the respective header and payload which are of type Packet
+     * This method deserializes the data bits obtained from the wire into the
+     * respective header and payload which are of type Packet
+     * 
      * @param byte[] data - data from wire to deserialize
-     * @param int bitOffset bit position where packet header starts in data array
+     * @param int bitOffset bit position where packet header starts in data
+     *        array
      * @param int size of packet in bits
      * @return Packet
-     * @throws Exception
+     * @throws PacketException
      */
 
     public Packet deserialize(byte[] data, int bitOffset, int size)
-            throws Exception {
+            throws PacketException {
         String hdrField;
         Integer startOffset = 0, numBits = 0;
         byte[] hdrFieldBytes;
@@ -94,18 +99,30 @@ public abstract class Packet {
             startOffset = bitOffset + this.getfieldOffset(hdrField);
             numBits = this.getfieldnumBits(hdrField);
 
-            hdrFieldBytes = BitBufferHelper.getBits(data, startOffset, numBits);
+            try {
+                hdrFieldBytes = BitBufferHelper.getBits(data, startOffset,
+                        numBits);
+            } catch (BufferException e) {
+                throw new PacketException(e.getMessage());
+            }
             /*
-             * Store the raw read value, checks the payload type and
-             * set the payloadClass accordingly
+             * Store the raw read value, checks the payload type and set the
+             * payloadClass accordingly
              */
+            if (logger.isTraceEnabled()) {
+              logger.trace("{}: {}: {} (offset {} bitsize {})",
+                      new Object[] { this.getClass().getSimpleName(), hdrField,
+                              HexEncode.bytesToHexString(hdrFieldBytes),
+                              startOffset, numBits });
+            }
+
             this.setHeaderField(hdrField, hdrFieldBytes);
         }
 
         postDeserializeCustomOperation(data, startOffset);
 
         int payloadStart = startOffset + numBits;
-        //int payloadSize = size - payloadStart;
+        // int payloadSize = size - payloadStart;
         int payloadSize = data.length * NetUtils.NumBitsInAByte - payloadStart;
 
         if (payloadClass != null) {
@@ -124,14 +141,14 @@ public abstract class Packet {
     }
 
     /**
-     * This method serializes the header and payload bytes from
-     * the respective packet class, into a single stream of bytes
-     * to be sent on the wire
+     * This method serializes the header and payload bytes from the respective
+     * packet class, into a single stream of bytes to be sent on the wire
+     * 
      * @return byte[] - serialized bytes
-     * @throws Exception
+     * @throws PacketException
      */
 
-    public byte[] serialize() throws Exception {
+    public byte[] serialize() throws PacketException {
         byte[] payloadBytes = null;
         int payloadSize = 0;
         int headerSize = this.getHeaderSize();
@@ -164,55 +181,65 @@ public abstract class Packet {
             if (fieldBytes != null) {
                 startOffset = this.getfieldOffset(field);
                 numBits = this.getfieldnumBits(field);
-                BitBufferHelper.setBytes(headerBytes, fieldBytes, startOffset,
-                        numBits);
+                try {
+                    BitBufferHelper.setBytes(headerBytes, fieldBytes,
+                            startOffset, numBits);
+                } catch (BufferException e) {
+                    throw new PacketException(e.getMessage());
+                }
             }
         }
         postSerializeCustomOperation(headerBytes);
 
+        if (logger.isTraceEnabled()) {
+          logger.trace("{}: {}", this.getClass().getSimpleName(),
+                  HexEncode.bytesToHexString(headerBytes));
+        }
         return headerBytes;
     }
 
     /**
-     * This method gets called at the end of the serialization process
-     * It is intended for the child packets to insert some custom data
-     * into the output byte stream which cannot be done or cannot be done
-     * efficiently during the normal Packet.serialize() path.
-     * An example is the checksum computation for IPv4
+     * This method gets called at the end of the serialization process It is
+     * intended for the child packets to insert some custom data into the output
+     * byte stream which cannot be done or cannot be done efficiently during the
+     * normal Packet.serialize() path. An example is the checksum computation
+     * for IPv4
+     * 
      * @param byte[] - serialized bytes
+     * @throws PacketException
      */
     protected void postSerializeCustomOperation(byte[] myBytes)
-            throws Exception {
+            throws PacketException {
         // no op
     }
 
     /**
-     * This method re-computes the checksum of the bits received on the
-     * wire and validates it with the checksum in the bits received
-     * Since the computation of checksum varies based on the protocol,
-     * this method is overridden
-     * Currently only IPv4 does checksum computation and validation
-     * TCP and UDP need to implement these if required
+     * This method re-computes the checksum of the bits received on the wire and
+     * validates it with the checksum in the bits received Since the computation
+     * of checksum varies based on the protocol, this method is overridden
+     * Currently only IPv4 does checksum computation and validation TCP and UDP
+     * need to implement these if required
+     * 
      * @param byte[] data
      * @param int endBitOffset
-     * @return void
+     * @throws PacketException
      */
     protected void postDeserializeCustomOperation(byte[] data, int endBitOffset)
-            throws Exception {
-        //             no op
+            throws PacketException {
+        // no op
     }
 
     /**
      * Gets the header length in bits
-     * @return int
-     * @throws Exception
+     * 
+     * @return int the header length in bits
      */
-    public int getHeaderSize() throws Exception {
+    public int getHeaderSize() {
         int size = 0;
         /*
-         *  We need to iterate over the fields that were read in the frame (hdrFieldsMap)
-         *  not all the possible ones described in hdrFieldCoordMap.
-         *  For ex, 802.1Q may or may not be there
+         * We need to iterate over the fields that were read in the frame
+         * (hdrFieldsMap) not all the possible ones described in
+         * hdrFieldCoordMap. For ex, 802.1Q may or may not be there
          */
         for (Map.Entry<String, byte[]> fieldEntry : hdrFieldsMap.entrySet()) {
             if (fieldEntry.getValue() != null) {
@@ -225,9 +252,11 @@ public abstract class Packet {
 
     /**
      * This method fetches the start bit offset for header field specified by
-     * 'fieldname'.  The offset is present in the hdrFieldCoordMap of the respective
-     * packet class
-     * @param String fieldName
+     * 'fieldname'. The offset is present in the hdrFieldCoordMap of the
+     * respective packet class
+     * 
+     * @param String
+     *            fieldName
      * @return Integer - startOffset of the requested field
      */
     public int getfieldOffset(String fieldName) {
@@ -237,12 +266,14 @@ public abstract class Packet {
 
     /**
      * This method fetches the number of bits for header field specified by
-     * 'fieldname'.  The numBits are present in the hdrFieldCoordMap of the respective
-     * packet class
-     * @param String fieldName
+     * 'fieldname'. The numBits are present in the hdrFieldCoordMap of the
+     * respective packet class
+     * 
+     * @param String
+     *            fieldName
      * @return Integer - number of bits of the requested field
      */
-    public int getfieldnumBits(String fieldName) throws Exception {
+    public int getfieldnumBits(String fieldName) {
         return (((Pair<Integer, Integer>) hdrFieldCoordMap.get(fieldName))
                 .getRight());
     }
@@ -257,15 +288,13 @@ public abstract class Packet {
             } else if (entry.getValue().length == 4) {
                 try {
                     ret.append(InetAddress.getByAddress(entry.getValue())
-                            .getHostAddress()
-                            + " ");
+                            .getHostAddress() + " ");
                 } catch (UnknownHostException e) {
-                    e.printStackTrace();
+                    logger.error("", e);
                 }
             } else {
                 ret.append(((Long) BitBufferHelper.getLong(entry.getValue()))
-                        .toString()
-                        + " ");
+                        .toString() + " ");
             }
         }
         return ret.toString();
@@ -273,6 +302,7 @@ public abstract class Packet {
 
     /**
      * Returns true if the packet is corrupted
+     * 
      * @return boolean
      */
     protected boolean isPacketCorrupted() {