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.md.controller.topology.manager;
10 import static org.opendaylight.md.controller.topology.manager.FlowCapableNodeMapping.getNodeConnectorKey;
11 import static org.opendaylight.md.controller.topology.manager.FlowCapableNodeMapping.getNodeKey;
12 import static org.opendaylight.md.controller.topology.manager.FlowCapableNodeMapping.toTerminationPoint;
13 import static org.opendaylight.md.controller.topology.manager.FlowCapableNodeMapping.toTerminationPointId;
14 import static org.opendaylight.md.controller.topology.manager.FlowCapableNodeMapping.toTopologyLink;
15 import static org.opendaylight.md.controller.topology.manager.FlowCapableNodeMapping.toTopologyNode;
16 import static org.opendaylight.md.controller.topology.manager.FlowCapableNodeMapping.toTopologyNodeId;
18 import java.util.concurrent.Future;
20 import org.opendaylight.controller.md.sal.binding.util.TypeSafeDataReader;
21 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
22 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
23 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.FlowTopologyDiscoveryListener;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkOverutilized;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkUtilizationNormal;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.opendaylight.yangtools.yang.common.RpcResult;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 import com.google.common.util.concurrent.FutureCallback;
57 import com.google.common.util.concurrent.Futures;
58 import com.google.common.util.concurrent.JdkFutureAdapters;
60 class FlowCapableTopologyExporter implements //
61 FlowTopologyDiscoveryListener, //
62 OpendaylightInventoryListener //
65 protected final static Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyExporter.class);
66 public static TopologyKey topology = new TopologyKey(new TopologyId("flow:1"));
68 // FIXME: Flow capable topology exporter should use transaction chaining API
69 private DataProviderService dataService;
71 public DataProviderService getDataService() {
75 public void setDataService(final DataProviderService dataService) {
76 this.dataService = dataService;
79 private InstanceIdentifier<Topology> topologyPath;
82 TopologyBuilder tb = new TopologyBuilder();
84 topologyPath = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, topology).build();
85 Topology top = tb.build();
86 DataModificationTransaction tx = dataService.beginTransaction();
87 tx.putOperationalData(topologyPath, top);
88 listenOnTransactionState(tx.getIdentifier(),tx.commit());
92 public synchronized void onNodeRemoved(final NodeRemoved notification) {
93 NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeRef()).getId());
94 InstanceIdentifier<Node> nodeInstance = toNodeIdentifier(notification.getNodeRef());
96 DataModificationTransaction tx = dataService.beginTransaction();
97 tx.removeOperationalData(nodeInstance);
98 removeAffectedLinks(tx, nodeId);
99 listenOnTransactionState(tx.getIdentifier(),tx.commit());
103 public synchronized void onNodeUpdated(final NodeUpdated notification) {
104 FlowCapableNodeUpdated fcnu = notification.getAugmentation(FlowCapableNodeUpdated.class);
106 Node node = toTopologyNode(toTopologyNodeId(notification.getId()), notification.getNodeRef());
107 InstanceIdentifier<Node> path = getNodePath(toTopologyNodeId(notification.getId()));
108 DataModificationTransaction tx = dataService.beginTransaction();
109 tx.putOperationalData(path, node);
110 listenOnTransactionState(tx.getIdentifier(),tx.commit());
115 public synchronized void onNodeConnectorRemoved(final NodeConnectorRemoved notification) {
116 InstanceIdentifier<TerminationPoint> tpInstance = toTerminationPointIdentifier(notification
117 .getNodeConnectorRef());
118 TpId tpId = toTerminationPointId(getNodeConnectorKey(notification.getNodeConnectorRef()).getId());
119 DataModificationTransaction tx = dataService.beginTransaction();
120 tx.removeOperationalData(tpInstance);
121 removeAffectedLinks(tx, tpId);
122 listenOnTransactionState(tx.getIdentifier(),tx.commit());
127 public synchronized void onNodeConnectorUpdated(final NodeConnectorUpdated notification) {
128 FlowCapableNodeConnectorUpdated fcncu = notification.getAugmentation(FlowCapableNodeConnectorUpdated.class);
130 NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeConnectorRef()).getId());
131 TerminationPoint point = toTerminationPoint(toTerminationPointId(notification.getId()),
132 notification.getNodeConnectorRef());
133 InstanceIdentifier<TerminationPoint> path = tpPath(nodeId, point.getKey().getTpId());
135 DataModificationTransaction tx = dataService.beginTransaction();
136 tx.putOperationalData(path, point);
137 if ((fcncu.getState() != null && fcncu.getState().isLinkDown())
138 || (fcncu.getConfiguration() != null && fcncu.getConfiguration().isPORTDOWN())) {
139 removeAffectedLinks(tx, point.getTpId());
141 listenOnTransactionState(tx.getIdentifier(),tx.commit());
146 public synchronized void onLinkDiscovered(final LinkDiscovered notification) {
147 Link link = toTopologyLink(notification);
148 InstanceIdentifier<Link> path = linkPath(link);
149 DataModificationTransaction tx = dataService.beginTransaction();
150 tx.putOperationalData(path, link);
151 listenOnTransactionState(tx.getIdentifier(),tx.commit());
156 public synchronized void onLinkOverutilized(final LinkOverutilized notification) {
161 public synchronized void onLinkRemoved(final LinkRemoved notification) {
162 InstanceIdentifier<Link> path = linkPath(toTopologyLink(notification));
163 DataModificationTransaction tx = dataService.beginTransaction();
164 tx.removeOperationalData(path);
165 listenOnTransactionState(tx.getIdentifier(),tx.commit());
169 public synchronized void onLinkUtilizationNormal(final LinkUtilizationNormal notification) {
173 private static InstanceIdentifier<Node> toNodeIdentifier(final NodeRef ref) {
174 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey invNodeKey = getNodeKey(ref);
176 NodeKey nodeKey = new NodeKey(toTopologyNodeId(invNodeKey.getId()));
177 return InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, topology)
178 .child(Node.class, nodeKey).build();
181 private InstanceIdentifier<TerminationPoint> toTerminationPointIdentifier(final NodeConnectorRef ref) {
182 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey invNodeKey = getNodeKey(ref);
183 NodeConnectorKey invNodeConnectorKey = getNodeConnectorKey(ref);
184 return tpPath(toTopologyNodeId(invNodeKey.getId()), toTerminationPointId(invNodeConnectorKey.getId()));
187 private void removeAffectedLinks(final DataModificationTransaction transaction, final NodeId id) {
188 TypeSafeDataReader reader = TypeSafeDataReader.forReader(transaction);
190 Topology topologyData = reader.readOperationalData(topologyPath);
191 if (topologyData == null) {
194 for (Link link : topologyData.getLink()) {
195 if (id.equals(link.getSource().getSourceNode()) || id.equals(link.getDestination().getDestNode())) {
196 InstanceIdentifier<Link> path = topologyPath.child(Link.class, link.getKey());
197 transaction.removeOperationalData(path);
202 private void removeAffectedLinks(final DataModificationTransaction transaction, final TpId id) {
203 TypeSafeDataReader reader = TypeSafeDataReader.forReader(transaction);
204 Topology topologyData = reader.readOperationalData(topologyPath);
205 if (topologyData == null) {
208 for (Link link : topologyData.getLink()) {
209 if (id.equals(link.getSource().getSourceTp()) || id.equals(link.getDestination().getDestTp())) {
210 InstanceIdentifier<Link> path = topologyPath.child(Link.class, link.getKey());
211 transaction.removeOperationalData(path);
216 private InstanceIdentifier<Node> getNodePath(final NodeId nodeId) {
217 NodeKey nodeKey = new NodeKey(nodeId);
218 return InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, topology)
219 .child(Node.class, nodeKey).build();
222 private InstanceIdentifier<TerminationPoint> tpPath(final NodeId nodeId, final TpId tpId) {
223 NodeKey nodeKey = new NodeKey(nodeId);
224 TerminationPointKey tpKey = new TerminationPointKey(tpId);
225 return InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, topology)
226 .child(Node.class, nodeKey).child(TerminationPoint.class, tpKey).build();
229 private InstanceIdentifier<Link> linkPath(final Link link) {
230 InstanceIdentifier<Link> linkInstanceId = InstanceIdentifier.builder(NetworkTopology.class)
231 .child(Topology.class, topology).child(Link.class, link.getKey()).build();
232 return linkInstanceId;
236 * @param txId transaction identificator
237 * @param future transaction result
239 private static void listenOnTransactionState(final Object txId, Future<RpcResult<TransactionStatus>> future) {
240 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(future),new FutureCallback<RpcResult<TransactionStatus>>() {
243 public void onFailure(Throwable t) {
244 LOG.error("Topology export failed for Tx:{}", txId, t);
249 public void onSuccess(RpcResult<TransactionStatus> result) {
250 if(!result.isSuccessful()) {
251 LOG.error("Topology export failed for Tx:{}", txId);