Merge "Custom Node Type jaxb string to id"
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / core / Node.java
index b199e5ce72f4bfaeab1e7a02426c8449ce8b357b..5a71ab8a692fce503c7dddb6d3fc768ec25723f4 100644 (file)
  */
 package org.opendaylight.controller.sal.core;
 
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.Set;
-import java.math.BigInteger;
-
 import java.io.Serializable;
-import java.lang.String;
+import java.math.BigInteger;
+import java.util.Set;
 import java.util.UUID;
-import java.lang.Long;
-import java.lang.Class;
-import org.apache.commons.lang3.builder.HashCodeBuilder;
-import org.apache.commons.lang3.builder.EqualsBuilder;
-import org.opendaylight.controller.sal.utils.HexEncode;
+import java.util.concurrent.ConcurrentHashMap;
 
-import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.opendaylight.controller.sal.utils.HexEncode;
+import org.opendaylight.controller.sal.utils.INodeFactory;
+import org.opendaylight.controller.sal.utils.ServiceHelper;
 
 /**
  * Describe a generic network element in multiple SDNs technologies. A
@@ -43,6 +40,7 @@ import javax.xml.bind.annotation.XmlAttribute;
  *
  */
 @XmlAccessorType(XmlAccessType.NONE)
+@XmlRootElement
 public class Node implements Serializable {
     private static final long serialVersionUID = 1L;
 
@@ -55,8 +53,8 @@ public class Node implements Serializable {
      * surround.
      */
     public static final class NodeIDType {
-        private static final ConcurrentHashMap<String, Class> compatibleType =
-            new ConcurrentHashMap<String, Class>();
+        private static final ConcurrentHashMap<String, Class<? extends Object>> compatibleType =
+            new ConcurrentHashMap<String, Class<? extends Object>>();
         /**
          * Identifier for an OpenFlow node
          */
@@ -115,7 +113,7 @@ public class Node implements Serializable {
          * @return true if registered, false otherwise
          */
         public static boolean registerIDType(String type,
-                                             Class compatibleID) {
+                                             Class<? extends Object> compatibleID) {
             if (compatibleType.get(type) != null) {
                 return false;
             }  else {
@@ -230,15 +228,20 @@ public class Node implements Serializable {
         } else if (typeStr.equals(NodeIDType.PRODUCTION)) {
             this.nodeID = IDStr;
         } else {
-            // We need to lookup via OSGi service registry for an
-            // handler for this
+            //Use plugin's method to get appropriate conversion from IDStr to nodeID
+            INodeFactory f = (INodeFactory) ServiceHelper
+                    .getGlobalInstance(INodeFactory.class, new Node(), "(protocolName="+typeStr+")");
+            if(f!=null){
+                Node n = f.fromString(typeStr, IDStr);
+                this.nodeID = n.nodeID;
+            }
         }
     }
 
-    /** 
+    /**
      * Private setter for nodeType to be called by JAXB not by anyone
      * else, Node is immutable
-     * 
+     *
      * @param type of node to be set
      */
     private void setType(String type) {
@@ -271,10 +274,10 @@ public class Node implements Serializable {
             return this.nodeID.toString();
         }
     }
-    
-    /** 
+
+    /**
      * private setter to be used by JAXB
-     * 
+     *
      * @param nodeIDString String representation for NodeID
      */
     private void setNodeIDString(String nodeIDString) {
@@ -286,24 +289,34 @@ public class Node implements Serializable {
 
     @Override
     public int hashCode() {
-        return new HashCodeBuilder(163841, 56473)
-            .append(nodeType)
-            .append(nodeID)
-            .hashCode();
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((nodeID == null) ? 0 : nodeID.hashCode());
+        result = prime * result
+                + ((nodeType == null) ? 0 : nodeType.hashCode());
+        return result;
     }
 
     @Override
     public boolean equals(Object obj) {
-        if (obj == null) { return false; }
-        if (obj == this) { return true; }
-        if (obj.getClass() != getClass()) {
+        if (this == obj)
+            return true;
+        if (obj == null)
             return false;
-        }
-        Node rhs = (Node)obj;
-        return new EqualsBuilder()
-            .append(this.getType(), rhs.getType())
-            .append(this.getID(), rhs.getID())
-            .isEquals();
+        if (getClass() != obj.getClass())
+            return false;
+        Node other = (Node) obj;
+        if (nodeID == null) {
+            if (other.nodeID != null)
+                return false;
+        } else if (!nodeID.equals(other.nodeID))
+            return false;
+        if (nodeType == null) {
+            if (other.nodeType != null)
+                return false;
+        } else if (!nodeType.equals(other.nodeType))
+            return false;
+        return true;
     }
 
     @Override
@@ -431,9 +444,13 @@ public class Node implements Serializable {
                 return null;
             }
         } else {
-            // We need to lookup via OSGi service registry for an
-            // handler for this
+            //Use INodeFactory to create a Node of registered Node type.
+            //The protocol plugin being used depends on typeStr.
+            INodeFactory f = (INodeFactory) ServiceHelper
+                    .getGlobalInstance(INodeFactory.class, new Node(), "(protocolName="+typeStr+")");
+            if(f==null)
+                return null;
+            return f.fromString(typeStr, IDStr);
         }
-        return null;
     }
 }