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.md.compatibility.topology;
10 import java.util.ArrayList;
14 import org.opendaylight.controller.sal.binding.api.data.RuntimeDataProvider;
15 import org.opendaylight.controller.sal.core.ConstructionException;
16 import org.opendaylight.controller.sal.core.Edge;
17 import org.opendaylight.controller.sal.core.NodeConnector;
18 import org.opendaylight.controller.sal.core.Property;
19 import org.opendaylight.controller.switchmanager.ISwitchManager;
20 import org.opendaylight.controller.topologymanager.ITopologyManager;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class TopologyReader implements RuntimeDataProvider {
40 private static final Logger LOG = LoggerFactory.getLogger(TopologyReader.class);
41 private final InstanceIdentifier<Topology> topologyPath;
42 private final TopologyKey topologyKey;
43 private final TopologyMapping mapping;
44 private ITopologyManager topologyManager;
45 private ISwitchManager switchManager;
47 public ISwitchManager getSwitchManager() {
48 return this.switchManager;
51 public void setSwitchManager(final ISwitchManager switchManager) {
52 this.switchManager = switchManager;
55 public ITopologyManager getTopologyManager() {
56 return this.topologyManager;
59 public void setTopologyManager(final ITopologyManager topologyManager) {
60 this.topologyManager = topologyManager;
63 public TopologyKey getTopologyKey() {
64 return this.topologyKey;
67 public TopologyMapping getMapping() {
71 public TopologyReader() {
72 this.topologyKey = new TopologyKey(new TopologyId("compatibility:ad-sal"));
73 this.topologyPath = InstanceIdentifier.builder(NetworkTopology.class)
74 .child(Topology.class, topologyKey)
76 this.mapping = new TopologyMapping(topologyKey, topologyPath);
80 public DataObject readConfigurationData(final InstanceIdentifier<? extends DataObject> path) {
81 // Topology and Inventory are operational only
85 @SuppressWarnings("unchecked")
87 public DataObject readOperationalData(final InstanceIdentifier<? extends DataObject> path) {
88 if (!topologyPath.contains(path)) {
92 final Class<? extends DataObject> type = path.getTargetType();
93 if (Link.class.equals(type)) {
94 return readLink((InstanceIdentifier<Link>) path);
96 if (Node.class.equals(type)) {
97 return readNode((InstanceIdentifier<Node>) path);
99 if (TerminationPoint.class.equals(type)) {
100 return readTerminationPoint((InstanceIdentifier<TerminationPoint>) path);
103 if (Topology.class.equals(type)) {
104 return readTopology((InstanceIdentifier<Topology>) path);
107 LOG.debug("Unsupported type {}", type);
111 private Link readLink(final InstanceIdentifier<Link> identifier) {
114 edge = this.mapping.toAdTopologyEdge(identifier);
115 } catch (ConstructionException e) {
116 throw new IllegalStateException(String.format("Failed to construct edge for link %s", identifier), e);
119 final Map<Edge,Set<Property>> edges;
120 if (topologyManager != null) {
121 edges = topologyManager.getEdges();
126 final Set<Property> properties;
128 properties = edges.get(edge);
133 return constructLink(edge);
136 private TerminationPoint readTerminationPoint(final InstanceIdentifier<TerminationPoint> identifier) {
137 return constructTerminationPoint(mapping.toAdTopologyNodeConnector(identifier));
140 private Node readNode(final InstanceIdentifier<Node> identifier) {
141 return constructNode(mapping.toAdTopologyNode(identifier));
144 private Topology readTopology(final InstanceIdentifier<Topology> identifier) {
145 final Set<org.opendaylight.controller.sal.core.Node> nodes = getSwitchManager().getNodes();
146 final ArrayList<Node> nodeList = new ArrayList<Node>(nodes.size());
147 for (final org.opendaylight.controller.sal.core.Node node : nodes) {
148 nodeList.add(constructNode(node));
151 final Map<Edge,Set<Property>> edges = getTopologyManager().getEdges();
152 final ArrayList<Link> linkList = new ArrayList<Link>(edges.size());
153 for (final Edge edge : edges.keySet()) {
154 linkList.add(constructLink(edge));
157 return new TopologyBuilder()
164 private Link constructLink(final Edge edge) {
165 final NodeConnector sourceNc = edge.getTailNodeConnector();
166 final NodeConnector destNc = edge.getHeadNodeConnector();
168 final LinkBuilder it = new LinkBuilder().setKey(mapping.toTopologyLinkKey(edge));
170 it.setSource(new SourceBuilder()
171 .setSourceNode(mapping.toTopologyNodeKey(sourceNc.getNode()).getNodeId())
172 .setSourceTp(mapping.toTopologyTerminationPointKey(sourceNc).getTpId())
175 it.setDestination(new DestinationBuilder()
176 .setDestNode(mapping.toTopologyNodeKey(destNc.getNode()).getNodeId())
177 .setDestTp(mapping.toTopologyTerminationPointKey(destNc).getTpId())
183 private Node constructNode(final org.opendaylight.controller.sal.core.Node node) {
184 final Set<NodeConnector> connectors = getSwitchManager().getNodeConnectors(node);
185 final ArrayList<TerminationPoint> tpList = new ArrayList<TerminationPoint>(connectors.size());
186 for (final NodeConnector connector : connectors) {
187 tpList.add(constructTerminationPoint(connector));
190 return new NodeBuilder()
191 .setKey(mapping.toTopologyNodeKey(node))
192 .setTerminationPoint(tpList)
196 private TerminationPoint constructTerminationPoint(final NodeConnector connector) {
197 return new TerminationPointBuilder().setKey(mapping.toTopologyTerminationPointKey(connector)).build();