Controller to listen to expired flow removal message
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / match / MatchType.java
index 93e5a5874c64787a5fa63cac604030e2f6b5ce68..5ad4bd5b660dbc58e4041a5422205d61edf4a3d0 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
  *
 package org.opendaylight.controller.sal.match;
 
 import java.net.InetAddress;
+import java.util.Arrays;
 
 import org.opendaylight.controller.sal.core.NodeConnector;
 import org.opendaylight.controller.sal.utils.HexEncode;
 import org.opendaylight.controller.sal.utils.NetUtils;
 
 /**
- * Represents the binding between the id, the value and mask type and the range values
- * of the elements type that can be matched on the network frame/packet/message
- *
- *
- *
+ * Represents the binding between the id, the value and mask type and the range
+ * values of the elements type that can be matched on the network
+ * frame/packet/message
+ * 
+ * 
+ * 
  */
 public enum MatchType {
-    IN_PORT("inPort", 1 << 0, NodeConnector.class, 1, 0), 
-    DL_SRC("dlSrc", 1 << 1, Byte[].class, 0, 0xffffffffffffL), 
-    DL_DST("dlDst", 1 << 2, Byte[].class, 0, 0xffffffffffffL), 
+    IN_PORT("inPort", 1 << 0, NodeConnector.class, 1, 0),
+    DL_SRC("dlSrc", 1 << 1, Byte[].class, 0, 0xffffffffffffL),
+    DL_DST("dlDst", 1 << 2, Byte[].class, 0, 0xffffffffffffL),
     DL_VLAN("dlVlan", 1 << 3, Short.class, 1, 0xfff), // 2 bytes
     DL_VLAN_PR("dlVlanPriority", 1 << 4, Byte.class, 0, 0x7), // 3 bits
-    DL_OUTER_VLAN("dlOuterVlan", 1 << 5, Short.class, 1, 0xfff), 
-    DL_OUTER_VLAN_PR("dlOuterVlanPriority", 1 << 6, Short.class, 0, 0x7), 
+    DL_OUTER_VLAN("dlOuterVlan", 1 << 5, Short.class, 1, 0xfff),
+    DL_OUTER_VLAN_PR("dlOuterVlanPriority", 1 << 6, Short.class, 0, 0x7),
     DL_TYPE("dlType", 1 << 7, Short.class, 0, 0xffff), // 2 bytes
     NW_TOS("nwTOS", 1 << 8, Byte.class, 0, 0x3f), // 6 bits (DSCP field)
     NW_PROTO("nwProto", 1 << 9, Byte.class, 0, 0xff), // 1 byte
-    NW_SRC("nwSrc", 1 << 10, InetAddress.class, 0, 0), 
-    NW_DST("nwDst", 1 << 11, InetAddress.class, 0, 0), 
+    NW_SRC("nwSrc", 1 << 10, InetAddress.class, 0, 0),
+    NW_DST("nwDst", 1 << 11, InetAddress.class, 0, 0),
     TP_SRC("tpSrc", 1 << 12, Short.class, 1, 0xffff), // 2 bytes
     TP_DST("tpDst", 1 << 13, Short.class, 1, 0xffff); // 2 bytes
 
@@ -71,7 +72,8 @@ public enum MatchType {
     }
 
     /**
-     *  Perform the assignment type validation
+     * Perform the assignment type validation
+     * 
      * @param value
      * @param mask
      * @return
@@ -85,7 +87,8 @@ public enum MatchType {
         Class<?> e = this.dataType();
         Class<?> g = value.getClass();
 
-        // This is all what we need, if value type is same of match required type
+        // This is all what we need, if value type is same of match required
+        // type
         if (g.equals(e)) {
             return true;
         }
@@ -116,6 +119,7 @@ public enum MatchType {
 
     /**
      * Perform the value and mask range validation
+     * 
      * @param value
      * @param mask
      * @return
@@ -158,12 +162,13 @@ public enum MatchType {
 
     /**
      * Return the mask value in 64 bits bitmask form
+     * 
      * @param mask
      * @return
      */
     public long getBitMask(Object mask) {
         if (this.dataType == InetAddress.class) {
-            //TODO handle Inet v4 and v6 v6 will have a second upper mask
+            // TODO handle Inet v4 and v6 v6 will have a second upper mask
             return 0;
         }
         if (this.dataType() == Byte[].class) {
@@ -173,7 +178,6 @@ public enum MatchType {
             byte mac[] = (byte[]) mask;
             long bitmask = 0;
             for (short i = 0; i < 6; i++) {
-                //                             bitmask |= (((long)mac[i] & 0xffL) << (long)((5-i)*8));
                 bitmask |= (((long) mac[i] & 0xffL) << ((5 - i) * 8));
             }
             return bitmask;
@@ -193,32 +197,76 @@ public enum MatchType {
         return 0L;
     }
 
-       public String stringify(Object value) {
-               if (value == null) {
-                       return null;
-               }
-               
-               switch (this) {
-               case DL_DST:
-               case DL_SRC:
-                       return HexEncode.bytesToHexStringFormat((byte[])value);
-               case DL_TYPE:
-               case DL_VLAN:
-                       return ((Integer) NetUtils.getUnsignedShort((Short)value))
-                                       .toString();
-               case NW_SRC:
-               case NW_DST:
-                       return ((InetAddress)value).getHostAddress();
-               case NW_TOS:
-                       return ((Integer) NetUtils.getUnsignedByte((Byte)value))
-                                       .toString();
-               case TP_SRC:
-               case TP_DST:
-                       return ((Integer) NetUtils.getUnsignedShort((Short)value))
-                                       .toString();
-               default:
-                       break;
-               }
-               return value.toString();
-       }
+    public String stringify(Object value) {
+        if (value == null) {
+            return null;
+        }
+
+        switch (this) {
+        case DL_DST:
+        case DL_SRC:
+            return HexEncode.bytesToHexStringFormat((byte[]) value);
+        case DL_TYPE:
+        case DL_VLAN:
+            return ((Integer) NetUtils.getUnsignedShort((Short) value))
+                    .toString();
+        case NW_SRC:
+        case NW_DST:
+            return ((InetAddress) value).getHostAddress();
+        case NW_TOS:
+            return ((Integer) NetUtils.getUnsignedByte((Byte) value))
+                    .toString();
+        case TP_SRC:
+        case TP_DST:
+            return ((Integer) NetUtils.getUnsignedShort((Short) value))
+                    .toString();
+        default:
+            break;
+        }
+        return value.toString();
+    }
+
+    public boolean equalValues(Object a, Object b) {
+        if (a == b) {
+            return true;
+        }
+        if (a == null || b == null) {
+            return false;
+        }
+        switch (this) {
+        case DL_DST:
+        case DL_SRC:
+            return Arrays.equals((byte[]) a, (byte[]) b);
+        default:
+            return a.equals(b);
+        }
+    }
+
+    public boolean equalMasks(Object a, Object b) {
+        if (a == b) {
+            return true;
+        }
+        switch (this) {
+        case NW_SRC:
+        case NW_DST:
+            /*
+             * For network address mask, network node may return full mask for
+             * flows the controller generated with a null mask object
+             */
+            byte maskBytes[] = null;
+            if (a == null) {
+                maskBytes = ((InetAddress) b).getAddress();
+            } else if (b == null) {
+                maskBytes = ((InetAddress) a).getAddress();
+            }
+            if (maskBytes != null) {
+                return (NetUtils.getSubnetMaskLength(maskBytes) == 0);
+            }
+        default:
+            if (a == null) {
+                return false;
+            }
+            return a.equals(b);
+        }
+    }
 }