2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.legacy.sal.compatibility.topology;
10 import com.google.common.base.Function;
11 import com.google.common.collect.FluentIterable;
13 import org.opendaylight.controller.md.sal.binding.util.TypeSafeDataReader;
14 import org.opendaylight.controller.sal.core.ConstructionException;
15 import org.opendaylight.controller.sal.core.Edge;
16 import org.opendaylight.controller.sal.core.Node;
17 import org.opendaylight.controller.sal.core.Node.NodeIDType;
18 import org.opendaylight.controller.sal.core.NodeConnector;
19 import org.opendaylight.controller.sal.core.NodeConnector.NodeConnectorIDType;
20 import org.opendaylight.controller.sal.core.Property;
21 import org.opendaylight.controller.sal.core.UpdateType;
22 import org.opendaylight.controller.sal.topology.TopoEdgeUpdate;
23 import org.opendaylight.openflowplugin.legacy.sal.compatibility.NodeMapping;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import java.util.List;
35 import java.util.concurrent.CopyOnWriteArrayList;
36 import java.util.regex.Pattern;
38 import static com.google.common.base.Preconditions.checkNotNull;
40 public final class TopologyMapping {
41 private static final Logger LOG = LoggerFactory.getLogger(TopologyMapping.class);
42 private final static Pattern NUMBERS_ONLY = Pattern.compile("[0-9]+");
44 private TopologyMapping() {
45 throw new UnsupportedOperationException("Utility class. Instantiation is not allowed.");
48 public static List<TopoEdgeUpdate> toADEdgeUpdates(final Topology topology,final TypeSafeDataReader reader) {
49 final List<TopoEdgeUpdate> result = new CopyOnWriteArrayList<>();
50 return FluentIterable.from(topology.getLink()).transform(
51 new Function<Link, TopoEdgeUpdate>() {
53 public TopoEdgeUpdate apply(final Link input) {
55 return toTopoEdgeUpdate(toAdEdge(input, topology), reader);
56 } catch (ConstructionException e) {
57 throw new IllegalArgumentException(String.format("Failed to construct edge update for {}", input), e);
63 public static Edge toAdEdge(final Link link, final Topology topology) throws ConstructionException {
64 final NodeConnector adSrc = toADNodeConnector(link.getSource().getSourceTp(), link.getSource().getSourceNode());
65 final NodeConnector adDst = toADNodeConnector(link.getDestination().getDestTp(), link.getDestination().getDestNode());
66 return new Edge(adSrc, adDst);
69 public static TopoEdgeUpdate toTopoEdgeUpdate(final Edge e, final TypeSafeDataReader reader) {
70 return toTopoEdgeUpdate(e, UpdateType.ADDED, reader);
73 public static TopoEdgeUpdate toTopoEdgeUpdate(final Edge e,final UpdateType type,final TypeSafeDataReader reader) {
74 return new TopoEdgeUpdate(e, toAdEdgeProperties(e, reader), type);
77 public static Set<Property> toAdEdgeProperties(final Edge e,final TypeSafeDataReader reader) {
78 final NodeConnectorRef ncref = NodeMapping.toNodeConnectorRef(e.getTailNodeConnector());
80 LOG.debug("Edge {} ncref {}",e,ncref);
84 @SuppressWarnings("unchecked")
85 final InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector> ncInstanceId =
86 (InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector>) ncref.getValue();
87 if(ncInstanceId == null) {
88 LOG.debug("Edge {} ncref {}",e,ncref);
92 final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector nc = reader.readOperationalData(ncInstanceId);
96 return NodeMapping.toADNodeConnectorProperties(nc);
99 public static String toADNodeId(final NodeId nodeId) {
100 return nodeId.getValue().replaceFirst("^.*:", "");
103 public static NodeConnector toADNodeConnector(final TpId source, final NodeId nodeId) throws ConstructionException {
104 checkNotNull(source);
105 String nodeConnectorIdStripped = toADNodeConnectorId(source);
106 if (NUMBERS_ONLY.matcher(nodeConnectorIdStripped).matches()) {
107 return new NodeConnector(NodeConnectorIDType.OPENFLOW, Short.valueOf(nodeConnectorIdStripped), toADNode(nodeId));
109 LOG.debug("NodeConnectorId does not match openflow id type, using " + NodeMapping.MD_SAL_TYPE + "instead");
110 NodeConnectorIDType.registerIDType(NodeMapping.MD_SAL_TYPE, String.class, NodeMapping.MD_SAL_TYPE);
111 return new NodeConnector(NodeMapping.MD_SAL_TYPE, nodeConnectorIdStripped, toADNode(nodeId));
114 public static String toADNodeConnectorId(final TpId nodeConnectorId) {
115 return nodeConnectorId.getValue().replaceFirst("^.*:", "");
118 public static Node toADNode(final NodeId nodeId) throws ConstructionException {
119 checkNotNull(nodeId);
120 String nodeIdStripped = toADNodeId(nodeId);
121 if (NUMBERS_ONLY.matcher(nodeIdStripped).matches()) {
122 return new Node(NodeIDType.OPENFLOW, Long.valueOf(nodeIdStripped));
124 LOG.debug("NodeId does not match openflow id type, using " + NodeMapping.MD_SAL_TYPE + "instead");
125 NodeIDType.registerIDType(NodeMapping.MD_SAL_TYPE, String.class);
126 return new Node(NodeMapping.MD_SAL_TYPE, nodeId.getValue());