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

©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.