Make use of NetUtils.getBroadcastMacAddr()
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / match / MatchField.java
index 19c365b29e041c338083e1ccc7f5e26a8e47509f..554d7e2fb3df93564cc7575fe6a83486deaa131f 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
  *
@@ -9,38 +8,52 @@
 
 package org.opendaylight.controller.sal.match;
 
-import org.apache.commons.lang3.builder.EqualsBuilder;
-import org.apache.commons.lang3.builder.HashCodeBuilder;
-import org.opendaylight.controller.sal.utils.HexEncode;
+import java.io.Serializable;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * Represents the generic matching field
  *
- *
- *
  */
-public class MatchField implements Cloneable {
-    private static final Logger logger = LoggerFactory
-            .getLogger(MatchField.class);
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.NONE)
+public class MatchField implements Cloneable, Serializable {
+    private static final long serialVersionUID = 1L;
+    private static final Logger logger = LoggerFactory.getLogger(MatchField.class);
     private MatchType type; // the field we want to match
     private Object value; // the value of the field we want to match
-    private Object mask; // the value of the mask we want to match on the specified field
-    private transient boolean isValid;
+    private Object mask; // the value of the mask we want to match on the
+    // specified field
+    private boolean isValid;
+
+    // To satisfy JAXB
+    @SuppressWarnings("unused")
+    private MatchField() {
+    }
 
     /**
      * Mask based match constructor
      *
      * @param type
      * @param value
-     * @param mask   has to be of the same class type of value. A null mask means full match
+     * @param mask
+     *            has to be of the same class type of value. A null mask means
+     *            full match
      */
     public MatchField(MatchType type, Object value, Object mask) {
         this.type = type;
         this.value = value;
         this.mask = mask;
-        this.isValid = checkValueType() && checkValues(); // Keep this logic, value checked only if type check is fine
+        // Keep this logic, value checked only if type check is fine
+        this.isValid = checkValueType() && checkValues();
     }
 
     /**
@@ -53,7 +66,8 @@ public class MatchField implements Cloneable {
         this.type = type;
         this.value = value;
         this.mask = null;
-        this.isValid = checkValueType() && checkValues(); // Keep this logic, value checked only if type check is fine
+        // Keep this logic, value checked only if type check is fine
+        this.isValid = checkValueType() && checkValues();
     }
 
     /**
@@ -65,6 +79,11 @@ public class MatchField implements Cloneable {
         return value;
     }
 
+    @XmlElement(name = "value")
+    private String getValueString() {
+        return type.stringify(value);
+    }
+
     /**
      * Returns the type field this match field object is for
      *
@@ -74,15 +93,26 @@ public class MatchField implements Cloneable {
         return type;
     }
 
+    @XmlElement(name = "type")
+    private String getTypeString() {
+        return type.toString();
+    }
+
     /**
-     * Returns the mask value set for this field match
-     * A null mask means this is a full match
+     * Returns the mask value set for this field match A null mask means this is
+     * a full match
+     *
      * @return
      */
     public Object getMask() {
         return mask;
     }
 
+    @XmlElement(name = "mask")
+    private String getMaskString() {
+        return type.stringify(mask);
+    }
+
     /**
      * Returns the bitmask set for this field match
      *
@@ -103,16 +133,11 @@ public class MatchField implements Cloneable {
 
     private boolean checkValueType() {
         if (type.isCongruentType(value, mask) == false) {
-            String valueClass = (value == null) ? "null" : value.getClass()
-                    .getSimpleName();
-            String maskClass = (mask == null) ? "null" : mask.getClass()
-                    .getSimpleName();
-            String error = "Invalid match field's value or mask types.For field: "
-                    + type.id()
-                    + " Expected:"
-                    + type.dataType().getSimpleName()
-                    + " or equivalent,"
-                    + " Got:(" + valueClass + "," + maskClass + ")";
+            String valueClass = (value == null) ? "null" : value.getClass().getSimpleName();
+            String maskClass = (mask == null) ? "null" : mask.getClass().getSimpleName();
+            String error = "Invalid match field's value or mask types.For field: " + type.id() + " Expected:"
+                    + type.dataType().getSimpleName() + " or equivalent," + " Got:(" + valueClass + "," + maskClass
+                    + ")";
             throwException(error);
             return false;
         }
@@ -123,13 +148,9 @@ public class MatchField implements Cloneable {
         if (type.isValid(value, mask) == false) {
             String maskString = (mask == null) ? "null" : ("0x" + Integer
                     .toHexString(Integer.parseInt(mask.toString())));
-            String error = "Invalid match field's value or mask assignement.For field: "
-                    + type.id()
-                    + " Expected: "
-                    + type.getRange()
-                    + ", Got:(0x"
-                    + Integer.toHexString(Integer.parseInt(value.toString()))
-                    + "," + maskString + ")";
+            String error = "Invalid match field's value or mask assignement.For field: " + type.id() + " Expected: "
+                    + type.getRange() + ", Got:(0x" + Integer.toHexString(Integer.parseInt(value.toString())) + ","
+                    + maskString + ")";
 
             throwException(error);
             return false;
@@ -156,31 +177,40 @@ public class MatchField implements Cloneable {
                     cloned.mask = ((byte[]) this.mask).clone();
                 }
             }
+            cloned.type = this.type;
+            cloned.isValid = this.isValid;
         } catch (CloneNotSupportedException e) {
-            e.printStackTrace();
+            logger.error("", e);
         }
         return cloned;
     }
 
     @Override
-    public int hashCode() {
-        return HashCodeBuilder.reflectionHashCode(this);
+    public String toString() {
+        return (mask == null) ? String.format("%s(%s)", getTypeString(), getValueString()) :
+            String.format("%s(%s,%s)", getTypeString(), getValueString(), getMaskString());
     }
 
     @Override
-    public boolean equals(Object obj) {
-        return EqualsBuilder.reflectionEquals(this, obj);
+    public int hashCode() {
+        return type.hashCode(value, mask);
     }
 
     @Override
-    public String toString() {
-        String valueString = (value == null) ? "null"
-                : (value instanceof byte[]) ? HexEncode
-                        .bytesToHexString((byte[]) value) : value.toString();
-        String maskString = (mask == null) ? "null"
-                : (mask instanceof byte[]) ? HexEncode
-                        .bytesToHexString((byte[]) mask) : mask.toString();
-
-        return type + "(" + valueString + "," + maskString + ")";
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        MatchField other = (MatchField) obj;
+        if (type != other.type) {
+            return false;
+        }
+        return type.equals(this.value, other.value, this.mask, other.mask);
     }
 }