Merge "BUG-1568 Remove ganymed Ssh client adapter"
[controller.git] / opendaylight / md-sal / topology-manager / src / main / java / org / opendaylight / md / controller / topology / manager / FlowCapableTopologyExporter.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.md.controller.topology.manager;
9
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;
17
18 import java.util.Collections;
19 import java.util.List;
20
21 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
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.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 import com.google.common.base.Optional;
52 import com.google.common.base.Preconditions;
53 import com.google.common.util.concurrent.CheckedFuture;
54 import com.google.common.util.concurrent.FutureCallback;
55 import com.google.common.util.concurrent.Futures;
56
57 class FlowCapableTopologyExporter implements FlowTopologyDiscoveryListener, OpendaylightInventoryListener {
58
59     private final Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyExporter.class);
60     private final InstanceIdentifier<Topology> topology;
61     private final OperationProcessor processor;
62
63     FlowCapableTopologyExporter(final OperationProcessor processor, final InstanceIdentifier<Topology> topology) {
64         this.processor = Preconditions.checkNotNull(processor);
65         this.topology = Preconditions.checkNotNull(topology);
66     }
67
68     @Override
69     public void onNodeRemoved(final NodeRemoved notification) {
70
71         final NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeRef()).getId());
72         final InstanceIdentifier<Node> nodeInstance = toNodeIdentifier(notification.getNodeRef());
73
74         processor.enqueueOperation(new TopologyOperation() {
75             @Override
76             public void applyOperation(final ReadWriteTransaction transaction) {
77                 removeAffectedLinks(nodeId);
78             }
79         });
80
81         processor.enqueueOperation(new TopologyOperation() {
82             @Override
83             public void applyOperation(ReadWriteTransaction transaction) {
84                 transaction.delete(LogicalDatastoreType.OPERATIONAL, nodeInstance);
85             }
86         });
87     }
88
89     @Override
90     public void onNodeUpdated(final NodeUpdated notification) {
91         FlowCapableNodeUpdated fcnu = notification.getAugmentation(FlowCapableNodeUpdated.class);
92         if (fcnu != null) {
93             processor.enqueueOperation(new TopologyOperation() {
94                 @Override
95                 public void applyOperation(final ReadWriteTransaction transaction) {
96                     final Node node = toTopologyNode(toTopologyNodeId(notification.getId()), notification.getNodeRef());
97                     final InstanceIdentifier<Node> path = getNodePath(toTopologyNodeId(notification.getId()));
98                     transaction.merge(LogicalDatastoreType.OPERATIONAL, path, node, true);
99                 }
100             });
101         }
102     }
103
104     @Override
105     public void onNodeConnectorRemoved(final NodeConnectorRemoved notification) {
106
107         final InstanceIdentifier<TerminationPoint> tpInstance = toTerminationPointIdentifier(notification
108                 .getNodeConnectorRef());
109
110         processor.enqueueOperation(new TopologyOperation() {
111             @Override
112             public void applyOperation(final ReadWriteTransaction transaction) {
113                 final TpId tpId = toTerminationPointId(getNodeConnectorKey(notification.getNodeConnectorRef()).getId());
114                 removeAffectedLinks(tpId);
115             }
116         });
117
118         processor.enqueueOperation(new TopologyOperation() {
119             @Override
120             public void applyOperation(ReadWriteTransaction transaction) {
121                 transaction.delete(LogicalDatastoreType.OPERATIONAL, tpInstance);
122             }
123         });
124     }
125
126     @Override
127     public void onNodeConnectorUpdated(final NodeConnectorUpdated notification) {
128         final FlowCapableNodeConnectorUpdated fcncu = notification.getAugmentation(FlowCapableNodeConnectorUpdated.class);
129         if (fcncu != null) {
130             processor.enqueueOperation(new TopologyOperation() {
131                 @Override
132                 public void applyOperation(final ReadWriteTransaction transaction) {
133                     final NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeConnectorRef()).getId());
134                     TerminationPoint point = toTerminationPoint(toTerminationPointId(notification.getId()),
135                             notification.getNodeConnectorRef());
136                     final InstanceIdentifier<TerminationPoint> path = tpPath(nodeId, point.getKey().getTpId());
137                     transaction.merge(LogicalDatastoreType.OPERATIONAL, path, point, true);
138                     if ((fcncu.getState() != null && fcncu.getState().isLinkDown())
139                             || (fcncu.getConfiguration() != null && fcncu.getConfiguration().isPORTDOWN())) {
140                         removeAffectedLinks(point.getTpId());
141                     }
142                 }
143             });
144         }
145     }
146
147     @Override
148     public void onLinkDiscovered(final LinkDiscovered notification) {
149         processor.enqueueOperation(new TopologyOperation() {
150             @Override
151             public void applyOperation(final ReadWriteTransaction transaction) {
152                 final Link link = toTopologyLink(notification);
153                 final InstanceIdentifier<Link> path = linkPath(link);
154                 transaction.put(LogicalDatastoreType.OPERATIONAL, path, link);
155             }
156         });
157     }
158
159     @Override
160     public void onLinkOverutilized(final LinkOverutilized notification) {
161         // NOOP
162     }
163
164     @Override
165     public void onLinkRemoved(final LinkRemoved notification) {
166         processor.enqueueOperation(new TopologyOperation() {
167             @Override
168             public void applyOperation(final ReadWriteTransaction transaction) {
169                 transaction.delete(LogicalDatastoreType.OPERATIONAL, linkPath(toTopologyLink(notification)));
170             }
171         });
172     }
173
174     @Override
175     public void onLinkUtilizationNormal(final LinkUtilizationNormal notification) {
176         // NOOP
177     }
178
179     private InstanceIdentifier<Node> toNodeIdentifier(final NodeRef ref) {
180         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey invNodeKey = getNodeKey(ref);
181         NodeKey nodeKey = new NodeKey(toTopologyNodeId(invNodeKey.getId()));
182         return topology.child(Node.class, nodeKey);
183     }
184
185     private InstanceIdentifier<TerminationPoint> toTerminationPointIdentifier(final NodeConnectorRef ref) {
186         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey invNodeKey = getNodeKey(ref);
187         NodeConnectorKey invNodeConnectorKey = getNodeConnectorKey(ref);
188         return tpPath(toTopologyNodeId(invNodeKey.getId()), toTerminationPointId(invNodeConnectorKey.getId()));
189     }
190
191     private void removeAffectedLinks(final NodeId id) {
192         processor.enqueueOperation(new TopologyOperation() {
193             @Override
194             public void applyOperation(final ReadWriteTransaction transaction) {
195                 CheckedFuture<Optional<Topology>, ReadFailedException> topologyDataFuture = transaction.read(LogicalDatastoreType.OPERATIONAL, topology);
196                 Futures.addCallback(topologyDataFuture, new FutureCallback<Optional<Topology>>() {
197                     @Override
198                     public void onSuccess(Optional<Topology> topologyOptional) {
199                         if (topologyOptional.isPresent()) {
200                             List<Link> linkList = topologyOptional.get().getLink() != null
201                                     ? topologyOptional.get().getLink() : Collections.<Link> emptyList();
202                             for (Link link : linkList) {
203                                 if (id.equals(link.getSource().getSourceNode()) || id.equals(link.getDestination().getDestNode())) {
204                                     transaction.delete(LogicalDatastoreType.OPERATIONAL, linkPath(link));
205                                 }
206                             }
207                         }
208                     }
209
210                     @Override
211                     public void onFailure(Throwable throwable) {
212                         LOG.error("Error reading topology data for topology {}", topology, throwable);
213                     }
214                 });
215             }
216         });
217     }
218
219     private void removeAffectedLinks(final TpId id) {
220         processor.enqueueOperation(new TopologyOperation() {
221             @Override
222             public void applyOperation(final ReadWriteTransaction transaction) {
223                 CheckedFuture<Optional<Topology>, ReadFailedException> topologyDataFuture = transaction.read(LogicalDatastoreType.OPERATIONAL, topology);
224                 Futures.addCallback(topologyDataFuture, new FutureCallback<Optional<Topology>>() {
225                     @Override
226                     public void onSuccess(Optional<Topology> topologyOptional) {
227                         if (topologyOptional.isPresent()) {
228                             List<Link> linkList = topologyOptional.get().getLink() != null
229                                     ? topologyOptional.get().getLink() : Collections.<Link> emptyList();
230                             for (Link link : linkList) {
231                                 if (id.equals(link.getSource().getSourceTp()) || id.equals(link.getDestination().getDestTp())) {
232                                     transaction.delete(LogicalDatastoreType.OPERATIONAL, linkPath(link));
233                                 }
234                             }
235                         }
236                     }
237
238                     @Override
239                     public void onFailure(Throwable throwable) {
240                         LOG.error("Error reading topology data for topology {}", topology, throwable);
241                     }
242                 });
243             }
244         });
245     }
246
247     private InstanceIdentifier<Node> getNodePath(final NodeId nodeId) {
248         return topology.child(Node.class, new NodeKey(nodeId));
249     }
250
251     private InstanceIdentifier<TerminationPoint> tpPath(final NodeId nodeId, final TpId tpId) {
252         NodeKey nodeKey = new NodeKey(nodeId);
253         TerminationPointKey tpKey = new TerminationPointKey(tpId);
254         return topology.child(Node.class, nodeKey).child(TerminationPoint.class, tpKey);
255     }
256
257     private InstanceIdentifier<Link> linkPath(final Link link) {
258         return topology.child(Link.class, link.getKey());
259     }
260 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.