Search in sources :

Example 6 with DOMDataTreeWriteCursor

use of org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor in project controller by opendaylight.

the class PrefixShardHandler method ensureListExists.

private ListenableFuture<Void> ensureListExists() {
    final CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = ImmutableNodes.mapNodeBuilder(ID_INT);
    // hardcoded initial list population for parallel produce-transactions testing on multiple nodes
    for (int i = 1; i < MAX_PREFIX; i++) {
        mapBuilder.withChild(ImmutableNodes.mapEntryBuilder(ID_INT, ID, PREFIX_TEMPLATE + i).withChild(ImmutableNodes.mapNodeBuilder(ITEM).build()).build());
    }
    final MapNode mapNode = mapBuilder.build();
    final ContainerNode containerNode = ImmutableContainerNodeBuilder.create().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(ID_INTS)).withChild(mapNode).build();
    final DOMDataTreeProducer producer = domDataTreeService.createProducer(Collections.singleton(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY)));
    final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
    final DOMDataTreeWriteCursor cursor = tx.createCursor(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY));
    cursor.merge(containerNode.getIdentifier(), containerNode);
    cursor.close();
    final ListenableFuture<Void> future = tx.submit();
    Futures.addCallback(future, new FutureCallback<Void>() {

        @Override
        public void onSuccess(@Nullable final Void result) {
            try {
                LOG.debug("Closing producer for initial list.");
                producer.close();
            } catch (DOMDataTreeProducerException e) {
                LOG.warn("Error while closing producer.", e);
            }
        }

        @Override
        public void onFailure(final Throwable throwable) {
        // NOOP handled by the caller of this method.
        }
    }, MoreExecutors.directExecutor());
    return future;
}
Also used : DOMDataTreeProducerException(org.opendaylight.mdsal.dom.api.DOMDataTreeProducerException) DOMDataTreeWriteCursor(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor) DOMDataTreeIdentifier(org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) DOMDataTreeProducer(org.opendaylight.mdsal.dom.api.DOMDataTreeProducer) DOMDataTreeCursorAwareTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction)

Example 7 with DOMDataTreeWriteCursor

use of org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor in project controller by opendaylight.

the class ProduceTransactionsHandler method execWrite.

@Override
ListenableFuture<Void> execWrite(final long txId) {
    final int i = random.nextInt(MAX_ITEM + 1);
    final DOMDataTreeCursorAwareTransaction tx = itemProducer.createTransaction(false);
    final DOMDataTreeWriteCursor cursor = tx.createCursor(idListItem);
    final NodeIdentifierWithPredicates entryId = new NodeIdentifierWithPredicates(ITEM, NUMBER, i);
    if (usedValues.contains(i)) {
        LOG.debug("Deleting item: {}", i);
        deleteTx++;
        cursor.delete(entryId);
        usedValues.remove(i);
    } else {
        LOG.debug("Inserting item: {}", i);
        insertTx++;
        final MapEntryNode entry = ImmutableNodes.mapEntryBuilder().withNodeIdentifier(entryId).withChild(ImmutableNodes.leafNode(NUMBER, i)).build();
        cursor.write(entryId, entry);
        usedValues.add(i);
    }
    cursor.close();
    return tx.submit();
}
Also used : DOMDataTreeWriteCursor(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor) DOMDataTreeCursorAwareTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)

Example 8 with DOMDataTreeWriteCursor

use of org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor in project controller by opendaylight.

the class ProduceTransactionsHandler method start.

public static ListenableFuture<RpcResult<ProduceTransactionsOutput>> start(final DOMDataTreeService domDataTreeService, final ProduceTransactionsInput input) {
    final String id = input.getId();
    LOG.debug("Filling the item list {} with initial values.", id);
    final YangInstanceIdentifier idListWithKey = ID_INT_YID.node(new NodeIdentifierWithPredicates(ID_INT, ID, id));
    final DOMDataTreeProducer itemProducer = domDataTreeService.createProducer(Collections.singleton(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, idListWithKey)));
    final DOMDataTreeCursorAwareTransaction tx = itemProducer.createTransaction(false);
    final DOMDataTreeWriteCursor cursor = tx.createCursor(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, idListWithKey));
    final MapNode list = ImmutableNodes.mapNodeBuilder(ITEM).build();
    cursor.write(list.getIdentifier(), list);
    cursor.close();
    try {
        tx.submit().get(INIT_TX_TIMEOUT_SECONDS, TimeUnit.SECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        LOG.warn("Unable to fill the initial item list.", e);
        closeProducer(itemProducer);
        return Futures.immediateFuture(RpcResultBuilder.<ProduceTransactionsOutput>failed().withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", e).build());
    }
    final ProduceTransactionsHandler handler = new ProduceTransactionsHandler(itemProducer, new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, idListWithKey.node(list.getIdentifier()).toOptimized()), input);
    // It is handler's responsibility to close itemProducer when the work is finished.
    handler.doStart();
    return handler.future;
}
Also used : DOMDataTreeWriteCursor(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor) DOMDataTreeIdentifier(org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) ProduceTransactionsOutput(org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.ProduceTransactionsOutput) DOMDataTreeProducer(org.opendaylight.mdsal.dom.api.DOMDataTreeProducer) DOMDataTreeCursorAwareTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 9 with DOMDataTreeWriteCursor

