X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fcompatibility%2Fsal-compatibility%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fcompatibility%2FNodeMapping.java;h=bcb2367e7a35685b8a660bf9b7d2a00c9effcb1b;hp=02964c6d5531553d23abaa0cce39dfae1db30881;hb=531621aac4cff9d39cbd8668a53bdeba8a0e6d81;hpb=eee61ea351b6179d2862dce6875bdc25dd0fb272 diff --git a/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/NodeMapping.java b/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/NodeMapping.java index 02964c6d55..bcb2367e7a 100644 --- a/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/NodeMapping.java +++ b/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/NodeMapping.java @@ -7,11 +7,14 @@ */ 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 java.util.regex.Pattern; import org.opendaylight.controller.sal.common.util.Arguments; import org.opendaylight.controller.sal.core.AdvertisedBandwidth; import org.opendaylight.controller.sal.core.Bandwidth; @@ -62,16 +65,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"; @@ -80,6 +81,12 @@ public final class NodeMapping { private final static Class NODECONNECTOR_CLASS = NodeConnector.class; + private final static Pattern COLON_NUMBERS_EOL = Pattern.compile(":[0-9]+$"); + + private final static Pattern NUMBERS_ONLY = Pattern.compile("[0-9]+"); + + private final static Pattern ALL_CHARS_TO_COLON = Pattern.compile("^.*:"); + private NodeMapping() { throw new UnsupportedOperationException("Utility class. Instantiation is not allowed."); } @@ -90,8 +97,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 +116,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 +150,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 +168,22 @@ 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())); + String targetPrefix = null; + if (NodeIDType.OPENFLOW.equals(aDNode.getType())) { + targetPrefix = OPENFLOW_ID_PREFIX; + } else { + targetPrefix = aDNode.getType() + ":"; + } + + return new NodeId(targetPrefix + String.valueOf(aDNode.getID())); + } + + /** + * @param aDNode + * @return md-sal {@link NodeKey} + */ + public static NodeKey toNodeKey(org.opendaylight.controller.sal.core.Node aDNode) { + return new NodeKey(toNodeId(aDNode)); } public static String toNodeConnectorType(final NodeConnectorId ncId, final NodeId nodeId) { @@ -176,15 +204,16 @@ public final class NodeMapping { return org.opendaylight.controller.sal.core.NodeConnector.SPECIALNODECONNECTORID; } - String nodeConnectorIdStripped = nodeConnectorId.getValue().replaceFirst("^.*:", ""); - if (nodeConnectorIdStripped.matches("[0-9]+")) { + String nodeConnectorIdStripped = ALL_CHARS_TO_COLON.matcher(nodeConnectorId.getValue()).replaceFirst(""); + + if (NUMBERS_ONLY.matcher(nodeConnectorIdStripped).matches()) { Short nodeConnectorIdVal = null; try { nodeConnectorIdVal = Short.valueOf(nodeConnectorIdStripped); + return nodeConnectorIdVal; } catch (NumberFormatException e) { - LOG.warn("nodeConnectorId not supported (short): {}", nodeConnectorIdStripped, e); + LOG.warn("nodeConnectorId not supported (long): {}", nodeConnectorIdStripped, e); } - return nodeConnectorIdVal; } return nodeConnectorIdStripped; } @@ -192,7 +221,7 @@ public final class NodeMapping { public static NodeId toAdNodeId(final NodeConnectorId nodeConnectorId) { NodeId nodeId = null; if (nodeConnectorId != null) { - nodeId = new NodeId(nodeConnectorId.getValue().replaceFirst(":[0-9]+$", "")); + nodeId = new NodeId(COLON_NUMBERS_EOL.matcher(nodeConnectorId.getValue()).replaceFirst("")); } return nodeId; } @@ -212,7 +241,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.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 nodePath = InstanceIdentifier.builder(Nodes.class).child(NODE_CLASS, nodeKey).toInstance(); return new NodeRef(nodePath); } @@ -257,7 +286,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 +492,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; + } }