Merge "Bug 2273: Removed unbuilt third-party code."
[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         processor.enqueueOperation(new TopologyOperation() {
72             @Override
73             public void applyOperation(ReadWriteTransaction transaction) {
74                     removeAffectedLinks(nodeId, transaction);
75                     transaction.delete(LogicalDatastoreType.OPERATIONAL, nodeInstance);
76             }
77
78             @Override
79             public String toString() {
80                 return "onNodeRemoved";
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                 @Override
98                 public String toString() {
99                     return "onNodeUpdated";
100                 }
101             });
102         }
103     }
104
105     @Override
106     public void onNodeConnectorRemoved(final NodeConnectorRemoved notification) {
107
108         final InstanceIdentifier<TerminationPoint> tpInstance = toTerminationPointIdentifier(
109                 notification.getNodeConnectorRef());
110
111         final InstanceIdentifier<Node> node = tpInstance.firstIdentifierOf(Node.class);
112
113         final TpId tpId = toTerminationPointId(getNodeConnectorKey(
114                 notification.getNodeConnectorRef()).getId());
115
116         processor.enqueueOperation(new TopologyOperation() {
117             @Override
118             public void applyOperation(ReadWriteTransaction transaction) {
119                 Optional<Node> nodeOptional = Optional.absent();
120                 try {
121                     nodeOptional = transaction.read(LogicalDatastoreType.OPERATIONAL, node).checkedGet();
122                 } catch (ReadFailedException e) {
123                     LOG.error("Error occured when trying to read NodeConnector ", e);
124                 }
125                 if (nodeOptional.isPresent()) {
126                     removeAffectedLinks(tpId, transaction);
127                     transaction.delete(LogicalDatastoreType.OPERATIONAL, tpInstance);
128                 }
129             }
130
131             @Override
132             public String toString() {
133                 return "onNodeConnectorRemoved";
134             }
135         });
136     }
137
138     @Override
139     public void onNodeConnectorUpdated(final NodeConnectorUpdated notification) {
140         final FlowCapableNodeConnectorUpdated fcncu = notification.getAugmentation(
141                 FlowCapableNodeConnectorUpdated.class);
142         if (fcncu != null) {
143             processor.enqueueOperation(new TopologyOperation() {
144                 @Override
145                 public void applyOperation(final ReadWriteTransaction transaction) {
146                     final NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeConnectorRef()).getId());
147                     TerminationPoint point = toTerminationPoint(toTerminationPointId(notification.getId()),
148                             notification.getNodeConnectorRef());
149                     final InstanceIdentifier<TerminationPoint> path = tpPath(nodeId, point.getKey().getTpId());
150                     transaction.merge(LogicalDatastoreType.OPERATIONAL, path, point, true);
151                     if ((fcncu.getState() != null && fcncu.getState().isLinkDown())
152                             || (fcncu.getConfiguration() != null && fcncu.getConfiguration().isPORTDOWN())) {
153                         removeAffectedLinks(point.getTpId(), transaction);
154                     }
155                 }
156
157                 @Override
158                 public String toString() {
159                     return "onNodeConnectorUpdated";
160                 }
161             });
162         }
163     }
164
165     @Override
166     public void onLinkDiscovered(final LinkDiscovered notification) {
167         processor.enqueueOperation(new TopologyOperation() {
168             @Override
169             public void applyOperation(final ReadWriteTransaction transaction) {
170                 final Link link = toTopologyLink(notification);
171                 final InstanceIdentifier<Link> path = linkPath(link);
172                 transaction.put(LogicalDatastoreType.OPERATIONAL, path, link, true);
173             }
174
175             @Override
176             public String toString() {
177                 return "onLinkDiscovered";
178             }
179         });
180     }
181
182     @Override
183     public void onLinkOverutilized(final LinkOverutilized notification) {
184         // NOOP
185     }
186
187     @Override
188     public void onLinkRemoved(final LinkRemoved notification) {
189         processor.enqueueOperation(new TopologyOperation() {
190             @Override
191             public void applyOperation(final ReadWriteTransaction transaction) {
192                 Optional<Link> linkOptional = Optional.absent();
193                 try {
194                     // read that checks if link exists (if we do not do this we might get an exception on delete)
195                     linkOptional = transaction.read(LogicalDatastoreType.OPERATIONAL,
196                             linkPath(toTopologyLink(notification))).checkedGet();
197                 } catch (ReadFailedException e) {
198                     LOG.error("Error occured when trying to read Link ", e);
199                 }
200                 if (linkOptional.isPresent()) {
201                     transaction.delete(LogicalDatastoreType.OPERATIONAL, linkPath(toTopologyLink(notification)));
202                 }
203             }
204
205             @Override
206             public String toString() {
207                 return "onLinkRemoved";
208             }
209         });
210     }
211
212     @Override
213     public void onLinkUtilizationNormal(final LinkUtilizationNormal notification) {
214         // NOOP
215     }
216
217     private InstanceIdentifier<Node> toNodeIdentifier(final NodeRef ref) {
218         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey invNodeKey = getNodeKey(ref);
219         NodeKey nodeKey = new NodeKey(toTopologyNodeId(invNodeKey.getId()));
220         return topology.child(Node.class, nodeKey);
221     }
222
223     private InstanceIdentifier<TerminationPoint> toTerminationPointIdentifier(final NodeConnectorRef ref) {
224         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey invNodeKey = getNodeKey(ref);
225         NodeConnectorKey invNodeConnectorKey = getNodeConnectorKey(ref);
226         return tpPath(toTopologyNodeId(invNodeKey.getId()), toTerminationPointId(invNodeConnectorKey.getId()));
227     }
228
229     private void removeAffectedLinks(final NodeId id, final ReadWriteTransaction transaction) {
230         Optional<Topology> topologyOptional = Optional.absent();
231         try {
232             topologyOptional = transaction.read(LogicalDatastoreType.OPERATIONAL, topology).checkedGet();
233         } catch (ReadFailedException e) {
234             LOG.error("Error reading topology data for topology {}", topology, e);
235         }
236         if (topologyOptional.isPresent()) {
237             removeAffectedLinks(id, topologyOptional, transaction);
238         }
239     }
240
241     private void removeAffectedLinks(final NodeId id, Optional<Topology> topologyOptional, ReadWriteTransaction transaction) {
242         if (!topologyOptional.isPresent()) {
243             return;
244         }
245
246         List<Link> linkList = topologyOptional.get().getLink() != null ?
247                 topologyOptional.get().getLink() : Collections.<Link> emptyList();
248         for (Link link : linkList) {
249             if (id.equals(link.getSource().getSourceNode()) ||
250                     id.equals(link.getDestination().getDestNode())) {
251                 transaction.delete(LogicalDatastoreType.OPERATIONAL, linkPath(link));
252             }
253         }
254     }
255
256     private void removeAffectedLinks(final TpId id, final ReadWriteTransaction transaction) {
257         Optional<Topology> topologyOptional = Optional.absent();
258         try {
259             topologyOptional = transaction.read(LogicalDatastoreType.OPERATIONAL, topology).checkedGet();
260         } catch (ReadFailedException e) {
261             LOG.error("Error reading topology data for topology {}", topology, e);
262         }
263         if (topologyOptional.isPresent()) {
264             removeAffectedLinks(id, topologyOptional, transaction);
265         }
266     }
267
268     private void removeAffectedLinks(final TpId id, Optional<Topology> topologyOptional, ReadWriteTransaction transaction) {
269         if (!topologyOptional.isPresent()) {
270             return;
271         }
272
273         List<Link> linkList = topologyOptional.get().getLink() != null
274                 ? topologyOptional.get().getLink() : Collections.<Link> emptyList();
275         for (Link link : linkList) {
276             if (id.equals(link.getSource().getSourceTp()) ||
277                     id.equals(link.getDestination().getDestTp())) {
278                 transaction.delete(LogicalDatastoreType.OPERATIONAL, linkPath(link));
279             }
280         }
281     }
282
283     private InstanceIdentifier<Node> getNodePath(final NodeId nodeId) {
284         return topology.child(Node.class, new NodeKey(nodeId));
285     }
286
287     private InstanceIdentifier<TerminationPoint> tpPath(final NodeId nodeId, final TpId tpId) {
288         NodeKey nodeKey = new NodeKey(nodeId);
289         TerminationPointKey tpKey = new TerminationPointKey(tpId);
290         return topology.child(Node.class, nodeKey).child(TerminationPoint.class, tpKey);
291     }
292
293     private InstanceIdentifier<Link> linkPath(final Link link) {
294         return topology.child(Link.class, link.getKey());
295     }
296 }