54f1fc0bb4fbcd0889fa185fd82248907594c3ec
[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.ExecutionException;
19
20 import org.opendaylight.controller.md.sal.binding.util.TypeSafeDataReader;
21 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
22 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.FlowTopologyDiscoveryListener;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkOverutilized;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkUtilizationNormal;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
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.TopologyId;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 class FlowCapableTopologyExporter implements //
55         FlowTopologyDiscoveryListener, //
56         OpendaylightInventoryListener //
57 {
58
59     private final static Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyExporter.class);
60     public static TopologyKey topology = new TopologyKey(new TopologyId("flow:1"));
61
62     // FIXME: Flow capable topology exporter should use transaction chaining API
63     private DataProviderService dataService;
64
65     public DataProviderService getDataService() {
66         return dataService;
67     }
68
69     public void setDataService(final DataProviderService dataService) {
70         this.dataService = dataService;
71     }
72
73     private InstanceIdentifier<Topology> topologyPath;
74
75     public void start() {
76         TopologyBuilder tb = new TopologyBuilder();
77         tb.setKey(topology);
78         topologyPath = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, topology).build();
79         Topology top = tb.build();
80         DataModificationTransaction tx = dataService.beginTransaction();
81         tx.putOperationalData(topologyPath, top);
82         tx.commit();
83     }
84
85     @Override
86     public synchronized void onNodeRemoved(final NodeRemoved notification) {
87         NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeRef()).getId());
88         InstanceIdentifier<Node> nodeInstance = toNodeIdentifier(notification.getNodeRef());
89
90         DataModificationTransaction tx = dataService.beginTransaction();
91         tx.removeOperationalData(nodeInstance);
92         removeAffectedLinks(tx, nodeId);
93         try {
94             tx.commit().get();
95         } catch (InterruptedException | ExecutionException e) {
96             LOG.error("Topology state export not successful. ",e);
97         }
98     }
99
100     @Override
101     public synchronized void onNodeUpdated(final NodeUpdated notification) {
102         FlowCapableNodeUpdated fcnu = notification.getAugmentation(FlowCapableNodeUpdated.class);
103         if (fcnu != null) {
104             Node node = toTopologyNode(toTopologyNodeId(notification.getId()), notification.getNodeRef());
105             InstanceIdentifier<Node> path = getNodePath(toTopologyNodeId(notification.getId()));
106             DataModificationTransaction tx = dataService.beginTransaction();
107             tx.putOperationalData(path, node);
108             try {
109                 tx.commit().get();
110             } catch (InterruptedException | ExecutionException e) {
111                 LOG.error("Topology state export not successful. ",e);
112             }
113         }
114     }
115
116     @Override
117     public synchronized void onNodeConnectorRemoved(final NodeConnectorRemoved notification) {
118         InstanceIdentifier<TerminationPoint> tpInstance = toTerminationPointIdentifier(notification
119                 .getNodeConnectorRef());
120         TpId tpId = toTerminationPointId(getNodeConnectorKey(notification.getNodeConnectorRef()).getId());
121         DataModificationTransaction tx = dataService.beginTransaction();
122         tx.removeOperationalData(tpInstance);
123         removeAffectedLinks(tx, tpId);
124         try {
125             tx.commit().get();
126         } catch (InterruptedException | ExecutionException e) {
127             LOG.error("Topology state export not successful. ",e);
128         }
129
130     }
131
132     @Override
133     public synchronized void onNodeConnectorUpdated(final NodeConnectorUpdated notification) {
134         FlowCapableNodeConnectorUpdated fcncu = notification.getAugmentation(FlowCapableNodeConnectorUpdated.class);
135         if (fcncu != null) {
136             NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeConnectorRef()).getId());
137             TerminationPoint point = toTerminationPoint(toTerminationPointId(notification.getId()),
138                     notification.getNodeConnectorRef());
139             InstanceIdentifier<TerminationPoint> path = tpPath(nodeId, point.getKey().getTpId());
140
141             DataModificationTransaction tx = dataService.beginTransaction();
142             tx.putOperationalData(path, point);
143             if ((fcncu.getState() != null && fcncu.getState().isLinkDown())
144                     || (fcncu.getConfiguration() != null && fcncu.getConfiguration().isPORTDOWN())) {
145                 removeAffectedLinks(tx, point.getTpId());
146             }
147             try {
148                 tx.commit().get();
149             } catch (InterruptedException | ExecutionException e) {
150                 LOG.error("Topology state export not successful. ",e);
151             }
152         }
153     }
154
155     @Override
156     public synchronized void onLinkDiscovered(final LinkDiscovered notification) {
157         Link link = toTopologyLink(notification);
158         InstanceIdentifier<Link> path = linkPath(link);
159         DataModificationTransaction tx = dataService.beginTransaction();
160         tx.putOperationalData(path, link);
161         try {
162             tx.commit().get();
163         } catch (InterruptedException | ExecutionException e) {
164             LOG.error("Topology state export not successful. ",e);
165         }
166     }
167
168     @Override
169     public synchronized void onLinkOverutilized(final LinkOverutilized notification) {
170         // NOOP
171     }
172
173     @Override
174     public synchronized void onLinkRemoved(final LinkRemoved notification) {
175         InstanceIdentifier<Link> path = linkPath(toTopologyLink(notification));
176         DataModificationTransaction tx = dataService.beginTransaction();
177         tx.removeOperationalData(path);
178         ;
179     }
180
181     @Override
182     public synchronized void onLinkUtilizationNormal(final LinkUtilizationNormal notification) {
183         // NOOP
184     }
185
186     private static InstanceIdentifier<Node> toNodeIdentifier(final NodeRef ref) {
187         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey invNodeKey = getNodeKey(ref);
188
189         NodeKey nodeKey = new NodeKey(toTopologyNodeId(invNodeKey.getId()));
190         return InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, topology)
191                 .child(Node.class, nodeKey).build();
192     }
193
194     private InstanceIdentifier<TerminationPoint> toTerminationPointIdentifier(final NodeConnectorRef ref) {
195         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey invNodeKey = getNodeKey(ref);
196         NodeConnectorKey invNodeConnectorKey = getNodeConnectorKey(ref);
197         return tpPath(toTopologyNodeId(invNodeKey.getId()), toTerminationPointId(invNodeConnectorKey.getId()));
198     }
199
200     private void removeAffectedLinks(final DataModificationTransaction transaction, final NodeId id) {
201         TypeSafeDataReader reader = TypeSafeDataReader.forReader(transaction);
202
203         Topology topologyData = reader.readOperationalData(topologyPath);
204         if (topologyData == null) {
205             return;
206         }
207         for (Link link : topologyData.getLink()) {
208             if (id.equals(link.getSource().getSourceNode()) || id.equals(link.getDestination().getDestNode())) {
209                 InstanceIdentifier<Link> path = InstanceIdentifier.builder(topologyPath)
210                         .child(Link.class, link.getKey()).build();
211                 transaction.removeOperationalData(path);
212             }
213         }
214     }
215
216     private void removeAffectedLinks(final DataModificationTransaction transaction, final TpId id) {
217         TypeSafeDataReader reader = TypeSafeDataReader.forReader(transaction);
218         Topology topologyData = reader.readOperationalData(topologyPath);
219         if (topologyData == null) {
220             return;
221         }
222         for (Link link : topologyData.getLink()) {
223             if (id.equals(link.getSource().getSourceTp()) || id.equals(link.getDestination().getDestTp())) {
224                 InstanceIdentifier<Link> path = InstanceIdentifier.builder(topologyPath)
225                         .child(Link.class, link.getKey()).build();
226                 transaction.removeOperationalData(path);
227             }
228         }
229     }
230
231     private InstanceIdentifier<Node> getNodePath(final NodeId nodeId) {
232         NodeKey nodeKey = new NodeKey(nodeId);
233         return InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, topology)
234                 .child(Node.class, nodeKey).build();
235     }
236
237     private InstanceIdentifier<TerminationPoint> tpPath(final NodeId nodeId, final TpId tpId) {
238         NodeKey nodeKey = new NodeKey(nodeId);
239         TerminationPointKey tpKey = new TerminationPointKey(tpId);
240         return InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, topology)
241                 .child(Node.class, nodeKey).child(TerminationPoint.class, tpKey).build();
242     }
243
244     private InstanceIdentifier<Link> linkPath(final Link link) {
245         InstanceIdentifier<Link> linkInstanceId = InstanceIdentifier.builder(NetworkTopology.class)
246                 .child(Topology.class, topology).child(Link.class, link.getKey()).build();
247         return linkInstanceId;
248     }
249 }

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