use of org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor in project controller by opendaylight.

the class DistributedShardFrontendTest method testClientTransaction.

@Test
public void testClientTransaction() throws Exception {
    final DistributedDataStore distributedDataStore = mock(DistributedDataStore.class);
    final ActorContext context = mock(ActorContext.class);
    doReturn(context).when(distributedDataStore).getActorContext();
    doReturn(SchemaContextHelper.full()).when(context).getSchemaContext();
    final DistributedShardFrontend rootShard = new DistributedShardFrontend(distributedDataStore, client, ROOT);
    try (DOMDataTreeProducer producer = shardedDOMDataTree.createProducer(Collections.singletonList(ROOT))) {
        shardedDOMDataTree.registerDataTreeShard(ROOT, rootShard, producer);
    }
    final DataStoreClient outerListClient = mock(DataStoreClient.class);
    final ClientTransaction outerListClientTransaction = mock(ClientTransaction.class);
    final ClientLocalHistory outerListClientHistory = mock(ClientLocalHistory.class);
    final DOMDataTreeWriteCursor outerListCursor = mock(DOMDataTreeWriteCursor.class);
    doNothing().when(outerListCursor).close();
    doNothing().when(outerListCursor).write(any(), any());
    doNothing().when(outerListCursor).merge(any(), any());
    doNothing().when(outerListCursor).delete(any());
    doReturn(outerListCursor).when(outerListClientTransaction).openCursor();
    doReturn(outerListClientTransaction).when(outerListClient).createTransaction();
    doReturn(outerListClientHistory).when(outerListClient).createLocalHistory();
    doReturn(outerListClientTransaction).when(outerListClientHistory).createTransaction();
    doReturn(commitCohort).when(outerListClientTransaction).ready();
    doNothing().when(outerListClientHistory).close();
    doNothing().when(outerListClient).close();
    final DistributedShardFrontend outerListShard = new DistributedShardFrontend(distributedDataStore, outerListClient, OUTER_LIST_ID);
    try (DOMDataTreeProducer producer = shardedDOMDataTree.createProducer(Collections.singletonList(OUTER_LIST_ID))) {
        shardedDOMDataTree.registerDataTreeShard(OUTER_LIST_ID, outerListShard, producer);
    }
    final DOMDataTreeProducer producer = shardedDOMDataTree.createProducer(Collections.singletonList(ROOT));
    final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
    final DOMDataTreeWriteCursor txCursor = tx.createCursor(ROOT);
    assertNotNull(txCursor);
    txCursor.write(TestModel.TEST_PATH.getLastPathArgument(), createCrossShardContainer());
    // check the lower shard got the correct modification
    verify(outerListCursor, times(2)).write(pathArgumentCaptor.capture(), nodeCaptor.capture());
    final YangInstanceIdentifier.PathArgument expectedYid = new NodeIdentifier(TestModel.ID_QNAME);
    final YangInstanceIdentifier.PathArgument actualIdYid = pathArgumentCaptor.getAllValues().get(0);
    assertEquals(expectedYid, actualIdYid);
    final YangInstanceIdentifier.PathArgument expectedInnerYid = new NodeIdentifier(TestModel.INNER_LIST_QNAME);
    final YangInstanceIdentifier.PathArgument actualInnerListYid = pathArgumentCaptor.getAllValues().get(1);
    assertEquals(expectedInnerYid, actualInnerListYid);
    final LeafNode<Integer> actualIdNode = (LeafNode<Integer>) nodeCaptor.getAllValues().get(0);
    assertEquals(ImmutableNodes.leafNode(TestModel.ID_QNAME, 1), actualIdNode);
    final MapNode actualInnerListNode = (MapNode) nodeCaptor.getAllValues().get(1);
    assertEquals(createInnerMapNode(1), actualInnerListNode);
    txCursor.close();
    tx.submit().checkedGet();
    verify(commitCohort, times(2)).canCommit();
    verify(commitCohort, times(2)).preCommit();
    verify(commitCohort, times(2)).commit();
}
Also used : DOMDataTreeWriteCursor(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor) ClientTransaction(org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) DataStoreClient(org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient) ActorContext(org.opendaylight.controller.cluster.datastore.utils.ActorContext) ClientLocalHistory(org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory) DistributedDataStore(org.opendaylight.controller.cluster.datastore.DistributedDataStore) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) LeafNode(org.opendaylight.yangtools.yang.data.api.schema.LeafNode) DOMDataTreeProducer(org.opendaylight.mdsal.dom.api.DOMDataTreeProducer) DOMDataTreeCursorAwareTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction) Test(org.junit.Test)

