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.controller.sal.compatibility.topology;
10 import static com.google.common.base.Preconditions.checkNotNull;
12 import java.util.List;
14 import java.util.concurrent.CopyOnWriteArrayList;
16 import org.opendaylight.controller.md.sal.binding.util.TypeSafeDataReader;
17 import org.opendaylight.controller.sal.compatibility.NodeMapping;
18 import org.opendaylight.controller.sal.core.ConstructionException;
19 import org.opendaylight.controller.sal.core.Edge;
20 import org.opendaylight.controller.sal.core.Node;
21 import org.opendaylight.controller.sal.core.Node.NodeIDType;
22 import org.opendaylight.controller.sal.core.NodeConnector;
23 import org.opendaylight.controller.sal.core.NodeConnector.NodeConnectorIDType;
24 import org.opendaylight.controller.sal.core.Property;
25 import org.opendaylight.controller.sal.core.UpdateType;
26 import org.opendaylight.controller.sal.topology.TopoEdgeUpdate;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import com.google.common.base.Function;
37 import com.google.common.collect.FluentIterable;
39 public final class TopologyMapping {
40 private static final Logger LOG = LoggerFactory.getLogger(TopologyMapping.class);
42 private TopologyMapping() {
43 throw new UnsupportedOperationException("Utility class. Instantiation is not allowed.");
46 public static List<TopoEdgeUpdate> toADEdgeUpdates(final Topology topology,final TypeSafeDataReader reader) {
47 final List<TopoEdgeUpdate> result = new CopyOnWriteArrayList<>();
48 return FluentIterable.from(topology.getLink()).transform(
49 new Function<Link, TopoEdgeUpdate>() {
51 public TopoEdgeUpdate apply(final Link input) {
53 return toTopoEdgeUpdate(toAdEdge(input, topology), reader);
54 } catch (ConstructionException e) {
55 throw new IllegalArgumentException(String.format("Failed to construct edge update for {}", input), e);
61 public static Edge toAdEdge(final Link link, final Topology topology) throws ConstructionException {
62 final NodeConnector adSrc = toADNodeConnector(link.getSource().getSourceTp(), link.getSource().getSourceNode());
63 final NodeConnector adDst = toADNodeConnector(link.getDestination().getDestTp(), link.getDestination().getDestNode());
64 return new Edge(adSrc, adDst);
67 public static TopoEdgeUpdate toTopoEdgeUpdate(final Edge e, final TypeSafeDataReader reader) {
68 return toTopoEdgeUpdate(e, UpdateType.ADDED, reader);
71 public static TopoEdgeUpdate toTopoEdgeUpdate(final Edge e,final UpdateType type,final TypeSafeDataReader reader) {
72 return new TopoEdgeUpdate(e, toAdEdgeProperties(e, reader), type);
75 public static Set<Property> toAdEdgeProperties(final Edge e,final TypeSafeDataReader reader) {
76 final NodeConnectorRef ncref = NodeMapping.toNodeConnectorRef(e.getTailNodeConnector());
78 LOG.debug("Edge {} ncref {}",e,ncref);
82 @SuppressWarnings("unchecked")
83 final InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector> ncInstanceId =
84 (InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector>) ncref.getValue();
85 if(ncInstanceId == null) {
86 LOG.debug("Edge {} ncref {}",e,ncref);
90 final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector nc = reader.readOperationalData(ncInstanceId);
94 return NodeMapping.toADNodeConnectorProperties(nc);
97 public static String toADNodeId(final NodeId nodeId) {
98 return nodeId.getValue().replaceFirst("^.*:", "");
101 public static NodeConnector toADNodeConnector(final TpId source, final NodeId nodeId) throws ConstructionException {
102 checkNotNull(source);
103 return new NodeConnector(NodeConnectorIDType.OPENFLOW, Short.valueOf(toADNodeConnectorId(source)), toADNode(nodeId));
106 public static String toADNodeConnectorId(final TpId nodeConnectorId) {
107 return nodeConnectorId.getValue().replaceFirst("^.*:", "");
110 public static Node toADNode(final NodeId nodeId) throws ConstructionException {
111 checkNotNull(nodeId);
112 return new Node(NodeIDType.OPENFLOW, Long.valueOf(toADNodeId(nodeId)));