Added new methods IPv4.getDscp() / IPv4.setDscp()
and IPv4.getEcn() / IPv4.setEcn().

Those can be used to get/set the DSCP and ECN bits in the IPv4 header
without explicit bit manipulation at the caller.

Change-Id: Ia7c5779abae5c4fc7a343e3f7ef3355eb7e86e3d
diff --git a/apps/fwd/src/main/java/org/onosproject/fwd/ReactiveForwarding.java b/apps/fwd/src/main/java/org/onosproject/fwd/ReactiveForwarding.java
index 7abf134..dcb28f9 100644
--- a/apps/fwd/src/main/java/org/onosproject/fwd/ReactiveForwarding.java
+++ b/apps/fwd/src/main/java/org/onosproject/fwd/ReactiveForwarding.java
@@ -505,10 +505,9 @@
                         .matchIPProtocol(ipv4Protocol);
 
                 if (matchIpv4Dscp) {
-                    int dscp = ipv4Packet.getDiffServ() >>> 2;
-                    int ecn = ipv4Packet.getDiffServ() % 4;
-                    builder.matchIPDscp((byte) (dscp))
-                            .matchIPEcn((byte) (ecn));
+                    byte dscp = ipv4Packet.getDscp();
+                    byte ecn = ipv4Packet.getEcn();
+                    builder.matchIPDscp(dscp).matchIPEcn(ecn);
                 }
 
                 if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_TCP) {
diff --git a/utils/misc/src/main/java/org/onlab/packet/IPv4.java b/utils/misc/src/main/java/org/onlab/packet/IPv4.java
index 574fd1a..1e00ca5 100644
--- a/utils/misc/src/main/java/org/onlab/packet/IPv4.java
+++ b/utils/misc/src/main/java/org/onlab/packet/IPv4.java
@@ -41,6 +41,10 @@
         IPv4.PROTOCOL_CLASS_MAP.put(IPv4.PROTOCOL_UDP, UDP.class);
     }
 
+    private static final byte DSCP_MASK = 0x3f;
+    private static final byte DSCP_OFFSET = 2;
+    private static final byte ECN_MASK = 0x3;
+
     protected byte version;
     protected byte headerLength;
     protected byte diffServ;
@@ -91,15 +95,61 @@
     }
 
     /**
-     * @return the diffServ
+     * Gets the DSCP value (6 bits).
+     *
+     * @return the DSCP value (6 bits)
+     */
+    public byte getDscp() {
+        return (byte) ((this.diffServ >>> DSCP_OFFSET) & DSCP_MASK);
+    }
+
+    /**
+     * Sets the DSCP value (6 bits).
+     *
+     * @param dscp the DSCP value (6 bits)
+     * @return this
+     */
+    public IPv4 setDscp(byte dscp) {
+        this.diffServ &= ~(DSCP_MASK << DSCP_OFFSET);
+        this.diffServ |= (dscp & DSCP_MASK) << DSCP_OFFSET;
+        return this;
+    }
+
+    /**
+     * Gets the ECN value (2 bits).
+     *
+     * @return the ECN value (2 bits)
+     */
+    public byte getEcn() {
+        return (byte) (this.diffServ & ECN_MASK);
+    }
+
+    /**
+     * Sets the ECN value (2 bits).
+     *
+     * @param ecn the ECN value (2 bits)
+     * @return this
+     */
+    public IPv4 setEcn(byte ecn) {
+        this.diffServ &= ~ECN_MASK;
+        this.diffServ |= (ecn & ECN_MASK);
+        return this;
+    }
+
+    /**
+     * Gets the DiffServ octet (including the DSCP and ECN bits).
+     *
+     * @return the diffServ octet (including the DSCP and ECN bits)
      */
     public byte getDiffServ() {
         return this.diffServ;
     }
 
     /**
-     * @param diffServ
-     *            the diffServ to set
+     * Sets the DiffServ octet (including the DSCP and ECN bits).
+     *
+     * @param diffServ the diffServ octet to set (including the DSCP and ECN
+     * bits)
      * @return this
      */
     public IPv4 setDiffServ(final byte diffServ) {