*/
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;
import org.opendaylight.controller.sal.core.Capabilities;
import org.opendaylight.controller.sal.core.Config;
import org.opendaylight.controller.sal.core.ConstructionException;
+import org.opendaylight.controller.sal.core.Description;
import org.opendaylight.controller.sal.core.MacAddress;
import org.opendaylight.controller.sal.core.Name;
import org.opendaylight.controller.sal.core.Node.NodeIDType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-
-import java.math.BigInteger;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.List;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public final class NodeMapping {
- /** openflow id prefix */
+ private static final Logger LOG = LoggerFactory
+ .getLogger(NodeMapping.class);
+
+ /**
+ * openflow id prefix
+ */
public static final String OPENFLOW_ID_PREFIX = "openflow:";
public final static String MD_SAL_TYPE = "MD_SAL_DEPRECATED";
private final static Class<NodeConnector> 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.");
}
}
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);
+ }
}
/**
* @param adNodeId
- * @return
+ * @return nodeId as long
*/
- private static Long openflowFullNodeIdToLong(String adNodeId) {
+ @VisibleForTesting
+ public static Long openflowFullNodeIdToLong(String adNodeId) {
if (adNodeId == null) {
return null;
}
- return Long.valueOf(adNodeId.replaceFirst("^.*:", ""));
+ return new BigInteger(adNodeId).longValue();
}
public static NodeId toNodeId(final InstanceIdentifier<?> id) {
return key.getId();
}
+ /**
+ * @param nodeId containing "<NodeTypeString>:<plainIntegerId>"
+ * @return adNodeId form
+ */
public static String toADNodeId(final NodeId nodeId) {
if (nodeId == null) {
return null;
return new org.opendaylight.controller.sal.core.NodeConnector(nodeConnectorType, aDNodeConnectorId, aDNode);
}
+ /**
+ * @param ncid nodeConnector identifier, e.g.: OF:21 or CTRL
+ * @param aDNode
+ * @return nodeConnector attached to given node
+ * @throws ConstructionException
+ */
+ public static org.opendaylight.controller.sal.core.NodeConnector toADNodeConnector(
+ final NodeConnectorId ncid, final org.opendaylight.controller.sal.core.Node aDNode) throws ConstructionException {
+ NodeId nid = NodeMapping.toNodeId(aDNode);
+ String nodeConnectorType = NodeMapping.toNodeConnectorType(ncid, nid);
+ Object aDNodeConnectorId = NodeMapping.toADNodeConnectorId(ncid, nid);
+ return new org.opendaylight.controller.sal.core.NodeConnector(nodeConnectorType, aDNodeConnectorId, aDNode);
+ }
+
+ /**
+ * @param aDNode
+ * @return
+ */
+ private static NodeId toNodeId(org.opendaylight.controller.sal.core.Node aDNode) {
+ return new NodeId(aDNode.getType() + ":" + String.valueOf(aDNode.getID()));
+ }
+
public static String toNodeConnectorType(final NodeConnectorId ncId, final NodeId nodeId) {
if (ncId.equals(toLocalNodeConnectorId(nodeId))) {
return NodeConnectorIDType.SWSTACK;
return org.opendaylight.controller.sal.core.NodeConnector.SPECIALNODECONNECTORID;
}
- return (short) Long.valueOf(nodeConnectorId.getValue().replaceFirst("^.*:", "")).longValue();
+ 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 (long): {}", nodeConnectorIdStripped, e);
+ }
+ }
+ return nodeConnectorIdStripped;
}
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;
}
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);
}
public static HashSet<Property> toADNodeConnectorProperties(final NodeConnectorUpdated nc) {
final FlowCapableNodeConnectorUpdated fcncu = nc.<FlowCapableNodeConnectorUpdated>getAugmentation(FlowCapableNodeConnectorUpdated.class);
if (!Objects.equal(fcncu, null)) {
- return NodeMapping.toADNodeConnectorProperties(fcncu);
+ HashSet<Property> adNodeConnectorProperties = NodeMapping.toADNodeConnectorProperties(fcncu);
+ return adNodeConnectorProperties;
}
return new HashSet<Property>();
}
+ /**
+ * @param nodeRef
+ * @return node description in AD form, e.g.: OF|00:00:00:...:01
+ */
+ private static Description toADDescription(NodeRef nodeRef) {
+ Description desc;
+ try {
+ desc = new Description(toADNode(nodeRef).toString());
+ } catch (ConstructionException e) {
+ desc = new Description("none");
+ LOG.warn("node description extraction failed: {}", nodeRef);
+ }
+ return desc;
+ }
+
public static HashSet<Property> toADNodeConnectorProperties(final NodeConnector nc) {
final FlowCapableNodeConnector fcnc = nc.<FlowCapableNodeConnector>getAugmentation(FlowCapableNodeConnector.class);
if (!Objects.equal(fcnc, null)) {
public static HashSet<Property> toADNodeProperties(final NodeUpdated nu) {
final FlowCapableNodeUpdated fcnu = nu.getAugmentation(FlowCapableNodeUpdated.class);
if (fcnu != null) {
- return toADNodeProperties(fcnu, nu.getId());
+ HashSet<Property> adNodeProperties = toADNodeProperties(fcnu, nu.getId());
+ adNodeProperties.add(toADDescription(nu.getNodeRef()));
+ return adNodeProperties;
}
return new HashSet<org.opendaylight.controller.sal.core.Property>();
}
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;
+ }
}