Merge "Bug 1308 - Unable to publish NodeUpdated message via NotificationService"
authorTony Tkacik <ttkacik@cisco.com>
Thu, 7 Aug 2014 20:05:07 +0000 (20:05 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 7 Aug 2014 20:05:07 +0000 (20:05 +0000)
opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/NodeMapping.java
opendaylight/md-sal/compatibility/sal-compatibility/src/test/java/org/opendaylight/controller/sal/compatibility/test/NodeMappingTest.java

index 02964c6d5531553d23abaa0cce39dfae1db30881..be087abdb41099b9a5d8505ed20f7ac59624f531 100644 (file)
@@ -7,11 +7,13 @@
  */
 package org.opendaylight.controller.sal.compatibility;
 
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Objects;
+import com.google.common.base.Preconditions;
 import java.math.BigInteger;
 import java.util.Date;
 import java.util.HashSet;
 import java.util.List;
-
 import org.opendaylight.controller.sal.common.util.Arguments;
 import org.opendaylight.controller.sal.core.AdvertisedBandwidth;
 import org.opendaylight.controller.sal.core.Bandwidth;
@@ -62,16 +64,14 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Objects;
-import com.google.common.base.Preconditions;
-
 public final class NodeMapping {
 
     private static final Logger LOG = LoggerFactory
             .getLogger(NodeMapping.class);
 
-    /** openflow id prefix */
+    /**
+     * openflow id prefix
+     */
     public static final String OPENFLOW_ID_PREFIX = "openflow:";
 
     public final static String MD_SAL_TYPE = "MD_SAL_DEPRECATED";
@@ -90,8 +90,14 @@ public final class NodeMapping {
     }
 
     public static org.opendaylight.controller.sal.core.Node toADNode(final NodeId id) throws ConstructionException {
-        Long aDNodeId = openflowFullNodeIdToLong(NodeMapping.toADNodeId(id));
-        return new org.opendaylight.controller.sal.core.Node(NodeIDType.OPENFLOW, aDNodeId);
+        String nodeId = NodeMapping.toADNodeId(id);
+        String nodeIdasNumber = nodeId.replaceFirst("^.*:", "");
+        if (isInteger(nodeIdasNumber)) {
+            Long aDNodeId = openflowFullNodeIdToLong(nodeIdasNumber);
+            return new org.opendaylight.controller.sal.core.Node(NodeIDType.OPENFLOW, aDNodeId);
+        } else {
+            return new org.opendaylight.controller.sal.core.Node(NodeIDType.PRODUCTION, nodeId);
+        }
     }
 
     /**
@@ -103,7 +109,7 @@ public final class NodeMapping {
         if (adNodeId == null) {
             return null;
         }
-        return new BigInteger(adNodeId.replaceFirst("^.*:", "")).longValue();
+        return new BigInteger(adNodeId).longValue();
     }
 
     public static NodeId toNodeId(final InstanceIdentifier<?> id) {
@@ -137,8 +143,8 @@ public final class NodeMapping {
     }
 
     /**
-     * @param ncid nodeConnector identifier, e.g.: OF:21 or CTRL
-     * @param node
+     * @param ncid   nodeConnector identifier, e.g.: OF:21 or CTRL
+     * @param aDNode
      * @return nodeConnector attached to given node
      * @throws ConstructionException
      */
@@ -155,7 +161,7 @@ public final class NodeMapping {
      * @return
      */
     private static NodeId toNodeId(org.opendaylight.controller.sal.core.Node aDNode) {
-        return new NodeId(aDNode.getType() + ":" +String.valueOf(aDNode.getID()));
+        return new NodeId(aDNode.getType() + ":" + String.valueOf(aDNode.getID()));
     }
 
     public static String toNodeConnectorType(final NodeConnectorId ncId, final NodeId nodeId) {
@@ -212,7 +218,7 @@ public final class NodeMapping {
     public static NodeRef toNodeRef(final org.opendaylight.controller.sal.core.Node node) {
         Preconditions.checkArgument(NodeIDType.OPENFLOW.equals(node.getType()));
         final Long nodeId = Arguments.<Long>checkInstanceOf(node.getID(), Long.class);
-        final NodeKey nodeKey = new NodeKey(new NodeId(OPENFLOW_ID_PREFIX+nodeId));
+        final NodeKey nodeKey = new NodeKey(new NodeId(OPENFLOW_ID_PREFIX + nodeId));
         final InstanceIdentifier<Node> nodePath = InstanceIdentifier.builder(Nodes.class).child(NODE_CLASS, nodeKey).toInstance();
         return new NodeRef(nodePath);
     }
@@ -257,7 +263,7 @@ public final class NodeMapping {
     }
 
     /**
-     * @param id
+     * @param nodeRef
      * @return node description in AD form, e.g.: OF|00:00:00:...:01
      */
     private static Description toADDescription(NodeRef nodeRef) {
@@ -463,4 +469,17 @@ public final class NodeMapping {
     public static Buffers toADBuffers(final Long buffers) {
         return new Buffers(buffers.intValue());
     }
+
+
+    private static final boolean isInteger(String value) {
+        if (value.isEmpty()) return false;
+        for (int i = 0; i < value.length(); i++) {
+            if (i == 0 && value.charAt(i) == '-') {
+                if (value.length() == 1) return false;
+                else continue;
+            }
+            if (Character.digit(value.charAt(i), 10) < 0) return false;
+        }
+        return true;
+    }
 }
index cef7ae7a427672b8276411b2e1ace77f66dc04fd..a776ef231292c1fa562cf19b7a1fe7571d7e8a06 100644 (file)
@@ -101,6 +101,16 @@ public class NodeMappingTest {
         } catch (ConstructionException e) {
             Assert.fail("should succeed to construct Node: "+e.getMessage());
         }
+
+        final String nodeUriPrefix = "opendaylight-inventory:nodes/node/";
+        nodeId = new NodeId(nodeUriPrefix + "iosv-2");
+        try {
+            observed = NodeMapping.toADNode(nodeId);
+            Assert.assertEquals("PR|opendaylight-inventory:nodes/node/iosv-2", observed.toString());
+        } catch (ConstructionException e) {
+            Assert.fail("should succeed to construct Node: "+e.getMessage());
+        }
+
     }
 
     /**