Search in sources :

Example 31 with ReadOnlyTransaction

use of org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction in project openflowplugin by opendaylight.

the class FlowWriterDirectOFRpc method getAllNodes.

private Set<String> getAllNodes() {
    Set<String> nodeIds = new HashSet<>();
    InstanceIdentifier<Nodes> nodes = InstanceIdentifier.create(Nodes.class);
    ReadOnlyTransaction readOnlyTransaction = dataBroker.newReadOnlyTransaction();
    try {
        Optional<Nodes> nodesDataNode = readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, nodes).checkedGet();
        if (nodesDataNode.isPresent()) {
            List<Node> nodesCollection = nodesDataNode.get().getNode();
            if (nodesCollection != null && !nodesCollection.isEmpty()) {
                for (Node node : nodesCollection) {
                    LOG.info("Switch with ID {} discovered !!", node.getId().getValue());
                    nodeIds.add(node.getId().getValue());
                }
            } else {
                return Collections.emptySet();
            }
        } else {
            return Collections.emptySet();
        }
    } catch (ReadFailedException rdFailedException) {
        LOG.error("Failed to read connected nodes {}", rdFailedException);
    }
    return nodeIds;
}
Also used : ReadFailedException(org.opendaylight.controller.md.sal.common.api.data.ReadFailedException) Node(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node) ReadOnlyTransaction(org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction) Nodes(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes) HashSet(java.util.HashSet)

Example 32 with ReadOnlyTransaction

use of org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction in project openflowplugin by opendaylight.

the class DeviceFlowRegistryImpl method fillFromDatastore.

private CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> fillFromDatastore(final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<FlowCapableNode> path) {
    // Create new read-only transaction
    final ReadOnlyTransaction transaction = dataBroker.newReadOnlyTransaction();
    // Bail out early if transaction is null
    if (transaction == null) {
        return Futures.immediateFailedCheckedFuture(new ReadFailedException("Read transaction is null"));
    }
    // Prepare read operation from datastore for path
    final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> future = transaction.read(logicalDatastoreType, path);
    // Bail out early if future is null
    if (future == null) {
        return Futures.immediateFailedCheckedFuture(new ReadFailedException("Future from read transaction is null"));
    }
    Futures.addCallback(future, new FutureCallback<Optional<FlowCapableNode>>() {

        @Override
        public void onSuccess(@Nonnull Optional<FlowCapableNode> result) {
            result.asSet().stream().filter(Objects::nonNull).filter(flowCapableNode -> Objects.nonNull(flowCapableNode.getTable())).flatMap(flowCapableNode -> flowCapableNode.getTable().stream()).filter(Objects::nonNull).filter(table -> Objects.nonNull(table.getFlow())).flatMap(table -> table.getFlow().stream()).filter(Objects::nonNull).filter(flow -> Objects.nonNull(flow.getId())).forEach(flowConsumer);
            // After we are done with reading from datastore, close the transaction
            transaction.close();
        }

        @Override
        public void onFailure(Throwable throwable) {
            // Even when read operation failed, close the transaction
            transaction.close();
        }
    }, MoreExecutors.directExecutor());
    return future;
}
Also used : MoreExecutors(com.google.common.util.concurrent.MoreExecutors) Arrays(java.util.Arrays) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) DeviceFlowRegistry(org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry) LoggerFactory(org.slf4j.LoggerFactory) CheckedFuture(com.google.common.util.concurrent.CheckedFuture) Flow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow) FlowCapableNode(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode) FlowId(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId) ArrayList(java.util.ArrayList) ReadOnlyTransaction(org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NodeKey(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey) Optional(com.google.common.base.Optional) Map(java.util.Map) FlowDescriptor(org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor) Nonnull(javax.annotation.Nonnull) Node(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node) FlowRegistryKey(org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey) BiMap(com.google.common.collect.BiMap) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) LogicalDatastoreType(org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType) ThreadSafe(javax.annotation.concurrent.ThreadSafe) Maps(com.google.common.collect.Maps) FutureCallback(com.google.common.util.concurrent.FutureCallback) DataBroker(org.opendaylight.controller.md.sal.binding.api.DataBroker) Objects(java.util.Objects) Consumer(java.util.function.Consumer) GeneralAugMatchNodesNodeTableFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow) HashBiMap(com.google.common.collect.HashBiMap) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) InstanceIdentifier(org.opendaylight.yangtools.yang.binding.InstanceIdentifier) ReadFailedException(org.opendaylight.controller.md.sal.common.api.data.ReadFailedException) KeyedInstanceIdentifier(org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ReadFailedException(org.opendaylight.controller.md.sal.common.api.data.ReadFailedException) Optional(com.google.common.base.Optional) FlowCapableNode(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode) ReadOnlyTransaction(org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction) Objects(java.util.Objects)

Example 33 with ReadOnlyTransaction

use of org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction in project openflowplugin by opendaylight.

the class TransactionChainManagerTest method setUp.

@Before
public void setUp() throws Exception {
    final ReadOnlyTransaction readOnlyTx = Mockito.mock(ReadOnlyTransaction.class);
    final CheckedFuture<Optional<Node>, ReadFailedException> noExistNodeFuture = Futures.immediateCheckedFuture(Optional.<Node>absent());
    Mockito.when(readOnlyTx.read(LogicalDatastoreType.OPERATIONAL, nodeKeyIdent)).thenReturn(noExistNodeFuture);
    Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTx);
    Mockito.when(dataBroker.createTransactionChain(Matchers.any(TransactionChainListener.class))).thenReturn(txChain);
    nodeId = new NodeId("h2g2:42");
    nodeKeyIdent = DeviceStateUtil.createNodeInstanceIdentifier(nodeId);
    Mockito.when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeKeyIdent);
    Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
    txChainManager = new TransactionChainManager(dataBroker, nodeId.getValue());
    Mockito.when(txChain.newReadWriteTransaction()).thenReturn(writeTx);
    path = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
    Mockito.when(writeTx.submit()).thenReturn(Futures.<Void, TransactionCommitFailedException>immediateCheckedFuture(null));
    txChainManager.activateTransactionManager();
}
Also used : ReadFailedException(org.opendaylight.controller.md.sal.common.api.data.ReadFailedException) Optional(com.google.common.base.Optional) TransactionChainListener(org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener) Node(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node) ReadOnlyTransaction(org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction) NodeId(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId) TransactionChainManager(org.opendaylight.openflowplugin.common.txchain.TransactionChainManager) NodeKey(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey) Before(org.junit.Before)

