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 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;
18 import com.google.common.base.Optional;
19 import com.google.common.base.Preconditions;
20 import com.google.common.util.concurrent.CheckedFuture;
21 import com.google.common.util.concurrent.FutureCallback;
22 import com.google.common.util.concurrent.Futures;
23 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.FlowTopologyDiscoveryListener;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkOverutilized;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkUtilizationNormal;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
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.topology.Link;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 class FlowCapableTopologyExporter implements FlowTopologyDiscoveryListener, OpendaylightInventoryListener {
55 private final Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyExporter.class);
56 private final InstanceIdentifier<Topology> topology;
57 private final OperationProcessor processor;
59 FlowCapableTopologyExporter(final OperationProcessor processor, final InstanceIdentifier<Topology> topology) {
60 this.processor = Preconditions.checkNotNull(processor);
61 this.topology = Preconditions.checkNotNull(topology);
65 public void onNodeRemoved(final NodeRemoved notification) {
67 final NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeRef()).getId());
68 final InstanceIdentifier<Node> nodeInstance = toNodeIdentifier(notification.getNodeRef());
70 processor.enqueueOperation(new TopologyOperation() {
72 public void applyOperation(final ReadWriteTransaction transaction) {
73 removeAffectedLinks(nodeId);
77 processor.enqueueOperation(new TopologyOperation() {
79 public void applyOperation(ReadWriteTransaction transaction) {
80 transaction.delete(LogicalDatastoreType.OPERATIONAL, nodeInstance);
86 public void onNodeUpdated(final NodeUpdated notification) {
87 FlowCapableNodeUpdated fcnu = notification.getAugmentation(FlowCapableNodeUpdated.class);
89 processor.enqueueOperation(new TopologyOperation() {
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.put(LogicalDatastoreType.OPERATIONAL, path, node);
101 public void onNodeConnectorRemoved(final NodeConnectorRemoved notification) {
103 final InstanceIdentifier<TerminationPoint> tpInstance = toTerminationPointIdentifier(notification
104 .getNodeConnectorRef());
106 processor.enqueueOperation(new TopologyOperation() {
108 public void applyOperation(final ReadWriteTransaction transaction) {
109 final TpId tpId = toTerminationPointId(getNodeConnectorKey(notification.getNodeConnectorRef()).getId());
110 removeAffectedLinks(tpId);
114 processor.enqueueOperation(new TopologyOperation() {
116 public void applyOperation(ReadWriteTransaction transaction) {
117 transaction.delete(LogicalDatastoreType.OPERATIONAL, tpInstance);
123 public void onNodeConnectorUpdated(final NodeConnectorUpdated notification) {
124 final FlowCapableNodeConnectorUpdated fcncu = notification.getAugmentation(FlowCapableNodeConnectorUpdated.class);
126 processor.enqueueOperation(new TopologyOperation() {
128 public void applyOperation(final ReadWriteTransaction transaction) {
129 final NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeConnectorRef()).getId());
130 TerminationPoint point = toTerminationPoint(toTerminationPointId(notification.getId()),
131 notification.getNodeConnectorRef());
132 final InstanceIdentifier<TerminationPoint> path = tpPath(nodeId, point.getKey().getTpId());
133 transaction.put(LogicalDatastoreType.OPERATIONAL, path, point);
134 if ((fcncu.getState() != null && fcncu.getState().isLinkDown())
135 || (fcncu.getConfiguration() != null && fcncu.getConfiguration().isPORTDOWN())) {
136 removeAffectedLinks(point.getTpId());
144 public void onLinkDiscovered(final LinkDiscovered notification) {
145 processor.enqueueOperation(new TopologyOperation() {
147 public void applyOperation(final ReadWriteTransaction transaction) {
148 final Link link = toTopologyLink(notification);
149 final InstanceIdentifier<Link> path = linkPath(link);
150 transaction.put(LogicalDatastoreType.OPERATIONAL, path, link);
156 public void onLinkOverutilized(final LinkOverutilized notification) {
161 public void onLinkRemoved(final LinkRemoved notification) {
162 processor.enqueueOperation(new TopologyOperation() {
164 public void applyOperation(final ReadWriteTransaction transaction) {
165 transaction.delete(LogicalDatastoreType.OPERATIONAL, linkPath(toTopologyLink(notification)));
171 public void onLinkUtilizationNormal(final LinkUtilizationNormal notification) {
175 private InstanceIdentifier<Node> toNodeIdentifier(final NodeRef ref) {
176 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey invNodeKey = getNodeKey(ref);
177 NodeKey nodeKey = new NodeKey(toTopologyNodeId(invNodeKey.getId()));
178 return topology.child(Node.class, nodeKey);
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()));
187 private void removeAffectedLinks(final NodeId id) {
188 processor.enqueueOperation(new TopologyOperation() {
190 public void applyOperation(final ReadWriteTransaction transaction) {
191 CheckedFuture<Optional<Topology>, ReadFailedException> topologyDataFuture = transaction.read(LogicalDatastoreType.OPERATIONAL, topology);
192 Futures.addCallback(topologyDataFuture, new FutureCallback<Optional<Topology>>() {
194 public void onSuccess(Optional<Topology> topologyOptional) {
195 if (topologyOptional.isPresent()) {
196 Topology topologyData = topologyOptional.get();
197 for (Link link : topologyData.getLink()) {
198 if (id.equals(link.getSource().getSourceNode()) || id.equals(link.getDestination().getDestNode())) {
199 transaction.delete(LogicalDatastoreType.OPERATIONAL, linkPath(link));
206 public void onFailure(Throwable throwable) {
207 LOG.error("Error reading topology data for topology {}", topology, throwable);
214 private void removeAffectedLinks(final TpId id) {
215 processor.enqueueOperation(new TopologyOperation() {
217 public void applyOperation(final ReadWriteTransaction transaction) {
218 CheckedFuture<Optional<Topology>, ReadFailedException> topologyDataFuture = transaction.read(LogicalDatastoreType.OPERATIONAL, topology);
219 Futures.addCallback(topologyDataFuture, new FutureCallback<Optional<Topology>>() {
221 public void onSuccess(Optional<Topology> topologyOptional) {
222 if (topologyOptional.isPresent()) {
223 Topology topologyData = topologyOptional.get();
224 for (Link link : topologyData.getLink()) {
225 if (id.equals(link.getSource().getSourceTp()) || id.equals(link.getDestination().getDestTp())) {
226 transaction.delete(LogicalDatastoreType.OPERATIONAL, linkPath(link));
233 public void onFailure(Throwable throwable) {
234 LOG.error("Error reading topology data for topology {}", topology, throwable);
241 private InstanceIdentifier<Node> getNodePath(final NodeId nodeId) {
242 return topology.child(Node.class, new NodeKey(nodeId));
245 private InstanceIdentifier<TerminationPoint> tpPath(final NodeId nodeId, final TpId tpId) {
246 NodeKey nodeKey = new NodeKey(nodeId);
247 TerminationPointKey tpKey = new TerminationPointKey(tpId);
248 return topology.child(Node.class, nodeKey).child(TerminationPoint.class, tpKey);
251 private InstanceIdentifier<Link> linkPath(final Link link) {
252 return topology.child(Link.class, link.getKey());