Example 10 with DOMDataTreeWriteCursor

use of org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor in project controller by opendaylight.

the class DistributedShardedDOMDataTreeRemotingTest method testWriteIntoMultipleShards.

@Test
public void testWriteIntoMultipleShards() throws Exception {
    LOG.info("testWriteIntoMultipleShards starting");
    initEmptyDatastores();
    leaderTestKit.waitForMembersUp("member-2");
    LOG.debug("registering first shard");
    final DistributedShardRegistration shardRegistration = waitOnAsyncTask(leaderShardFactory.createDistributedShard(TEST_ID, Lists.newArrayList(AbstractTest.MEMBER_NAME, AbstractTest.MEMBER_2_NAME)), DistributedShardedDOMDataTree.SHARD_FUTURE_TIMEOUT_DURATION);
    leaderTestKit.waitUntilLeader(leaderConfigDatastore.getActorContext(), ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
    findLocalShard(followerConfigDatastore.getActorContext(), ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()));
    final Set<String> peers = new HashSet<>();
    IntegrationTestKit.verifyShardState(leaderConfigDatastore, ClusterUtils.getCleanShardName(TEST_ID.getRootIdentifier()), onDemandShardState -> peers.addAll(onDemandShardState.getPeerAddresses().values()));
    assertEquals(peers.size(), 1);
    LOG.debug("Got after waiting for nonleader");
    final DOMDataTreeProducer producer = leaderShardFactory.createProducer(Collections.singleton(TEST_ID));
    final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(true);
    final DOMDataTreeWriteCursor cursor = tx.createCursor(TEST_ID);
    Assert.assertNotNull(cursor);
    final YangInstanceIdentifier nameId = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(TestModel.NAME_QNAME).build();
    cursor.write(nameId.getLastPathArgument(), ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.NAME_QNAME)).withValue("Test Value").build());
    cursor.close();
    LOG.warn("Got to pre submit");
    tx.submit().checkedGet();
    shardRegistration.close().toCompletableFuture().get();
    LOG.info("testWriteIntoMultipleShards ending");
}
Also used : DistributedShardRegistration(org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration) DOMDataTreeWriteCursor(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) AddressFromURIString(akka.actor.AddressFromURIString) DOMDataTreeProducer(org.opendaylight.mdsal.dom.api.DOMDataTreeProducer) DOMDataTreeCursorAwareTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) HashSet(java.util.HashSet) Test(org.junit.Test) AbstractTest(org.opendaylight.controller.cluster.datastore.AbstractTest)

Aggregations

DOMDataTreeWriteCursor (org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor)14 DOMDataTreeCursorAwareTransaction (org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction)9 DOMDataTreeProducer (org.opendaylight.mdsal.dom.api.DOMDataTreeProducer)8 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)8 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)8 Test (org.junit.Test)7 ClientTransaction (org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction)5 AbstractTest (org.opendaylight.controller.cluster.datastore.AbstractTest)5 DOMDataTreeIdentifier (org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier)5 MapNode (org.opendaylight.yangtools.yang.data.api.schema.MapNode)5 DistributedShardRegistration (org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration)4 NodeIdentifierWithPredicates (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates)4 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)4 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)4 DOMDataTreeListener (org.opendaylight.mdsal.dom.api.DOMDataTreeListener)3 ActorRef (akka.actor.ActorRef)2 AddressFromURIString (akka.actor.AddressFromURIString)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Matchers.anyCollection (org.mockito.Matchers.anyCollection)2