Example 34 with ReadOnlyTransaction

use of org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction in project lispflowmapping by opendaylight.

the class DataStoreBackEnd method readTransaction.

private <U extends org.opendaylight.yangtools.yang.binding.DataObject> U readTransaction(InstanceIdentifier<U> readIID, LogicalDatastoreType logicalDatastoreType) {
    ReadOnlyTransaction readTx = txChain.newReadOnlyTransaction();
    CheckedFuture<Optional<U>, ReadFailedException> readFuture = readTx.read(logicalDatastoreType, readIID);
    readTx.close();
    try {
        Optional<U> optionalDataObject = readFuture.checkedGet();
        if (optionalDataObject != null && optionalDataObject.isPresent()) {
            return optionalDataObject.get();
        } else {
            LOG.debug("{}: Failed to read", Thread.currentThread().getStackTrace()[1]);
        }
    } catch (ReadFailedException e) {
        LOG.warn("Failed to ....", e);
    }
    return null;
}
Also used : ReadFailedException(org.opendaylight.controller.md.sal.common.api.data.ReadFailedException) Optional(com.google.common.base.Optional) ReadOnlyTransaction(org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction)

Example 35 with ReadOnlyTransaction

use of org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction in project genius by opendaylight.

the class ResourceBatchingManager method read.

/**
 * Reads the identifier of the given resource type.
 * Not to be used by the applications  which uses their own resource queue
 *
 * @param resourceType resource type that was registered with batch manager
 * @param identifier   identifier to be read
 * @return a CheckFuture containing the result of the read
 */
public <T extends DataObject> CheckedFuture<Optional<T>, ReadFailedException> read(String resourceType, InstanceIdentifier<T> identifier) {
    BlockingQueue<ActionableResource> queue = getQueue(resourceType);
    if (queue != null) {
        if (pendingModificationByResourceType.get(resourceType).contains(identifier)) {
            SettableFuture<Optional<T>> readFuture = SettableFuture.create();
            queue.add(new ActionableReadResource<>(identifier, readFuture));
            return Futures.makeChecked(readFuture, ReadFailedException.MAPPER);
        } else {
            ResourceHandler resourceHandler = resourceHandlerMapper.get(resourceType).getRight();
            try (ReadOnlyTransaction tx = resourceHandler.getResourceBroker().newReadOnlyTransaction()) {
                return tx.read(resourceHandler.getDatastoreType(), identifier);
            }
        }
    }
    return Futures.immediateFailedCheckedFuture(new ReadFailedException("No batch handler was registered for resource " + resourceType));
}
Also used : ReadFailedException(org.opendaylight.controller.md.sal.common.api.data.ReadFailedException) Optional(com.google.common.base.Optional) ReadOnlyTransaction(org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction)

Aggregations

ReadOnlyTransaction (org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction)65 Optional (com.google.common.base.Optional)27 ReadFailedException (org.opendaylight.controller.md.sal.common.api.data.ReadFailedException)23 ExecutionException (java.util.concurrent.ExecutionException)17 FlowCapableNode (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode)16 Node (org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node)16 NodeKey (org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey)13 Nodes (org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes)12 InstanceIdentifier (org.opendaylight.yangtools.yang.binding.InstanceIdentifier)8 ArrayList (java.util.ArrayList)7 LogicalDatastoreType (org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType)7 Node (org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node)7 List (java.util.List)6 DataBroker (org.opendaylight.controller.md.sal.binding.api.DataBroker)6 Topology (org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology)6 Test (org.junit.Test)5 Table (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table)5 Nonnull (javax.annotation.Nonnull)4 TableKey (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey)4 CacheBuilder (com.google.common.cache.CacheBuilder)3