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;
}
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;
}
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();
}
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;
}
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));
}
Aggregations