import static org.opendaylight.md.controller.topology.manager.FlowCapableNodeMapping.toTopologyNode;
import static org.opendaylight.md.controller.topology.manager.FlowCapableNodeMapping.toTopologyNodeId;
-import java.util.concurrent.ExecutionException;
-
import org.opendaylight.controller.md.sal.binding.util.TypeSafeDataReader;
import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
-import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.FlowTopologyDiscoveryListener;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-class FlowCapableTopologyExporter implements //
- FlowTopologyDiscoveryListener, //
- OpendaylightInventoryListener //
-{
-
- private final static Logger LOG = LoggerFactory.getLogger(FlowCapableTopologyExporter.class);
- public static TopologyKey topology = new TopologyKey(new TopologyId("flow:1"));
-
- // FIXME: Flow capable topology exporter should use transaction chaining API
- private DataProviderService dataService;
-
- public DataProviderService getDataService() {
- return dataService;
- }
- public void setDataService(final DataProviderService dataService) {
- this.dataService = dataService;
- }
+import com.google.common.base.Preconditions;
- private InstanceIdentifier<Topology> topologyPath;
+class FlowCapableTopologyExporter implements FlowTopologyDiscoveryListener, OpendaylightInventoryListener {
+ private final InstanceIdentifier<Topology> topology;
+ private final OperationProcessor processor;
- public void start() {
- TopologyBuilder tb = new TopologyBuilder();
- tb.setKey(topology);
- topologyPath = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, topology).build();
- Topology top = tb.build();
- DataModificationTransaction tx = dataService.beginTransaction();
- tx.putOperationalData(topologyPath, top);
- tx.commit();
+ FlowCapableTopologyExporter(final OperationProcessor processor, final InstanceIdentifier<Topology> topology) {
+ this.processor = Preconditions.checkNotNull(processor);
+ this.topology = Preconditions.checkNotNull(topology);
}
@Override
- public synchronized void onNodeRemoved(final NodeRemoved notification) {
- NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeRef()).getId());
- InstanceIdentifier<Node> nodeInstance = toNodeIdentifier(notification.getNodeRef());
-
- DataModificationTransaction tx = dataService.beginTransaction();
- tx.removeOperationalData(nodeInstance);
- removeAffectedLinks(tx, nodeId);
- try {
- tx.commit().get();
- } catch (InterruptedException | ExecutionException e) {
- LOG.error("Topology state export not successful. ",e);
- }
+ public void onNodeRemoved(final NodeRemoved notification) {
+ processor.enqueueOperation(new TopologyOperation() {
+ @Override
+ public void applyOperation(final DataModificationTransaction transaction) {
+ NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeRef()).getId());
+ InstanceIdentifier<Node> nodeInstance = toNodeIdentifier(notification.getNodeRef());
+ transaction.removeOperationalData(nodeInstance);
+ removeAffectedLinks(transaction, nodeId);
+ }
+ });
}
@Override
- public synchronized void onNodeUpdated(final NodeUpdated notification) {
+ public void onNodeUpdated(final NodeUpdated notification) {
FlowCapableNodeUpdated fcnu = notification.getAugmentation(FlowCapableNodeUpdated.class);
if (fcnu != null) {
- Node node = toTopologyNode(toTopologyNodeId(notification.getId()), notification.getNodeRef());
- InstanceIdentifier<Node> path = getNodePath(toTopologyNodeId(notification.getId()));
- DataModificationTransaction tx = dataService.beginTransaction();
- tx.putOperationalData(path, node);
- try {
- tx.commit().get();
- } catch (InterruptedException | ExecutionException e) {
- LOG.error("Topology state export not successful. ",e);
- }
+ processor.enqueueOperation(new TopologyOperation() {
+ @Override
+ public void applyOperation(final DataModificationTransaction transaction) {
+ Node node = toTopologyNode(toTopologyNodeId(notification.getId()), notification.getNodeRef());
+ InstanceIdentifier<Node> path = getNodePath(toTopologyNodeId(notification.getId()));
+ transaction.putOperationalData(path, node);
+ }
+ });
}
}
@Override
- public synchronized void onNodeConnectorRemoved(final NodeConnectorRemoved notification) {
- InstanceIdentifier<TerminationPoint> tpInstance = toTerminationPointIdentifier(notification
- .getNodeConnectorRef());
- TpId tpId = toTerminationPointId(getNodeConnectorKey(notification.getNodeConnectorRef()).getId());
- DataModificationTransaction tx = dataService.beginTransaction();
- tx.removeOperationalData(tpInstance);
- removeAffectedLinks(tx, tpId);
- try {
- tx.commit().get();
- } catch (InterruptedException | ExecutionException e) {
- LOG.error("Topology state export not successful. ",e);
- }
-
+ public void onNodeConnectorRemoved(final NodeConnectorRemoved notification) {
+ processor.enqueueOperation(new TopologyOperation() {
+ @Override
+ public void applyOperation(final DataModificationTransaction transaction) {
+ InstanceIdentifier<TerminationPoint> tpInstance = toTerminationPointIdentifier(notification
+ .getNodeConnectorRef());
+ TpId tpId = toTerminationPointId(getNodeConnectorKey(notification.getNodeConnectorRef()).getId());
+
+ transaction.removeOperationalData(tpInstance);
+ removeAffectedLinks(transaction, tpId);
+ }
+ });
}
@Override
- public synchronized void onNodeConnectorUpdated(final NodeConnectorUpdated notification) {
- FlowCapableNodeConnectorUpdated fcncu = notification.getAugmentation(FlowCapableNodeConnectorUpdated.class);
+ public void onNodeConnectorUpdated(final NodeConnectorUpdated notification) {
+ final FlowCapableNodeConnectorUpdated fcncu = notification.getAugmentation(FlowCapableNodeConnectorUpdated.class);
if (fcncu != null) {
- NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeConnectorRef()).getId());
- TerminationPoint point = toTerminationPoint(toTerminationPointId(notification.getId()),
- notification.getNodeConnectorRef());
- InstanceIdentifier<TerminationPoint> path = tpPath(nodeId, point.getKey().getTpId());
-
- DataModificationTransaction tx = dataService.beginTransaction();
- tx.putOperationalData(path, point);
- if ((fcncu.getState() != null && fcncu.getState().isLinkDown())
- || (fcncu.getConfiguration() != null && fcncu.getConfiguration().isPORTDOWN())) {
- removeAffectedLinks(tx, point.getTpId());
- }
- try {
- tx.commit().get();
- } catch (InterruptedException | ExecutionException e) {
- LOG.error("Topology state export not successful. ",e);
- }
+ processor.enqueueOperation(new TopologyOperation() {
+ @Override
+ public void applyOperation(final DataModificationTransaction transaction) {
+ NodeId nodeId = toTopologyNodeId(getNodeKey(notification.getNodeConnectorRef()).getId());
+ TerminationPoint point = toTerminationPoint(toTerminationPointId(notification.getId()),
+ notification.getNodeConnectorRef());
+ InstanceIdentifier<TerminationPoint> path = tpPath(nodeId, point.getKey().getTpId());
+
+ transaction.putOperationalData(path, point);
+ if ((fcncu.getState() != null && fcncu.getState().isLinkDown())
+ || (fcncu.getConfiguration() != null && fcncu.getConfiguration().isPORTDOWN())) {
+ removeAffectedLinks(transaction, point.getTpId());
+ }
+ }
+ });
}
}
@Override
- public synchronized void onLinkDiscovered(final LinkDiscovered notification) {
- Link link = toTopologyLink(notification);
- InstanceIdentifier<Link> path = linkPath(link);
- DataModificationTransaction tx = dataService.beginTransaction();
- tx.putOperationalData(path, link);
- try {
- tx.commit().get();
- } catch (InterruptedException | ExecutionException e) {
- LOG.error("Topology state export not successful. ",e);
- }
+ public void onLinkDiscovered(final LinkDiscovered notification) {
+ processor.enqueueOperation(new TopologyOperation() {
+ @Override
+ public void applyOperation(final DataModificationTransaction transaction) {
+ Link link = toTopologyLink(notification);
+ InstanceIdentifier<Link> path = linkPath(link);
+ transaction.putOperationalData(path, link);
+ }
+ });
}
@Override
- public synchronized void onLinkOverutilized(final LinkOverutilized notification) {
+ public void onLinkOverutilized(final LinkOverutilized notification) {
// NOOP
}
@Override
- public synchronized void onLinkRemoved(final LinkRemoved notification) {
- InstanceIdentifier<Link> path = linkPath(toTopologyLink(notification));
- DataModificationTransaction tx = dataService.beginTransaction();
- tx.removeOperationalData(path);
- ;
+ public void onLinkRemoved(final LinkRemoved notification) {
+ processor.enqueueOperation(new TopologyOperation() {
+ @Override
+ public void applyOperation(final DataModificationTransaction transaction) {
+ transaction.removeOperationalData(linkPath(toTopologyLink(notification)));
+ }
+ });
}
@Override
- public synchronized void onLinkUtilizationNormal(final LinkUtilizationNormal notification) {
+ public void onLinkUtilizationNormal(final LinkUtilizationNormal notification) {
// NOOP
}
- private static InstanceIdentifier<Node> toNodeIdentifier(final NodeRef ref) {
+ private InstanceIdentifier<Node> toNodeIdentifier(final NodeRef ref) {
org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey invNodeKey = getNodeKey(ref);
-
NodeKey nodeKey = new NodeKey(toTopologyNodeId(invNodeKey.getId()));
- return InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, topology)
- .child(Node.class, nodeKey).build();
+ return topology.child(Node.class, nodeKey);
}
private InstanceIdentifier<TerminationPoint> toTerminationPointIdentifier(final NodeConnectorRef ref) {
private void removeAffectedLinks(final DataModificationTransaction transaction, final NodeId id) {
TypeSafeDataReader reader = TypeSafeDataReader.forReader(transaction);
-
- Topology topologyData = reader.readOperationalData(topologyPath);
- if (topologyData == null) {
- return;
- }
- for (Link link : topologyData.getLink()) {
- if (id.equals(link.getSource().getSourceNode()) || id.equals(link.getDestination().getDestNode())) {
- InstanceIdentifier<Link> path = InstanceIdentifier.builder(topologyPath)
- .child(Link.class, link.getKey()).build();
- transaction.removeOperationalData(path);
+ Topology topologyData = reader.readOperationalData(topology);
+ if (topologyData != null) {
+ for (Link link : topologyData.getLink()) {
+ if (id.equals(link.getSource().getSourceNode()) || id.equals(link.getDestination().getDestNode())) {
+ transaction.removeOperationalData(linkPath(link));
+ }
}
}
}
private void removeAffectedLinks(final DataModificationTransaction transaction, final TpId id) {
TypeSafeDataReader reader = TypeSafeDataReader.forReader(transaction);
- Topology topologyData = reader.readOperationalData(topologyPath);
- if (topologyData == null) {
- return;
- }
- for (Link link : topologyData.getLink()) {
- if (id.equals(link.getSource().getSourceTp()) || id.equals(link.getDestination().getDestTp())) {
- InstanceIdentifier<Link> path = InstanceIdentifier.builder(topologyPath)
- .child(Link.class, link.getKey()).build();
- transaction.removeOperationalData(path);
+ Topology topologyData = reader.readOperationalData(topology);
+ if (topologyData != null) {
+ for (Link link : topologyData.getLink()) {
+ if (id.equals(link.getSource().getSourceTp()) || id.equals(link.getDestination().getDestTp())) {
+ transaction.removeOperationalData(linkPath(link));
+ }
}
}
}
private InstanceIdentifier<Node> getNodePath(final NodeId nodeId) {
- NodeKey nodeKey = new NodeKey(nodeId);
- return InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, topology)
- .child(Node.class, nodeKey).build();
+ return topology.child(Node.class, new NodeKey(nodeId));
}
private InstanceIdentifier<TerminationPoint> tpPath(final NodeId nodeId, final TpId tpId) {
NodeKey nodeKey = new NodeKey(nodeId);
TerminationPointKey tpKey = new TerminationPointKey(tpId);
- return InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, topology)
- .child(Node.class, nodeKey).child(TerminationPoint.class, tpKey).build();
+ return topology.child(Node.class, nodeKey).child(TerminationPoint.class, tpKey);
}
private InstanceIdentifier<Link> linkPath(final Link link) {
- InstanceIdentifier<Link> linkInstanceId = InstanceIdentifier.builder(NetworkTopology.class)
- .child(Topology.class, topology).child(Link.class, link.getKey()).build();
- return linkInstanceId;
+ return topology.child(Link.class, link.getKey());
}
}