2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.md.controller.topology.manager;
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;
42 import java.util.Collections;
43 import java.util.List;
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;
53 class FlowCapableTopologyExporter implements FlowTopologyDiscoveryListener, OpendaylightInventoryListener {
55 private static final Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyExporter.class);
56 private final InstanceIdentifier<Topology> topology;
57 private final OperationProcessor processor;
59 FlowCapableTopologyExporter(final OperationProcessor processor,
60 final InstanceIdentifier<Topology> topology) {
61 this.processor = Preconditions.checkNotNull(processor);
62 this.topology = Preconditions.checkNotNull(topology);
66 public void onNodeRemoved(final NodeRemoved notification) {
68 final NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeRef()).getId());
69 final InstanceIdentifier<Node> nodeInstance = toNodeIdentifier(notification.getNodeRef());
72 processor.enqueueOperation(new TopologyOperation() {
74 public void applyOperation(ReadWriteTransaction transaction) {
75 Optional<Node> nodeOptional = Optional.absent();
77 nodeOptional = transaction.read(LogicalDatastoreType.OPERATIONAL, nodeInstance).checkedGet();
78 } catch (ReadFailedException e) {
79 LOG.error("Error occured when trying to read Node ", e);
81 if (nodeOptional.isPresent()) {
82 removeAffectedLinks(nodeId, transaction);
83 transaction.delete(LogicalDatastoreType.OPERATIONAL, nodeInstance);
88 public String toString() {
89 return "onNodeRemoved";
95 public void onNodeUpdated(final NodeUpdated notification) {
96 FlowCapableNodeUpdated fcnu = notification.getAugmentation(FlowCapableNodeUpdated.class);
98 processor.enqueueOperation(new TopologyOperation() {
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);
107 public String toString() {
108 return "onNodeUpdated";
115 public void onNodeConnectorRemoved(final NodeConnectorRemoved notification) {
117 final InstanceIdentifier<TerminationPoint> tpInstance = toTerminationPointIdentifier(
118 notification.getNodeConnectorRef());
120 final TpId tpId = toTerminationPointId(getNodeConnectorKey(
121 notification.getNodeConnectorRef()).getId());
123 processor.enqueueOperation(new TopologyOperation() {
125 public void applyOperation(ReadWriteTransaction transaction) {
126 Optional<TerminationPoint> terminationPointOptional = Optional.absent();
128 terminationPointOptional = transaction.read(LogicalDatastoreType.OPERATIONAL, tpInstance).checkedGet();
129 } catch (ReadFailedException e) {
130 LOG.error("Error occured when trying to read NodeConnector ", e);
132 if (terminationPointOptional.isPresent()) {
133 removeAffectedLinks(tpId, transaction);
134 transaction.delete(LogicalDatastoreType.OPERATIONAL, tpInstance);
139 public String toString() {
140 return "onNodeConnectorRemoved";
146 public void onNodeConnectorUpdated(final NodeConnectorUpdated notification) {
147 final FlowCapableNodeConnectorUpdated fcncu = notification.getAugmentation(
148 FlowCapableNodeConnectorUpdated.class);
150 processor.enqueueOperation(new TopologyOperation() {
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);
165 public String toString() {
166 return "onNodeConnectorUpdated";
173 public void onLinkDiscovered(final LinkDiscovered notification) {
174 processor.enqueueOperation(new TopologyOperation() {
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);
183 public String toString() {
184 return "onLinkDiscovered";
190 public void onLinkOverutilized(final LinkOverutilized notification) {
195 public void onLinkRemoved(final LinkRemoved notification) {
196 processor.enqueueOperation(new TopologyOperation() {
198 public void applyOperation(final ReadWriteTransaction transaction) {
199 Optional<Link> linkOptional = Optional.absent();
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);
207 if (linkOptional.isPresent()) {
208 transaction.delete(LogicalDatastoreType.OPERATIONAL, linkPath(toTopologyLink(notification)));
213 public String toString() {
214 return "onLinkRemoved";
221 public void onLinkUtilizationNormal(final LinkUtilizationNormal notification) {
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);
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()));
237 private void removeAffectedLinks(final NodeId id, final ReadWriteTransaction transaction) {
238 Optional<Topology> topologyOptional = Optional.absent();
240 topologyOptional = transaction.read(LogicalDatastoreType.OPERATIONAL, topology).checkedGet();
241 } catch (ReadFailedException e) {
242 LOG.error("Error reading topology data for topology {}", topology, e);
244 if (topologyOptional.isPresent()) {
245 removeAffectedLinks(id, topologyOptional, transaction);
249 private void removeAffectedLinks(final NodeId id, Optional<Topology> topologyOptional, ReadWriteTransaction transaction) {
250 if (!topologyOptional.isPresent()) {
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));
264 private void removeAffectedLinks(final TpId id, final ReadWriteTransaction transaction) {
265 Optional<Topology> topologyOptional = Optional.absent();
267 topologyOptional = transaction.read(LogicalDatastoreType.OPERATIONAL, topology).checkedGet();
268 } catch (ReadFailedException e) {
269 LOG.error("Error reading topology data for topology {}", topology, e);
271 if (topologyOptional.isPresent()) {
272 removeAffectedLinks(id, topologyOptional, transaction);
276 private void removeAffectedLinks(final TpId id, Optional<Topology> topologyOptional, ReadWriteTransaction transaction) {
277 if (!topologyOptional.isPresent()) {
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));
291 private InstanceIdentifier<Node> getNodePath(final NodeId nodeId) {
292 return topology.child(Node.class, new NodeKey(nodeId));
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);
301 private InstanceIdentifier<Link> linkPath(final Link link) {
302 return topology.child(Link.class, link.getKey());