Bug 1808: Don't deserialize IPv4 payload in non-first fragments.
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / packet / Packet.java
index b19c0f862b9e559219d5a00e8a3d289122a6c1a8..789aa126533c93c16660e5eafcfc995dc510fab1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2013-2014 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -13,6 +13,7 @@ import java.util.Map;
 import java.util.Map.Entry;
 
 import org.apache.commons.lang3.tuple.Pair;
+import org.opendaylight.controller.sal.match.Match;
 import org.opendaylight.controller.sal.utils.HexEncode;
 import org.opendaylight.controller.sal.utils.NetUtils;
 import org.slf4j.Logger;
@@ -77,7 +78,7 @@ 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
-     * 
+     *
      * @param byte[] data - data from wire to deserialize
      * @param int bitOffset bit position where packet header starts in data
      *        array
@@ -151,7 +152,7 @@ public abstract class Packet {
     /**
      * This method serializes the header and payload from the respective
      * packet class, into a single stream of bytes to be sent on the wire
-     * 
+     *
      * @return The byte array representing the serialized Packet
      * @throws PacketException
      */
@@ -206,7 +207,7 @@ public abstract class Packet {
      * 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
      */
@@ -221,7 +222,7 @@ public abstract class Packet {
      * of checksum varies based on the protocol, this method is overridden.
      * Currently only IPv4 and ICMP do checksum computation and validation. TCP
      * and UDP need to implement these if required.
-     * 
+     *
      * @param byte[] data The byte stream representing the Ethernet frame
      * @param int startBitOffset The bit offset from where the byte array corresponding to this Packet starts in the frame
      * @throws PacketException
@@ -233,7 +234,7 @@ public abstract class Packet {
 
     /**
      * Gets the header length in bits
-     * 
+     *
      * @return int the header length in bits
      */
     public int getHeaderSize() {
@@ -256,7 +257,7 @@ 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
      * @return Integer - startOffset of the requested field
@@ -269,7 +270,7 @@ 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
      * @return Integer - number of bits of the requested field
@@ -297,7 +298,7 @@ public abstract class Packet {
     /**
      * Returns the raw payload carried by this packet in case payload was not
      * parsed. Caller can call this function in case the getPaylod() returns null.
-     * 
+     *
      * @return The raw payload if not parsable as an array of bytes, null otherwise
      */
     public byte[] getRawPayload() {
@@ -306,7 +307,7 @@ public abstract class Packet {
 
     /**
      * Set a raw payload in the packet class
-     * 
+     *
      * @param payload The raw payload as byte array
      */
     public void setRawPayload(byte[] payload) {
@@ -319,8 +320,8 @@ public abstract class Packet {
      * packet received from wire is not equal to the checksum read from the
      * stream. For the Packet class which do not have a checksum field, this
      * function will always return false.
-     * 
-     * 
+     *
+     *
      * @return true if the deserialized packet's recomputed checksum is not
      *         equal to the packet carried checksum
      */
@@ -364,4 +365,31 @@ public abstract class Packet {
         return true;
     }
 
+    /**
+     * Adds to the passed Match this packet's header fields
+     *
+     * @param match
+     *            The Match object to populate
+     */
+    public void populateMatch(Match match) {
+        // To be overridden by derived packet classes which have well known
+        // header fields so that Packet.getMatch would return desired result
+    }
+
+    /**
+     * Returns the Match object containing this packet and its payload
+     * encapsulated packets' header fields
+     *
+     * @return The Match containing the header fields of this packet and of its
+     *         payload encapsulated packets
+     */
+    public Match getMatch() {
+        Match match = new Match();
+        Packet packet = this;
+        while (packet != null) {
+            packet.populateMatch(match);
+            packet = packet.getPayload();
+        }
+        return match;
+    }
 }