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 org.opendaylight.controller.md.sal.binding.util.TypeSafeDataReader;
19 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.FlowTopologyDiscoveryListener;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkOverutilized;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkUtilizationNormal;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import com.google.common.base.Preconditions;
47 class FlowCapableTopologyExporter implements FlowTopologyDiscoveryListener, OpendaylightInventoryListener {
48 private final InstanceIdentifier<Topology> topology;
49 private final OperationProcessor processor;
51 FlowCapableTopologyExporter(final OperationProcessor processor, final InstanceIdentifier<Topology> topology) {
52 this.processor = Preconditions.checkNotNull(processor);
53 this.topology = Preconditions.checkNotNull(topology);
57 public void onNodeRemoved(final NodeRemoved notification) {
58 processor.enqueueOperation(new TopologyOperation() {
60 public void applyOperation(final DataModificationTransaction transaction) {
61 NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeRef()).getId());
62 InstanceIdentifier<Node> nodeInstance = toNodeIdentifier(notification.getNodeRef());
63 transaction.removeOperationalData(nodeInstance);
64 removeAffectedLinks(transaction, nodeId);
70 public void onNodeUpdated(final NodeUpdated notification) {
71 FlowCapableNodeUpdated fcnu = notification.getAugmentation(FlowCapableNodeUpdated.class);
73 processor.enqueueOperation(new TopologyOperation() {
75 public void applyOperation(final DataModificationTransaction transaction) {
76 Node node = toTopologyNode(toTopologyNodeId(notification.getId()), notification.getNodeRef());
77 InstanceIdentifier<Node> path = getNodePath(toTopologyNodeId(notification.getId()));
78 transaction.putOperationalData(path, node);
85 public void onNodeConnectorRemoved(final NodeConnectorRemoved notification) {
86 processor.enqueueOperation(new TopologyOperation() {
88 public void applyOperation(final DataModificationTransaction transaction) {
89 InstanceIdentifier<TerminationPoint> tpInstance = toTerminationPointIdentifier(notification
90 .getNodeConnectorRef());
91 TpId tpId = toTerminationPointId(getNodeConnectorKey(notification.getNodeConnectorRef()).getId());
93 transaction.removeOperationalData(tpInstance);
94 removeAffectedLinks(transaction, tpId);
100 public void onNodeConnectorUpdated(final NodeConnectorUpdated notification) {
101 final FlowCapableNodeConnectorUpdated fcncu = notification.getAugmentation(FlowCapableNodeConnectorUpdated.class);
103 processor.enqueueOperation(new TopologyOperation() {
105 public void applyOperation(final DataModificationTransaction transaction) {
106 NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeConnectorRef()).getId());
107 TerminationPoint point = toTerminationPoint(toTerminationPointId(notification.getId()),
108 notification.getNodeConnectorRef());
109 InstanceIdentifier<TerminationPoint> path = tpPath(nodeId, point.getKey().getTpId());
111 transaction.putOperationalData(path, point);
112 if ((fcncu.getState() != null && fcncu.getState().isLinkDown())
113 || (fcncu.getConfiguration() != null && fcncu.getConfiguration().isPORTDOWN())) {
114 removeAffectedLinks(transaction, point.getTpId());
122 public void onLinkDiscovered(final LinkDiscovered notification) {
123 processor.enqueueOperation(new TopologyOperation() {
125 public void applyOperation(final DataModificationTransaction transaction) {
126 Link link = toTopologyLink(notification);
127 InstanceIdentifier<Link> path = linkPath(link);
128 transaction.putOperationalData(path, link);
134 public void onLinkOverutilized(final LinkOverutilized notification) {
139 public void onLinkRemoved(final LinkRemoved notification) {
140 processor.enqueueOperation(new TopologyOperation() {
142 public void applyOperation(final DataModificationTransaction transaction) {
143 transaction.removeOperationalData(linkPath(toTopologyLink(notification)));
149 public void onLinkUtilizationNormal(final LinkUtilizationNormal notification) {
153 private InstanceIdentifier<Node> toNodeIdentifier(final NodeRef ref) {
154 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey invNodeKey = getNodeKey(ref);
155 NodeKey nodeKey = new NodeKey(toTopologyNodeId(invNodeKey.getId()));
156 return topology.child(Node.class, nodeKey);
159 private InstanceIdentifier<TerminationPoint> toTerminationPointIdentifier(final NodeConnectorRef ref) {
160 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey invNodeKey = getNodeKey(ref);
161 NodeConnectorKey invNodeConnectorKey = getNodeConnectorKey(ref);
162 return tpPath(toTopologyNodeId(invNodeKey.getId()), toTerminationPointId(invNodeConnectorKey.getId()));
165 private void removeAffectedLinks(final DataModificationTransaction transaction, final NodeId id) {
166 TypeSafeDataReader reader = TypeSafeDataReader.forReader(transaction);
167 Topology topologyData = reader.readOperationalData(topology);
168 if (topologyData != null) {
169 for (Link link : topologyData.getLink()) {
170 if (id.equals(link.getSource().getSourceNode()) || id.equals(link.getDestination().getDestNode())) {
171 transaction.removeOperationalData(linkPath(link));
177 private void removeAffectedLinks(final DataModificationTransaction transaction, final TpId id) {
178 TypeSafeDataReader reader = TypeSafeDataReader.forReader(transaction);
179 Topology topologyData = reader.readOperationalData(topology);
180 if (topologyData != null) {
181 for (Link link : topologyData.getLink()) {
182 if (id.equals(link.getSource().getSourceTp()) || id.equals(link.getDestination().getDestTp())) {
183 transaction.removeOperationalData(linkPath(link));
189 private InstanceIdentifier<Node> getNodePath(final NodeId nodeId) {
190 return topology.child(Node.class, new NodeKey(nodeId));
193 private InstanceIdentifier<TerminationPoint> tpPath(final NodeId nodeId, final TpId tpId) {
194 NodeKey nodeKey = new NodeKey(nodeId);
195 TerminationPointKey tpKey = new TerminationPointKey(tpId);
196 return topology.child(Node.class, nodeKey).child(TerminationPoint.class, tpKey);
199 private InstanceIdentifier<Link> linkPath(final Link link) {
200 return topology.child(Link.class, link.getKey());