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.NodeConnector;
22 import org.opendaylight.controller.sal.core.Property;
23 import org.opendaylight.controller.sal.core.UpdateType;
24 import org.opendaylight.controller.sal.topology.TopoEdgeUpdate;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import com.google.common.base.Function;
35 import com.google.common.collect.FluentIterable;
37 public final class TopologyMapping {
38 private static final Logger LOG = LoggerFactory.getLogger(TopologyMapping.class);
40 private TopologyMapping() {
41 throw new UnsupportedOperationException("Utility class. Instantiation is not allowed.");
44 public static List<TopoEdgeUpdate> toADEdgeUpdates(final Topology topology,final TypeSafeDataReader reader) {
45 final List<TopoEdgeUpdate> result = new CopyOnWriteArrayList<>();
46 return FluentIterable.from(topology.getLink()).transform(
47 new Function<Link, TopoEdgeUpdate>() {
49 public TopoEdgeUpdate apply(final Link input) {
51 return toTopoEdgeUpdate(toAdEdge(input, topology), reader);
52 } catch (ConstructionException e) {
53 throw new IllegalArgumentException(String.format("Failed to construct edge update for {}", input), e);
59 public static Edge toAdEdge(final Link link, final Topology topology) throws ConstructionException {
60 final NodeConnector adSrc = toADNodeConnector(link.getSource().getSourceTp(), link.getSource().getSourceNode());
61 final NodeConnector adDst = toADNodeConnector(link.getDestination().getDestTp(), link.getDestination().getDestNode());
62 return new Edge(adSrc, adDst);
65 public static TopoEdgeUpdate toTopoEdgeUpdate(final Edge e, final TypeSafeDataReader reader) {
66 return toTopoEdgeUpdate(e, UpdateType.ADDED, reader);
69 public static TopoEdgeUpdate toTopoEdgeUpdate(final Edge e,final UpdateType type,final TypeSafeDataReader reader) {
70 return new TopoEdgeUpdate(e, toAdEdgeProperties(e, reader), type);
73 public static Set<Property> toAdEdgeProperties(final Edge e,final TypeSafeDataReader reader) {
74 final NodeConnectorRef ncref = NodeMapping.toNodeConnectorRef(e.getTailNodeConnector());
76 LOG.debug("Edge {} ncref {}",e,ncref);
80 @SuppressWarnings("unchecked")
81 final InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector> ncInstanceId =
82 (InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector>) ncref.getValue();
83 if(ncInstanceId == null) {
84 LOG.debug("Edge {} ncref {}",e,ncref);
88 final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector nc = reader.readOperationalData(ncInstanceId);
92 return NodeMapping.toADNodeConnectorProperties(nc);
95 public static String toADNodeId(final NodeId nodeId) {
96 return nodeId.getValue();
99 public static NodeConnector toADNodeConnector(final TpId source, final NodeId nodeId) throws ConstructionException {
100 checkNotNull(source);
101 return new NodeConnector(NodeMapping.MD_SAL_TYPE, toADNodeConnectorId(source), toADNode(nodeId));
104 public static String toADNodeConnectorId(final TpId nodeConnectorId) {
105 return nodeConnectorId.getValue();
108 public static Node toADNode(final NodeId nodeId) throws ConstructionException {
109 checkNotNull(nodeId);
110 return new Node(NodeMapping.MD_SAL_TYPE, toADNodeId(nodeId));