a4ac6f94ee5460fb224a98ed5be83eb6bc22ad15
[controller.git] / opendaylight / md-sal / compatibility / inventory-topology-compatibility / src / main / java / org / opendaylight / controller / md / compatibility / topology / TopologyReader.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.compatibility.topology;
9
10 import java.util.ArrayList;
11 import java.util.Map;
12 import java.util.Set;
13
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;
38
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;
46
47     public ISwitchManager getSwitchManager() {
48         return this.switchManager;
49     }
50
51     public void setSwitchManager(final ISwitchManager switchManager) {
52         this.switchManager = switchManager;
53     }
54
55     public ITopologyManager getTopologyManager() {
56         return this.topologyManager;
57     }
58
59     public void setTopologyManager(final ITopologyManager topologyManager) {
60         this.topologyManager = topologyManager;
61     }
62
63     public TopologyKey getTopologyKey() {
64         return this.topologyKey;
65     }
66
67     public TopologyMapping getMapping() {
68         return this.mapping;
69     }
70
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)
75                 .toInstance();
76         this.mapping = new TopologyMapping(topologyKey, topologyPath);
77     }
78
79     @Override
80     public DataObject readConfigurationData(final InstanceIdentifier<? extends DataObject> path) {
81         // Topology and Inventory are operational only
82         return null;
83     }
84
85     @SuppressWarnings("unchecked")
86     @Override
87     public DataObject readOperationalData(final InstanceIdentifier<? extends DataObject> path) {
88         if (!topologyPath.contains(path)) {
89             return null;
90         }
91
92         final Class<? extends DataObject> type = path.getTargetType();
93         if (Link.class.equals(type)) {
94             return readLink((InstanceIdentifier<Link>) path);
95         }
96         if (Node.class.equals(type)) {
97             return readNode((InstanceIdentifier<Node>) path);
98         }
99         if (TerminationPoint.class.equals(type)) {
100             return readTerminationPoint((InstanceIdentifier<TerminationPoint>) path);
101
102         }
103         if (Topology.class.equals(type)) {
104             return readTopology((InstanceIdentifier<Topology>) path);
105         }
106
107         LOG.debug("Unsupported type {}", type);
108         return null;
109     }
110
111     private Link readLink(final InstanceIdentifier<Link> identifier) {
112         final Edge edge;
113         try {
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);
117         }
118
119         final Map<Edge,Set<Property>> edges;
120         if (topologyManager != null) {
121             edges = topologyManager.getEdges();
122         } else {
123             edges = null;
124         }
125
126         final Set<Property> properties;
127         if (edges != null) {
128             properties = edges.get(edge);
129         } else {
130             properties = null;
131         }
132
133         return constructLink(edge);
134     }
135
136     private TerminationPoint readTerminationPoint(final InstanceIdentifier<TerminationPoint> identifier) {
137         return constructTerminationPoint(mapping.toAdTopologyNodeConnector(identifier));
138     }
139
140     private Node readNode(final InstanceIdentifier<Node> identifier) {
141         return constructNode(mapping.toAdTopologyNode(identifier));
142     }
143
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));
149         }
150
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));
155         }
156
157         return new TopologyBuilder()
158         .setKey(topologyKey)
159         .setNode(nodeList)
160         .setLink(linkList)
161         .build();
162     }
163
164     private Link constructLink(final Edge edge) {
165         final NodeConnector sourceNc = edge.getTailNodeConnector();
166         final NodeConnector destNc = edge.getHeadNodeConnector();
167
168         final LinkBuilder it = new LinkBuilder().setKey(mapping.toTopologyLinkKey(edge));
169
170         it.setSource(new SourceBuilder()
171         .setSourceNode(mapping.toTopologyNodeKey(sourceNc.getNode()).getNodeId())
172         .setSourceTp(mapping.toTopologyTerminationPointKey(sourceNc).getTpId())
173         .build());
174
175         it.setDestination(new DestinationBuilder()
176         .setDestNode(mapping.toTopologyNodeKey(destNc.getNode()).getNodeId())
177         .setDestTp(mapping.toTopologyTerminationPointKey(destNc).getTpId())
178         .build());
179
180         return it.build();
181     }
182
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));
188         }
189
190         return new NodeBuilder()
191         .setKey(mapping.toTopologyNodeKey(node))
192         .setTerminationPoint(tpList)
193         .build();
194     }
195
196     private TerminationPoint constructTerminationPoint(final NodeConnector connector) {
197         return new TerminationPointBuilder().setKey(mapping.toTopologyTerminationPointKey(connector)).build();
198     }
199 }