BUG-509 transaction future result listening
[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.concurrent.Future;
19
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;
55
56 import com.google.common.util.concurrent.FutureCallback;
57 import com.google.common.util.concurrent.Futures;
58 import com.google.common.util.concurrent.JdkFutureAdapters;
59
60 class FlowCapableTopologyExporter implements //
61         FlowTopologyDiscoveryListener, //
62         OpendaylightInventoryListener //
63 {
64
65     protected final static Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyExporter.class);
66     public static TopologyKey topology = new TopologyKey(new TopologyId("flow:1"));
67
68     // FIXME: Flow capable topology exporter should use transaction chaining API
69     private DataProviderService dataService;
70
71     public DataProviderService getDataService() {
72         return dataService;
73     }
74
75     public void setDataService(final DataProviderService dataService) {
76         this.dataService = dataService;
77     }
78
79     private InstanceIdentifier<Topology> topologyPath;
80
81     public void start() {
82         TopologyBuilder tb = new TopologyBuilder();
83         tb.setKey(topology);
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());
89     }
90
91     @Override
92     public synchronized void onNodeRemoved(final NodeRemoved notification) {
93         NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeRef()).getId());
94         InstanceIdentifier<Node> nodeInstance = toNodeIdentifier(notification.getNodeRef());
95
96         DataModificationTransaction tx = dataService.beginTransaction();
97         tx.removeOperationalData(nodeInstance);
98         removeAffectedLinks(tx, nodeId);
99         listenOnTransactionState(tx.getIdentifier(),tx.commit());
100     }
101
102     @Override
103     public synchronized void onNodeUpdated(final NodeUpdated notification) {
104         FlowCapableNodeUpdated fcnu = notification.getAugmentation(FlowCapableNodeUpdated.class);
105         if (fcnu != null) {
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());
111         }
112     }
113
114     @Override
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());
123
124     }
125
126     @Override
127     public synchronized void onNodeConnectorUpdated(final NodeConnectorUpdated notification) {
128         FlowCapableNodeConnectorUpdated fcncu = notification.getAugmentation(FlowCapableNodeConnectorUpdated.class);
129         if (fcncu != null) {
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());
134
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());
140             }
141             listenOnTransactionState(tx.getIdentifier(),tx.commit());
142         }
143     }
144
145     @Override
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());
152
153     }
154
155     @Override
156     public synchronized void onLinkOverutilized(final LinkOverutilized notification) {
157         // NOOP
158     }
159
160     @Override
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());
166     }
167
168     @Override
169     public synchronized void onLinkUtilizationNormal(final LinkUtilizationNormal notification) {
170         // NOOP
171     }
172
173     private static InstanceIdentifier<Node> toNodeIdentifier(final NodeRef ref) {
174         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey invNodeKey = getNodeKey(ref);
175
176         NodeKey nodeKey = new NodeKey(toTopologyNodeId(invNodeKey.getId()));
177         return InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, topology)
178                 .child(Node.class, nodeKey).build();
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 DataModificationTransaction transaction, final NodeId id) {
188         TypeSafeDataReader reader = TypeSafeDataReader.forReader(transaction);
189
190         Topology topologyData = reader.readOperationalData(topologyPath);
191         if (topologyData == null) {
192             return;
193         }
194         for (Link link : topologyData.getLink()) {
195             if (id.equals(link.getSource().getSourceNode()) || id.equals(link.getDestination().getDestNode())) {
196                 InstanceIdentifier<Link> path = InstanceIdentifier.builder(topologyPath)
197                         .child(Link.class, link.getKey()).build();
198                 transaction.removeOperationalData(path);
199             }
200         }
201     }
202
203     private void removeAffectedLinks(final DataModificationTransaction transaction, final TpId id) {
204         TypeSafeDataReader reader = TypeSafeDataReader.forReader(transaction);
205         Topology topologyData = reader.readOperationalData(topologyPath);
206         if (topologyData == null) {
207             return;
208         }
209         for (Link link : topologyData.getLink()) {
210             if (id.equals(link.getSource().getSourceTp()) || id.equals(link.getDestination().getDestTp())) {
211                 InstanceIdentifier<Link> path = InstanceIdentifier.builder(topologyPath)
212                         .child(Link.class, link.getKey()).build();
213                 transaction.removeOperationalData(path);
214             }
215         }
216     }
217
218     private InstanceIdentifier<Node> getNodePath(final NodeId nodeId) {
219         NodeKey nodeKey = new NodeKey(nodeId);
220         return InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, topology)
221                 .child(Node.class, nodeKey).build();
222     }
223
224     private InstanceIdentifier<TerminationPoint> tpPath(final NodeId nodeId, final TpId tpId) {
225         NodeKey nodeKey = new NodeKey(nodeId);
226         TerminationPointKey tpKey = new TerminationPointKey(tpId);
227         return InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, topology)
228                 .child(Node.class, nodeKey).child(TerminationPoint.class, tpKey).build();
229     }
230
231     private InstanceIdentifier<Link> linkPath(final Link link) {
232         InstanceIdentifier<Link> linkInstanceId = InstanceIdentifier.builder(NetworkTopology.class)
233                 .child(Topology.class, topology).child(Link.class, link.getKey()).build();
234         return linkInstanceId;
235     }
236     
237     /**
238      * @param txId transaction identificator
239      * @param future transaction result
240      */
241     private static void listenOnTransactionState(final Object txId, Future<RpcResult<TransactionStatus>> future) {
242         Futures.addCallback(JdkFutureAdapters.listenInPoolThread(future),new FutureCallback<RpcResult<TransactionStatus>>() {
243             
244             @Override
245             public void onFailure(Throwable t) {
246                 LOG.error("Topology export failed for Tx:{}", txId, t);
247                 
248             }
249             
250             @Override
251             public void onSuccess(RpcResult<TransactionStatus> result) {
252                 if(!result.isSuccessful()) {
253                     LOG.error("Topology export failed for Tx:{}", txId);
254                 }
255             }
256         });
257     }
258 }

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