Merge "Bug 1309 - Cannot publish LinkDiscovered event"
[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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.FlowTopologyDiscoveryListener;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkOverutilized;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkUtilizationNormal;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import java.util.Collections;
43 import java.util.List;
44
45 import static org.opendaylight.md.controller.topology.manager.FlowCapableNodeMapping.getNodeConnectorKey;
46 import static org.opendaylight.md.controller.topology.manager.FlowCapableNodeMapping.getNodeKey;
47 import static org.opendaylight.md.controller.topology.manager.FlowCapableNodeMapping.toTerminationPoint;
48 import static org.opendaylight.md.controller.topology.manager.FlowCapableNodeMapping.toTerminationPointId;
49 import static org.opendaylight.md.controller.topology.manager.FlowCapableNodeMapping.toTopologyLink;
50 import static org.opendaylight.md.controller.topology.manager.FlowCapableNodeMapping.toTopologyNode;
51 import static org.opendaylight.md.controller.topology.manager.FlowCapableNodeMapping.toTopologyNodeId;
52
53 class FlowCapableTopologyExporter implements FlowTopologyDiscoveryListener, OpendaylightInventoryListener {
54
55     private static final Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyExporter.class);
56     private final InstanceIdentifier<Topology> topology;
57     private final OperationProcessor processor;
58
59     FlowCapableTopologyExporter(final OperationProcessor processor,
60             final InstanceIdentifier<Topology> topology) {
61         this.processor = Preconditions.checkNotNull(processor);
62         this.topology = Preconditions.checkNotNull(topology);
63     }
64
65     @Override
66     public void onNodeRemoved(final NodeRemoved notification) {
67
68         final NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeRef()).getId());
69         final InstanceIdentifier<Node> nodeInstance = toNodeIdentifier(notification.getNodeRef());
70
71
72         processor.enqueueOperation(new TopologyOperation() {
73             @Override
74             public void applyOperation(ReadWriteTransaction transaction) {
75                 Optional<Node> nodeOptional = Optional.absent();
76                 try {
77                     nodeOptional = transaction.read(LogicalDatastoreType.OPERATIONAL, nodeInstance).checkedGet();
78                 } catch (ReadFailedException e) {
79                     LOG.error("Error occured when trying to read Node ", e);
80                 }
81                 if (nodeOptional.isPresent()) {
82                     removeAffectedLinks(nodeId, transaction);
83                     transaction.delete(LogicalDatastoreType.OPERATIONAL, nodeInstance);
84                 }
85             }
86
87             @Override
88             public String toString() {
89                 return "onNodeRemoved";
90             }
91         });
92     }
93
94     @Override
95     public void onNodeUpdated(final NodeUpdated notification) {
96         FlowCapableNodeUpdated fcnu = notification.getAugmentation(FlowCapableNodeUpdated.class);
97         if (fcnu != null) {
98             processor.enqueueOperation(new TopologyOperation() {
99                 @Override
100                 public void applyOperation(final ReadWriteTransaction transaction) {
101                     final Node node = toTopologyNode(toTopologyNodeId(notification.getId()), notification.getNodeRef());
102                     final InstanceIdentifier<Node> path = getNodePath(toTopologyNodeId(notification.getId()));
103                     transaction.merge(LogicalDatastoreType.OPERATIONAL, path, node, true);
104                 }
105
106                 @Override
107                 public String toString() {
108                     return "onNodeUpdated";
109                 }
110             });
111         }
112     }
113
114     @Override
115     public void onNodeConnectorRemoved(final NodeConnectorRemoved notification) {
116
117         final InstanceIdentifier<TerminationPoint> tpInstance = toTerminationPointIdentifier(
118                 notification.getNodeConnectorRef());
119
120         final TpId tpId = toTerminationPointId(getNodeConnectorKey(
121                 notification.getNodeConnectorRef()).getId());
122
123         processor.enqueueOperation(new TopologyOperation() {
124             @Override
125             public void applyOperation(ReadWriteTransaction transaction) {
126                 Optional<TerminationPoint> terminationPointOptional = Optional.absent();
127                 try {
128                     terminationPointOptional = transaction.read(LogicalDatastoreType.OPERATIONAL, tpInstance).checkedGet();
129                 } catch (ReadFailedException e) {
130                     LOG.error("Error occured when trying to read NodeConnector ", e);
131                 }
132                 if (terminationPointOptional.isPresent()) {
133                     removeAffectedLinks(tpId, transaction);
134                     transaction.delete(LogicalDatastoreType.OPERATIONAL, tpInstance);
135                 }
136             }
137
138             @Override
139             public String toString() {
140                 return "onNodeConnectorRemoved";
141             }
142         });
143     }
144
145     @Override
146     public void onNodeConnectorUpdated(final NodeConnectorUpdated notification) {
147         final FlowCapableNodeConnectorUpdated fcncu = notification.getAugmentation(
148                 FlowCapableNodeConnectorUpdated.class);
149         if (fcncu != null) {
150             processor.enqueueOperation(new TopologyOperation() {
151                 @Override
152                 public void applyOperation(final ReadWriteTransaction transaction) {
153                     final NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeConnectorRef()).getId());
154                     TerminationPoint point = toTerminationPoint(toTerminationPointId(notification.getId()),
155                             notification.getNodeConnectorRef());
156                     final InstanceIdentifier<TerminationPoint> path = tpPath(nodeId, point.getKey().getTpId());
157                     transaction.merge(LogicalDatastoreType.OPERATIONAL, path, point, true);
158                     if ((fcncu.getState() != null && fcncu.getState().isLinkDown())
159                             || (fcncu.getConfiguration() != null && fcncu.getConfiguration().isPORTDOWN())) {
160                         removeAffectedLinks(point.getTpId(), transaction);
161                     }
162                 }
163
164                 @Override
165                 public String toString() {
166                     return "onNodeConnectorUpdated";
167                 }
168             });
169         }
170     }
171
172     @Override
173     public void onLinkDiscovered(final LinkDiscovered notification) {
174         processor.enqueueOperation(new TopologyOperation() {
175             @Override
176             public void applyOperation(final ReadWriteTransaction transaction) {
177                 final Link link = toTopologyLink(notification);
178                 final InstanceIdentifier<Link> path = linkPath(link);
179                 transaction.put(LogicalDatastoreType.OPERATIONAL, path, link, true);
180             }
181
182             @Override
183             public String toString() {
184                 return "onLinkDiscovered";
185             }
186         });
187     }
188
189     @Override
190     public void onLinkOverutilized(final LinkOverutilized notification) {
191         // NOOP
192     }
193
194     @Override
195     public void onLinkRemoved(final LinkRemoved notification) {
196         processor.enqueueOperation(new TopologyOperation() {
197             @Override
198             public void applyOperation(final ReadWriteTransaction transaction) {
199                 Optional<Link> linkOptional = Optional.absent();
200                 try {
201                     // read that checks if link exists (if we do not do this we might get an exception on delete)
202                     linkOptional = transaction.read(LogicalDatastoreType.OPERATIONAL,
203                             linkPath(toTopologyLink(notification))).checkedGet();
204                 } catch (ReadFailedException e) {
205                     LOG.error("Error occured when trying to read Link ", e);
206                 }
207                 if (linkOptional.isPresent()) {
208                     transaction.delete(LogicalDatastoreType.OPERATIONAL, linkPath(toTopologyLink(notification)));
209                 }
210             }
211
212             @Override
213             public String toString() {
214                 return "onLinkRemoved";
215             }
216         });
217     }
218
219
220     @Override
221     public void onLinkUtilizationNormal(final LinkUtilizationNormal notification) {
222         // NOOP
223     }
224
225     private InstanceIdentifier<Node> toNodeIdentifier(final NodeRef ref) {
226         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey invNodeKey = getNodeKey(ref);
227         NodeKey nodeKey = new NodeKey(toTopologyNodeId(invNodeKey.getId()));
228         return topology.child(Node.class, nodeKey);
229     }
230
231     private InstanceIdentifier<TerminationPoint> toTerminationPointIdentifier(final NodeConnectorRef ref) {
232         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey invNodeKey = getNodeKey(ref);
233         NodeConnectorKey invNodeConnectorKey = getNodeConnectorKey(ref);
234         return tpPath(toTopologyNodeId(invNodeKey.getId()), toTerminationPointId(invNodeConnectorKey.getId()));
235     }
236
237     private void removeAffectedLinks(final NodeId id, final ReadWriteTransaction transaction) {
238         Optional<Topology> topologyOptional = Optional.absent();
239         try {
240             topologyOptional = transaction.read(LogicalDatastoreType.OPERATIONAL, topology).checkedGet();
241         } catch (ReadFailedException e) {
242             LOG.error("Error reading topology data for topology {}", topology, e);
243         }
244         if (topologyOptional.isPresent()) {
245             removeAffectedLinks(id, topologyOptional, transaction);
246         }
247     }
248
249     private void removeAffectedLinks(final NodeId id, Optional<Topology> topologyOptional, ReadWriteTransaction transaction) {
250         if (!topologyOptional.isPresent()) {
251             return;
252         }
253
254         List<Link> linkList = topologyOptional.get().getLink() != null ?
255                 topologyOptional.get().getLink() : Collections.<Link> emptyList();
256         for (Link link : linkList) {
257             if (id.equals(link.getSource().getSourceNode()) ||
258                     id.equals(link.getDestination().getDestNode())) {
259                 transaction.delete(LogicalDatastoreType.OPERATIONAL, linkPath(link));
260             }
261         }
262     }
263
264     private void removeAffectedLinks(final TpId id, final ReadWriteTransaction transaction) {
265         Optional<Topology> topologyOptional = Optional.absent();
266         try {
267             topologyOptional = transaction.read(LogicalDatastoreType.OPERATIONAL, topology).checkedGet();
268         } catch (ReadFailedException e) {
269             LOG.error("Error reading topology data for topology {}", topology, e);
270         }
271         if (topologyOptional.isPresent()) {
272             removeAffectedLinks(id, topologyOptional, transaction);
273         }
274     }
275
276     private void removeAffectedLinks(final TpId id, Optional<Topology> topologyOptional, ReadWriteTransaction transaction) {
277         if (!topologyOptional.isPresent()) {
278             return;
279         }
280
281         List<Link> linkList = topologyOptional.get().getLink() != null
282                 ? topologyOptional.get().getLink() : Collections.<Link> emptyList();
283         for (Link link : linkList) {
284             if (id.equals(link.getSource().getSourceTp()) ||
285                     id.equals(link.getDestination().getDestTp())) {
286                 transaction.delete(LogicalDatastoreType.OPERATIONAL, linkPath(link));
287             }
288         }
289     }
290
291     private InstanceIdentifier<Node> getNodePath(final NodeId nodeId) {
292         return topology.child(Node.class, new NodeKey(nodeId));
293     }
294
295     private InstanceIdentifier<TerminationPoint> tpPath(final NodeId nodeId, final TpId tpId) {
296         NodeKey nodeKey = new NodeKey(nodeId);
297         TerminationPointKey tpKey = new TerminationPointKey(tpId);
298         return topology.child(Node.class, nodeKey).child(TerminationPoint.class, tpKey);
299     }
300
301     private InstanceIdentifier<Link> linkPath(final Link link) {
302         return topology.child(Link.class, link.getKey());
303     }
304 }